1 #include "necprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
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
19 // Each command runs for 110000 usec before another can be executed.
20 // The normal carrier frequency is 38 kHz.
22 NECProtocol::NECProtocol(
36 isExtendedNEC(extNEC),
39 setMinimumRepetitions(1);
43 void NECProtocol::startSendingCommand(
44 unsigned int threadableID,
47 // Exceptions here are problematic; I'll try to weed them out by putting the
48 // whole thing in a try/catch block:
51 // First, check if we are meant to be the recipient of this command:
52 if (threadableID != id) return;
54 // An object that helps keep track of the number of commands:
55 // PIRCommandCounter commandCounter;
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);
63 KeycodeCollection::const_iterator i = keycodes.find(command);
65 // Do we even have this key defined?
66 if (i == keycodes.end())
68 QMutexLocker cifLocker(&commandIFMutex);
69 commandInFlight = false;
71 // std::string s = "Tried to send a non-existent command.\n";
72 // throw PIRException(s);
75 // construct the device:
76 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
79 int commandDuration = 0;
80 while (repeatCount < MAX_REPEAT_COUNT)
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)
86 commandDuration = generateRepeatCommand(rx51device);
90 commandDuration = generateStandardCommand((*i).second, rx51device);
93 // Now, tell the device to send the whole command:
94 rx51device.sendCommandToDevice();
96 // sleep until the next repetition of command:
97 sleepUntilRepeat(commandDuration);
99 // Check whether we've reached the minimum required number of repetitons:
100 if (repeatCount >= minimumRepetitions)
102 // Check whether we've been asked to stop:
103 if (checkRepeatFlag())
107 QMutexLocker cifLocker(&commandIFMutex);
108 commandInFlight = false;
117 QMutexLocker cifLocker(&commandIFMutex);
118 commandInFlight = false;
120 catch (PIRException e)
123 emit commandFailed(e.getError().c_str());
128 int NECProtocol::generateStandardCommand(
129 const PIRKeyBits &pkb,
130 PIRRX51Hardware &rx51device)
134 // First, the "header" pulse:
135 rx51device.addPair(headerPulse, headerSpace);
136 duration += (headerPulse + headerSpace);
138 // Now, check the encoding format:
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);
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);
163 // Finally add the "trail":
164 rx51device.addSingle(trailerPulse);
165 duration += trailerPulse;
171 int NECProtocol::generateRepeatCommand(
172 PIRRX51Hardware &rx51device)
176 // Add the repeat pulse:
177 rx51device.addPair(repeatPulse, repeatSpace);
178 duration += (repeatPulse + repeatSpace);
181 rx51device.addSingle(trailerPulse);
182 duration += trailerPulse;