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;
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
22 // Each command runs for 110000 usec before another can be executed.
23 // The normal carrier frequency is 38 kHz.
25 NECProtocol::NECProtocol(
39 isExtendedNEC(extNEC),
42 setMinimumRepetitions(1);
46 void NECProtocol::startSendingCommand(
47 unsigned int threadableID,
50 // Exceptions here are problematic; I'll try to weed them out by putting the
51 // whole thing in a try/catch block:
54 // First, check if we are meant to be the recipient of this command:
55 if (threadableID != id) return;
57 // An object that helps keep track of the number of commands:
58 // PIRCommandCounter commandCounter;
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);
66 KeycodeCollection::const_iterator i = keycodes.find(command);
68 // Do we even have this key defined?
69 if (i == keycodes.end())
71 QMutexLocker cifLocker(&commandIFMutex);
72 commandInFlight = false;
74 // std::string s = "Tried to send a non-existent command.\n";
75 // throw PIRException(s);
78 // construct the device:
79 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
82 int commandDuration = 0;
83 while (repeatCount < MAX_REPEAT_COUNT)
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)
89 commandDuration = generateRepeatCommand(rx51device);
93 commandDuration = generateStandardCommand((*i).second, rx51device);
96 // Now, tell the device to send the whole command:
97 rx51device.sendCommandToDevice();
99 // sleep until the next repetition of command:
100 sleepUntilRepeat(commandDuration);
102 // Check whether we've reached the minimum required number of repetitons:
103 if (repeatCount >= minimumRepetitions)
105 // Check whether we've been asked to stop:
106 if (checkRepeatFlag())
110 QMutexLocker cifLocker(&commandIFMutex);
111 commandInFlight = false;
120 QMutexLocker cifLocker(&commandIFMutex);
121 commandInFlight = false;
123 catch (PIRException e)
126 emit commandFailed(e.getError().c_str());
131 int NECProtocol::generateStandardCommand(
132 const PIRKeyBits &pkb,
133 PIRRX51Hardware &rx51device)
137 // First, the "header" pulse:
138 rx51device.addPair(headerPulse, headerSpace);
139 duration += (headerPulse + headerSpace);
141 // Now, check the encoding format:
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);
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);
166 // Finally add the "trail":
167 rx51device.addSingle(trailerPulse);
168 duration += trailerPulse;
174 int NECProtocol::generateRepeatCommand(
175 PIRRX51Hardware &rx51device)
179 // Add the repeat pulse:
180 rx51device.addPair(repeatPulse, repeatSpace);
181 duration += (repeatPulse + repeatSpace);
184 rx51device.addSingle(trailerPulse);
185 duration += trailerPulse;