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 std::string s = "Tried to send a non-existent command.\n";
62 throw PIRException(s);
65 // construct the device:
66 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
69 int commandDuration = 0;
70 while (repeatCount < MAX_REPEAT_COUNT)
72 // Every other repeat count, we invert everything but the address:
75 commandDuration = generateToggledCommand((*i).second, rx51device);
79 commandDuration = generateStandardCommand((*i).second, rx51device);
82 // Now, tell the device to send the whole command:
83 rx51device.sendCommandToDevice();
85 // sleep until the next repetition of command:
86 sleepUntilRepeat(commandDuration);
88 // Check whether we've reached the minimum required number of repetitons:
89 // if (repeatCount >= minimumRepetitions)
92 // Check whether we've been asked to stop:
93 if (checkRepeatFlag())
95 QMutexLocker cifLocker(&commandIFMutex);
96 commandInFlight = false;
104 catch (PIRException e)
107 emit commandFailed(e.getError().c_str());
110 QMutexLocker cifLocker(&commandIFMutex);
111 commandInFlight = false;
115 int SharpProtocol::generateStandardCommand(
116 const PIRKeyBits &pkb,
117 PIRRX51Hardware &rx51device)
121 // First, push the address:
122 duration += pushReverseBits(pkb.firstCode, rx51device);
124 // Next, push the command:
125 duration += pushReverseBits(pkb.secondCode, rx51device);
127 // Next, there is an "expansion" bit and a "check" bit. Not entirely sure
128 // what these two do. The check bit is fixed at "0".
131 rx51device.addPair(onePulse, oneSpace);
132 duration += (onePulse + oneSpace);
136 rx51device.addPair(zeroPulse, zeroSpace);
137 duration += (zeroPulse + zeroSpace);
140 rx51device.addPair(zeroPulse, zeroSpace);
141 duration += (zeroPulse + zeroSpace);
143 // Finally add the "trail":
144 rx51device.addSingle(trailerPulse);
145 duration += trailerPulse;
151 // This is the same as the standard command, except all bits but the address
153 int SharpProtocol::generateToggledCommand(
154 const PIRKeyBits &pkb,
155 PIRRX51Hardware &rx51device)
159 pushReverseBits(pkb.firstCode, rx51device);
161 // This time we invert the command bits:
162 pushInvertedReverseBits(pkb.secondCode, rx51device);
164 // We'll also invert the two administrative bits here:
167 rx51device.addPair(zeroPulse, zeroSpace);
168 duration += (zeroPulse + zeroSpace);
172 rx51device.addPair(onePulse, oneSpace);
173 duration += (onePulse + oneSpace);
176 rx51device.addPair(onePulse, oneSpace);
177 duration += (onePulse + oneSpace);
180 rx51device.addSingle(trailerPulse);
181 duration += trailerPulse;