1 #include "lircprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
9 // Some global communications stuff:
11 extern bool commandInFlight;
12 extern QMutex commandIFMutex;
14 LIRCProtocol::LIRCProtocol(
31 hasTrailerPulse(false),
33 repeatNeedsHeader(false),
34 fullHeadlessRepeat(false)
38 void LIRCProtocol::setHeaderPair(
47 void LIRCProtocol::setTrailerPulse(
51 hasTrailerPulse = true;
54 void LIRCProtocol::setRepeatPair(
63 void LIRCProtocol::setRepeatNeedsHeader(
66 repeatNeedsHeader = flag;
69 void LIRCProtocol::setFullHeadlessRepeat(
72 fullHeadlessRepeat = flag;
75 void LIRCProtocol::startSendingCommand(
76 unsigned int threadableID,
79 // Exceptions here are problematic; I'll try to weed them out by putting the
80 // whole thing in a try/catch block:
83 // First, check if we are meant to be the recipient of this command:
84 if (threadableID != id) return;
88 KeycodeCollection::const_iterator i = keycodes.find(command);
90 // Do we even have this key defined?
91 if (i == keycodes.end())
93 QMutexLocker cifLocker(&commandIFMutex);
94 commandInFlight = false;
96 // std::string s = "Tried to send a non-existent command.\n";
97 // throw PIRException(s);
100 // construct the device:
101 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
104 int commandDuration = 0;
105 while (repeatCount < MAX_REPEAT_COUNT)
107 // If we are currently repeating, and have a special "repeat signal",
108 // use that signal. Otherwise, generate a normal command string.
109 if (hasRepeatPair && repeatCount)
111 commandDuration = generateRepeatCommand(rx51device);
113 else if (fullHeadlessRepeat && repeatCount)
115 commandDuration = generateHeadlessCommand((*i).second, rx51device);
119 commandDuration = generateStandardCommand((*i).second, rx51device);
122 // Now, tell the device to send the whole command:
123 rx51device.sendCommandToDevice();
125 // sleep until the next repetition of command:
126 sleepUntilRepeat(commandDuration);
128 // Check whether we've reached the minimum required number of repetitons:
129 if (repeatCount >= minimumRepetitions)
131 // Check whether we've been asked to stop:
132 if (checkRepeatFlag())
136 QMutexLocker cifLocker(&commandIFMutex);
137 commandInFlight = false;
146 QMutexLocker cifLocker(&commandIFMutex);
147 commandInFlight = false;
149 catch (PIRException e)
152 emit commandFailed(e.getError().c_str());
157 int LIRCProtocol::generateStandardCommand(
158 const PIRKeyBits &pkb,
159 PIRRX51Hardware &rx51device)
163 // First, the "header" pulse (if any):
166 rx51device.addPair(headerPulse, headerSpace);
167 duration += (headerPulse + headerSpace);
170 // For LIRC, just dump the bits straight into the device, one by one:
171 duration += pushBits(preData, rx51device);
172 duration += pushBits(pkb.firstCode, rx51device);
173 duration += pushBits(postData, rx51device);
175 // Finally add the "trail":
178 rx51device.addSingle(trailerPulse);
179 duration += trailerPulse;
186 int LIRCProtocol::generateHeadlessCommand(
187 const PIRKeyBits &pkb,
188 PIRRX51Hardware &rx51device)
192 // First, the "pre" data:
193 duration += pushBits(preData, rx51device);
195 // Next, add the actual command:
196 duration += pushBits(pkb.firstCode, rx51device);
198 // Next, add the "post" data:
199 duration += pushBits(postData, rx51device);
201 // Finally add the "trail":
204 rx51device.addSingle(trailerPulse);
205 duration += trailerPulse;
212 int LIRCProtocol::generateRepeatCommand(
213 PIRRX51Hardware &rx51device)
217 // Do we need the header?
218 if (repeatNeedsHeader)
220 // Do we even have a header?
223 // Ok, then add the header to the repeat:
224 rx51device.addPair(headerPulse, headerSpace);
225 duration += (headerPulse + headerSpace);
229 // Add the repeat pulse:
230 rx51device.addPair(repeatPulse, repeatSpace);
231 duration += (repeatPulse + repeatSpace);
233 // Finally add the trailer:
236 rx51device.addSingle(trailerPulse);
237 duration += trailerPulse;