2f4a2a87c77faaf18fa8bf5bec0c411d73361b20
[pierogi] / protocols / protonprotocol.cpp
1 #include "protonprotocol.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 proton protocol seems fairly similar to the NEC protocol, although
13 // somewhat simplified.
14 // A "zero" is encoded with a 500 usec pulse, 500 usec space.
15 // A "one" is encoded with a 500 usec pulse, and 3*500 (1500) usec space.
16 // The header is a 8000 usec pulse, 4000 usec space.
17 // Commands end with a trailing 500 usec pulse.
18 // When repeating, the entire pulse-train is retransmitted.
19 // The duration of each full frame is 63000 usec.
20 // The normal carrier frequency is 38 kHz.
21
22 ProtonProtocol::ProtonProtocol(
23   QObject *guiObject,
24   unsigned int index)
25   : SpaceProtocol(
26       guiObject, index,
27       500, 500,
28       500, 1500,
29       8000, 4000,
30       500,
31       63000, true)
32 {
33 }
34
35
36 void ProtonProtocol::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       commandDuration = generateStandardCommand((*i).second, rx51device);
69
70       // Now, tell the device to send the whole command:
71       rx51device.sendCommandToDevice();
72
73       // sleep until the next repetition of command:
74       sleepUntilRepeat(commandDuration);
75
76       // Check whether we've reached the minimum required number of repetitons:
77       if (repeatCount >= minimumRepetitions)
78       {
79         // Check whether we've been asked to stop:
80         if (checkRepeatFlag())
81         {
82           break;
83 /*
84           QMutexLocker cifLocker(&commandIFMutex);
85           commandInFlight = false;
86           return;
87 */
88         }
89       }
90
91       ++repeatCount;
92     }
93
94     QMutexLocker cifLocker(&commandIFMutex);
95     commandInFlight = false;
96   }
97   catch (PIRException e)
98   {
99     // inform the gui:
100     emit commandFailed(e.getError().c_str());
101   }
102 }
103
104
105 int ProtonProtocol::generateStandardCommand(
106   const PIRKeyBits &pkb,
107   PIRRX51Hardware &rx51device)
108 {
109   int duration = 0;
110
111   // First, the "header" pulse:
112   rx51device.addPair(headerPulse, headerSpace);
113   duration += (headerPulse + headerSpace);
114
115   // The Proton protocol contains an 8 bit address and an 8 bit command,
116   // in LSB order.  Between these two is a gap made up of a 500 usec pulse
117   // and a 4000 usec space.
118   // - "preData" should contain the address.
119   // - "firstCode" should contain the command.
120
121   duration += pushReverseBits(preData, rx51device);
122
123   rx51device.addPair(500, 4000);
124   duration += 4500;
125
126   duration += pushReverseBits(pkb.firstCode, rx51device);
127
128   // Finally add the "trail":
129   rx51device.addSingle(trailerPulse);
130   duration += trailerPulse;
131
132   return duration;
133 }
134