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 std::string s = "Tried to send a non-existent command.\n";
94 throw PIRException(s);
97 // construct the device:
98 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
101 int commandDuration = 0;
102 while (repeatCount < MAX_REPEAT_COUNT)
104 // If we are currently repeating, and have a special "repeat signal",
105 // use that signal. Otherwise, generate a normal command string.
106 if (hasRepeatPair && repeatCount)
108 commandDuration = generateRepeatCommand(rx51device);
110 else if (fullHeadlessRepeat && repeatCount)
112 commandDuration = generateHeadlessCommand((*i).second, rx51device);
116 commandDuration = generateStandardCommand((*i).second, rx51device);
119 // Now, tell the device to send the whole command:
120 rx51device.sendCommandToDevice();
122 // sleep until the next repetition of command:
123 sleepUntilRepeat(commandDuration);
125 // Check whether we've reached the minimum required number of repetitons:
126 if (repeatCount >= minimumRepetitions)
128 // Check whether we've been asked to stop:
129 if (checkRepeatFlag())
131 QMutexLocker cifLocker(&commandIFMutex);
132 commandInFlight = false;
140 catch (PIRException e)
143 emit commandFailed(e.getError().c_str());
146 QMutexLocker cifLocker(&commandIFMutex);
147 commandInFlight = false;
151 int LIRCProtocol::generateStandardCommand(
152 const PIRKeyBits &pkb,
153 PIRRX51Hardware &rx51device)
157 // First, the "header" pulse (if any):
160 rx51device.addPair(headerPulse, headerSpace);
161 duration += (headerPulse + headerSpace);
164 // For LIRC, just dump the bits straight into the device, one by one:
165 duration += pushBits(preData, rx51device);
166 duration += pushBits(pkb.firstCode, rx51device);
167 duration += pushBits(postData, rx51device);
169 // Finally add the "trail":
172 rx51device.addSingle(trailerPulse);
173 duration += trailerPulse;
180 int LIRCProtocol::generateHeadlessCommand(
181 const PIRKeyBits &pkb,
182 PIRRX51Hardware &rx51device)
186 // First, the "pre" data:
187 duration += pushBits(preData, rx51device);
189 // Next, add the actual command:
190 duration += pushBits(pkb.firstCode, rx51device);
192 // Next, add the "post" data:
193 duration += pushBits(postData, rx51device);
195 // Finally add the "trail":
198 rx51device.addSingle(trailerPulse);
199 duration += trailerPulse;
206 int LIRCProtocol::generateRepeatCommand(
207 PIRRX51Hardware &rx51device)
211 // Do we need the header?
212 if (repeatNeedsHeader)
214 // Do we even have a header?
217 // Ok, then add the header to the repeat:
218 rx51device.addPair(headerPulse, headerSpace);
219 duration += (headerPulse + headerSpace);
223 // Add the repeat pulse:
224 rx51device.addPair(repeatPulse, repeatSpace);
225 duration += (repeatPulse + repeatSpace);
227 // Finally add the trailer:
230 rx51device.addSingle(trailerPulse);
231 duration += trailerPulse;