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   setMinimumRepetitions(1);
40 }
41
42
43 void NECProtocol::startSendingCommand(
44   unsigned int threadableID,
45   PIRKeyName command)
46 {
47   // Exceptions here are problematic; I'll try to weed them out by putting the
48   // whole thing in a try/catch block:
49   try
50   {
51     // First, check if we are meant to be the recipient of this command:
52     if (threadableID != id) return;
53
54     // An object that helps keep track of the number of commands:
55 //    PIRCommandCounter commandCounter;
56
57     // Ok, we're going to lock down this method and make sure
58     // only one guy at a time passes this point:
59 //    QMutexLocker commandLocker(&commandMutex);
60
61     clearRepeatFlag();
62
63     KeycodeCollection::const_iterator i = keycodes.find(command);
64
65     // Do we even have this key defined?
66     if (i == keycodes.end())
67     {
68       QMutexLocker cifLocker(&commandIFMutex);
69       commandInFlight = false;
70       return;
71 //      std::string s = "Tried to send a non-existent command.\n";
72 //      throw PIRException(s);
73     }
74
75     // construct the device:
76     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
77
78     int repeatCount = 0;
79     int commandDuration = 0;
80     while (repeatCount < MAX_REPEAT_COUNT)
81     {
82       // If we are currently repeating, and have a special "repeat signal",
83       // use that signal.  Otherwise, generate a normal command string.
84       if (isShortRepeat && repeatCount)
85       {
86         commandDuration = generateRepeatCommand(rx51device);
87       }
88       else
89       {
90         commandDuration = generateStandardCommand((*i).second, rx51device);
91       }
92
93       // Now, tell the device to send the whole command:
94       rx51device.sendCommandToDevice();
95
96       // sleep until the next repetition of command:
97       sleepUntilRepeat(commandDuration);
98
99       // Check whether we've reached the minimum required number of repetitons:
100       if (repeatCount >= minimumRepetitions)
101       {
102         // Check whether we've been asked to stop:
103         if (checkRepeatFlag())
104         {
105           break;
106 /*
107           QMutexLocker cifLocker(&commandIFMutex);
108           commandInFlight = false;
109           return;
110 */
111         }
112       }
113
114       ++repeatCount;
115     }
116
117     QMutexLocker cifLocker(&commandIFMutex);
118     commandInFlight = false;
119   }
120   catch (PIRException e)
121   {
122     // inform the gui:
123     emit commandFailed(e.getError().c_str());
124   }
125 }
126
127
128 int NECProtocol::generateStandardCommand(
129   const PIRKeyBits &pkb,
130   PIRRX51Hardware &rx51device)
131 {
132   int duration = 0;
133
134   // First, the "header" pulse:
135   rx51device.addPair(headerPulse, headerSpace);
136   duration += (headerPulse + headerSpace);
137
138   // Now, check the encoding format:
139   if (isExtendedNEC)
140   {
141     // In extended NEC, the address has been extended to 16 bits, and is only
142     // sent once.  The command portion stays the same.
143     // - "preData" should contain 16-bit value
144     // - "bits" should contain 8-bit value
145     duration += pushReverseBits(preData, rx51device);
146     duration += pushReverseBits(pkb.firstCode, rx51device);
147     duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
148   }
149   else
150   {
151     // Standard NEC is made up of an eight-bit "address" and an eight-bit
152     // "command".  First the address bits are sent (in reverse order), then
153     // the address bits are inverted and sent again (in reverse order).
154     // Next, we do the same to the command bits.
155     // - "preData" should contain 8-bit value
156     // - "bits" should contain 8-bit value
157     duration += pushReverseBits(preData, rx51device);
158     duration += pushInvertedReverseBits(preData, rx51device);
159     duration += pushReverseBits(pkb.firstCode, rx51device);
160     duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
161   }
162
163   // Finally add the "trail":
164   rx51device.addSingle(trailerPulse);
165   duration += trailerPulse;
166
167   return duration;
168 }
169
170
171 int NECProtocol::generateRepeatCommand(
172   PIRRX51Hardware &rx51device)
173 {
174   int duration = 0;
175
176   // Add the repeat pulse:
177   rx51device.addPair(repeatPulse, repeatSpace);
178   duration += (repeatPulse + repeatSpace);
179
180   // Add the trailer:
181   rx51device.addSingle(trailerPulse);
182   duration += trailerPulse;
183
184   return duration;
185 }