Moved Mute button, lots new keysets
[pierogi] / protocols / necprotocol.cpp
1 #include "necprotocol.h"
2
3 #include "pirrx51hardware.h"
4
5 #include "pirexception.h"
6
7 // Some global communications stuff:
8 #include <QMutex>
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
11
12 // The official NEC protocol, as I understand it, has the following attributes:
13 // A "zero" is encoded with a 560 usec pulse, 560 usec space.
14 // A "one" is encoded with a 560 usec pulse, and 3*560 (1680) usec space.
15 // The header is a 9000 usec pulse, 4500 usec space.
16 // Commands end with a trailing 560 usec pulse.
17 // A repeat block (if used) is a 9000 usec pulse, 2250 usec space, then
18 // trailing pulse.
19 // Each command runs for 110000 usec before another can be executed.
20 // The normal carrier frequency is 38 kHz.
21
22 NECProtocol::NECProtocol(
23   QObject *guiObject,
24   unsigned int index,
25   bool extNEC,
26   bool srtRep)
27   : SpaceProtocol(
28       guiObject, index,
29       560, 560,
30       560, 1680,
31       9000, 4500,
32       560,
33       110000, true),
34     repeatPulse(9000),
35     repeatSpace(2250),
36     isExtendedNEC(extNEC),
37     isShortRepeat(srtRep)
38 {
39 }
40
41
42 void NECProtocol::startSendingCommand(
43   unsigned int threadableID,
44   PIRKeyName command)
45 {
46   // Exceptions here are problematic; I'll try to weed them out by putting the
47   // whole thing in a try/catch block:
48   try
49   {
50     // First, check if we are meant to be the recipient of this command:
51     if (threadableID != id) return;
52
53     // An object that helps keep track of the number of commands:
54 //    PIRCommandCounter commandCounter;
55
56     // Ok, we're going to lock down this method and make sure
57     // only one guy at a time passes this point:
58 //    QMutexLocker commandLocker(&commandMutex);
59
60     clearRepeatFlag();
61
62     KeycodeCollection::const_iterator i = keycodes.find(command);
63
64     // Do we even have this key defined?
65     if (i == keycodes.end())
66     {
67       std::string s = "Tried to send a non-existent command.\n";
68       throw PIRException(s);
69     }
70
71     // construct the device:
72     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
73
74     int repeatCount = 0;
75     int commandDuration = 0;
76     while (repeatCount < MAX_REPEAT_COUNT)
77     {
78       // If we are currently repeating, and have a special "repeat signal",
79       // use that signal.  Otherwise, generate a normal command string.
80       if (isShortRepeat && repeatCount)
81       {
82         commandDuration = generateRepeatCommand(rx51device);
83       }
84       else
85       {
86         commandDuration = generateStandardCommand((*i).second, rx51device);
87       }
88
89       // Now, tell the device to send the whole command:
90       rx51device.sendCommandToDevice();
91
92       // sleep until the next repetition of command:
93       sleepUntilRepeat(commandDuration);
94
95       // Check whether we've reached the minimum required number of repetitons:
96       if (repeatCount >= minimumRepetitions)
97       {
98         // Check whether we've been asked to stop:
99         if (checkRepeatFlag())
100         {
101           QMutexLocker cifLocker(&commandIFMutex);
102           commandInFlight = false;
103           return;
104         }
105       }
106
107       ++repeatCount;
108     }
109   }
110   catch (PIRException e)
111   {
112     // inform the gui:
113     emit commandFailed(e.getError().c_str());
114   }
115
116   QMutexLocker cifLocker(&commandIFMutex);
117   commandInFlight = false;
118 }
119
120
121 int NECProtocol::generateStandardCommand(
122   const PIRKeyBits &pkb,
123   PIRRX51Hardware &rx51device)
124 {
125   int duration = 0;
126
127   // First, the "header" pulse:
128   rx51device.addPair(headerPulse, headerSpace);
129   duration += (headerPulse + headerSpace);
130
131   // Now, check the encoding format:
132   if (isExtendedNEC)
133   {
134     // In extended NEC, the address has been extended to 16 bits, and is only
135     // sent once.  The command portion stays the same.
136     // - "preData" should contain 16-bit value
137     // - "bits" should contain 8-bit value
138     duration += pushReverseBits(preData, rx51device);
139     duration += pushReverseBits(pkb.firstCode, rx51device);
140     duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
141   }
142   else
143   {
144     // Standard NEC is made up of an eight-bit "address" and an eight-bit
145     // "command".  First the address bits are sent (in reverse order), then
146     // the address bits are inverted and sent again (in reverse order).
147     // Next, we do the same to the command bits.
148     // - "preData" should contain 8-bit value
149     // - "bits" should contain 8-bit value
150     duration += pushReverseBits(preData, rx51device);
151     duration += pushInvertedReverseBits(preData, rx51device);
152     duration += pushReverseBits(pkb.firstCode, rx51device);
153     duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
154   }
155
156   // Finally add the "trail":
157   rx51device.addSingle(trailerPulse);
158   duration += trailerPulse;
159
160   return duration;
161 }
162
163
164 int NECProtocol::generateRepeatCommand(
165   PIRRX51Hardware &rx51device)
166 {
167   int duration = 0;
168
169   // Add the repeat pulse:
170   rx51device.addPair(repeatPulse, repeatSpace);
171   duration += (repeatPulse + repeatSpace);
172
173   // Add the trailer:
174   rx51device.addSingle(trailerPulse);
175   duration += trailerPulse;
176
177   return duration;
178 }