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 std::string s = "Tried to send a non-existent command.\n";
68 throw PIRException(s);
71 // construct the device:
72 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
75 int commandDuration = 0;
76 while (repeatCount < MAX_REPEAT_COUNT)
78 // If we are currently repeating, and have a special "repeat signal",
79 // use that signal. Otherwise, generate a normal command string.
80 if (isShortRepeat && repeatCount)
82 commandDuration = generateRepeatCommand(rx51device);
86 commandDuration = generateStandardCommand((*i).second, rx51device);
89 // Now, tell the device to send the whole command:
90 rx51device.sendCommandToDevice();
92 // sleep until the next repetition of command:
93 sleepUntilRepeat(commandDuration);
95 // Check whether we've reached the minimum required number of repetitons:
96 if (repeatCount >= minimumRepetitions)
98 // Check whether we've been asked to stop:
99 if (checkRepeatFlag())
101 QMutexLocker cifLocker(&commandIFMutex);
102 commandInFlight = false;
110 catch (PIRException e)
113 emit commandFailed(e.getError().c_str());
116 QMutexLocker cifLocker(&commandIFMutex);
117 commandInFlight = false;
121 int NECProtocol::generateStandardCommand(
122 const PIRKeyBits &pkb,
123 PIRRX51Hardware &rx51device)
127 // First, the "header" pulse:
128 rx51device.addPair(headerPulse, headerSpace);
129 duration += (headerPulse + headerSpace);
131 // Now, check the encoding format:
134 // In extended NEC, the address has been extended to 16 bits, and is only
135 // sent once. The command portion stays the same.
136 // - "preData" should contain 16-bit value
137 // - "bits" should contain 8-bit value
138 duration += pushReverseBits(preData, rx51device);
139 duration += pushReverseBits(pkb.firstCode, rx51device);
140 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
144 // Standard NEC is made up of an eight-bit "address" and an eight-bit
145 // "command". First the address bits are sent (in reverse order), then
146 // the address bits are inverted and sent again (in reverse order).
147 // Next, we do the same to the command bits.
148 // - "preData" should contain 8-bit value
149 // - "bits" should contain 8-bit value
150 duration += pushReverseBits(preData, rx51device);
151 duration += pushInvertedReverseBits(preData, rx51device);
152 duration += pushReverseBits(pkb.firstCode, rx51device);
153 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
156 // Finally add the "trail":
157 rx51device.addSingle(trailerPulse);
158 duration += trailerPulse;
164 int NECProtocol::generateRepeatCommand(
165 PIRRX51Hardware &rx51device)
169 // Add the repeat pulse:
170 rx51device.addPair(repeatPulse, repeatSpace);
171 duration += (repeatPulse + repeatSpace);
174 rx51device.addSingle(trailerPulse);
175 duration += trailerPulse;