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),
42 void NECProtocol::startSendingCommand(
43 unsigned int threadableID,
46 // Exceptions here are problematic; I'll try to weed them out by putting the
47 // whole thing in a try/catch block:
50 // First, check if we are meant to be the recipient of this command:
51 if (threadableID != id) return;
53 // An object that helps keep track of the number of commands:
54 // PIRCommandCounter commandCounter;
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);
62 KeycodeCollection::const_iterator i = keycodes.find(command);
64 // Do we even have this key defined?
65 if (i == keycodes.end())
67 QMutexLocker cifLocker(&commandIFMutex);
68 commandInFlight = false;
70 // std::string s = "Tried to send a non-existent command.\n";
71 // throw PIRException(s);
74 // construct the device:
75 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
78 int commandDuration = 0;
79 while (repeatCount < MAX_REPEAT_COUNT)
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)
85 commandDuration = generateRepeatCommand(rx51device);
89 commandDuration = generateStandardCommand((*i).second, rx51device);
92 // Now, tell the device to send the whole command:
93 rx51device.sendCommandToDevice();
95 // sleep until the next repetition of command:
96 sleepUntilRepeat(commandDuration);
98 // Check whether we've reached the minimum required number of repetitons:
99 if (repeatCount >= minimumRepetitions)
101 // Check whether we've been asked to stop:
102 if (checkRepeatFlag())
106 QMutexLocker cifLocker(&commandIFMutex);
107 commandInFlight = false;
116 QMutexLocker cifLocker(&commandIFMutex);
117 commandInFlight = false;
119 catch (PIRException e)
122 emit commandFailed(e.getError().c_str());
127 int NECProtocol::generateStandardCommand(
128 const PIRKeyBits &pkb,
129 PIRRX51Hardware &rx51device)
133 // First, the "header" pulse:
134 rx51device.addPair(headerPulse, headerSpace);
135 duration += (headerPulse + headerSpace);
137 // Now, check the encoding format:
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);
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);
162 // Finally add the "trail":
163 rx51device.addSingle(trailerPulse);
164 duration += trailerPulse;
170 int NECProtocol::generateRepeatCommand(
171 PIRRX51Hardware &rx51device)
175 // Add the repeat pulse:
176 rx51device.addPair(repeatPulse, repeatSpace);
177 duration += (repeatPulse + repeatSpace);
180 rx51device.addSingle(trailerPulse);
181 duration += trailerPulse;