1 #include "rc6skyprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
8 extern bool commandInFlight;
9 extern QMutex commandIFMutex;
11 // These defines might need to be turned into variables, for odd devices.
12 #define HEADER_PULSE 2666
13 #define HEADER_SPACE 888
14 #define TRAILER_BIPHASE 888
16 // This version of Mode 6 RC6 is used in Sky and Sky+ receivers. It seems to
17 // be pretty close to vanilla RC6.
18 // The biphase unit of time is 444 usec.
19 // The RC6 header block starts with the normal 2666 usec pulse, 888 usec space.
20 // The next bit is fixed as a "1", as usual.
21 // The next three bits are 110, marking this as a mode 6 protocol.
22 // The trailer bit has an 888 usec biphase. It is normally a toggle bit, but
23 // for Sky, it appears to be fixed at "0".
24 // Next comes 8 bits of address, 4 bits I don't know about (subdevice?),
25 // and finally 8 bits of command.
26 // A space of (at least) 2666 usec must follow any command.
27 // The carrier frequency is 36 kHZ, duty cycle between 25 and 50 %.
29 RC6SkyProtocol::RC6SkyProtocol(
32 : PIRProtocol(guiObject, index, 2666, false),
37 setCarrierFrequency(36000);
41 void RC6SkyProtocol::startSendingCommand(
42 unsigned int threadableID,
47 // Is this command meant for us?
48 if (threadableID != id) return;
52 KeycodeCollection::const_iterator i = keycodes.find(command);
55 if (i == keycodes.end())
57 std::string s = "Tried to send a non-existent command.\n";
58 throw PIRException(s);
61 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
65 while (repeatCount < MAX_REPEAT_COUNT)
67 bufferContainsSpace = false;
68 bufferContainsPulse = false;
69 // First, construct the "Header" segment of the pulse train.
71 // The header involves:
72 // a) a "lead" of 2666 us pulse, 888 us space;
73 // b) a "start bit", value 1 (so 444 us pulse, 444 us space)
74 // c) three control bits, set to "110" (i.e., mode "6")
75 // d) the double-sized "trailer" bit, set based on the keypress count:
77 rx51device.addSingle(HEADER_PULSE); // lead pulse
78 duration += HEADER_PULSE;
79 rx51device.addSingle(HEADER_SPACE); // lead space
80 duration += HEADER_SPACE;
81 rx51device.addSingle(biphaseUnit); // start bit pulse
82 duration += biphaseUnit;
83 rx51device.addSingle(biphaseUnit); // start bit space
84 duration += biphaseUnit;
85 rx51device.addSingle(biphaseUnit); // bit 1 pulse;
86 duration += biphaseUnit;
87 rx51device.addSingle(biphaseUnit); // bit 1 space;
88 duration += biphaseUnit;
89 rx51device.addSingle(biphaseUnit); // bit 2 pulse;
90 duration += biphaseUnit;
91 rx51device.addSingle(2 * biphaseUnit); // bit 2 space + bit 3 space;
92 duration += 2 * biphaseUnit;
93 rx51device.addSingle(biphaseUnit); // bit 3 pulse;
94 duration += biphaseUnit;
95 rx51device.addSingle(2 * biphaseUnit); // trailer space
96 duration += 2 * biphaseUnit;
97 buffer = 2 * biphaseUnit; // trailer pulse goes into the buffer
98 bufferContainsPulse = true;
100 // Now, we can start the normal buffering process:
102 // push the address data:
103 duration += pushBits(preData, rx51device);
105 // push the command data:
106 duration += pushBits((*i).second.firstCode, rx51device);
108 // Flush out the buffer, if necessary:
111 rx51device.addSingle(buffer);
116 // Actually send out the command:
117 rx51device.sendCommandToDevice();
119 // Sleep for an amount of time. (RC6 demands an addtional 6 unit space
120 // at the end of any command...)
121 sleepUntilRepeat(duration + 6 * biphaseUnit);
123 // Have we been told to stop yet?
124 if (checkRepeatFlag())
126 // Yes, we can now quit repeating:
128 QMutexLocker ciflocker(&commandIFMutex);
129 commandInFlight = false;
134 catch (PIRException e)
136 emit commandFailed(e.getError().c_str());
140 QMutexLocker cifLocker(&commandIFMutex);
141 commandInFlight = false;
145 int RC6SkyProtocol::pushBits(
146 const CommandSequence &bits,
147 PIRRX51Hardware &rx51device)
151 CommandSequence::const_iterator i = bits.begin();
153 while (i != bits.end())
157 duration += pushOne(rx51device);
161 duration += pushZero(rx51device);
171 // This should be part of a general RC6 parent maybe?
172 int RC6SkyProtocol::pushZero(
173 PIRRX51Hardware &rx51device)
175 // Need to add a space, then a pulse.
178 if (bufferContainsSpace)
180 // Merge this space and the previous one, and send to device:
181 rx51device.addSingle(buffer + biphaseUnit);
182 duration += (buffer + biphaseUnit);
184 bufferContainsSpace = false;
188 if (bufferContainsPulse)
190 // Flush out the buffer:
191 rx51device.addSingle(buffer);
194 bufferContainsPulse = false;
197 // push a space onto the device:
198 rx51device.addSingle(biphaseUnit);
199 duration += biphaseUnit;
202 // Put a pulse into the buffer to wait:
203 buffer = biphaseUnit;
204 bufferContainsPulse = true;
210 int RC6SkyProtocol::pushOne(
211 PIRRX51Hardware &rx51device)
213 // Need to add a pulse, then a space.
217 if (bufferContainsPulse)
219 rx51device.addSingle(buffer + biphaseUnit);
220 duration += (buffer + biphaseUnit);
222 bufferContainsPulse = false;
226 if (bufferContainsSpace)
229 rx51device.addSingle(buffer);
232 bufferContainsSpace = false;
234 // Now, add the pulse:
235 rx51device.addSingle(biphaseUnit);
236 duration += biphaseUnit;
239 // Next, push a space onto the buffer:
240 buffer = biphaseUnit;
241 bufferContainsSpace = true;