1 #include "paceprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // The Pace protocol is fairly simple. (Not sure I've got good data, though)
13 // A "zero" is encoded with a 630 usec pulse, 7*630 (4410) usec space.
14 // A "one" is encoded with a 630 usec pulse, 11*630 (6930) usec space.
15 // Different opinions exist on what is the header; I'm going to go with
16 // a 630 usec pulse, 6930 usec space one. Hope it works.
17 // Commands end with a trailing 630 usec pulse.
18 // Full pulse train is re-sent when repeating.
19 // Each command runs for 120000 usec.
20 // The carrier frequency is 38 kHz.
22 PaceProtocol::PaceProtocol(
37 void PaceProtocol::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 commandDuration = generateStandardCommand((*i).second, rx51device);
68 // Now, tell the device to send the whole command:
69 rx51device.sendCommandToDevice();
71 // sleep until the next repetition of command:
72 sleepUntilRepeat(commandDuration);
74 // Check whether we've reached the minimum required number of repetitons:
75 if (repeatCount >= minimumRepetitions)
77 // Check whether we've been asked to stop:
78 if (checkRepeatFlag())
81 QMutexLocker cifLocker(&commandIFMutex);
82 commandInFlight = false;
90 catch (PIRException e)
93 emit commandFailed(e.getError().c_str());
97 QMutexLocker cifLocker(&commandIFMutex);
98 commandInFlight = false;
102 // Pace seems to be going with a header pulse/space, a toggle bit, and nine
103 // more bits of data. Information is sketchy, however. I'll be going with
104 // three bits of pre-data (address?), and six bits of command...
106 int PaceProtocol::generateStandardCommand(
107 const PIRKeyBits &pkb,
108 PIRRX51Hardware &rx51device)
112 // First, the "header" pulse:
113 rx51device.addPair(headerPulse, headerSpace);
114 duration += (headerPulse + headerSpace);
116 // Next, the toggle bit:
117 if (keypressCount % 2)
119 rx51device.addPair(onePulse, oneSpace);
120 duration += (onePulse + oneSpace);
124 rx51device.addPair(zeroPulse, zeroSpace);
125 duration += (zeroPulse + zeroSpace);
128 // Next, three bits of pre-data:
129 duration += pushBits(preData, rx51device);
131 // Next, six bits of data:
132 duration += pushBits(pkb.firstCode, rx51device);
134 // Finally add the "trail":
135 rx51device.addSingle(trailerPulse);
136 duration += trailerPulse;