1 #include "jvcprotocol.h"
3 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
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.
21 JVCProtocol::JVCProtocol(
24 : PIRProtocol(guiObject, index, 60000, true),
33 setCarrierFrequency(38000);
38 void JVCProtocol::startSendingCommand(
39 unsigned int threadableID,
42 // Exceptions here are problematic; I'll try to weed them out by putting the
43 // whole thing in a try/catch block:
46 // First, check if we are meant to be the recipient of this command:
47 if (threadableID != id) return;
51 KeycodeCollection::const_iterator i = keycodes.find(command);
53 // Do we even have this key defined?
54 if (i == keycodes.end())
56 std::string s = "Tried to send a non-existent command.\n";
57 throw PIRException(s);
60 // construct the device:
61 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
64 int commandDuration = 0;
65 while (repeatCount < MAX_REPEAT_COUNT)
67 // If we are currently repeating, and have a special "repeat signal",
68 // use that signal. Otherwise, generate a normal command string.
71 commandDuration = generateHeadlessCommand((*i).second, rx51device);
75 commandDuration = generateStandardCommand((*i).second, rx51device);
78 // Now, tell the device to send the whole command:
79 rx51device.sendCommandToDevice();
81 // sleep until the next repetition of command:
82 sleepUntilRepeat(commandDuration);
84 // Check whether we've reached the minimum required number of repetitons:
85 if (repeatCount >= minimumRepetitions)
87 // Check whether we've been asked to stop:
88 if (checkRepeatFlag())
90 QMutexLocker cifLocker(&commandIFMutex);
91 commandInFlight = false;
99 catch (PIRException e)
102 emit commandFailed(e.getError().c_str());
105 QMutexLocker cifLocker(&commandIFMutex);
106 commandInFlight = false;
110 // JVC data is sent in reverse order, i.e., the least signficant bit is
112 int JVCProtocol::generateStandardCommand(
113 const CommandSequence &bits,
114 PIRRX51Hardware &rx51device)
118 // First, the "header" pulse:
119 rx51device.addPair(headerPulse, headerSpace);
120 duration += (headerPulse + headerSpace);
122 // Now, push the actual data:
123 duration += pushReverseBits(preData, rx51device);
124 duration += pushReverseBits(bits, rx51device);
126 // Finally add the "trail":
127 rx51device.addSingle(trailerPulse);
128 duration += trailerPulse;
134 int JVCProtocol::generateHeadlessCommand(
135 const CommandSequence &bits,
136 PIRRX51Hardware &rx51device)
140 // Push the actual data:
141 duration += pushReverseBits(preData, rx51device);
142 duration += pushReverseBits(bits, rx51device);
144 // Finally add the "trail":
145 rx51device.addSingle(trailerPulse);
146 duration += trailerPulse;
152 int JVCProtocol::pushReverseBits(
153 const CommandSequence &bits,
154 PIRRX51Hardware &rx51device)
157 CommandSequence::const_reverse_iterator i = bits.rbegin();
158 while (i != bits.rend())
162 // Send the pulse for "One":
163 rx51device.addPair(onePulse, oneSpace);
164 duration += (onePulse + oneSpace);
168 // Send the pulse for "Zero":
169 rx51device.addPair(zeroPulse, zeroSpace);
170 duration += (zeroPulse + zeroSpace);