1 #include "pirprotocol.h"
9 #include "pirexception.h"
11 // A flag for communicating with the main thread:
12 extern bool stopRepeatingFlag;
13 extern QMutex stopRepeatingMutex;
15 // Total of all running commands
16 extern bool commandInFlight;
17 extern QMutex commandIFMutex;
19 // From what I understand (mostly from reading LIRC config files), NEC
20 // protocol based remotes mostly use a frequency of 38000 units and a
21 // duty cycle of 50%. They'll be set to these defaults here, and overridden
22 // as needed by child classes.
24 PIRProtocol::PIRProtocol(
29 : carrierFrequency(38000),
31 isConstantLength(iclflag),
33 minimumRepetitions(0),
36 qRegisterMetaType<PIRKeyName>("PIRKeyName");
40 SIGNAL(buttonPressed(unsigned int, PIRKeyName)),
42 SLOT(startSendingCommand(unsigned int, PIRKeyName)),
43 Qt::QueuedConnection);
47 SIGNAL(commandFailed(const char *)),
49 SLOT(receivedExternalWarning(const char *)),
50 Qt::QueuedConnection);
54 void PIRProtocol::addKey(
56 unsigned long command,
59 // First, if key already exists, clear it out:
61 KeycodeCollection::iterator i = keycodes.find(key);
62 if (i != keycodes.end())
65 pkb->firstCode.clear();
69 pkb = &(keycodes[key]);
72 appendToBitSeq(pkb->firstCode, command, size);
76 void PIRProtocol::addSIRCKey(
78 unsigned int addressData,
80 unsigned int commandData)
82 // First, if key already exists, clear it out:
84 KeycodeCollection::iterator i = keycodes.find(key);
85 if (i != keycodes.end())
88 pkb->firstCode.clear();
89 pkb->secondCode.clear();
90 pkb->thirdCode.clear();
94 pkb = &(keycodes[key]);
97 // First, append the address data:
98 appendToBitSeq(pkb->firstCode, addressData, size);
100 // Next, the command data. The size is always 7 bits:
101 appendToBitSeq(pkb->secondCode, commandData, 7);
105 void PIRProtocol::addSIRC20Key(
107 unsigned int secondaryAddressData,
108 unsigned int primaryAddressData,
109 unsigned int commandData)
111 // First, if key already exists, clear it out:
113 KeycodeCollection::iterator i = keycodes.find(key);
114 if (i != keycodes.end())
117 pkb->firstCode.clear();
118 pkb->secondCode.clear();
119 pkb->thirdCode.clear();
123 pkb = &(keycodes[key]);
126 // First, append the secondary address data:
127 appendToBitSeq(pkb->firstCode, secondaryAddressData, 8);
129 // Next, the primary address data:
130 appendToBitSeq(pkb->secondCode, primaryAddressData, 5);
132 // Next, the command data. The size is always 7 bits:
133 appendToBitSeq(pkb->thirdCode, commandData, 7);
137 void PIRProtocol::addSharpKey(
139 unsigned int addressData,
140 unsigned int commandData)
142 // First, if key already exists, clear it out:
144 KeycodeCollection::iterator i = keycodes.find(key);
145 if (i != keycodes.end())
148 pkb->firstCode.clear();
149 pkb->secondCode.clear();
153 pkb = &(keycodes[key]);
156 // Sharp commands are all 5 bit address, 8 bit command:
157 appendToBitSeq(pkb->firstCode, addressData, 5);
158 appendToBitSeq(pkb->secondCode, commandData, 8);
162 void PIRProtocol::addNECKey(
164 unsigned int addressData,
165 unsigned int commandData)
168 KeycodeCollection::iterator i = keycodes.find(key);
169 if (i != keycodes.end())
172 pkb->firstCode.clear();
173 pkb->secondCode.clear();
174 pkb->thirdCode.clear();
175 pkb->fourthCode.clear();
179 pkb = &(keycodes[key]);
182 // NEC commands should always be 8 bits address, 8 bits command:
183 appendToBitSeq(pkb->firstCode, addressData, 8);
184 appendToBitSeq(pkb->secondCode, commandData, 8);
188 // Most Pioneer keys use the NEC key format, but some are pairs of
189 // NEC keys sent together:
190 void PIRProtocol::addPioneerKey(
192 unsigned int firstAddress,
193 unsigned int firstCommand,
194 unsigned int secondAddress,
195 unsigned int secondCommand)
198 KeycodeCollection::iterator i = keycodes.find(key);
199 if (i != keycodes.end())
202 pkb->firstCode.clear();
203 pkb->secondCode.clear();
204 pkb->thirdCode.clear();
205 pkb->fourthCode.clear();
209 pkb = &(keycodes[key]);
212 // All four codes should be 8 bits in length:
213 appendToBitSeq(pkb->firstCode, firstAddress, 8);
214 appendToBitSeq(pkb->secondCode, firstCommand, 8);
215 appendToBitSeq(pkb->thirdCode, secondAddress, 8);
216 appendToBitSeq(pkb->fourthCode, secondCommand, 8);
221 void PIRProtocol::addRCAKey(
223 unsigned int addressData,
224 unsigned int commandData)
227 KeycodeCollection::iterator i = keycodes.find(key);
228 if (i != keycodes.end())
231 pkb->firstcode.clear();
232 pkb->secondCode.clear();
236 pkb = &(keycodes[key]);
239 // Address is 4 bits, command is 8 bits:
240 appendToBitSeq(pkb->firstCode, addressData, 4);
241 appendToBitSeq(pkb->secondCode, commandData, 8);
246 void PIRProtocol::setCarrierFrequency(
249 carrierFrequency = freq;
253 void PIRProtocol::setDutyCycle(
260 void PIRProtocol::setMinimumRepetitions(
263 minimumRepetitions = minrep;
267 void PIRProtocol::setPreData(
271 // If the container is not empty, first clear it out:
272 if (!preData.empty())
277 appendToBitSeq(preData, data, bits);
281 void PIRProtocol::setPostData(
285 // If the container is not empty, first clear it out:
286 if (!postData.empty())
291 appendToBitSeq(postData, data, bits);
295 bool PIRProtocol::isCommandSupported(
298 return (keycodes.find(command) != keycodes.end());
302 void PIRProtocol::appendToBitSeq(
303 CommandSequence &sequence,
309 // This is bad, but just return silently for now...
313 // For each bit in the char, append a 1 or a 0 into the sequence.
314 // Starting with the largest bit, move forward one bit at a time:
315 unsigned int currentBit = 1 << (size - 1);
319 if (bits & currentBit)
321 sequence.push_back(1);
325 sequence.push_back(0);
328 currentBit = currentBit >> 1;
330 while (currentBit > 0);
334 void PIRProtocol::clearRepeatFlag()
336 QMutexLocker locker(&stopRepeatingMutex);
337 stopRepeatingFlag = false;
341 bool PIRProtocol::checkRepeatFlag()
343 QMutexLocker locker(&stopRepeatingMutex);
344 return stopRepeatingFlag;
348 // Note that the following routine blindly sleeps for the amount of time
349 // specified by the LIRC config file. The extra overhead of processing
350 // each command will mean that repeated commands will overshoot the config
351 // time by some amount. We could improve accuracy by waiting a little less
352 // than the specified time, if we could get a good handle on how long the
353 // overhead is delaying the command...
354 #define PIEROGI_OVERHEAD_HACK 13260
356 void PIRProtocol::sleepUntilRepeat(
361 // If the LIRC config file specifies the flag "CONST_LENGTH", that means
362 // the "gap" value is the exact amount of time to wait between kicking off
363 // each command. If not, then the "gap" needs to be added on to the total
364 // time of the previous command to see how long to sleep.
366 if (isConstantLength)
368 microseconds = (gap - commandDuration) - PIEROGI_OVERHEAD_HACK;
372 microseconds = gap - PIEROGI_OVERHEAD_HACK;
376 // Don't even bother sleeping if there's only a few microseconds:
377 if (microseconds < 1000)
382 // For now, I'm going to enforce a minimum sleep of 10 ms, so that we
383 // don't get runaway commands:
384 if (microseconds < 10000)
386 microseconds = 10000;
390 sleeptime.tv_sec = 0;
391 sleeptime.tv_nsec = microseconds * 1000;
393 timespec remainingtime;
395 if (nanosleep(&sleeptime, &remainingtime) == -1)
397 std::stringstream ss;
398 ss << "Problem while sleeping.\n";
399 ss << "Trying to sleep for: " << microseconds << "\n";
400 ss << "Nanosleep returned error: " << strerror(errno) << "\n";
401 throw PIRException(ss.str());