1 #include "necprotocol.h"
3 #include "pirexception.h"
7 NECProtocol::NECProtocol(
16 : PIRProtocol(guiObject, index, gSpace, iclflag),
21 hasTrailerPulse(false),
24 repeatNeedsHeader(false),
25 fullHeadlessRepeat(false)
29 void NECProtocol::setHeaderPair(
38 void NECProtocol::setTrailerPulse(
42 hasTrailerPulse = true;
45 void NECProtocol::setRepeatPair(
54 void NECProtocol::setRepeatNeedsHeader(
57 repeatNeedsHeader = flag;
60 void NECProtocol::setFullHeadlessRepeat(
63 fullHeadlessRepeat = flag;
66 void NECProtocol::setPreData(
70 appendToBitSeq(preData, data, bits);
73 void NECProtocol::setPostData(
77 appendToBitSeq(postData, data, bits);
80 void NECProtocol::startSendingCommand(
81 unsigned int threadableID,
84 // Exceptions here are problematic; I'll try to weed them out by putting the
85 // whole thing in a try/catch block:
90 // Check if we are meant to be the recipient of this command:
91 if (threadableID != id) return;
93 KeycodeCollection::const_iterator i = keycodes.find(command);
95 // Do we even have this key defined?
96 if (i == keycodes.end())
98 std::string s = "Tried to send a non-existent command.\n";
99 throw PIRException(s);
102 // construct the device:
103 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
106 while (repeatCount < MAX_REPEAT_COUNT)
110 // If we are currently repeating, and have a special "repeat signal",
111 // use that signal. Otherwise, generate a normal command string.
112 if (hasRepeatPair && repeatCount)
114 commandDuration = generateRepeatCommand(rx51device);
116 else if (fullHeadlessRepeat && repeatCount)
118 commandDuration = generateHeadlessCommand((*i).second, rx51device);
122 commandDuration = generateStandardCommand((*i).second, rx51device);
125 // Now, tell the device to send the whole command:
126 rx51device.sendCommandToDevice();
128 // sleep until the next repetition of command:
129 sleepUntilRepeat(commandDuration);
131 // Check whether we've reached the minimum required number of repetitons:
132 if (repeatCount >= minimumRepetitions)
134 // Check whether we've been asked to stop:
135 if (checkRepeatFlag())
144 catch (PIRException e)
147 emit commandFailed(e.getError().c_str());
152 int NECProtocol::generateStandardCommand(
153 const CommandSequence &bits,
154 PIRRX51Hardware &rx51device)
158 // First, the "header" pulse (if any):
161 rx51device.addPair(headerPulse, headerSpace);
162 duration += (headerPulse + headerSpace);
165 // Next, the "pre" data:
166 duration += pushBits(preData, rx51device);
168 // Next, add the actual command:
169 duration += pushBits(bits, rx51device);
171 // Next, add the "post" data:
172 duration += pushBits(postData, rx51device);
174 // Finally add the "trail":
177 rx51device.addSingle(trailerPulse);
178 duration += trailerPulse;
185 int NECProtocol::generateHeadlessCommand(
186 const CommandSequence &bits,
187 PIRRX51Hardware &rx51device)
191 // First, the "pre" data:
192 duration += pushBits(preData, rx51device);
194 // Next, add the actual command:
195 duration += pushBits(bits, rx51device);
197 // Next, add the "post" data:
198 duration += pushBits(postData, rx51device);
200 // Finally add the "trail":
203 rx51device.addSingle(trailerPulse);
204 duration += trailerPulse;
211 int NECProtocol::generateRepeatCommand(
212 PIRRX51Hardware &rx51device)
216 // Do we need the header?
217 if (repeatNeedsHeader)
219 // Do we even have a header?
222 // Ok, then add the header to the repeat:
223 rx51device.addPair(headerPulse, headerSpace);
224 duration += (headerPulse + headerSpace);
228 // Add the repeat pulse:
229 rx51device.addPair(repeatPulse, repeatSpace);
230 duration += (repeatPulse + repeatSpace);
232 // Finally add the trailer:
235 rx51device.addSingle(trailerPulse);
236 duration += trailerPulse;
243 int NECProtocol::pushBits(
244 const CommandSequence &bits,
245 PIRRX51Hardware &rx51device)
248 CommandSequence::const_iterator i = bits.begin();
249 while (i != bits.end())
253 // Send the pulse for "One":
254 rx51device.addPair(onePulse, oneSpace);
255 duration += (onePulse + oneSpace);
259 // Send the pulse for "Zero":
260 rx51device.addPair(zeroPulse, zeroSpace);
261 duration += (zeroPulse + zeroSpace);