1 #include "panasonicoldprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
8 extern bool commandInFlight;
9 extern QMutex commandIFMutex;
11 // The "Panasonic Old" protocol appears to have the following features:
12 // A "zero" is encoded with an 833 usec pulse, 833 usec space.
13 // A "one" is encoded with an 833 usec pulse, and 3*833 (2499) usec space.
14 // The header is a 3332 usec pulse, 3332 usec space.
15 // Commands end with a trailing 833 usec pulse.
16 // When repeating, the entire command is re-sent.
17 // Each command runs for approximately 105000 usec (need to check this)
18 // The carrier frequency might be 57.6 kHz.
20 PanasonicOldProtocol::PanasonicOldProtocol(
34 void PanasonicOldProtocol::startSendingCommand(
35 unsigned int threadableID,
38 // Exceptions here are problematic; I'll try to weed them out by putting the
39 // whole thing in a try/catch block:
42 // First, check if we are meant to be the recipient of this command:
43 if (threadableID != id) return;
47 KeycodeCollection::const_iterator i = keycodes.find(command);
49 // Do we even have this key defined?
50 if (i == keycodes.end())
52 QMutexLocker cifLocker(&commandIFMutex);
53 commandInFlight = false;
55 // std::string s = "Tried to send a non-existent command.\n";
56 // throw PIRException(s);
59 // construct the device:
60 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
63 int commandDuration = 0;
64 while (repeatCount < MAX_REPEAT_COUNT)
66 commandDuration = generateStandardCommand((*i).second, rx51device);
68 // Now, tell the device to send the whole command:
69 rx51device.sendCommandToDevice();
71 // sleep until the next repetition of command:
72 sleepUntilRepeat(commandDuration);
74 // Check whether we've reached the minimum required number of repetitons:
75 if (repeatCount >= minimumRepetitions)
77 // Check whether we've been asked to stop:
78 if (checkRepeatFlag())
82 QMutexLocker cifLocker(&commandIFMutex);
83 commandInFlight = false;
92 QMutexLocker cifLocker(&commandIFMutex);
93 commandInFlight = false;
95 catch (PIRException e)
98 emit commandFailed(e.getError().c_str());
103 int PanasonicOldProtocol::generateStandardCommand(
104 const PIRKeyBits &pkb,
105 PIRRX51Hardware &rx51device)
109 // First, the header pulse:
110 rx51device.addPair(headerPulse, headerSpace);
111 duration += (headerPulse + headerSpace);
113 // Supposedly, this protocol uses 5 bits of address and 6 bits of command,
114 // but it looks like the address can be variable. So, I'm going to treat
115 // them as more-or-less two separate sets of command data.
117 // It is laid out as follows:
118 // 1) the five bits of "address" data
119 // 2) the six bits of command data
120 // 3) repeat of the five bits of address data, inverted
121 // 4) repeat of the six bits of command data, inverted
123 duration += pushReverseBits(pkb.firstCode, rx51device);
124 duration += pushReverseBits(pkb.secondCode, rx51device);
125 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
126 duration += pushInvertedReverseBits(pkb.secondCode, rx51device);
128 // Add the trailer pulse:
129 rx51device.addSingle(trailerPulse);
130 duration += trailerPulse;