1 #include "lircprotocol.h"
3 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 LIRCProtocol::LIRCProtocol(
29 hasTrailerPulse(false),
31 repeatNeedsHeader(false),
32 fullHeadlessRepeat(false)
36 void LIRCProtocol::setHeaderPair(
45 void LIRCProtocol::setTrailerPulse(
49 hasTrailerPulse = true;
52 void LIRCProtocol::setRepeatPair(
61 void LIRCProtocol::setRepeatNeedsHeader(
64 repeatNeedsHeader = flag;
67 void LIRCProtocol::setFullHeadlessRepeat(
70 fullHeadlessRepeat = flag;
73 void LIRCProtocol::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:
81 // First, check if we are meant to be the recipient of this command:
82 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 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
99 int commandDuration = 0;
100 while (repeatCount < MAX_REPEAT_COUNT)
102 // If we are currently repeating, and have a special "repeat signal",
103 // use that signal. Otherwise, generate a normal command string.
104 if (hasRepeatPair && repeatCount)
106 commandDuration = generateRepeatCommand(rx51device);
108 else if (fullHeadlessRepeat && repeatCount)
110 commandDuration = generateHeadlessCommand((*i).second, rx51device);
114 commandDuration = generateStandardCommand((*i).second, rx51device);
117 // Now, tell the device to send the whole command:
118 rx51device.sendCommandToDevice();
120 // sleep until the next repetition of command:
121 sleepUntilRepeat(commandDuration);
123 // Check whether we've reached the minimum required number of repetitons:
124 if (repeatCount >= minimumRepetitions)
126 // Check whether we've been asked to stop:
127 if (checkRepeatFlag())
129 QMutexLocker cifLocker(&commandIFMutex);
130 commandInFlight = false;
138 catch (PIRException e)
141 emit commandFailed(e.getError().c_str());
144 QMutexLocker cifLocker(&commandIFMutex);
145 commandInFlight = false;
149 int LIRCProtocol::generateStandardCommand(
150 const PIRKeyBits &pkb,
151 PIRRX51Hardware &rx51device)
155 // First, the "header" pulse (if any):
158 rx51device.addPair(headerPulse, headerSpace);
159 duration += (headerPulse + headerSpace);
162 // For LIRC, just dump the bits straight into the device, one by one:
163 duration += pushBits(preData, rx51device);
164 duration += pushBits(pkb.firstCode, rx51device);
165 duration += pushBits(postData, rx51device);
167 // Finally add the "trail":
170 rx51device.addSingle(trailerPulse);
171 duration += trailerPulse;
178 int LIRCProtocol::generateHeadlessCommand(
179 const PIRKeyBits &pkb,
180 PIRRX51Hardware &rx51device)
184 // First, the "pre" data:
185 duration += pushBits(preData, rx51device);
187 // Next, add the actual command:
188 duration += pushBits(pkb.firstCode, rx51device);
190 // Next, add the "post" data:
191 duration += pushBits(postData, rx51device);
193 // Finally add the "trail":
196 rx51device.addSingle(trailerPulse);
197 duration += trailerPulse;
204 int LIRCProtocol::generateRepeatCommand(
205 PIRRX51Hardware &rx51device)
209 // Do we need the header?
210 if (repeatNeedsHeader)
212 // Do we even have a header?
215 // Ok, then add the header to the repeat:
216 rx51device.addPair(headerPulse, headerSpace);
217 duration += (headerPulse + headerSpace);
221 // Add the repeat pulse:
222 rx51device.addPair(repeatPulse, repeatSpace);
223 duration += (repeatPulse + repeatSpace);
225 // Finally add the trailer:
228 rx51device.addSingle(trailerPulse);
229 duration += trailerPulse;