17d037bc476584c369c05d22444e9f7a8d088694
[pierogi] / protocols / panasonicoldprotocol.cpp
1 #include "panasonicoldprotocol.h"
2
3 #include "pirrx51hardware.h"
4
5 #include "pirexception.h"
6
7 #include <QMutex>
8 extern bool commandInFlight;
9 extern QMutex commandIFMutex;
10
11 // The "Panasonic Old" protocol appears to have the following features:
12 // A "zero" is encoded with an 833 usec pulse, 833 usec space.
13 // A "one" is encoded with an 833 usec pulse, and 3*833 (2499) usec space.
14 // The header is a 3332 usec pulse, 3332 usec space.
15 // Commands end with a trailing 833 usec pulse.
16 // When repeating, the entire command is re-sent.
17 // Each command runs for approximately 105000 usec (need to check this)
18 // The carrier frequency might be 57.6 kHz.
19
20 PanasonicOldProtocol::PanasonicOldProtocol(
21   QObject *guiObject,
22   unsigned int index)
23   : SpaceProtocol(
24       guiObject, index,
25       833, 833,
26       833, 2499,
27       3332, 3332,
28       833,
29       105000, true)
30 {
31 }
32
33
34 void PanasonicOldProtocol::startSendingCommand(
35   unsigned int threadableID,
36   PIRKeyName command)
37 {
38   // Exceptions here are problematic; I'll try to weed them out by putting the
39   // whole thing in a try/catch block:
40   try
41   {
42     // First, check if we are meant to be the recipient of this command:
43     if (threadableID != id) return;
44
45     clearRepeatFlag();
46
47     KeycodeCollection::const_iterator i = keycodes.find(command);
48
49     // Do we even have this key defined?
50     if (i == keycodes.end())
51     {
52       std::string s = "Tried to send a non-existent command.\n";
53       throw PIRException(s);
54     }
55
56     // construct the device:
57     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
58
59     int repeatCount = 0;
60     int commandDuration = 0;
61     while (repeatCount < MAX_REPEAT_COUNT)
62     {
63       commandDuration = generateStandardCommand((*i).second, rx51device);
64
65       // Now, tell the device to send the whole command:
66       rx51device.sendCommandToDevice();
67
68       // sleep until the next repetition of command:
69       sleepUntilRepeat(commandDuration);
70
71       // Check whether we've reached the minimum required number of repetitons:
72       if (repeatCount >= minimumRepetitions)
73       {
74         // Check whether we've been asked to stop:
75         if (checkRepeatFlag())
76         {
77           QMutexLocker cifLocker(&commandIFMutex);
78           commandInFlight = false;
79           return;
80         }
81       }
82
83       ++repeatCount;
84     }
85   }
86   catch (PIRException e)
87   {
88     // inform the gui:
89     emit commandFailed(e.getError().c_str());
90   }
91
92   QMutexLocker cifLocker(&commandIFMutex);
93   commandInFlight = false;
94 }
95
96
97 int PanasonicOldProtocol::generateStandardCommand(
98   const PIRKeyBits &pkb,
99   PIRRX51Hardware &rx51device)
100 {
101   int duration = 0;
102
103   // First, the header pulse:
104   rx51device.addPair(headerPulse, headerSpace);
105   duration += (headerPulse + headerSpace);
106
107   // Supposedly, this protocol uses 5 bits of address and 6 bits of command,
108   // but it looks like the address can be variable.  So, I'm going to treat
109   // them as more-or-less two separate sets of command data.
110   //
111   // It is laid out as follows:
112   // 1) the five bits of "address" data
113   // 2) the six bits of command data
114   // 3) repeat of the five bits of address data, inverted
115   // 4) repeat of the six bits of command data, inverted
116
117   duration += pushReverseBits(pkb.firstCode, rx51device);
118   duration += pushReverseBits(pkb.secondCode, rx51device);
119   duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
120   duration += pushInvertedReverseBits(pkb.secondCode, rx51device);
121
122   // Add the trailer pulse:
123   rx51device.addSingle(trailerPulse);
124   duration += trailerPulse;
125
126   return duration;
127 }