Moved Mute button, lots new keysets
[pierogi] / protocols / sharpprotocol.cpp
1 #include "sharpprotocol.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 // Sharp's protocol should have the following attributes:
14 // A "zero" is encoded with a 320 usec pulse, 680 usec space.
15 // A "one" is encoded with a 320 usec pulse, and 1680 usec space.
16 // There is no header pulse.
17 // The pulse train ends with a trailing 320 usec pulse.
18 // For repeating, the entire train is re-sent, except that the command
19 // section (and the last two bits) are inverted in each odd repeat.
20 // There is a 40000 usec delay between the end of one command and the start
21 // of the next.
22 // The command should be repeated at least once.
23 // The carrier frequency is 38 kHz, duty cycle is 1/3.
24
25 SharpProtocol::SharpProtocol(
26   QObject *guiObject,
27   unsigned int index,
28   bool expBit)
29   : SpaceProtocol(
30       guiObject, index,
31       320, 680,
32       320, 1680,
33       0, 0,
34       320,
35       40000, false),
36     expansionBit(expBit)
37 {
38   setCarrierFrequency(38000);
39   setDutyCycle(33);
40 }
41
42
43 void SharpProtocol::startSendingCommand(
44   unsigned int threadableID,
45   PIRKeyName command)
46 {
47   // Exceptions here are problematic; I'll try to weed them out by putting the
48   // whole thing in a try/catch block:
49   try
50   {
51     // First, check if we are meant to be the recipient of this command:
52     if (threadableID != id) return;
53
54     clearRepeatFlag();
55
56     KeycodeCollection::const_iterator i = keycodes.find(command);
57
58     // Do we even have this key defined?
59     if (i == keycodes.end())
60     {
61       std::string s = "Tried to send a non-existent command.\n";
62       throw PIRException(s);
63     }
64
65     // construct the device:
66     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
67
68     int repeatCount = 0;
69     int commandDuration = 0;
70     while (repeatCount < MAX_REPEAT_COUNT)
71     {
72       // Every other repeat count, we invert everything but the address:
73       if (repeatCount % 2)
74       {
75         commandDuration = generateToggledCommand((*i).second, rx51device);
76       }
77       else
78       {
79         commandDuration = generateStandardCommand((*i).second, rx51device);
80       }
81
82       // Now, tell the device to send the whole command:
83       rx51device.sendCommandToDevice();
84
85       // sleep until the next repetition of command:
86       sleepUntilRepeat(commandDuration);
87
88       // Check whether we've reached the minimum required number of repetitons:
89 //      if (repeatCount >= minimumRepetitions)
90       if (repeatCount >= 1)
91       {
92         // Check whether we've been asked to stop:
93         if (checkRepeatFlag())
94         {
95           QMutexLocker cifLocker(&commandIFMutex);
96           commandInFlight = false;
97           return;
98         }
99       }
100
101       ++repeatCount;
102     }
103   }
104   catch (PIRException e)
105   {
106     // inform the gui:
107     emit commandFailed(e.getError().c_str());
108   }
109
110   QMutexLocker cifLocker(&commandIFMutex);
111   commandInFlight = false;
112 }
113
114
115 int SharpProtocol::generateStandardCommand(
116   const PIRKeyBits &pkb,
117   PIRRX51Hardware &rx51device)
118 {
119   int duration = 0;
120
121   // First, push the address:
122   duration += pushReverseBits(pkb.firstCode, rx51device);
123
124   // Next, push the command:
125   duration += pushReverseBits(pkb.secondCode, rx51device);
126
127   // Next, there is an "expansion" bit and a "check" bit.  Not entirely sure
128   // what these two do.  The check bit is fixed at "0".
129   if (expansionBit)
130   {
131     rx51device.addPair(onePulse, oneSpace);
132     duration += (onePulse + oneSpace);
133   }
134   else
135   {
136     rx51device.addPair(zeroPulse, zeroSpace);
137     duration += (zeroPulse + zeroSpace);
138   }
139
140   rx51device.addPair(zeroPulse, zeroSpace);
141   duration += (zeroPulse + zeroSpace);
142
143   // Finally add the "trail":
144   rx51device.addSingle(trailerPulse);
145   duration += trailerPulse;
146
147   return duration;
148 }
149
150
151 // This is the same as the standard command, except all bits but the address
152 // are inverted:
153 int SharpProtocol::generateToggledCommand(
154   const PIRKeyBits &pkb,
155   PIRRX51Hardware &rx51device)
156 {
157   int duration = 0;
158
159   pushReverseBits(pkb.firstCode, rx51device);
160
161   // This time we invert the command bits:
162   pushInvertedReverseBits(pkb.secondCode, rx51device);
163
164   // We'll also invert the two administrative bits here:
165   if (expansionBit)
166   {
167     rx51device.addPair(zeroPulse, zeroSpace);
168     duration += (zeroPulse + zeroSpace);
169   }
170   else
171   {
172     rx51device.addPair(onePulse, oneSpace);
173     duration += (onePulse + oneSpace);
174   }
175
176   rx51device.addPair(onePulse, oneSpace);
177   duration += (onePulse + oneSpace);
178
179   // Add trail on end:
180   rx51device.addSingle(trailerPulse);
181   duration += trailerPulse;
182
183   return duration;
184 }