1 #include "necprotocol.h"
3 #include "pirexception.h"
7 NECProtocol::NECProtocol(
16 : PIRProtocol(guiObject, index, gSpace, iclflag),
21 hasTrailerPulse(false),
24 repeatNeedsHeader(false)
28 void NECProtocol::setHeaderPair(
37 void NECProtocol::setTrailerPulse(
41 hasTrailerPulse = true;
44 void NECProtocol::setRepeatPair(
53 void NECProtocol::setRepeatNeedsHeader(
56 repeatNeedsHeader = flag;
59 void NECProtocol::setPreData(
63 appendToBitSeq(preData, data, bits);
66 void NECProtocol::setPostData(
70 appendToBitSeq(postData, data, bits);
73 void NECProtocol::startSendingCommand(
74 unsigned int threadableID,
77 // Exceptions here are problematic; I'll try to weed them out by putting the
78 // whole thing in a try/catch block:
83 // Check if we are meant to be the recipient of this command:
84 if (threadableID != id) return;
86 KeycodeCollection::const_iterator i = keycodes.find(command);
88 // Do we even have this key defined?
89 if (i == keycodes.end())
91 std::string s = "Tried to send a non-existent command.\n";
92 throw PIRException(s);
95 // construct the device:
96 PIRDevice device(carrierFrequency, dutyCycle);
99 while (repeatCount < MAX_REPEAT_COUNT)
103 // If we are currently repeating, and have a special "repeat signal",
104 // use that signal. Otherwise, generate a normal command string.
105 if ((hasRepeatPair) && repeatCount)
107 commandDuration = generateRepeatCommand(device);
111 commandDuration = generateStandardCommand((*i).second, device);
114 // Now, tell the device to send the whole command:
115 device.sendCommandToDevice();
117 // sleep until the next repetition of command:
118 sleepUntilRepeat(commandDuration);
120 // Check whether we've reached the minimum required number of repetitons:
121 if (repeatCount >= minimumRepetitions)
123 // Check whether we've been asked to stop:
124 if (checkRepeatFlag())
133 catch (PIRException e)
136 emit commandFailed(e.getError().c_str());
141 int NECProtocol::generateStandardCommand(
142 const CommandSequence &bits,
147 // First, the "header" pulse (if any):
150 device.addPair(headerPulse, headerSpace);
151 duration += (headerPulse + headerSpace);
154 // Next, the "pre" data:
155 duration += pushBits(preData, device);
157 // Next, add the actual command:
158 duration += pushBits(bits, device);
160 // Next, add the "post" data:
161 duration += pushBits(postData, device);
163 // Finally add the "trail":
166 device.addSingle(trailerPulse);
167 duration += trailerPulse;
174 int NECProtocol::generateRepeatCommand(
179 // Do we need the header?
180 if (repeatNeedsHeader)
182 // Do we even have a header?
185 // Ok, then add the header to the repeat:
186 device.addPair(headerPulse, headerSpace);
187 duration += (headerPulse + headerSpace);
191 // Add the repeat pulse:
192 device.addPair(repeatPulse, repeatSpace);
193 duration += (repeatPulse + repeatSpace);
195 // Finally add the trailer:
198 device.addSingle(trailerPulse);
199 duration += trailerPulse;
206 int NECProtocol::pushBits(
207 const CommandSequence &bits,
211 CommandSequence::const_iterator i = bits.begin();
212 while (i != bits.end())
216 // Send the pulse for "One":
217 device.addPair(onePulse, oneSpace);
218 duration += (onePulse + oneSpace);
222 // Send the pulse for "Zero":
223 device.addPair(zeroPulse, zeroSpace);
224 duration += (zeroPulse + zeroSpace);