Search for Power Button Panel
[pierogi] / protocols / sircprotocol.cpp
1 #include "sircprotocol.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 // The SIRC protocol should have the following attributes:
14 // A "zero" is encoded with a 600 usec pulse, 600 usec space.
15 // A "one" is encoded with a 1200 usec pulse, and 600 usec space.
16 // The header is a 2400 usec pulse, 600 usec space.
17 // There is no trailing pulse.
18 // When repeating a command, the entire train is re-broadcast every 45000 usec.
19 // The carrier frequency is 40 kHz, duty cycle is 1/3.
20
21 SIRCProtocol::SIRCProtocol(
22   QObject *guiObject,
23   unsigned int index)
24   : SpaceProtocol(
25       guiObject, index,
26       600, 600,
27       1200, 600,
28       2400, 600,
29       0,
30       45000, true)
31 {
32   setCarrierFrequency(40000);
33 //  setDutyCycle(33);
34 }
35
36
37 void SIRCProtocol::startSendingCommand(
38   unsigned int threadableID,
39   PIRKeyName command)
40 {
41   // Exceptions here are problematic; I'll try to weed them out by putting the
42   // whole thing in a try/catch block:
43   try
44   {
45     // First, check if we are meant to be the recipient of this command:
46     if (threadableID != id) return;
47
48     clearRepeatFlag();
49
50     KeycodeCollection::const_iterator i = keycodes.find(command);
51
52     // Do we even have this key defined?
53     if (i == keycodes.end())
54     {
55       QMutexLocker cifLocker(&commandIFMutex);
56       commandInFlight = false;
57       return;
58 //      std::string s = "Tried to send a non-existent command.\n";
59 //      throw PIRException(s);
60     }
61
62     // construct the device:
63     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
64
65     int repeatCount = 0;
66     int commandDuration = 0;
67     while (repeatCount < MAX_REPEAT_COUNT)
68     {
69       commandDuration = generateStandardCommand((*i).second, rx51device);
70
71       // Now, tell the device to send the whole command:
72       rx51device.sendCommandToDevice();
73
74       // sleep until the next repetition of command:
75       sleepUntilRepeat(commandDuration);
76
77       // Check whether we've reached the minimum required number of repetitons:
78 //      if (repeatCount >= minimumRepetitions)
79       if (repeatCount >= 3)
80       {
81         // Check whether we've been asked to stop:
82         if (checkRepeatFlag())
83         {
84           break;
85 /*
86           QMutexLocker cifLocker(&commandIFMutex);
87           commandInFlight = false;
88           return;
89 */
90         }
91       }
92
93       ++repeatCount;
94     }
95
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 SIRCProtocol::generateStandardCommand(
108   const PIRKeyBits &pkb,
109   PIRRX51Hardware &rx51device)
110 {
111   int duration = 0;
112
113   // First, the "header" pulse:
114   rx51device.addPair(headerPulse, headerSpace);
115   duration += (headerPulse + headerSpace);
116
117   // Next, push the data.
118   // These bits are sent in reverse order, and moreover, the codes are sent
119   // in reverse order as well:
120   duration += pushReverseBits(pkb.thirdCode, rx51device);
121   duration += pushReverseBits(pkb.secondCode, rx51device);
122   duration += pushReverseBits(pkb.firstCode, rx51device);
123
124   return duration;
125 }