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, 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(
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 std::string s = "Tried to send a non-existent command.\n";
56 throw PIRException(s);
59 // construct the device:
60 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
63 int commandDuration = 0;
64 while (repeatCount < MAX_REPEAT_COUNT)
66 // If we are currently repeating, and have a special "repeat signal",
67 // use that signal. Otherwise, generate a normal command string.
70 commandDuration = generateHeadlessCommand((*i).second, rx51device);
74 commandDuration = generateStandardCommand((*i).second, rx51device);
77 // Now, tell the device to send the whole command:
78 rx51device.sendCommandToDevice();
80 // sleep until the next repetition of command:
81 sleepUntilRepeat(commandDuration);
83 // Check whether we've reached the minimum required number of repetitons:
84 if (repeatCount >= minimumRepetitions)
86 // Check whether we've been asked to stop:
87 if (checkRepeatFlag())
89 QMutexLocker cifLocker(&commandIFMutex);
90 commandInFlight = false;
98 catch (PIRException e)
101 emit commandFailed(e.getError().c_str());
104 QMutexLocker cifLocker(&commandIFMutex);
105 commandInFlight = false;
109 // JVC data is sent in reverse order, i.e., the least signficant bit is
111 int JVCProtocol::generateStandardCommand(
112 const PIRKeyBits &pkb,
113 PIRRX51Hardware &rx51device)
117 // First, the "header" pulse:
118 rx51device.addPair(headerPulse, headerSpace);
119 duration += (headerPulse + headerSpace);
121 // Now, push the actual data:
122 duration += pushReverseBits(preData, rx51device);
123 duration += pushReverseBits(pkb.firstCode, rx51device);
125 // Finally add the "trail":
126 rx51device.addSingle(trailerPulse);
127 duration += trailerPulse;
133 int JVCProtocol::generateHeadlessCommand(
134 const PIRKeyBits &pkb,
135 PIRRX51Hardware &rx51device)
139 // Push the actual data:
140 duration += pushReverseBits(preData, rx51device);
141 duration += pushReverseBits(pkb.firstCode, rx51device);
143 // Finally add the "trail":
144 rx51device.addSingle(trailerPulse);
145 duration += trailerPulse;