9fd1f5c74dffe74ab3bb27cd6876e1be52837c92
[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       std::string s = "Tried to send a non-existent command.\n";
56       throw PIRException(s);
57     }
58
59     // construct the device:
60     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
61
62     int repeatCount = 0;
63     int commandDuration = 0;
64     while (repeatCount < MAX_REPEAT_COUNT)
65     {
66       commandDuration = generateStandardCommand((*i).second, rx51device);
67
68       // Now, tell the device to send the whole command:
69       rx51device.sendCommandToDevice();
70
71       // sleep until the next repetition of command:
72       sleepUntilRepeat(commandDuration);
73
74       // Check whether we've reached the minimum required number of repetitons:
75 //      if (repeatCount >= minimumRepetitions)
76       if (repeatCount >= 3)
77       {
78         // Check whether we've been asked to stop:
79         if (checkRepeatFlag())
80         {
81           QMutexLocker cifLocker(&commandIFMutex);
82           commandInFlight = false;
83           return;
84         }
85       }
86
87       ++repeatCount;
88     }
89   }
90   catch (PIRException e)
91   {
92     // inform the gui:
93     emit commandFailed(e.getError().c_str());
94   }
95
96   QMutexLocker cifLocker(&commandIFMutex);
97   commandInFlight = false;
98 }
99
100
101 int SIRCProtocol::generateStandardCommand(
102   const PIRKeyBits &pkb,
103   PIRRX51Hardware &rx51device)
104 {
105   int duration = 0;
106
107   // First, the "header" pulse:
108   rx51device.addPair(headerPulse, headerSpace);
109   duration += (headerPulse + headerSpace);
110
111   // Next, push the data.
112   // These bits are sent in reverse order, and moreover, the codes are sent
113   // in reverse order as well:
114   duration += pushReverseBits(pkb.thirdCode, rx51device);
115   duration += pushReverseBits(pkb.secondCode, rx51device);
116   duration += pushReverseBits(pkb.firstCode, rx51device);
117
118   return duration;
119 }