Update to UI
[pierogi] / protocols / directvprotocol.cpp
1 #include "directvprotocol.h"
2
3 #include "pirrx51hardware.h"
4
5 #include "pirexception.h"
6 #include <string>
7
8 // Some global communications stuff:
9 #include <QMutex>
10 extern bool commandInFlight;
11 extern QMutex commandIFMutex;
12
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
23 // 30000 usec.
24 // There are three different carrier frequencies: 38 kHz, 40 kHz, and 57 kHz.
25
26 DirectvProtocol::DirectvProtocol(
27   QObject *guiObject,
28   unsigned int index)
29   : PIRProtocol(guiObject, index, 9000, false)
30 {
31 }
32
33
34 void DirectvProtocol::setProtocolParms(
35   DirectvGapSize gap,
36   DirectvFreq freq)
37 {
38   if (gap == ShortGap_Directv)
39   {
40     setGapSize(9000, false);
41   }
42   else
43   {
44     setGapSize(30000, false);
45   }
46
47   switch (freq)
48   {
49   case LowFreq_Directv:
50     setCarrierFrequency(38000);
51     break;
52
53   case MediumFreq_Directv:
54     setCarrierFrequency(40000);
55     break;
56
57   case HighFreq_Directv: default:
58     setCarrierFrequency(57000);
59     break;
60   }
61 }
62
63
64 void DirectvProtocol::startSendingCommand(
65   unsigned int threadableID,
66   PIRKeyName command)
67 {
68   // Exceptions here are problematic; I'll try to weed them out by putting the
69   // whole thing in a try/catch block:
70   try
71   {
72     // First, check if we are meant to be the recipient of this command:
73     if (threadableID != id) return;
74
75     clearRepeatFlag();
76
77     KeycodeCollection::const_iterator i = keycodes.find(command);
78
79     // Do we even have this key defined?
80     if (i == keycodes.end())
81     {
82       std::string s = "Tried to send a non-existent command.\n";
83       throw PIRException(s);
84     }
85
86     // construct the device:
87     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
88
89     int repeatCount = 0;
90     int commandDuration = 0;
91     while (repeatCount < MAX_REPEAT_COUNT)
92     {
93       // When repeating, we use a short-pulse header.  Otherwise, we use
94       // a long-pulse header.
95       if (repeatCount)
96       {
97         rx51device.addPair(3000, 1200);
98         commandDuration += 4200;
99       }
100       else
101       {
102         rx51device.addPair(6000, 1200);
103         commandDuration += 7200;
104       }
105
106       // Now, generate the rest of the command:
107       commandDuration += generateStandardCommand((*i).second, rx51device);
108
109       // Now, tell the device to send the whole command:
110       rx51device.sendCommandToDevice();
111
112       // sleep until the next repetition of command:
113       sleepUntilRepeat(commandDuration);
114
115       // Check whether we've reached the minimum required number of repetitons:
116       if (repeatCount >= minimumRepetitions)
117       {
118         // Check whether we've been asked to stop:
119         if (checkRepeatFlag())
120         {
121           QMutexLocker cifLocker(&commandIFMutex);
122           commandInFlight = false;
123           return;
124         }
125       }
126
127       ++repeatCount;
128     }
129   }
130   catch (PIRException e)
131   {
132     // inform the gui:
133     emit commandFailed(e.getError().c_str());
134   }
135
136   QMutexLocker cifLocker(&commandIFMutex);
137   commandInFlight = false;
138 }
139
140
141 int DirectvProtocol::generateStandardCommand(
142   const PIRKeyBits &pkb,
143   PIRRX51Hardware &rx51device)
144 {
145   int duration = 0;
146
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.
155
156   // - "preData" will contain the address
157   // - "firstCode" will contain the command
158   duration += pushDTVBits(preData, rx51device);
159   duration += pushDTVBits(pkb.firstCode, rx51device);
160
161   CommandSequence checksumBits;
162   generateChecksum(pkb.firstCode, checksumBits);
163   duration += pushDTVBits(checksumBits, rx51device);
164
165   // Finally add the "trail":
166   rx51device.addSingle(600);
167   duration += 600;
168
169   return duration;
170 }
171
172
173 int DirectvProtocol::pushDTVBits(
174   const CommandSequence &bits,
175   PIRRX51Hardware &rx51device)
176 {
177   int duration = 0;
178   CommandSequence::const_iterator i = bits.begin();
179   while (i != bits.end())
180   {
181     if (*i)
182     {
183       // Send the value for "One":
184       rx51device.addSingle(1200);
185       duration += 1200;
186     }
187     else
188     {
189       // Send the value for "Zero":
190       rx51device.addSingle(600);
191       duration += 600;
192     }
193     ++i;
194   }
195
196   return duration;
197 }
198
199
200 void DirectvProtocol::generateChecksum(
201   const CommandSequence &bits,
202   CommandSequence &checksumBits)
203 {
204   // We'll generate a 4-bit value based on the 8 bits of the command:
205
206   int checksumVal = 0;
207
208   CommandSequence::const_iterator i = bits.begin();
209
210   if (i == bits.end()) return;
211
212   if (*i) checksumVal += 14;
213
214   ++i;
215
216   if (i == bits.end()) return;
217
218   if (*i) checksumVal += 7;
219
220   ++i;
221
222   if (i == bits.end()) return;
223
224   if (*i) checksumVal += 10;
225
226   ++i;
227
228   if (i == bits.end()) return;
229
230   if (*i) checksumVal += 5;
231
232   ++i;
233
234   if (i == bits.end()) return;
235
236   if (*i) checksumVal += 6;
237
238   ++i;
239
240   if (i == bits.end()) return;
241
242   if (*i) checksumVal += 3;
243
244   ++i;
245
246   if (i == bits.end()) return;
247
248   if (*i) checksumVal += 2;
249
250   ++i;
251
252   if (i == bits.end()) return;
253
254   if (*i) checksumVal += 1;
255
256   // Push the last four bits into the command sequence:
257   if (checksumVal & 0x8)  
258   {
259     checksumBits.push_back(1);
260   }
261   else
262   {
263     checksumBits.push_back(0);
264   }
265
266   if (checksumVal & 0x4)
267   {
268     checksumBits.push_back(1);
269   }
270   else
271   {
272     checksumBits.push_back(0);
273   }
274
275   if (checksumVal & 0x2)
276   {
277     checksumBits.push_back(1);
278   }
279   else
280   {
281     checksumBits.push_back(0);
282   }
283
284   if (checksumVal & 0x1)
285   {
286     checksumBits.push_back(1);
287   }
288   else
289   {
290     checksumBits.push_back(0);
291   }
292 }