New GUI, many changes
[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       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       if (repeatCount)
66       {
67         commandDuration = generateRepeatCommand((*i).second, rx51device);
68       }
69       else
70       {
71         commandDuration = generateStandardCommand((*i).second, rx51device);
72       }
73
74       // Now, tell the device to send the whole command:
75       rx51device.sendCommandToDevice();
76
77       // sleep until the next repetition of command:
78       sleepUntilRepeat(commandDuration);
79
80       // Check whether we've reached the minimum required number of repetitons:
81       if (repeatCount >= minimumRepetitions)
82       {
83         // Check whether we've been asked to stop:
84         if (checkRepeatFlag())
85         {
86           QMutexLocker cifLocker(&commandIFMutex);
87           commandInFlight = false;
88           return;
89         }
90       }
91
92       ++repeatCount;
93     }
94   }
95   catch (PIRException e)
96   {
97     // inform the gui:
98     emit commandFailed(e.getError().c_str());
99   }
100
101   QMutexLocker cifLocker(&commandIFMutex);
102   commandInFlight = false;
103 }
104
105
106 int KathreinProtocol::generateStandardCommand(
107   const PIRKeyBits &pkb,
108   PIRRX51Hardware &rx51device)
109 {
110   int duration = 0;
111
112   // First, the "header" pulse:
113   rx51device.addPair(headerPulse, headerSpace);
114   duration += (headerPulse + headerSpace);
115
116   // Kathrein protocol has four bits of address and eight bits of command.
117   // As in NEC protocol, the address and command are complemented.
118   // - "preData" should contain the 4-bit address
119   // - "firstCode" should contain the 8-bit command
120   duration += pushReverseBits(preData, rx51device);
121   duration += pushInvertedReverseBits(preData, rx51device);
122   duration += pushReverseBits(pkb.firstCode, rx51device);
123   duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
124
125   // Finally add the "trail":
126   rx51device.addSingle(trailerPulse);
127   duration += trailerPulse;
128
129   return duration;
130 }
131
132
133 int KathreinProtocol::generateRepeatCommand(
134   const PIRKeyBits &pkb,
135   PIRRX51Hardware &rx51device)
136 {
137   int duration = 0;
138
139   // First, the "header" pulse:
140   rx51device.addPair(headerPulse, headerSpace);
141   duration += (headerPulse + headerSpace);
142
143   // The Kathrein repeat block contains the 8-bit command and nothing else:
144   duration += pushReverseBits(pkb.firstCode, rx51device);
145
146   // Finally add the "trail":
147   rx51device.addSingle(trailerPulse);
148   duration += trailerPulse;
149
150   return duration;
151 }