1 #include "necprotocol.h"
2 #include "pirexception.h"
4 // Some global communications stuff:
6 extern bool commandInFlight;
7 extern QMutex commandIFMutex;
9 // The official NEC protocol, as I understand it, has the following attributes:
10 // A "zero" is encoded with a 560 usec pulse, 560 usec space.
11 // A "one" is encoded with a 560 usec pulse, and 3*560 (1680) usec space.
12 // The header is a 9000 usec pulse, 4500 usec space.
13 // Commands end with a trailing 560 usec pulse.
14 // A repeat block is a 9000 usec pulse, 2250 usec space, then trailing pulse.
15 // Each command runs for 110000 usec before another can be executed.
17 // For standard NEC, use this constructor:
18 NECProtocol::NECProtocol(
32 isExtendedNEC(extNEC),
38 void NECProtocol::startSendingCommand(
39 unsigned int threadableID,
42 // Exceptions here are problematic; I'll try to weed them out by putting the
43 // whole thing in a try/catch block:
46 // First, check if we are meant to be the recipient of this command:
47 if (threadableID != id) return;
49 // An object that helps keep track of the number of commands:
50 // PIRCommandCounter commandCounter;
52 // Ok, we're going to lock down this method and make sure
53 // only one guy at a time passes this point:
54 // QMutexLocker commandLocker(&commandMutex);
58 KeycodeCollection::const_iterator i = keycodes.find(command);
60 // Do we even have this key defined?
61 if (i == keycodes.end())
63 std::string s = "Tried to send a non-existent command.\n";
64 throw PIRException(s);
67 // construct the device:
68 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
71 int commandDuration = 0;
72 while (repeatCount < MAX_REPEAT_COUNT)
74 // If we are currently repeating, and have a special "repeat signal",
75 // use that signal. Otherwise, generate a normal command string.
76 if (isShortRepeat && repeatCount)
78 commandDuration = generateRepeatCommand(rx51device);
82 commandDuration = generateStandardCommand((*i).second, rx51device);
85 // Now, tell the device to send the whole command:
86 rx51device.sendCommandToDevice();
88 // sleep until the next repetition of command:
89 sleepUntilRepeat(commandDuration);
91 // Check whether we've reached the minimum required number of repetitons:
92 if (repeatCount >= minimumRepetitions)
94 // Check whether we've been asked to stop:
95 if (checkRepeatFlag())
97 QMutexLocker cifLocker(&commandIFMutex);
98 commandInFlight = false;
106 catch (PIRException e)
109 emit commandFailed(e.getError().c_str());
112 QMutexLocker cifLocker(&commandIFMutex);
113 commandInFlight = false;
117 int NECProtocol::generateStandardCommand(
118 const PIRKeyBits &pkb,
119 PIRRX51Hardware &rx51device)
123 // First, the "header" pulse:
124 rx51device.addPair(headerPulse, headerSpace);
125 duration += (headerPulse + headerSpace);
127 // Now, check the encoding format:
130 // In extended NEC, the address has been extended to 16 bits, and is only
131 // sent once. The command portion stays the same.
132 // - "preData" should contain 16-bit value
133 // - "bits" should contain 8-bit value
134 duration += pushReverseBits(preData, rx51device);
135 duration += pushReverseBits(pkb.firstCode, rx51device);
136 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
140 // Standard NEC is made up of an eight-bit "address" and an eight-bit
141 // "command". First the address bits are sent (in reverse order), then
142 // the address bits are inverted and sent again (in reverse order).
143 // Next, we do the same to the command bits.
144 // - "preData" should contain 8-bit value
145 // - "bits" should contain 8-bit value
146 duration += pushReverseBits(preData, rx51device);
147 duration += pushInvertedReverseBits(preData, rx51device);
148 duration += pushReverseBits(pkb.firstCode, rx51device);
149 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
152 // Finally add the "trail":
153 rx51device.addSingle(trailerPulse);
154 duration += trailerPulse;
160 int NECProtocol::generateRepeatCommand(
161 PIRRX51Hardware &rx51device)
165 // Add the repeat pulse:
166 rx51device.addPair(repeatPulse, repeatSpace);
167 duration += (repeatPulse + repeatSpace);
170 rx51device.addSingle(trailerPulse);
171 duration += trailerPulse;