da106011cd5deb0f1f61fa0c91cc1bceb2ca3a85
[pierogi] / protocols / jvcprotocol.cpp
1 #include "jvcprotocol.h"
2
3 #include "pirexception.h"
4 #include <string>
5 //#include <iostream>
6
7 // Some global communications stuff:
8 #include <QMutex>
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
11
12 // The JVC protocol should have the following attributes:
13 // A "zero" is encoded with a 526 usec pulse, 52626 usec space.
14 // A "one" is encoded with a 526 usec pulse, and 3*526 (1578) usec space.
15 // The header is a 8400 usec pulse, 4200 usec space.
16 // Commands end with a trailing 526 usec pulse.
17 // Commands are repeated by re-sending entire command without the header.
18 // Repeats are broadcast every 60000 usec.
19 // The carrier frequency is 38 kHz, duty cycle is 1/3.
20
21 JVCProtocol::JVCProtocol(
22   QObject *guiObject,
23   unsigned int index)
24   : SpaceProtocol(
25       guiObject, index,
26       526, 526,
27       526, 1578,
28       8400, 4200,
29       526,
30       60000, true)
31 {
32   setCarrierFrequency(38000);
33   setDutyCycle(33);
34 }
35
36
37 void JVCProtocol::startSendingCommand(
38   unsigned int threadableID,
39   PIRKeyName command)
40 {
41   // Exceptions here are problematic; I'll try to weed them out by putting the
42   // whole thing in a try/catch block:
43   try
44   {
45     // First, check if we are meant to be the recipient of this command:
46     if (threadableID != id) return;
47
48     clearRepeatFlag();
49
50     KeycodeCollection::const_iterator i = keycodes.find(command);
51
52     // Do we even have this key defined?
53     if (i == keycodes.end())
54     {
55       std::string s = "Tried to send a non-existent command.\n";
56       throw PIRException(s);
57     }
58
59     // construct the device:
60     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
61
62     int repeatCount = 0;
63     int commandDuration = 0;
64     while (repeatCount < MAX_REPEAT_COUNT)
65     {
66       // If we are currently repeating, and have a special "repeat signal",
67       // use that signal.  Otherwise, generate a normal command string.
68       if (repeatCount)
69       {
70         commandDuration = generateHeadlessCommand((*i).second, rx51device);
71       }
72       else
73       {
74         commandDuration = generateStandardCommand((*i).second, rx51device);
75       }
76
77       // Now, tell the device to send the whole command:
78       rx51device.sendCommandToDevice();
79
80       // sleep until the next repetition of command:
81       sleepUntilRepeat(commandDuration);
82
83       // Check whether we've reached the minimum required number of repetitons:
84       if (repeatCount >= minimumRepetitions)
85       {
86         // Check whether we've been asked to stop:
87         if (checkRepeatFlag())
88         {
89           QMutexLocker cifLocker(&commandIFMutex);
90           commandInFlight = false;
91           return;
92         }
93       }
94
95       ++repeatCount;
96     }
97   }
98   catch (PIRException e)
99   {
100     // inform the gui:
101     emit commandFailed(e.getError().c_str());
102   }
103
104   QMutexLocker cifLocker(&commandIFMutex);
105   commandInFlight = false;
106 }
107
108
109 // JVC data is sent in reverse order, i.e., the least signficant bit is
110 // sent first.
111 int JVCProtocol::generateStandardCommand(
112   const PIRKeyBits &pkb,
113   PIRRX51Hardware &rx51device)
114 {
115   int duration = 0;
116
117   // First, the "header" pulse:
118   rx51device.addPair(headerPulse, headerSpace);
119   duration += (headerPulse + headerSpace);
120
121   // Now, push the actual data:
122   duration += pushReverseBits(preData, rx51device);
123   duration += pushReverseBits(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 JVCProtocol::generateHeadlessCommand(
134   const PIRKeyBits &pkb,
135   PIRRX51Hardware &rx51device)
136 {
137   int duration = 0;
138
139   // Push the actual data:
140   duration += pushReverseBits(preData, rx51device);
141   duration += pushReverseBits(pkb.firstCode, rx51device);
142
143   // Finally add the "trail":
144   rx51device.addSingle(trailerPulse);
145   duration += trailerPulse;
146
147   return duration;
148 }