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