1 #include "kaseikyoprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // Getting good data on the Kaseikyo protocol(s) is difficult. The following
13 // is my current understanding:
14 // A "zero" is encoded with a 432 usec pulse, 432 usec space.
15 // A "one" is encoded with a 432 usec pulse, and 3*432 (1296) usec space.
16 // The header has a 3456 usec pulse, and a 1728 usec space.
17 // Commands end with a trailing 432 usec pulse.
18 // When repeating, the entire pulse train is re-sent.
19 // There is a 74736 usec gap between repeated commands.
20 // The carrier frequency is 37 kHz.
22 KaseikyoProtocol::KaseikyoProtocol(
33 setCarrierFrequency(37000);
37 void KaseikyoProtocol::startSendingCommand(
38 unsigned int threadableID,
41 // Exceptions here are problematic; I'll try to weed them out by putting the
42 // whole thing in a try/catch block:
45 // First, check if we are meant to be the recipient of this command:
46 if (threadableID != id) return;
50 KeycodeCollection::const_iterator i = keycodes.find(command);
52 // Do we even have this key defined?
53 if (i == keycodes.end())
55 QMutexLocker cifLocker(&commandIFMutex);
56 commandInFlight = false;
58 // std::string s = "Tried to send a non-existent command.\n";
59 // throw PIRException(s);
62 // construct the device:
63 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
66 int commandDuration = 0;
67 while (repeatCount < MAX_REPEAT_COUNT)
69 commandDuration = generateStandardCommand((*i).second, rx51device);
71 // Now, tell the device to send the whole command:
72 rx51device.sendCommandToDevice();
74 // sleep until the next repetition of command:
75 sleepUntilRepeat(commandDuration);
77 // Check whether we've reached the minimum required number of repetitons:
78 if (repeatCount >= minimumRepetitions)
80 // Check whether we've been asked to stop:
81 if (checkRepeatFlag())
85 QMutexLocker cifLocker(&commandIFMutex);
86 commandInFlight = false;
95 QMutexLocker cifLocker(&commandIFMutex);
96 commandInFlight = false;
98 catch (PIRException e)
101 emit commandFailed(e.getError().c_str());
106 int KaseikyoProtocol::generateStandardCommand(
107 const PIRKeyBits &pkb,
108 PIRRX51Hardware &rx51device)
112 // First, the header pulse:
113 rx51device.addPair(headerPulse, headerSpace);
114 duration += (headerPulse + headerSpace);
116 // While I don't yet fully understand the contents of the protocol, the
117 // data is obviously split into a first half (supposedly containing
118 // manufacturer ID codes) and a second half (containing address and command
119 // data). The first half is 16 bits long plus a 4 bit checksum, and the
120 // second half is 24 bits long plus a 4 bit checksum. Of that second half,
121 // the first 12 bits are probably the address, and the last 12 bits are split
122 // into 8 bits of command followed by 4 bits that turn out to be the last
123 // four bits of the command xored with the middle 4 bits of the address.
124 // (At least for Panasonic.) Very strange.
125 // For now, I'm going with this game plan:
126 // -- The "preData" will contain the 16 bits of manufacturer data as a
128 // -- The "firstCode" will contain the 12 address bits.
129 // -- The "secondCode" will contain the 8 command bits.
130 // -- I'll generate the three checksums below.
132 CommandSequence checksum;
134 // The "manufacturer codes":
135 duration += pushReverseBits(preData, rx51device);
137 generateChecksum(preData, checksum);
138 duration += pushReverseBits(checksum, rx51device);
140 // The command portion:
141 // First, the address and command:
142 duration += pushReverseBits(pkb.firstCode, rx51device);
143 duration += pushReverseBits(pkb.secondCode, rx51device);
145 // Next, the odd little checksum:
146 CommandSequence littleChecksum;
147 generateLittleChecksum(pkb.firstCode, pkb.secondCode, littleChecksum);
148 duration += pushReverseBits(littleChecksum, rx51device);
150 // Finally, the last checksum:
152 generateChecksum(pkb.firstCode, checksum);
153 generateChecksum(pkb.secondCode, checksum);
154 generateChecksum(littleChecksum, checksum);
155 duration += pushReverseBits(checksum, rx51device);
157 // Add the trailer pulse:
158 rx51device.addSingle(trailerPulse);
159 duration += trailerPulse;
165 void KaseikyoProtocol::generateChecksum(
166 const CommandSequence &bits,
167 CommandSequence &checksum)
174 CommandSequence::const_iterator i = bits.begin();
176 // Set up the first four bits:
177 if (i == bits.end()) return; // this shouldn't happen, throw an error?
181 if (i == bits.end()) return;
185 if (i == bits.end()) return;
189 if (i == bits.end()) return;
193 while (i != bits.end())
217 // Now, either insert these bits into the checksum, or xor them with the
218 // bits already in the checksum.
219 if (checksum.empty())
221 checksum.push_back(bit1);
222 checksum.push_back(bit2);
223 checksum.push_back(bit3);
224 checksum.push_back(bit4);
228 CommandSequence::iterator j = checksum.begin();
229 if (j != checksum.end())
235 if (j != checksum.end())
241 if (j != checksum.end())
247 if (j != checksum.end())
255 void KaseikyoProtocol::generateLittleChecksum(
256 const CommandSequence &firstBits,
257 const CommandSequence &secondBits,
258 CommandSequence &littleChecksum)
260 CommandSequence::const_iterator i = firstBits.begin();
261 CommandSequence::const_iterator j = secondBits.begin();
263 // Advance both iterators by 4 bits:
265 while ((index < 4) && (i != firstBits.end()) && (j != secondBits.end()))
272 // Xor the next four bits and store them:
274 while ((index < 4) && (i != firstBits.end()) && (j != secondBits.end()))
276 littleChecksum.push_back(*i ^ *j);