1 #include "sharpprotocol.h"
3 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // Sharp's protocol should have the following attributes:
13 // A "zero" is encoded with a 320 usec pulse, 680 usec space.
14 // A "one" is encoded with a 320 usec pulse, and 1680 usec space.
15 // There is no header pulse.
16 // The pulse train ends with a trailing 320 usec pulse.
17 // For repeating, the entire train is re-sent, except that the command
18 // section (and the last two bits) are inverted in each odd repeat.
19 // There is a 40000 usec delay between the end of one command and the start
21 // The command should be repeated at least once.
22 // The carrier frequency is 38 kHz, duty cycle is 1/3.
24 SharpProtocol::SharpProtocol(
37 setCarrierFrequency(38000);
42 void SharpProtocol::startSendingCommand(
43 unsigned int threadableID,
46 // Exceptions here are problematic; I'll try to weed them out by putting the
47 // whole thing in a try/catch block:
50 // First, check if we are meant to be the recipient of this command:
51 if (threadableID != id) return;
55 KeycodeCollection::const_iterator i = keycodes.find(command);
57 // Do we even have this key defined?
58 if (i == keycodes.end())
60 std::string s = "Tried to send a non-existent command.\n";
61 throw PIRException(s);
64 // construct the device:
65 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
68 int commandDuration = 0;
69 while (repeatCount < MAX_REPEAT_COUNT)
71 // Every other repeat count, we invert everything but the address:
74 commandDuration = generateToggledCommand((*i).second, rx51device);
78 commandDuration = generateStandardCommand((*i).second, rx51device);
81 // Now, tell the device to send the whole command:
82 rx51device.sendCommandToDevice();
84 // sleep until the next repetition of command:
85 sleepUntilRepeat(commandDuration);
87 // Check whether we've reached the minimum required number of repetitons:
88 // if (repeatCount >= minimumRepetitions)
91 // Check whether we've been asked to stop:
92 if (checkRepeatFlag())
94 QMutexLocker cifLocker(&commandIFMutex);
95 commandInFlight = false;
103 catch (PIRException e)
106 emit commandFailed(e.getError().c_str());
109 QMutexLocker cifLocker(&commandIFMutex);
110 commandInFlight = false;
114 int SharpProtocol::generateStandardCommand(
115 const PIRKeyBits &pkb,
116 PIRRX51Hardware &rx51device)
120 // First, push the address:
121 duration += pushReverseBits(pkb.firstCode, rx51device);
123 // Next, push the command:
124 duration += pushReverseBits(pkb.secondCode, rx51device);
126 // Next, there is an "expansion" bit and a "check" bit. Not entirely sure
127 // what these two do. The check bit is fixed at "1".
130 rx51device.addPair(onePulse, oneSpace);
131 duration += (onePulse + oneSpace);
135 rx51device.addPair(zeroPulse, zeroSpace);
136 duration += (zeroPulse + zeroSpace);
139 rx51device.addPair(zeroPulse, zeroSpace);
140 duration += (zeroPulse + zeroSpace);
142 // Finally add the "trail":
143 rx51device.addSingle(trailerPulse);
144 duration += trailerPulse;
150 // This is the same as the standard command, except all bits but the address
152 int SharpProtocol::generateToggledCommand(
153 const PIRKeyBits &pkb,
154 PIRRX51Hardware &rx51device)
158 pushReverseBits(pkb.firstCode, rx51device);
160 // This time we invert the command bits:
161 pushInvertedReverseBits(pkb.secondCode, rx51device);
163 // We'll also invert the two administrative bits here:
166 rx51device.addPair(zeroPulse, zeroSpace);
167 duration += (zeroPulse + zeroSpace);
171 rx51device.addPair(onePulse, oneSpace);
172 duration += (onePulse + oneSpace);
175 rx51device.addPair(onePulse, oneSpace);
176 duration += (onePulse + oneSpace);
179 rx51device.addSingle(trailerPulse);
180 duration += trailerPulse;