Search for Power Button Panel
[pierogi] / protocols / thomsonprotocol.cpp
1 #include "thomsonprotocol.h"
2
3 #include "pirrx51hardware.h"
4
5 #include "pirexception.h"
6
7 // Some global communications stuff:
8 #include <QMutex>
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
11
12 // This Thomson protocol is a very simple one:
13 // A "zero" is encoded with a 500 usec pulse, 2000 usec space.
14 // A "one" is encoded with a 500 usec pulse, and 4500 usec space.
15 // There is no header.
16 // Commands end with a trailing 500 usec pulse.
17 // When repeating, the full pulse train is re-sent.
18 // Each command runs for 80000 usec total.
19 // The carrier frequency should be 33 kHz, but I'm not so sure...
20
21 ThomsonProtocol::ThomsonProtocol(
22   QObject *guiObject,
23   unsigned int index)
24   : SpaceProtocol(
25       guiObject, index,
26       500, 2000,
27       500, 4500,
28       0, 0,
29       500,
30       80000, true),
31     keypressCount(0)
32 {
33 }
34
35
36 void ThomsonProtocol::startSendingCommand(
37   unsigned int threadableID,
38   PIRKeyName command)
39 {
40   // Exceptions here are problematic; I'll try to weed them out by putting the
41   // whole thing in a try/catch block:
42   try
43   {
44     // First, check if we are meant to be the recipient of this command:
45     if (threadableID != id) return;
46
47     clearRepeatFlag();
48
49     KeycodeCollection::const_iterator i = keycodes.find(command);
50
51     // Do we even have this key defined?
52     if (i == keycodes.end())
53     {
54       QMutexLocker cifLocker(&commandIFMutex);
55       commandInFlight = false;
56       return;
57 //      std::string s = "Tried to send a non-existent command.\n";
58 //      throw PIRException(s);
59     }
60
61     // construct the device:
62     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
63
64     int repeatCount = 0;
65     int commandDuration = 0;
66     while (repeatCount < MAX_REPEAT_COUNT)
67     {
68       commandDuration = generateStandardCommand((*i).second, rx51device);
69
70       // Now, tell the device to send the whole command:
71       rx51device.sendCommandToDevice();
72
73       // sleep until the next repetition of command:
74       sleepUntilRepeat(commandDuration);
75
76       // Check whether we've reached the minimum required number of repetitons:
77       if (repeatCount >= minimumRepetitions)
78       {
79         // Check whether we've been asked to stop:
80         if (checkRepeatFlag())
81         {
82           break;
83 /*
84           ++keypressCount;
85           QMutexLocker cifLocker(&commandIFMutex);
86           commandInFlight = false;
87           return;
88 */
89         }
90       }
91
92       ++repeatCount;
93     }
94
95     ++keypressCount;
96     QMutexLocker cifLocker(&commandIFMutex);
97     commandInFlight = false;
98   }
99   catch (PIRException e)
100   {
101     // inform the gui:
102     emit commandFailed(e.getError().c_str());
103   }
104 }
105
106
107 int ThomsonProtocol::generateStandardCommand(
108   const PIRKeyBits &pkb,
109   PIRRX51Hardware &rx51device)
110 {
111   int duration = 0;
112
113   // First, four bits of address:
114   duration += pushBits(preData, rx51device);
115
116   // Next, the toggle bit:
117   if (keypressCount % 2)
118   {
119     rx51device.addPair(onePulse, oneSpace);
120     duration += (onePulse + oneSpace);
121   }
122   else
123   {
124     rx51device.addPair(zeroPulse, zeroSpace);
125     duration += (zeroPulse + zeroSpace);
126   }
127
128   // Next, seven bits of command:
129   duration += pushBits(pkb.firstCode, rx51device);
130
131   // Finally add the "trail":
132   rx51device.addSingle(trailerPulse);
133   duration += trailerPulse;
134
135   return duration;
136 }