Search for Power Button Panel
[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       QMutexLocker cifLocker(&commandIFMutex);
68       commandInFlight = false;
69       return;
70 //      std::string s = "Tried to send a non-existent command.\n";
71 //      throw PIRException(s);
72     }
73
74     // construct the device:
75     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
76
77     int repeatCount = 0;
78     int commandDuration = 0;
79     while (repeatCount < MAX_REPEAT_COUNT)
80     {
81       // If we are currently repeating, and have a special "repeat signal",
82       // use that signal.  Otherwise, generate a normal command string.
83       if (isShortRepeat && repeatCount)
84       {
85         commandDuration = generateRepeatCommand(rx51device);
86       }
87       else
88       {
89         commandDuration = generateStandardCommand((*i).second, rx51device);
90       }
91
92       // Now, tell the device to send the whole command:
93       rx51device.sendCommandToDevice();
94
95       // sleep until the next repetition of command:
96       sleepUntilRepeat(commandDuration);
97
98       // Check whether we've reached the minimum required number of repetitons:
99       if (repeatCount >= minimumRepetitions)
100       {
101         // Check whether we've been asked to stop:
102         if (checkRepeatFlag())
103         {
104           break;
105 /*
106           QMutexLocker cifLocker(&commandIFMutex);
107           commandInFlight = false;
108           return;
109 */
110         }
111       }
112
113       ++repeatCount;
114     }
115
116     QMutexLocker cifLocker(&commandIFMutex);
117     commandInFlight = false;
118   }
119   catch (PIRException e)
120   {
121     // inform the gui:
122     emit commandFailed(e.getError().c_str());
123   }
124 }
125
126
127 int NECProtocol::generateStandardCommand(
128   const PIRKeyBits &pkb,
129   PIRRX51Hardware &rx51device)
130 {
131   int duration = 0;
132
133   // First, the "header" pulse:
134   rx51device.addPair(headerPulse, headerSpace);
135   duration += (headerPulse + headerSpace);
136
137   // Now, check the encoding format:
138   if (isExtendedNEC)
139   {
140     // In extended NEC, the address has been extended to 16 bits, and is only
141     // sent once.  The command portion stays the same.
142     // - "preData" should contain 16-bit value
143     // - "bits" should contain 8-bit value
144     duration += pushReverseBits(preData, rx51device);
145     duration += pushReverseBits(pkb.firstCode, rx51device);
146     duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
147   }
148   else
149   {
150     // Standard NEC is made up of an eight-bit "address" and an eight-bit
151     // "command".  First the address bits are sent (in reverse order), then
152     // the address bits are inverted and sent again (in reverse order).
153     // Next, we do the same to the command bits.
154     // - "preData" should contain 8-bit value
155     // - "bits" should contain 8-bit value
156     duration += pushReverseBits(preData, rx51device);
157     duration += pushInvertedReverseBits(preData, rx51device);
158     duration += pushReverseBits(pkb.firstCode, rx51device);
159     duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
160   }
161
162   // Finally add the "trail":
163   rx51device.addSingle(trailerPulse);
164   duration += trailerPulse;
165
166   return duration;
167 }
168
169
170 int NECProtocol::generateRepeatCommand(
171   PIRRX51Hardware &rx51device)
172 {
173   int duration = 0;
174
175   // Add the repeat pulse:
176   rx51device.addPair(repeatPulse, repeatSpace);
177   duration += (repeatPulse + repeatSpace);
178
179   // Add the trailer:
180   rx51device.addSingle(trailerPulse);
181   duration += trailerPulse;
182
183   return duration;
184 }