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(
29 : PIRProtocol(guiObject, index, 9000, false)
34 void DirectvProtocol::setProtocolParms(
38 if (gap == ShortGap_Directv)
40 setGapSize(9000, false);
44 setGapSize(30000, false);
50 setCarrierFrequency(38000);
53 case MediumFreq_Directv:
54 setCarrierFrequency(40000);
57 case HighFreq_Directv: default:
58 setCarrierFrequency(57000);
64 void DirectvProtocol::startSendingCommand(
65 unsigned int threadableID,
68 // Exceptions here are problematic; I'll try to weed them out by putting the
69 // whole thing in a try/catch block:
72 // First, check if we are meant to be the recipient of this command:
73 if (threadableID != id) return;
77 KeycodeCollection::const_iterator i = keycodes.find(command);
79 // Do we even have this key defined?
80 if (i == keycodes.end())
82 std::string s = "Tried to send a non-existent command.\n";
83 throw PIRException(s);
86 // construct the device:
87 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
90 int commandDuration = 0;
91 while (repeatCount < MAX_REPEAT_COUNT)
93 // When repeating, we use a short-pulse header. Otherwise, we use
94 // a long-pulse header.
97 rx51device.addPair(3000, 1200);
98 commandDuration += 4200;
102 rx51device.addPair(6000, 1200);
103 commandDuration += 7200;
106 // Now, generate the rest of the command:
107 commandDuration += generateStandardCommand((*i).second, rx51device);
109 // Now, tell the device to send the whole command:
110 rx51device.sendCommandToDevice();
112 // sleep until the next repetition of command:
113 sleepUntilRepeat(commandDuration);
115 // Check whether we've reached the minimum required number of repetitons:
116 if (repeatCount >= minimumRepetitions)
118 // Check whether we've been asked to stop:
119 if (checkRepeatFlag())
121 QMutexLocker cifLocker(&commandIFMutex);
122 commandInFlight = false;
130 catch (PIRException e)
133 emit commandFailed(e.getError().c_str());
136 QMutexLocker cifLocker(&commandIFMutex);
137 commandInFlight = false;
141 int DirectvProtocol::generateStandardCommand(
142 const PIRKeyBits &pkb,
143 PIRRX51Hardware &rx51device)
147 // DirecTV's protocol appears to consist of four bits of address data,
148 // eight bits of command data, and four bits of checksum data.
149 // The checksum is calculated off of the command, as so: Add up:
150 // 7 * the first two bits of the command, plus
151 // 5 * the second two bits of the command, plus
152 // 3 * the third two bits of the command, plus
153 // 1 * the last two bits of the command,
154 // then just take the last four bits of this value.
156 // - "preData" will contain the address
157 // - "firstCode" will contain the command
158 duration += pushDTVBits(preData, rx51device);
159 duration += pushDTVBits(pkb.firstCode, rx51device);
161 CommandSequence checksumBits;
162 generateChecksum(pkb.firstCode, checksumBits);
163 duration += pushDTVBits(checksumBits, rx51device);
165 // Finally add the "trail":
166 rx51device.addSingle(600);
173 int DirectvProtocol::pushDTVBits(
174 const CommandSequence &bits,
175 PIRRX51Hardware &rx51device)
178 CommandSequence::const_iterator i = bits.begin();
179 while (i != bits.end())
183 // Send the value for "One":
184 rx51device.addSingle(1200);
189 // Send the value for "Zero":
190 rx51device.addSingle(600);
200 void DirectvProtocol::generateChecksum(
201 const CommandSequence &bits,
202 CommandSequence &checksumBits)
204 // We'll generate a 4-bit value based on the 8 bits of the command:
208 CommandSequence::const_iterator i = bits.begin();
210 if (i == bits.end()) return;
212 if (*i) checksumVal += 14;
216 if (i == bits.end()) return;
218 if (*i) checksumVal += 7;
222 if (i == bits.end()) return;
224 if (*i) checksumVal += 10;
228 if (i == bits.end()) return;
230 if (*i) checksumVal += 5;
234 if (i == bits.end()) return;
236 if (*i) checksumVal += 6;
240 if (i == bits.end()) return;
242 if (*i) checksumVal += 3;
246 if (i == bits.end()) return;
248 if (*i) checksumVal += 2;
252 if (i == bits.end()) return;
254 if (*i) checksumVal += 1;
256 // Push the last four bits into the command sequence:
257 if (checksumVal & 0x8)
259 checksumBits.push_back(1);
263 checksumBits.push_back(0);
266 if (checksumVal & 0x4)
268 checksumBits.push_back(1);
272 checksumBits.push_back(0);
275 if (checksumVal & 0x2)
277 checksumBits.push_back(1);
281 checksumBits.push_back(0);
284 if (checksumVal & 0x1)
286 checksumBits.push_back(1);
290 checksumBits.push_back(0);