1 #include "necprotocol.h"
3 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 NECProtocol::NECProtocol(
21 : PIRProtocol(guiObject, index, gSpace, iclflag),
26 hasTrailerPulse(false),
29 repeatNeedsHeader(false),
30 fullHeadlessRepeat(false)
34 void NECProtocol::setHeaderPair(
43 void NECProtocol::setTrailerPulse(
47 hasTrailerPulse = true;
50 void NECProtocol::setRepeatPair(
59 void NECProtocol::setRepeatNeedsHeader(
62 repeatNeedsHeader = flag;
65 void NECProtocol::setFullHeadlessRepeat(
68 fullHeadlessRepeat = flag;
71 void NECProtocol::setPreData(
75 appendToBitSeq(preData, data, bits);
78 void NECProtocol::setPostData(
82 appendToBitSeq(postData, data, bits);
85 void NECProtocol::startSendingCommand(
86 unsigned int threadableID,
89 // Exceptions here are problematic; I'll try to weed them out by putting the
90 // whole thing in a try/catch block:
93 // First, check if we are meant to be the recipient of this command:
94 if (threadableID != id) return;
96 // An object that helps keep track of the number of commands:
97 // PIRCommandCounter commandCounter;
99 // Ok, we're going to lock down this method and make sure
100 // only one guy at a time passes this point:
101 // QMutexLocker commandLocker(&commandMutex);
105 KeycodeCollection::const_iterator i = keycodes.find(command);
107 // Do we even have this key defined?
108 if (i == keycodes.end())
110 std::string s = "Tried to send a non-existent command.\n";
111 throw PIRException(s);
114 // construct the device:
115 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
118 while (repeatCount < MAX_REPEAT_COUNT)
122 // If we are currently repeating, and have a special "repeat signal",
123 // use that signal. Otherwise, generate a normal command string.
124 if (hasRepeatPair && repeatCount)
126 commandDuration = generateRepeatCommand(rx51device);
128 else if (fullHeadlessRepeat && repeatCount)
130 commandDuration = generateHeadlessCommand((*i).second, rx51device);
134 commandDuration = generateStandardCommand((*i).second, rx51device);
137 // Now, tell the device to send the whole command:
138 rx51device.sendCommandToDevice();
140 // sleep until the next repetition of command:
141 sleepUntilRepeat(commandDuration);
143 // Check whether we've reached the minimum required number of repetitons:
144 if (repeatCount >= minimumRepetitions)
146 // Check whether we've been asked to stop:
147 if (checkRepeatFlag())
149 QMutexLocker cifLocker(&commandIFMutex);
150 commandInFlight = false;
158 catch (PIRException e)
161 emit commandFailed(e.getError().c_str());
164 QMutexLocker cifLocker(&commandIFMutex);
165 commandInFlight = false;
169 int NECProtocol::generateStandardCommand(
170 const CommandSequence &bits,
171 PIRRX51Hardware &rx51device)
175 // First, the "header" pulse (if any):
178 rx51device.addPair(headerPulse, headerSpace);
179 duration += (headerPulse + headerSpace);
182 // Next, the "pre" data:
183 duration += pushBits(preData, rx51device);
185 // Next, add the actual command:
186 duration += pushBits(bits, rx51device);
188 // Next, add the "post" data:
189 duration += pushBits(postData, rx51device);
191 // Finally add the "trail":
194 rx51device.addSingle(trailerPulse);
195 duration += trailerPulse;
202 int NECProtocol::generateHeadlessCommand(
203 const CommandSequence &bits,
204 PIRRX51Hardware &rx51device)
208 // First, the "pre" data:
209 duration += pushBits(preData, rx51device);
211 // Next, add the actual command:
212 duration += pushBits(bits, rx51device);
214 // Next, add the "post" data:
215 duration += pushBits(postData, rx51device);
217 // Finally add the "trail":
220 rx51device.addSingle(trailerPulse);
221 duration += trailerPulse;
228 int NECProtocol::generateRepeatCommand(
229 PIRRX51Hardware &rx51device)
233 // Do we need the header?
234 if (repeatNeedsHeader)
236 // Do we even have a header?
239 // Ok, then add the header to the repeat:
240 rx51device.addPair(headerPulse, headerSpace);
241 duration += (headerPulse + headerSpace);
245 // Add the repeat pulse:
246 rx51device.addPair(repeatPulse, repeatSpace);
247 duration += (repeatPulse + repeatSpace);
249 // Finally add the trailer:
252 rx51device.addSingle(trailerPulse);
253 duration += trailerPulse;
260 int NECProtocol::pushBits(
261 const CommandSequence &bits,
262 PIRRX51Hardware &rx51device)
265 CommandSequence::const_iterator i = bits.begin();
266 while (i != bits.end())
270 // Send the pulse for "One":
271 rx51device.addPair(onePulse, oneSpace);
272 duration += (onePulse + oneSpace);
276 // Send the pulse for "Zero":
277 rx51device.addPair(zeroPulse, zeroSpace);
278 duration += (zeroPulse + zeroSpace);