35208f310aa9a10308452b0fc421fcd83ef4b575
[pierogi] / protocols / kathreinprotocol.cpp
1 #include "kathreinprotocol.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 // The Kathrein protocol is similar to the NEC protocol:
13 // A "zero" is encoded with a 560 usec pulse, 560 usec space.
14 // A "one" is encoded with a 560 usec pulse, and 3*560 (1680) usec space.
15 // The header is a 9000 usec pulse, 4500 usec space.
16 // Commands end with a trailing 560 usec pulse.
17 // The repeat block is unique -- it has the normal header and trailer, and
18 // includes the command code, but no address, and no complement.
19 // Each command runs for 110000 usec before another can be executed.
20 // The normal carrier frequency is 38 kHz.
21
22 KathreinProtocol::KathreinProtocol(
23   QObject *guiObject,
24   unsigned int index)
25   : SpaceProtocol(
26       guiObject, index,
27       560, 560,
28       560, 1680,
29       9000, 4500,
30       560,
31       110000, true)
32 {
33 }
34
35
36 void KathreinProtocol::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       if (repeatCount)
69       {
70         commandDuration = generateRepeatCommand((*i).second, rx51device);
71       }
72       else
73       {
74         commandDuration = generateStandardCommand((*i).second, rx51device);
75       }
76
77       // Now, tell the device to send the whole command:
78       rx51device.sendCommandToDevice();
79
80       // sleep until the next repetition of command:
81       sleepUntilRepeat(commandDuration);
82
83       // Check whether we've reached the minimum required number of repetitons:
84       if (repeatCount >= minimumRepetitions)
85       {
86         // Check whether we've been asked to stop:
87         if (checkRepeatFlag())
88         {
89           break;
90 /*
91           QMutexLocker cifLocker(&commandIFMutex);
92           commandInFlight = false;
93           return;
94 */
95         }
96       }
97
98       ++repeatCount;
99     }
100
101     QMutexLocker cifLocker(&commandIFMutex);
102     commandInFlight = false;
103   }
104   catch (PIRException e)
105   {
106     // inform the gui:
107     emit commandFailed(e.getError().c_str());
108   }
109 }
110
111
112 int KathreinProtocol::generateStandardCommand(
113   const PIRKeyBits &pkb,
114   PIRRX51Hardware &rx51device)
115 {
116   int duration = 0;
117
118   // First, the "header" pulse:
119   rx51device.addPair(headerPulse, headerSpace);
120   duration += (headerPulse + headerSpace);
121
122   // Kathrein protocol has four bits of address and eight bits of command.
123   // As in NEC protocol, the address and command are complemented.
124   // - "preData" should contain the 4-bit address
125   // - "firstCode" should contain the 8-bit command
126   duration += pushReverseBits(preData, rx51device);
127   duration += pushInvertedReverseBits(preData, rx51device);
128   duration += pushReverseBits(pkb.firstCode, rx51device);
129   duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
130
131   // Finally add the "trail":
132   rx51device.addSingle(trailerPulse);
133   duration += trailerPulse;
134
135   return duration;
136 }
137
138
139 int KathreinProtocol::generateRepeatCommand(
140   const PIRKeyBits &pkb,
141   PIRRX51Hardware &rx51device)
142 {
143   int duration = 0;
144
145   // First, the "header" pulse:
146   rx51device.addPair(headerPulse, headerSpace);
147   duration += (headerPulse + headerSpace);
148
149   // The Kathrein repeat block contains the 8-bit command and nothing else:
150   duration += pushReverseBits(pkb.firstCode, rx51device);
151
152   // Finally add the "trail":
153   rx51device.addSingle(trailerPulse);
154   duration += trailerPulse;
155
156   return duration;
157 }