Memory Management Improved
[pierogi] / protocols / thomsonprotocol.cpp
1 #include "thomsonprotocol.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 // This Thomson protocol is a very simple one:
13 // A "zero" is encoded with a 500 usec pulse, 2000 usec space.
14 // A "one" is encoded with a 500 usec pulse, and 4500 usec space.
15 // There is no header.
16 // Commands end with a trailing 500 usec pulse.
17 // When repeating, the full pulse train is re-sent.
18 // Each command runs for 80000 usec total.
19 // The carrier frequency should be 33 kHz, but I'm not so sure...
20
21 ThomsonProtocol::ThomsonProtocol(
22   QObject *guiObject,
23   unsigned int index)
24   : SpaceProtocol(
25       guiObject, index,
26       500, 2000,
27       500, 4500,
28       0, 0,
29       500,
30       80000, true),
31     keypressCount(0)
32 {
33 }
34
35
36 void ThomsonProtocol::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           ++keypressCount;
80           QMutexLocker cifLocker(&commandIFMutex);
81           commandInFlight = false;
82           return;
83         }
84       }
85
86       ++repeatCount;
87     }
88   }
89   catch (PIRException e)
90   {
91     // inform the gui:
92     emit commandFailed(e.getError().c_str());
93   }
94
95   ++keypressCount;
96   QMutexLocker cifLocker(&commandIFMutex);
97   commandInFlight = false;
98 }
99
100
101 int ThomsonProtocol::generateStandardCommand(
102   const PIRKeyBits &pkb,
103   PIRRX51Hardware &rx51device)
104 {
105   int duration = 0;
106
107   // First, four bits of address:
108   duration += pushBits(preData, rx51device);
109
110   // Next, the toggle bit:
111   if (keypressCount % 2)
112   {
113     rx51device.addPair(onePulse, oneSpace);
114     duration += (onePulse + oneSpace);
115   }
116   else
117   {
118     rx51device.addPair(zeroPulse, zeroSpace);
119     duration += (zeroPulse + zeroSpace);
120   }
121
122   // Next, seven bits of command:
123   duration += pushBits(pkb.firstCode, rx51device);
124
125   // Finally add the "trail":
126   rx51device.addSingle(trailerPulse);
127   duration += trailerPulse;
128
129   return duration;
130 }