A couple of UI additions
[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       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       {
76         // Check whether we've been asked to stop:
77         if (checkRepeatFlag())
78         {
79           QMutexLocker cifLocker(&commandIFMutex);
80           commandInFlight = false;
81           return;
82         }
83       }
84
85       ++repeatCount;
86     }
87   }
88   catch (PIRException e)
89   {
90     // inform the gui:
91     emit commandFailed(e.getError().c_str());
92   }
93
94   QMutexLocker cifLocker(&commandIFMutex);
95   commandInFlight = false;
96 }
97
98
99 int ProtonProtocol::generateStandardCommand(
100   const PIRKeyBits &pkb,
101   PIRRX51Hardware &rx51device)
102 {
103   int duration = 0;
104
105   // First, the "header" pulse:
106   rx51device.addPair(headerPulse, headerSpace);
107   duration += (headerPulse + headerSpace);
108
109   // The Proton protocol contains an 8 bit address and an 8 bit command,
110   // in LSB order.  Between these two is a gap made up of a 500 usec pulse
111   // and a 4000 usec space.
112   // - "preData" should contain the address.
113   // - "firstCode" should contain the command.
114
115   duration += pushReverseBits(preData, rx51device);
116
117   rx51device.addPair(500, 4000);
118   duration += 4500;
119
120   duration += pushReverseBits(pkb.firstCode, rx51device);
121
122   // Finally add the "trail":
123   rx51device.addSingle(trailerPulse);
124   duration += trailerPulse;
125
126   return duration;
127 }
128