Improved to "half-way usable" (version 0.5)
[pierogi] / sircprotocol.cpp
1 #include "sircprotocol.h"
2
3 #include "pirexception.h"
4 #include <string>
5 //#include <iostream>
6
7 // Some global communications stuff:
8 #include <QMutex>
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
11
12 // The SIRC protocol should have the following attributes:
13 // A "zero" is encoded with a 600 usec pulse, 600 usec space.
14 // A "one" is encoded with a 1200 usec pulse, and 600 usec space.
15 // The header is a 2400 usec pulse, 600 usec space.
16 // There is no trailing pulse.
17 // When repeating a command, the entire train is re-broadcast every 45000 usec.
18 // The carrier frequency is 40 kHz, duty cycle is 1/3.
19
20 SIRCProtocol::SIRCProtocol(
21   QObject *guiObject,
22   unsigned int index)
23   : PIRProtocol(guiObject, index, 45000, true),
24     zeroPulse(600),
25     zeroSpace(600),
26     onePulse(1200),
27     oneSpace(600),
28     headerPulse(2400),
29     headerSpace(600)
30 {
31   setCarrierFrequency(40000);
32   setDutyCycle(33);
33 }
34
35
36 void SIRCProtocol::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       std::string s = "Tried to send a non-existent command.\n";
55       throw PIRException(s);
56     }
57
58     // construct the device:
59     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
60
61     int repeatCount = 0;
62     int commandDuration = 0;
63     while (repeatCount < MAX_REPEAT_COUNT)
64     {
65       commandDuration = generateStandardCommand((*i).second, rx51device);
66
67       // Now, tell the device to send the whole command:
68       rx51device.sendCommandToDevice();
69
70       // sleep until the next repetition of command:
71       sleepUntilRepeat(commandDuration);
72
73       // Check whether we've reached the minimum required number of repetitons:
74 //      if (repeatCount >= minimumRepetitions)
75       if (repeatCount >= 3)
76       {
77         // Check whether we've been asked to stop:
78         if (checkRepeatFlag())
79         {
80           QMutexLocker cifLocker(&commandIFMutex);
81           commandInFlight = false;
82           return;
83         }
84       }
85
86       ++repeatCount;
87     }
88   }
89   catch (PIRException e)
90   {
91     // inform the gui:
92     emit commandFailed(e.getError().c_str());
93   }
94
95   QMutexLocker cifLocker(&commandIFMutex);
96   commandInFlight = false;
97 }
98
99
100 int SIRCProtocol::generateStandardCommand(
101   const CommandSequence &bits,
102   PIRRX51Hardware &rx51device)
103 {
104   int duration = 0;
105
106   // First, the "header" pulse:
107   rx51device.addPair(headerPulse, headerSpace);
108   duration += (headerPulse + headerSpace);
109
110   // Next, push the data.  Each key _must_ contain all 12, 15, or 20 bits.
111   // These bits are sent in reverse order.
112   duration += pushReverseBits(bits, rx51device);
113
114   return duration;
115 }
116
117
118 int SIRCProtocol::pushReverseBits(
119   const CommandSequence &bits,
120   PIRRX51Hardware &rx51device)
121 {
122   int duration = 0;
123   CommandSequence::const_reverse_iterator i = bits.rbegin();
124   while (i != bits.rend())
125   {
126     if (*i)
127     {
128       // Send the pulse for "One":
129       rx51device.addPair(onePulse, oneSpace);
130       duration += (onePulse + oneSpace);
131     }
132     else
133     {
134       // Send the pulse for "Zero":
135       rx51device.addPair(zeroPulse, zeroSpace);
136       duration += (zeroPulse + zeroSpace);
137     }
138     ++i;
139   }
140
141   return duration;
142 }
143