1 #include "sharpprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
8 // Some global communications stuff:
10 extern bool commandInFlight;
11 extern QMutex commandIFMutex;
13 // Sharp's protocol should have the following attributes:
14 // A "zero" is encoded with a 320 usec pulse, 680 usec space.
15 // A "one" is encoded with a 320 usec pulse, and 1680 usec space.
16 // There is no header pulse.
17 // The pulse train ends with a trailing 320 usec pulse.
18 // For repeating, the entire train is re-sent, except that the command
19 // section (and the last two bits) are inverted in each odd repeat.
20 // There is a 40000 usec delay between the end of one command and the start
22 // The command should be repeated at least once.
23 // The carrier frequency is 38 kHz, duty cycle is 1/3.
25 SharpProtocol::SharpProtocol(
38 setCarrierFrequency(38000);
43 void SharpProtocol::startSendingCommand(
44 unsigned int threadableID,
47 // Exceptions here are problematic; I'll try to weed them out by putting the
48 // whole thing in a try/catch block:
51 // First, check if we are meant to be the recipient of this command:
52 if (threadableID != id) return;
56 KeycodeCollection::const_iterator i = keycodes.find(command);
58 // Do we even have this key defined?
59 if (i == keycodes.end())
61 QMutexLocker cifLocker(&commandIFMutex);
62 commandInFlight = false;
64 // std::string s = "Tried to send a non-existent command.\n";
65 // throw PIRException(s);
68 // construct the device:
69 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
72 int commandDuration = 0;
73 while (repeatCount < MAX_REPEAT_COUNT)
75 // Every other repeat count, we invert everything but the address:
78 commandDuration = generateToggledCommand((*i).second, rx51device);
82 commandDuration = generateStandardCommand((*i).second, rx51device);
85 // Now, tell the device to send the whole command:
86 rx51device.sendCommandToDevice();
88 // sleep until the next repetition of command:
89 sleepUntilRepeat(commandDuration);
91 // Check whether we've reached the minimum required number of repetitons:
92 // if (repeatCount >= minimumRepetitions)
95 // Check whether we've been asked to stop:
96 if (checkRepeatFlag())
100 QMutexLocker cifLocker(&commandIFMutex);
101 commandInFlight = false;
110 QMutexLocker cifLocker(&commandIFMutex);
111 commandInFlight = false;
113 catch (PIRException e)
116 emit commandFailed(e.getError().c_str());
121 int SharpProtocol::generateStandardCommand(
122 const PIRKeyBits &pkb,
123 PIRRX51Hardware &rx51device)
127 // First, push the address:
128 duration += pushReverseBits(pkb.firstCode, rx51device);
130 // Next, push the command:
131 duration += pushReverseBits(pkb.secondCode, rx51device);
133 // Next, there is an "expansion" bit and a "check" bit. Not entirely sure
134 // what these two do. The check bit is fixed at "0".
137 rx51device.addPair(onePulse, oneSpace);
138 duration += (onePulse + oneSpace);
142 rx51device.addPair(zeroPulse, zeroSpace);
143 duration += (zeroPulse + zeroSpace);
146 rx51device.addPair(zeroPulse, zeroSpace);
147 duration += (zeroPulse + zeroSpace);
149 // Finally add the "trail":
150 rx51device.addSingle(trailerPulse);
151 duration += trailerPulse;
157 // This is the same as the standard command, except all bits but the address
159 int SharpProtocol::generateToggledCommand(
160 const PIRKeyBits &pkb,
161 PIRRX51Hardware &rx51device)
165 pushReverseBits(pkb.firstCode, rx51device);
167 // This time we invert the command bits:
168 pushInvertedReverseBits(pkb.secondCode, rx51device);
170 // We'll also invert the two administrative bits here:
173 rx51device.addPair(zeroPulse, zeroSpace);
174 duration += (zeroPulse + zeroSpace);
178 rx51device.addPair(onePulse, oneSpace);
179 duration += (onePulse + oneSpace);
182 rx51device.addPair(onePulse, oneSpace);
183 duration += (onePulse + oneSpace);
186 rx51device.addSingle(trailerPulse);
187 duration += trailerPulse;