1 #include "directvprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
8 // Some global communications stuff:
10 extern bool commandInFlight;
11 extern QMutex commandIFMutex;
13 // Directv is using a protocol that is novel to me: each pulse and space
14 // encodes an entire bit in an of themselves.
15 // Here are the details I've got:
16 // For a 0 bit, a duration of 600 usecs is used.
17 // For a 1 bit, the duration is 1200 usecs.
18 // The header is a 6000 usec pulse, 1200 usec space.
19 // Commands end with a trailing 600 usec pulse.
20 // Commands are repeated by re-sending the entire pulse train, except that
21 // the header pulse is cut in half to 3000 usec.
22 // There are two different sizes of gap between commands: 9000 usec and
24 // There are three different carrier frequencies: 38 kHz, 40 kHz, and 57 kHz.
26 DirectvProtocol::DirectvProtocol(
31 : PIRProtocol(guiObject, index, 9000, false)
35 setGapSize(30000, false);
41 setCarrierFrequency(38000);
45 setCarrierFrequency(40000);
48 case HighFreq: default:
49 setCarrierFrequency(57000);
55 void DirectvProtocol::startSendingCommand(
56 unsigned int threadableID,
59 // Exceptions here are problematic; I'll try to weed them out by putting the
60 // whole thing in a try/catch block:
63 // First, check if we are meant to be the recipient of this command:
64 if (threadableID != id) return;
68 KeycodeCollection::const_iterator i = keycodes.find(command);
70 // Do we even have this key defined?
71 if (i == keycodes.end())
73 std::string s = "Tried to send a non-existent command.\n";
74 throw PIRException(s);
77 // construct the device:
78 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
81 int commandDuration = 0;
82 while (repeatCount < MAX_REPEAT_COUNT)
84 // When repeating, we use a short-pulse header. Otherwise, we use
85 // a long-pulse header.
88 rx51device.addPair(3000, 1200);
89 commandDuration += 4200;
93 rx51device.addPair(6000, 1200);
94 commandDuration += 7200;
97 // Now, generate the rest of the command:
98 commandDuration += generateStandardCommand((*i).second, rx51device);
100 // Now, tell the device to send the whole command:
101 rx51device.sendCommandToDevice();
103 // sleep until the next repetition of command:
104 sleepUntilRepeat(commandDuration);
106 // Check whether we've reached the minimum required number of repetitons:
107 if (repeatCount >= minimumRepetitions)
109 // Check whether we've been asked to stop:
110 if (checkRepeatFlag())
112 QMutexLocker cifLocker(&commandIFMutex);
113 commandInFlight = false;
121 catch (PIRException e)
124 emit commandFailed(e.getError().c_str());
127 QMutexLocker cifLocker(&commandIFMutex);
128 commandInFlight = false;
132 int DirectvProtocol::generateStandardCommand(
133 const PIRKeyBits &pkb,
134 PIRRX51Hardware &rx51device)
138 // DirecTV's protocol appears to consist of four bits of address data,
139 // eight bits of command data, and four bits of checksum data.
140 // The checksum is calculated off of the command, as so: Add up:
141 // 7 * the first two bits of the command, plus
142 // 5 * the second two bits of the command, plus
143 // 3 * the third two bits of the command, plus
144 // 1 * the last two bits of the command,
145 // then just take the last four bits of this value.
147 // - "preData" will contain the address
148 // - "firstCode" will contain the command
149 duration += pushDTVBits(preData, rx51device);
150 duration += pushDTVBits(pkb.firstCode, rx51device);
152 CommandSequence checksumBits;
153 generateChecksum(pkb.firstCode, checksumBits);
154 duration += pushDTVBits(checksumBits, rx51device);
156 // Finally add the "trail":
157 rx51device.addSingle(600);
164 int DirectvProtocol::pushDTVBits(
165 const CommandSequence &bits,
166 PIRRX51Hardware &rx51device)
169 CommandSequence::const_iterator i = bits.begin();
170 while (i != bits.end())
174 // Send the value for "One":
175 rx51device.addSingle(1200);
180 // Send the value for "Zero":
181 rx51device.addSingle(600);
191 void DirectvProtocol::generateChecksum(
192 const CommandSequence &bits,
193 CommandSequence &checksumBits)
195 // We'll generate a 4-bit value based on the 8 bits of the command:
199 CommandSequence::const_iterator i = bits.begin();
201 if (i == bits.end()) return;
203 if (*i) checksumVal += 14;
207 if (i == bits.end()) return;
209 if (*i) checksumVal += 7;
213 if (i == bits.end()) return;
215 if (*i) checksumVal += 10;
219 if (i == bits.end()) return;
221 if (*i) checksumVal += 5;
225 if (i == bits.end()) return;
227 if (*i) checksumVal += 6;
231 if (i == bits.end()) return;
233 if (*i) checksumVal += 3;
237 if (i == bits.end()) return;
239 if (*i) checksumVal += 2;
243 if (i == bits.end()) return;
245 if (*i) checksumVal += 1;
247 // Push the last four bits into the command sequence:
248 if (checksumVal & 0x8)
250 checksumBits.push_back(1);
254 checksumBits.push_back(0);
257 if (checksumVal & 0x4)
259 checksumBits.push_back(1);
263 checksumBits.push_back(0);
266 if (checksumVal & 0x2)
268 checksumBits.push_back(1);
272 checksumBits.push_back(0);
275 if (checksumVal & 0x1)
277 checksumBits.push_back(1);
281 checksumBits.push_back(0);