1 #include "jvcprotocol.h"
2 #include "pirrx51hardware.h"
4 #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, 526 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(
32 setCarrierFrequency(38000);
37 void JVCProtocol::startSendingCommand(
38 unsigned int threadableID,
41 // Exceptions here are problematic; I'll try to weed them out by putting the
42 // whole thing in a try/catch block:
45 // First, check if we are meant to be the recipient of this command:
46 if (threadableID != id) return;
50 KeycodeCollection::const_iterator i = keycodes.find(command);
52 // Do we even have this key defined?
53 if (i == keycodes.end())
55 QMutexLocker cifLocker(&commandIFMutex);
56 commandInFlight = false;
58 // std::string s = "Tried to send a non-existent command.\n";
59 // throw PIRException(s);
62 // construct the device:
63 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
66 int commandDuration = 0;
67 while (repeatCount < MAX_REPEAT_COUNT)
69 // If we are currently repeating, and have a special "repeat signal",
70 // use that signal. Otherwise, generate a normal command string.
73 commandDuration = generateHeadlessCommand((*i).second, rx51device);
77 commandDuration = generateStandardCommand((*i).second, rx51device);
80 // Now, tell the device to send the whole command:
81 rx51device.sendCommandToDevice();
83 // sleep until the next repetition of command:
84 sleepUntilRepeat(commandDuration);
86 // Check whether we've reached the minimum required number of repetitons:
87 if (repeatCount >= minimumRepetitions)
89 // Check whether we've been asked to stop:
90 if (checkRepeatFlag())
94 QMutexLocker cifLocker(&commandIFMutex);
95 commandInFlight = false;
104 QMutexLocker cifLocker(&commandIFMutex);
105 commandInFlight = false;
107 catch (PIRException e)
110 emit commandFailed(e.getError().c_str());
115 // JVC data is sent in reverse order, i.e., the least signficant bit is
117 int JVCProtocol::generateStandardCommand(
118 const PIRKeyBits &pkb,
119 PIRRX51Hardware &rx51device)
123 // First, the "header" pulse:
124 rx51device.addPair(headerPulse, headerSpace);
125 duration += (headerPulse + headerSpace);
127 // Now, push the actual data:
128 duration += pushReverseBits(preData, rx51device);
129 duration += pushReverseBits(pkb.firstCode, rx51device);
131 // Finally add the "trail":
132 rx51device.addSingle(trailerPulse);
133 duration += trailerPulse;
139 int JVCProtocol::generateHeadlessCommand(
140 const PIRKeyBits &pkb,
141 PIRRX51Hardware &rx51device)
145 // Push the actual data:
146 duration += pushReverseBits(preData, rx51device);
147 duration += pushReverseBits(pkb.firstCode, rx51device);
149 // Finally add the "trail":
150 rx51device.addSingle(trailerPulse);
151 duration += trailerPulse;