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 // Check if a command is running:
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 unsigned int PIRProtocol::getCarrierFrequency() const
56 return carrierFrequency;
60 void PIRProtocol::setCarrierFrequency(
63 carrierFrequency = cf;
67 unsigned int PIRProtocol::getDutyCycle() const
73 void PIRProtocol::setDutyCycle(
80 void PIRProtocol::addKey(
82 unsigned long command,
85 // First, if key already exists, clear it out:
87 KeycodeCollection::iterator i = keycodes.find(key);
88 if (i != keycodes.end())
91 pkb->firstCode.clear();
95 pkb = &(keycodes[key]);
98 appendToBitSeq(pkb->firstCode, command, size);
102 void PIRProtocol::addSIRCKey(
104 unsigned int addressData,
106 unsigned int commandData)
108 // First, if key already exists, clear it out:
110 KeycodeCollection::iterator i = keycodes.find(key);
111 if (i != keycodes.end())
114 pkb->firstCode.clear();
115 pkb->secondCode.clear();
116 pkb->thirdCode.clear();
120 pkb = &(keycodes[key]);
123 // First, append the address data:
124 appendToBitSeq(pkb->firstCode, addressData, size);
126 // Next, the command data. The size is always 7 bits:
127 appendToBitSeq(pkb->secondCode, commandData, 7);
131 void PIRProtocol::addSIRC20Key(
133 unsigned int secondaryAddressData,
134 unsigned int primaryAddressData,
135 unsigned int commandData)
137 // First, if key already exists, clear it out:
139 KeycodeCollection::iterator i = keycodes.find(key);
140 if (i != keycodes.end())
143 pkb->firstCode.clear();
144 pkb->secondCode.clear();
145 pkb->thirdCode.clear();
149 pkb = &(keycodes[key]);
152 // First, append the secondary address data:
153 appendToBitSeq(pkb->firstCode, secondaryAddressData, 8);
155 // Next, the primary address data:
156 appendToBitSeq(pkb->secondCode, primaryAddressData, 5);
158 // Next, the command data. The size is always 7 bits:
159 appendToBitSeq(pkb->thirdCode, commandData, 7);
163 void PIRProtocol::addSharpKey(
165 unsigned int addressData,
166 unsigned int commandData)
168 // First, if key already exists, clear it out:
170 KeycodeCollection::iterator i = keycodes.find(key);
171 if (i != keycodes.end())
174 pkb->firstCode.clear();
175 pkb->secondCode.clear();
179 pkb = &(keycodes[key]);
182 // Sharp commands are all 5 bit address, 8 bit command:
183 appendToBitSeq(pkb->firstCode, addressData, 5);
184 appendToBitSeq(pkb->secondCode, commandData, 8);
188 void PIRProtocol::addNECKey(
190 unsigned int addressData,
191 unsigned int commandData)
194 KeycodeCollection::iterator i = keycodes.find(key);
195 if (i != keycodes.end())
198 pkb->firstCode.clear();
199 pkb->secondCode.clear();
200 pkb->thirdCode.clear();
201 pkb->fourthCode.clear();
205 pkb = &(keycodes[key]);
208 // NEC commands should always be 8 bits address, 8 bits command:
209 appendToBitSeq(pkb->firstCode, addressData, 8);
210 appendToBitSeq(pkb->secondCode, commandData, 8);
214 void PIRProtocol::addPanOldKey(
216 unsigned int addressData,
217 unsigned int commandData)
220 KeycodeCollection::iterator i = keycodes.find(key);
221 if (i != keycodes.end())
224 pkb->firstCode.clear();
225 pkb->secondCode.clear();
226 pkb->thirdCode.clear();
227 pkb->fourthCode.clear();
231 pkb = &(keycodes[key]);
234 // The "Old Panasonic" commands have 5 bits address, 6 bits command:
235 appendToBitSeq(pkb->firstCode, addressData, 5);
236 appendToBitSeq(pkb->secondCode, commandData, 6);
240 // Most Pioneer keys use the NEC key format, but some are pairs of
241 // NEC keys sent together:
242 void PIRProtocol::addPioneerKey(
244 unsigned int firstAddress,
245 unsigned int firstCommand,
246 unsigned int secondAddress,
247 unsigned int secondCommand)
250 KeycodeCollection::iterator i = keycodes.find(key);
251 if (i != keycodes.end())
254 pkb->firstCode.clear();
255 pkb->secondCode.clear();
256 pkb->thirdCode.clear();
257 pkb->fourthCode.clear();
261 pkb = &(keycodes[key]);
264 // All four codes should be 8 bits in length:
265 appendToBitSeq(pkb->firstCode, firstAddress, 8);
266 appendToBitSeq(pkb->secondCode, firstCommand, 8);
267 appendToBitSeq(pkb->thirdCode, secondAddress, 8);
268 appendToBitSeq(pkb->fourthCode, secondCommand, 8);
273 void PIRProtocol::addRCAKey(
275 unsigned int addressData,
276 unsigned int commandData)
279 KeycodeCollection::iterator i = keycodes.find(key);
280 if (i != keycodes.end())
283 pkb->firstcode.clear();
284 pkb->secondCode.clear();
288 pkb = &(keycodes[key]);
291 // Address is 4 bits, command is 8 bits:
292 appendToBitSeq(pkb->firstCode, addressData, 4);
293 appendToBitSeq(pkb->secondCode, commandData, 8);
298 void PIRProtocol::addKaseikyoKey(
300 unsigned int addressData,
301 unsigned int commandData)
304 KeycodeCollection::iterator i = keycodes.find(key);
305 if (i != keycodes.end())
308 pkb->firstCode.clear();
309 pkb->secondCode.clear();
313 pkb = &(keycodes[key]);
316 appendToBitSeq(pkb->firstCode, addressData, 12);
317 appendToBitSeq(pkb->secondCode, commandData, 8);
321 void PIRProtocol::addDishKey(
323 unsigned int firstCommand,
324 unsigned int secondCommand)
327 KeycodeCollection::iterator i = keycodes.find(key);
328 if (i != keycodes.end())
331 pkb->firstCode.clear();
332 pkb->secondCode.clear();
336 pkb = &(keycodes[key]);
339 appendToBitSeq(pkb->firstCode, firstCommand, 6);
340 appendToBitSeq(pkb->secondCode, secondCommand, 5);
344 void PIRProtocol::addXMPKey(
346 unsigned int firstCommand,
347 unsigned int secondCommand)
350 KeycodeCollection::iterator i = keycodes.find(key);
351 if (i != keycodes.end())
354 pkb->firstCode.clear();
355 pkb->secondCode.clear();
359 pkb = &(keycodes[key]);
362 appendToBitSeq(pkb->firstCode, firstCommand, 8);
363 appendToBitSeq(pkb->secondCode, secondCommand, 8);
367 void PIRProtocol::setMinimumRepetitions(
370 minimumRepetitions = minrep;
374 void PIRProtocol::setPreData(
378 // If the container is not empty, first clear it out:
379 if (!preData.empty())
384 appendToBitSeq(preData, data, bits);
388 void PIRProtocol::setPostData(
392 // If the container is not empty, first clear it out:
393 if (!postData.empty())
398 appendToBitSeq(postData, data, bits);
402 bool PIRProtocol::isCommandSupported(
405 return (keycodes.find(command) != keycodes.end());
409 void PIRProtocol::appendToBitSeq(
410 CommandSequence &sequence,
416 // This is bad, but just return silently for now...
420 // For each bit in the char, append a 1 or a 0 into the sequence.
421 // Starting with the largest bit, move forward one bit at a time:
422 unsigned int currentBit = 1 << (size - 1);
426 if (bits & currentBit)
428 sequence.push_back(1);
432 sequence.push_back(0);
435 currentBit = currentBit >> 1;
437 while (currentBit > 0);
441 void PIRProtocol::clearRepeatFlag()
443 QMutexLocker locker(&stopRepeatingMutex);
444 stopRepeatingFlag = false;
448 bool PIRProtocol::checkRepeatFlag()
450 QMutexLocker locker(&stopRepeatingMutex);
451 return stopRepeatingFlag;
455 // Note that the following routine blindly sleeps for the amount of time
456 // specified by the LIRC config file. The extra overhead of processing
457 // each command will mean that repeated commands will overshoot the config
458 // time by some amount. We could improve accuracy by waiting a little less
459 // than the specified time, if we could get a good handle on how long the
460 // overhead is delaying the command...
461 #define PIEROGI_OVERHEAD_HACK 13260
463 void PIRProtocol::sleepUntilRepeat(
468 // If the LIRC config file specifies the flag "CONST_LENGTH", that means
469 // the "gap" value is the exact amount of time to wait between kicking off
470 // each command. If not, then the "gap" needs to be added on to the total
471 // time of the previous command to see how long to sleep.
473 if (isConstantLength)
475 microseconds = (gap - commandDuration) - PIEROGI_OVERHEAD_HACK;
479 microseconds = gap - PIEROGI_OVERHEAD_HACK;
483 // Don't even bother sleeping if there's only a few microseconds:
484 if (microseconds < 1000)
489 // For now, I'm going to enforce a minimum sleep of 10 ms, so that we
490 // don't get runaway commands:
491 if (microseconds < 10000)
493 microseconds = 10000;
497 sleeptime.tv_sec = 0;
498 sleeptime.tv_nsec = microseconds * 1000;
500 timespec remainingtime;
502 if (nanosleep(&sleeptime, &remainingtime) == -1)
504 std::stringstream ss;
505 ss << "Problem while sleeping.\n";
506 ss << "Trying to sleep for: " << microseconds << "\n";
507 ss << "Nanosleep returned error: " << strerror(errno) << "\n";
508 throw PIRException(ss.str());
513 void PIRProtocol::setGapSize(
518 isConstantLength = iclFlag;