1 #include "mceprotocol.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 // Ok, what I've got on MCE protocol is this:
17 // It is based on RC6 mode 6A, with a few odd tweaks.
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, is set to 0, and is _not_ toggled!
23 // The next 16 bits I'm not sure about. Hopefully they will be a fixed value
24 // for any given device...
25 // Then, the next bit is the new toggle bit.
26 // Following this, there are seven "device address" bits, and then eight
28 // A space of (at least) 2666 usec must follow any command.
29 // The carrier frequency is 36 kHZ, duty cycle between 25 and 50 %.
31 MCEProtocol::MCEProtocol(
35 : PIRProtocol(guiObject, index, 2666, false),
40 setCarrierFrequency(36000);
41 appendToBitSeq(oemBits, oemData, 16);
45 void MCEProtocol::startSendingCommand(
46 unsigned int threadableID,
51 // Is this command meant for us?
52 if (threadableID != id) return;
56 KeycodeCollection::const_iterator i = keycodes.find(command);
59 if (i == keycodes.end())
61 std::string s = "Tried to send a non-existent command.\n";
62 throw PIRException(s);
65 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
69 while (repeatCount < MAX_REPEAT_COUNT)
71 bufferContainsSpace = false;
72 bufferContainsPulse = false;
73 // First, construct the "Header" segment of the pulse train.
75 // The header involves:
76 // a) a "lead" of 2666 us pulse, 888 us space;
77 // b) a "start bit", value 1 (so 444 us pulse, 444 us space)
78 // c) three control bits, set to "110" (i.e., mode "6")
79 // d) the double-sized "trailer" bit, set to 0.
81 rx51device.addSingle(HEADER_PULSE); // lead pulse
82 duration += HEADER_PULSE;
83 rx51device.addSingle(HEADER_SPACE); // lead space
84 duration += HEADER_SPACE;
85 rx51device.addSingle(biphaseUnit); // start bit pulse
86 duration += biphaseUnit;
87 rx51device.addSingle(biphaseUnit); // start bit space
88 duration += biphaseUnit;
89 rx51device.addSingle(biphaseUnit); // bit 1 pulse;
90 duration += biphaseUnit;
91 rx51device.addSingle(biphaseUnit); // bit 1 space;
92 duration += biphaseUnit;
93 rx51device.addSingle(biphaseUnit); // bit 2 pulse;
94 duration += biphaseUnit;
95 rx51device.addSingle(2 * biphaseUnit); // bit 2 space + bit 3 space;
96 duration += 2 * biphaseUnit;
97 rx51device.addSingle(biphaseUnit); // bit 3 pulse;
98 duration += biphaseUnit;
99 rx51device.addSingle(2 * biphaseUnit); // trailer space
100 duration += 2 * biphaseUnit;
101 buffer = 2 * biphaseUnit; // trailer pulse goes into the buffer
102 bufferContainsPulse = true;
104 // Now, we can start the normal buffering process:
106 // push the "OEM" data:
107 duration += pushBits(oemBits, rx51device);
109 // The next bit is the MCE toggle bit:
110 if (keypressCount % 2)
116 pushZero(rx51device);
119 // push the device address data:
120 duration += pushBits(preData, rx51device);
122 // push the command data:
123 duration += pushBits((*i).second.firstCode, rx51device);
125 // Flush out the buffer, if necessary:
128 rx51device.addSingle(buffer);
133 // Actually send out the command:
134 rx51device.sendCommandToDevice();
136 // Sleep for an amount of time. (RC6 demands an addtional 6 unit space
137 // at the end of any command...)
138 sleepUntilRepeat(duration + 6 * biphaseUnit);
140 // Have we been told to stop yet?
141 if (checkRepeatFlag())
143 // Yes, we can now quit repeating:
145 QMutexLocker ciflocker(&commandIFMutex);
146 commandInFlight = false;
151 catch (PIRException e)
153 emit commandFailed(e.getError().c_str());
157 QMutexLocker cifLocker(&commandIFMutex);
158 commandInFlight = false;
162 int MCEProtocol::pushBits(
163 const CommandSequence &bits,
164 PIRRX51Hardware &rx51device)
168 CommandSequence::const_iterator i = bits.begin();
170 while (i != bits.end())
174 duration += pushOne(rx51device);
178 duration += pushZero(rx51device);
188 // This should be part of a general RC6 parent maybe?
189 int MCEProtocol::pushZero(
190 PIRRX51Hardware &rx51device)
192 // Need to add a space, then a pulse.
195 if (bufferContainsSpace)
197 // Merge this space and the previous one, and send to device:
198 rx51device.addSingle(buffer + biphaseUnit);
199 duration += (buffer + biphaseUnit);
201 bufferContainsSpace = false;
205 if (bufferContainsPulse)
207 // Flush out the buffer:
208 rx51device.addSingle(buffer);
211 bufferContainsPulse = false;
214 // push a space onto the device:
215 rx51device.addSingle(biphaseUnit);
216 duration += biphaseUnit;
219 // Put a pulse into the buffer to wait:
220 buffer = biphaseUnit;
221 bufferContainsPulse = true;
227 int MCEProtocol::pushOne(
228 PIRRX51Hardware &rx51device)
230 // Need to add a pulse, then a space.
234 if (bufferContainsPulse)
236 rx51device.addSingle(buffer + biphaseUnit);
237 duration += (buffer + biphaseUnit);
239 bufferContainsPulse = false;
243 if (bufferContainsSpace)
246 rx51device.addSingle(buffer);
249 bufferContainsSpace = false;
251 // Now, add the pulse:
252 rx51device.addSingle(biphaseUnit);
253 duration += biphaseUnit;
256 // Next, push a space onto the buffer:
257 buffer = biphaseUnit;
258 bufferContainsSpace = true;