1 #include "rc5protocol.h"
3 #include "pirexception.h"
5 RC5Protocol::RC5Protocol(
13 : PIRProtocol(guiObject, index, gSpace, iclflag),
23 void RC5Protocol::setHeaderPair(
33 void RC5Protocol::setPreData(
37 appendToBitSeq(preData, data, bits);
41 void RC5Protocol::setToggleBit(
48 void RC5Protocol::startSendingCommand(
49 unsigned int threadableID,
52 // Dumping entire method inside of try/catch, to deal with cross-thread
53 // exception handling:
58 if (threadableID != id) return;
60 KeycodeCollection::const_iterator i = keycodes.find(command);
62 // Sanity check, make sure command exists first:
63 if (i == keycodes.end())
65 std::string s = "Tried to send a non-existent command.\n";
66 throw PIRException(s);
69 // Construct the object that communicates with the device driver:
70 PIRDevice device(carrierFrequency, dutyCycle);
73 while (repeatCount < MAX_REPEAT_COUNT)
75 int commandDuration = 0;
77 // Now, throw together an RC5 protocol command string:
79 bufferContainsPulse = false;
80 bufferContainsSpace = false;
82 // Start off the first pulse with the lead, if any:
86 bufferContainsPulse = true;
90 commandDuration += pushBits((*i).second, device);
92 // Clear out the buffer, if necessary:
95 device.addSingle(buffer);
96 commandDuration += buffer;
98 bufferContainsSpace = false;
99 bufferContainsPulse = false;
102 // Now, tell the device to send the whole command:
103 device.sendCommandToDevice();
105 // Sleep for an amount of time. (Need to make this interruptable!)
106 sleepUntilRepeat(commandDuration);
108 // Have we satisfied the minimum number of repetitions?
109 if (repeatCount >= minimumRepetitions)
111 // Have we been told to stop yet?
112 if (checkRepeatFlag())
114 // Ok, then we can quit now:
123 catch (PIRException e)
125 emit commandFailed(e.getError().c_str());
132 int RC5Protocol::pushBits(
133 const CommandSequence &bits,
136 int bitsDuration = 0;
138 // Rather than encoding a 0 or 1 through the timing of a pulse, RC5 encodes
139 // a bit by swapping the order of pulses and spaces. (This is called
142 CommandSequence::const_iterator i = bits.begin();
146 while (i != bits.end())
150 if (bitCount == toggleBit) // are we on a toggled bit?
152 if (keypressCount % 2) // is it time to toggle?
154 bitValue = !bitValue; // then flip the bit
160 // We've got a "1". First add a space, then a pulse.
161 if (bufferContainsSpace)
163 // Merge our space with the previous space, and send them to
165 device.addSingle(buffer + biphaseSpace);
166 bitsDuration += (buffer + biphaseSpace);
168 bufferContainsSpace = false;
172 if (bufferContainsPulse)
175 device.addSingle(buffer);
176 bitsDuration += buffer;
178 bufferContainsPulse = false;
181 device.addSingle(biphaseSpace);
182 bitsDuration += biphaseSpace;
185 // Put a pulse into the buffer to wait.
186 buffer = biphasePulse;
187 bufferContainsPulse = true;
191 // We've got a "0". First add a pulse, then a space.
192 if (bufferContainsPulse)
194 // Merge our pulse with the previous one, and send them to the device:
195 device.addSingle(buffer + biphasePulse);
196 bitsDuration += (buffer + biphasePulse);
198 bufferContainsPulse = false;
202 if (bufferContainsSpace)
204 // Flush out the buffer:
205 device.addSingle(buffer);
206 bitsDuration += buffer;
208 bufferContainsSpace = false;
212 device.addSingle(biphasePulse);
213 bitsDuration += biphasePulse;
216 // Put a space into the buffer to wait:
217 buffer = biphaseSpace;
218 bufferContainsSpace = true;