An I/O controller for virtual pinball machines: accelerometer nudge sensing, analog plunger input, button input encoding, LedWiz compatible output controls, and more.
Dependencies: mbed FastIO FastPWM USBDevice
Fork of Pinscape_Controller by
IRProtocols.h
00001 // IR Remote Protocols 00002 // 00003 // This file implements a handlers for specific IR protocols. A protocol 00004 // is the set of rules that a particular IR remote uses to convert binary 00005 // data to and from a series of timed infrared pulses. A protocol generally 00006 // encompasses a bit-level encoding scheme (how each data bit is represented 00007 // as one or more pulses of IR light) and a message or packet format (how a 00008 // series of bits is assembled to form a higher level datatype, which in the 00009 // case of an IR remote usually amounts to a representation of a key press 00010 // on a remote control). 00011 // 00012 // Lots of companies make CE products with remotes, and many of them use 00013 // their own custom protocols. There's some commonality; a few proprietary 00014 // protocols, such as those from NEC and Philips, are quasi industry 00015 // standards that multiple companies adopted, more or less intact. On 00016 // the other hand, other companies not only have their own systems but 00017 // use multiple proprietary systems across different products. So it'll 00018 // never be possible for us to recognize every remote code out there. 00019 // So we'll cover the widely used ones, and then add the rarer ones as 00020 // needed. 00021 // 00022 // For each protocol we recognize, we define a subclass of IRReceiver 00023 // that implements the code to encode and decode signals for the protocol. 00024 // To send a command, we call the sender function for the protocol we want 00025 // to use for the transmission. When we receive a signal, we run it through 00026 // each protocol class's decode routine, to determine which protocol the 00027 // signal was encoded with and what the signal means. The decoders can 00028 // usually tell if a signal uses their protocol, since there's enough 00029 // structure in most of the protocols that you can distinguish signals that 00030 // use the protocol from those that don't. This allows the decoders to 00031 // serve the dual purposes of decoding signals and also classifying them 00032 // by protocol. 00033 // 00034 // To add support for a new protocol, we (a) define a class for it here, 00035 // and (b) add an entry for the class to IRProtocolList.h. The list entry 00036 // automatically adds the new protocol to the tables we use to look up the 00037 // desired protocol class when sending, and to check each supported protocol 00038 // when receiving. 00039 // 00040 // The protocol decoders operate in parallel: as a transmission is received, 00041 // we run the signal through all of the decoders at the same time. This 00042 // allows each decoder to keep track of the incoming pulse stream and 00043 // recognize messages that conform to its protocol. The parallel operation 00044 // means that each protocol object needs its own complete, independent 00045 // receiver state. 00046 // 00047 // In contrast, there can only be one transmission in progress at a time, 00048 // since a transmission obviously requires exclusive access to the IR LED. 00049 // (You can't eve interleave two transmissions in theory, since the coding 00050 // is all about pulse timing and order.) That means that we *don't* need 00051 // separate independent transmitter state per object. That lets us save 00052 // a little memory by using a single, shared transmitter state object, 00053 // managed by the global transmitter class and passed to the current 00054 // transmitter on each send. The exception would be any state that has 00055 // to be maintained across, which would have to be tracked per protocol. 00056 // The only common example is "toggle bits". 00057 // 00058 00059 #ifndef _IRPROTOCOLS_H_ 00060 #define _IRPROTOCOLS_H_ 00061 00062 #include <ctype.h> 00063 #include "NewPwm.h" 00064 #include "IRRemote.h" 00065 #include "IRReceiver.h" 00066 #include "IRProtocolID.h" 00067 #include "IRCommand.h" 00068 00069 using namespace IRRemote; 00070 00071 struct DebugItem { char c; int t; DebugItem(char c, int t) : c(c),t(t) {} 00072 DebugItem(char c) : c(c), t(0) { } DebugItem() : c(0), t(0) { } 00073 }; 00074 extern CircBuf<DebugItem,256> debug; 00075 00076 00077 // IR transmitter state object. 00078 // 00079 // We can only transmit one signal at a time, so we only need to keep 00080 // track of the state of one transmission at a time. This lets us 00081 // save a little memory by using a single combined state object that's 00082 // shared by all transmitters. The transmitter currently operating 00083 // uses the shared object for as long as the transmission takes. 00084 struct IRTXState 00085 { 00086 // Time since the transmission started 00087 Timer txTime; 00088 00089 // The command code we're transmitting 00090 uint64_t cmdCode; 00091 00092 // Bit stream to transmit. Many of the protocols use a universal 00093 // command representation (in IRCommand.code) that rearranges the 00094 // bits from the transmission order. This is a scratch pad where the 00095 // protocol handler can translate a command code back into transmission 00096 // order once at the start of the transmission, then just shift bits out 00097 // of here to transmit them in sequence. 00098 uint64_t bitstream; 00099 00100 // The IR LED control pin 00101 NewPwmOut *pin; 00102 00103 // Protocol ID 00104 uint8_t protocolId; 00105 00106 // Transmission step. The meaning is up to the individual protocols, 00107 // but generally this is used to keep track of the current structural 00108 // part of the pulse stream we're generating. E.g., a protocol might 00109 // use 0 for the header mark, 1 for the header space, etc. 00110 uint8_t step; 00111 00112 // number of bits to transmit 00113 uint8_t nbits; 00114 00115 // Current bit position within the data 00116 uint8_t bit; 00117 00118 // Substep within the current bit. Many of the protocols represent each 00119 // bit as multiple IR symbols, such as mark+space. This keeps track of 00120 // the step within the current bit. 00121 uint8_t bitstep; 00122 00123 // Repeat phase. Some protocols have rules about minimum repeat counts, 00124 // or use different coding for auto-repeats. This lets the sender keep 00125 // track of the current step. For example, the Sony protocol requires 00126 // each message to be sent a minimum of 3 times. The NEC protocol uses 00127 // a "ditto" code for each repeat of a code after the initial command. 00128 // The OrtekMCE protocol requires a minimum of 2 sends per code, and has 00129 // a position counter within the code that indicates which copy we're on. 00130 uint8_t rep; 00131 00132 // Is the virtual button that initiated this transmission still pressed? 00133 // The global transmitter sets this before each call to txStep() to let 00134 // the protocol know if it should auto-repeat at the end of the code. 00135 uint8_t pressed : 1; 00136 00137 // Use "ditto" codes when sending repeats? Some protocols use special 00138 // coding for auto-repeats, so that receivers can tell whether a key 00139 // is being held down or pressed repeatedly. But in some cases, the 00140 // same protocol may be used with dittos on some devices but without 00141 // dittos on other devices. It's therefore not always enough to know 00142 // that the protocol supports dittos; we have to know separately whether 00143 // the device we're sending to wants them. This flag lets the caller 00144 // tell us which format to use. This is ignored if the protocol either 00145 // never uses dittos or always does: in that case we'll do whatever the 00146 // protocol specifies. To implement a "learning remote", you should 00147 // make sure that the user holds down each key long enough for several 00148 // repeats when learning codes, so that the learning remote can determine 00149 // when dittos are used by observing how the repeats are sent from the 00150 // reference remote. Then you can set this bit if you saw any ditto 00151 // codes during training for a given key. 00152 uint8_t dittos : 1; 00153 00154 // TX toggle bit. We provide this for protocols that need it. Note 00155 // that this is a global toggle bit, so if we switch from transmitting 00156 // one protocol to another and then return to the first, we'll lose 00157 // continuity with the toggle sequence in the original protocol. But 00158 // that shouldn't be a problem: the protocols with toggles only use it 00159 // to distinguish two rapid presses of the same key in succession from 00160 // auto-repeat while the key is held down. If we had an intervening 00161 // transmission to another protocol, the original receiver will see a 00162 // long gap between the earlier and later messages; the toggle bit isn't 00163 // necessary in this case to tell that these were two key presses. 00164 uint8_t toggle : 1; 00165 }; 00166 00167 00168 // Base class for all protocols 00169 // 00170 // Note that most of the data parameters are set through virtuals rather 00171 // than member variables. This helps minimize the RAM footprint. 00172 // 00173 class IRProtocol 00174 { 00175 public: 00176 IRProtocol() { } 00177 virtual ~IRProtocol() { } 00178 00179 // look up a protocol by ID 00180 static IRProtocol *senderForId(int id); 00181 00182 // name and ID 00183 virtual const char *name() const = 0; 00184 virtual int id() const = 0; 00185 00186 // Are we a transmitter for the given protocol? Some protocol 00187 // handlers send and receive variations on a protocol that have 00188 // distinct protocol IDs, such as the various Sony codes at 00189 // different bit sizes. By default, we assume we handle only 00190 // our nominal protocol as returned by id(). 00191 virtual bool isSenderFor(int protocolId) const { return protocolId == id(); } 00192 00193 // parse a pulse on receive 00194 virtual void rxPulse(IRRecvProIfc *receiver, uint32_t t, bool mark) = 0; 00195 00196 // PWM carrier frequency used for the IR signal, expressed as a PWM 00197 // cycle period in seconds. We use this to set the appropriate PWM 00198 // frequency for transmissions. The most common carrier is 38kHz. 00199 // 00200 // We can't use adjust the carrier frequency for receiving signals, since 00201 // the TSOP sensor we use does the demodulation at a fixed frequency. 00202 // You can choose a frequency by choosing your sensor, since TSOP sensors 00203 // are available in a range of carrier frequencies, but once you choose a 00204 // sensor we can't change its frequency in software. Fortunately, the 00205 // TSOP384xx seems tolerant in practice of a fairly wide range around 00206 // its nominal 38kHz carrier. I've successfully tested it with remotes 00207 // documented to use frequencies from 36 to 40 kHz. Most CE remotes 00208 // fall within this range, so the 38kHz sensor makes a good universal 00209 // receiver. 00210 virtual float pwmPeriod(IRTXState *state) const { return 26.31578947e-6f; } // 38kHz 00211 00212 // PWM duty cycle when transmitting. This is the proportion of On to 00213 // Off time for each PWM pulse. A few of the IR protocols that have 00214 // official documentation do specify a duty cycle, so when this is laid 00215 // out in the spec, it's probably a good idea to use the same value in 00216 // our protocol implementation. In practice, though, it doesn't seem 00217 // to be an important parameter as far as the receivers are concerned, 00218 // and I'm not sure it actually matters for any of the protocols. To 00219 // the extent it's specified at all, they might just be documenting the 00220 // original manufacturer's implementation rather than specifying a 00221 // requirement. 00222 virtual float pwmDutyCycle() const { return .3f; } 00223 00224 // Begin transmitting the given command. Before calling, the caller 00225 // turns off the IR LED and sets its PWM period to the one given by 00226 // our pwmPeriod() method. The caller also clears 'state', and then 00227 // sets the 'cmd', 'pin', and 'pressed' fields. The rest of the struct 00228 // is for the protocol handler's private use during the transmission. 00229 // handler is free to store its interim state here to pass information 00230 // from txStart() to txStep() and from one txStep() to the next. 00231 // 00232 // Subclass implementations should start by setting up 'state' with any 00233 // data they need during the transmission. E.g., convert the code word 00234 // value into a series of bits to transmit, and store this in the 00235 // 'bitstream' field. Once that's set up, determine how long the initial 00236 // gap should be (the IR off time between transmissions in the protocol), 00237 // and return this time in microseconds. The caller will return to 00238 // other work while the gap time is elapsing, then it will call txStep() 00239 // to advance to the next step of the transmission. 00240 // 00241 // DON'T do a timer pause or spin wait here. This routine is called in 00242 // interrupt context, so it must return as quickly as possible. All 00243 // waits should be done simply by returning the desired wait time. 00244 // 00245 // By convention, implementations should start each transmission with a 00246 // sufficient gap time (IR off) to allow senders to recognize the new 00247 // transmission. It's best to put the gap time at the *start* of each 00248 // new transmission rather than at the end because two consecutive 00249 // transmissions might use different protocols with different timing 00250 // requirements. If the first protocol has a short gap time and the 00251 // second has a long gap time, putting the gap at the end of the first 00252 // transmission would only use the shorter time, which might not be 00253 // sufficient for the second protocol's receiver. 00254 virtual int txStart(IRTXState *state) = 0; 00255 00256 // Continue a transmission with the next pulse step. This carries 00257 // out the next step of the transmission, then returns a time value 00258 // with the number of microseconds until the next event. For example, 00259 // if the current step in the transmission is a 600us mark, turn on the 00260 // IR transmitter, update 'state' to indicate that we're in the mark, 00261 // and return 600 to tell the caller to go do other work while the 00262 // mark is being sent. Don't wait or spin here, since this is called 00263 // in interrupt context and should thus return as quickly as possible. 00264 // 00265 // Before calling this, the caller will update the 'pressed' field in 00266 // 'state' to let us know if the virtual button is still being pressed. 00267 // The protocol handler can auto-repeat if the button is still pressed 00268 // at the end of the current transmission, if that's appropriate for 00269 // the protocol. We let the handlers choose how to handle auto-repeat 00270 // rather than trying to manage it globally, since there's a lot of 00271 // variation in how it needs to be handled. Some protocols, for example, 00272 // use "ditto" codes (distinct codes that mean "same as last time" 00273 // rather than actually re-transmitting a full command), while others 00274 // have internal position markers to indicate a series of repeats 00275 // for one key press. 00276 virtual int txStep(IRTXState *state) = 0; 00277 00278 // protocol singletons 00279 static class IRProtocols *protocols; 00280 00281 // allocate the protocol singletons, if we haven't already 00282 static void allocProtocols(); 00283 00284 protected: 00285 // report code with a specific protocol and sub-protocol 00286 void reportCode(IRRecvProIfc *receiver, int pro, uint64_t code, bool3 toggle, bool3 ditto); 00287 }; 00288 00289 // ----------------------------------------------------------------------- 00290 // 00291 // Protocol containing a decoded command value 00292 // 00293 template<class CodeType> class IRPWithCode: public IRProtocol 00294 { 00295 public: 00296 IRPWithCode() 00297 { 00298 rxState = 0; 00299 bit = 0; 00300 code = 0; 00301 } 00302 00303 // Minimum gap on receive (space before header) 00304 virtual uint32_t minRxGap() const = 0; 00305 00306 // Gap time to send on transmit between before the first code, and 00307 // between repeats. By default, we use the the generic txGap() in 00308 // both cases. 00309 virtual uint32_t txGap(IRTXState *state) const = 0; 00310 virtual uint32_t txPreGap(IRTXState *state) const { return txGap(state); } 00311 virtual uint32_t txPostGap(IRTXState *state) const { return txGap(state); } 00312 00313 // Minimum number of repetitions when transmitting. Some codes 00314 // (e.g., Sony) require a minimum repeat count, no matter how 00315 // quickly the button was released. 1 means that only a single 00316 // transmission is required, which is true of most codes. 00317 virtual int txMinReps(IRTXState *state) const { return 1; } 00318 00319 // Header mark and space length. Most IR protocols have an initial 00320 // mark and space of fixed length as a lead-in or header. Being of 00321 // fixed length, they carry no data themselves, but they serve three 00322 // useful functions: the initial mark can be used to set an AGC level 00323 // if the receiver needs it (the TSOP sensors don't, but some older 00324 // devices did); they can help distinguish one protocol from another 00325 // by observing the distinctive timing of the header; and they serve 00326 // as synchronization markers, by using timing that can't possibly 00327 // occur in the middle of a code word to tell us unambiguously that 00328 // we're at the start of a code word. 00329 // 00330 // Not all protocols have the lead-in mark and/or space. Some 00331 // protocols simply open with the initial data bit, and others use 00332 // an AGC mark but follow it immediately with a data bit, with no 00333 // intervening fixed space. 00334 // 00335 // * If the protocol doesn't use a header mark at all but opens with 00336 // a mark that's part of a data bit, set tHeaderMark() to 0. 00337 // 00338 // * If the protocol has a fixed AGC mark, but follows it with a 00339 // varying-length space that's part of the first data bit, set 00340 // tHeaderMark() to the fixed mark length and set tHeaderSpace() to 0. 00341 // 00342 virtual uint32_t tHeaderMark() const = 0; 00343 virtual uint32_t tHeaderSpace() const = 0; 00344 00345 // Can the header space be adjacent to a data space? In most protocols, 00346 // the header space is of constant length because it's always followed 00347 // by a mark. This is accomplished in some protocols with explicit 00348 // structural marks; in some, it happens naturally because all bits start 00349 // with marks (e.g., NEC, Sony); and in others, the protocol requires a 00350 // a "start bit" with a fixed 0 or 1 value whose representation starts 00351 // with a mark (e.g., RC6). But in a few protocols (e.g., OrtekMCE), 00352 // there's no such care taken, so the header space can flow into a space 00353 // at the start of the first data bit. Set this to true for such 00354 // protocols, and we'll divvy up the space after the header mark into 00355 // a fixed part and a data portion for the first bit. 00356 virtual bool headerSpaceToData() const { return false; } 00357 00358 // Ditto header. For codes with dittos that use a distinct header 00359 // format, this gives the header timing that identifies a ditto. 00360 // Return zero for a code that doesn't use dittos at all or encodes 00361 // them in some other way than a distinctive header (e.g., in the 00362 // payload data). 00363 virtual uint32_t tDittoMark() const { return 0; } 00364 virtual uint32_t tDittoSpace() const { return 0; } 00365 00366 // Stop mark length. Many codes have a fixed-length mark following 00367 // the data bits. Return 0 if there's no final mark. 00368 virtual uint32_t tStopMark() const { return 0; } 00369 00370 // Number of bits in the code. For protocols with multiple bit 00371 // lengths, use the longest here. 00372 virtual int nbits() const = 0; 00373 00374 // true -> bits arrive LSB-first, false -> MSB first 00375 virtual bool lsbFirst() const { return true; } 00376 00377 // Pulse processing state machine. 00378 // This state machine handles the basic protocol structure used by 00379 // most IR remotes: 00380 // 00381 // Header mark of fixed duration 00382 // Header space (possibly followed directly by the first bit's space) 00383 // Data bits 00384 // Stop mark 00385 // Gap between codes 00386 // Ditto header mark } a pattern that's distinguishable from 00387 // Ditto header space } the standard header mark 00388 // Ditto data bits 00389 // Gap betwee codes 00390 // 00391 // The state machine can handle protocols that use all of these sections, 00392 // or only a subset of them. For example, a few protocols (Denon, RC5) 00393 // have no header at all and start directly wtih the data bits. Most 00394 // protocols have no "ditto" coding and just repeat the main code to 00395 // signal auto-repeat. 00396 // 00397 // The monolithic state machine switch looks kind of ugly, but it seems 00398 // to be the cleanest way to handle this. My initial design was more OO, 00399 // using virtual subroutines to handle each step. But that turned out to 00400 // be fragile, because there was too much interaction between the handlers 00401 // and the overall state machine sequencing. The monolithic switch actually 00402 // seems much cleaner in practice. The variations are all handled through 00403 // simple data parameters. The resulting design isn't as flexible in 00404 // principle as something more virtualized at each step, but nearly all 00405 // of the IR protocols I've seen so far are so close to the same basic 00406 // structure that this routine works for practically everything. Any 00407 // protocols that don't fit the same structure will be best served by 00408 // replacing the whole state machine for the individual protocols. 00409 virtual void rxPulse(IRRecvProIfc *receiver, uint32_t t, bool mark) 00410 { 00411 uint32_t tRef; 00412 switch (rxState) 00413 { 00414 case 0: 00415 s0: 00416 // Initial gap or inter-code gap. When we see a space of 00417 // sufficient length, switch to Header Mark mode. 00418 rxState = (!mark && t > minRxGap() ? 1 : 0); 00419 break; 00420 00421 case 1: 00422 s1: 00423 // Header mark. If the protocol has no header mark, go 00424 // straight to the data section. Otherwise, if we have 00425 // a mark that matches the header mark we're expecting, 00426 // go to Header Space mode. Otherwise, we're probably in 00427 // the middle of a code that we missed the beginning of, or 00428 // we're just receiving a code using another protocol. Go 00429 // back to Gap mode - that will ignore everything until we 00430 // get radio silence for long enough to be sure we're 00431 // starting a brand new code word. 00432 if ((tRef = tHeaderMark()) == 0) 00433 goto s3; 00434 rxState = (mark && inRange(t, tRef) ? 2 : 0); 00435 break; 00436 00437 case 2: 00438 s2: 00439 // Header space. If the protocol doesn't have a header 00440 // space, go straight to the data. 00441 if ((tRef = tHeaderSpace()) == 0) 00442 goto s3; 00443 00444 // If this protocol has an undelimited header space, make 00445 // sure this space is long enough to qualify, but allow it 00446 // to be longer. If it qualifies, deduct the header space 00447 // span from it and apply the balance as the first data bit. 00448 if (headerSpaceToData() && aboveRange(t, tRef, tRef)) 00449 { 00450 t -= tRef; 00451 goto s3; 00452 } 00453 00454 // If we have a space matching the header space, enter the 00455 // data section. 00456 if (!mark && inRange(t, tRef)) 00457 { 00458 rxState = 3; 00459 break; 00460 } 00461 00462 // Not a match - go back to gap mode 00463 goto s0; 00464 00465 case 3: 00466 s3: 00467 // enter data section 00468 rxReset(); 00469 if (mark) 00470 goto s4; 00471 else 00472 goto s5; 00473 00474 case 4: 00475 s4: 00476 // data mark 00477 if (mark && rxMark(receiver, t)) 00478 { 00479 rxState = bit < nbits() ? 5 : 7; 00480 break; 00481 } 00482 goto s0; 00483 00484 case 5: 00485 s5: 00486 // data space 00487 if (!mark && rxSpace(receiver, t)) 00488 { 00489 rxState = bit < nbits() ? 4 : 6; 00490 break; 00491 } 00492 else if (!mark && t > minRxGap()) 00493 goto s7; 00494 goto s0; 00495 00496 case 6: 00497 // stop mark - if we don't have a mark, go to the gap instead 00498 if (!mark) 00499 goto s7; 00500 00501 // check to see if the protocol even has a stop mark 00502 if ((tRef = tStopMark()) == 0) 00503 { 00504 // The protocol has no stop mark, and we've processed 00505 // the last data bit. Close the data section and go 00506 // straight to the next header. 00507 rxClose(receiver, false); 00508 goto s8; 00509 } 00510 00511 // there is a mark - make sure it's in range 00512 if (inRange(t, tRef)) 00513 { 00514 rxState = 7; 00515 break; 00516 } 00517 goto s0; 00518 00519 case 7: 00520 s7: 00521 // end of data - require minimum gap 00522 if (!mark && t > minRxGap()) 00523 { 00524 // close the data section 00525 rxClose(receiver, false); 00526 rxState = 8; 00527 break; 00528 } 00529 goto s0; 00530 00531 case 8: 00532 s8: 00533 // Ditto header. If the protocol has a ditto header at all, 00534 // and this mark matches, proceed to the ditto space. Otherwise 00535 // try interepreting this as a new regular header instead. 00536 if (mark && (tRef = tDittoMark()) != 0 && inRange(t, tRef)) 00537 { 00538 rxState = 9; 00539 break; 00540 } 00541 goto s1; 00542 00543 case 9: 00544 // Ditto space. If this doesn't match the ditto space, and 00545 // the ditto header and regular header are the same, try 00546 // re-interpreting the space as a new regular header space. 00547 if (!mark && (tRef = tDittoSpace()) != 0 && inRange(t, tRef)) 00548 { 00549 rxState = 10; 00550 break; 00551 } 00552 else if (!mark && tDittoMark() == tHeaderMark()) 00553 goto s2; 00554 goto s0; 00555 00556 case 10: 00557 // Enter ditto data 00558 rxDittoReset(); 00559 goto s11; 00560 00561 case 11: 00562 s11: 00563 // Ditto data - mark 00564 if (mark && rxMark(receiver, t)) 00565 { 00566 rxState = bit < nbits() ? 12 : 13; 00567 break; 00568 } 00569 goto s0; 00570 00571 case 12: 00572 // data space 00573 if (!mark && rxSpace(receiver, t)) 00574 { 00575 rxState = bit < nbits() ? 11 : 13; 00576 break; 00577 } 00578 else if (!mark && t > minRxGap()) 00579 goto s13; 00580 goto s0; 00581 00582 case 13: 00583 s13: 00584 // end ditto data 00585 if (!mark && t > minRxGap()) 00586 { 00587 // close the ditto data section 00588 rxClose(receiver, true); 00589 rxState = 8; 00590 break; 00591 } 00592 goto s0; 00593 } 00594 00595 // if this is a space longer than the timeout, go into idle mode 00596 if (!mark && t >= IRReceiver::MAX_PULSE) 00597 rxIdle(receiver); 00598 } 00599 00600 // Start transmission. By convention, we start each transmission with 00601 // a gap of sufficient length to allow receivers to recognize a new 00602 // transmission. The only protocol-specific work we usually have to 00603 // do here is to prepare a bit string to send. 00604 virtual int txStart(IRTXState *state) 00605 { 00606 // convert the code into a bitstream to send 00607 codeToBitstream(state); 00608 00609 // transmit the initial gap to make sure we've been silent long enough 00610 return txPreGap(state); 00611 } 00612 00613 // Continue transmission. Most protocols have a similar structure, 00614 // with a header mark, header gap, data section, and trailing gap. 00615 // We implement the framework for this common structure with a 00616 // simple state machine: 00617 // 00618 // state 0 = done with gap, transmitting header mark 00619 // state 1 = done with header, transmitting header space 00620 // state 2 = transmitting data bits, via txDataStep() in subclasses 00621 // state 3 = done with data, sending post-code gap 00622 // 00623 // Subclasses for protocols that don't follow the usual structure can 00624 // override this entire routine as needed and redefine these internal 00625 // states. Protocols that match the common structure will only have 00626 // to define txDataStep(). 00627 // 00628 // Returns the time to the next step, or a negative value if we're 00629 // done with the transmission. 00630 virtual int txStep(IRTXState *state) 00631 { 00632 // The individual step handlers can return 0 to indicate 00633 // that we should go immediately to the next step without 00634 // a delay, so iterate as long as they return 0. 00635 for (;;) 00636 { 00637 // Use the first-code or "ditto" handling, as appropriate 00638 int t = state->rep > 0 && state->dittos ? 00639 txDittoStep(state) : 00640 txMainStep(state); 00641 00642 // If it's a positive time, it's a delay; if it's a negative 00643 // time, it's the end of the transmission. In either case, 00644 // return the time to the main transmitter routine. If it's 00645 // zero, though, it means to proceed without delay, so we'll 00646 // simply continue iterating. 00647 if (t != 0) 00648 return t; 00649 } 00650 } 00651 00652 // Main transmission handler. This handles the txStep() work for 00653 // the first code in a repeat group. 00654 virtual int txMainStep(IRTXState *state) 00655 { 00656 // One state might transition directly to the next state 00657 // without any time delay, so keep going until we come to 00658 // a wait state. 00659 int t; 00660 for (;;) 00661 { 00662 switch (state->step) 00663 { 00664 case 0: 00665 // Finished the pre-code gap. This is the actual start 00666 // of the transmission, so mark the time. 00667 state->txTime.reset(); 00668 00669 // if there's a header mark, transmit it 00670 state->step++; 00671 if ((t = this->tHeaderMark()) > 0) 00672 { 00673 state->pin->write(pwmDutyCycle()); 00674 return t; 00675 } 00676 break; 00677 00678 case 1: 00679 // finished header mark, start header space 00680 state->step++; 00681 if ((t = this->tHeaderSpace()) > 0) 00682 { 00683 state->pin->write(0); 00684 return this->tHeaderSpace(); 00685 } 00686 break; 00687 00688 case 2: 00689 // data section - this is up to the subclass 00690 if ((t = txDataStep(state)) != 0) 00691 return t; 00692 break; 00693 00694 case 3: 00695 // done with data; send the stop mark, if applicable 00696 state->step++; 00697 if ((t = tStopMark()) > 0) 00698 { 00699 state->pin->write(pwmDutyCycle()); 00700 return t; 00701 } 00702 break; 00703 00704 case 4: 00705 // post-code gap 00706 state->step++; 00707 state->pin->write(0); 00708 if ((t = this->txPostGap(state)) > 0) 00709 return t; 00710 break; 00711 00712 default: 00713 // Done with the iteration. Finalize the transmission; 00714 // this will figure out if we're going to repeat. 00715 return this->txEnd(state); 00716 } 00717 } 00718 } 00719 00720 // Ditto step handler. This handles txStep() work for a repeated 00721 // code. Most protocols just re-transmit the same code each time, 00722 // so by default we use the main step handling. Subclasses for 00723 // protocols that transmit different codes on repeat (such as NEC) 00724 // can override this to send the ditto code instead. 00725 virtual int txDittoStep(IRTXState *state) 00726 { 00727 return txMainStep(state); 00728 } 00729 00730 // Handle a txStep() iteration for a data bit. Subclasses must 00731 // override this to handle the particulars of sending the data bits. 00732 // At the end of the data transmission, the subclass should increment 00733 // state->step to tell us that we've reached the post-code gap. 00734 virtual int txDataStep(IRTXState *state) 00735 { 00736 state->step++; 00737 return 0; 00738 } 00739 00740 // Send the stop bit, if applicable. If there's no stop bit or 00741 // equivalent, simply increment state->step and return 0; 00742 int txStopBit(IRTXState *state) 00743 { 00744 state->step++; 00745 return 0; 00746 } 00747 00748 // Handle the end of a transmission. This figures out if we're 00749 // going to auto-repeat, and if so, resets for the next iteration. 00750 // If we're going to repeat, we return the time to the next tx step, 00751 // as usual. If the transmission is done, we return -1 to indicate 00752 // that no more steps are required. 00753 int txEnd(IRTXState *state) 00754 { 00755 // count the repeat 00756 state->rep++; 00757 00758 // If the button is still down, or we haven't reached our minimum 00759 // repetition count, repeat the code. 00760 if (state->pressed || state->rep < txMinReps(state)) 00761 { 00762 // return to the first transmission step 00763 state->step = 0; 00764 state->bit = 0; 00765 state->bitstep = 0; 00766 00767 // re-generate the bitstream, in case we need to encode positional 00768 // information such as a toggle bit or position counter 00769 codeToBitstream(state); 00770 00771 // restart the transmission timer 00772 state->txTime.reset(); 00773 00774 // we can go immediately to the next step, so return a zero delay 00775 return 0; 00776 } 00777 00778 // we're well and truly done - tell the caller not to call us 00779 // again by returning a negative time interval 00780 return -1; 00781 } 00782 00783 protected: 00784 // Reset the receiver. This is called when the receiver enters 00785 // the data section of the frame, after parsing the header (or 00786 // after a gap, if the protocol doesn't use a header). 00787 virtual void rxReset() 00788 { 00789 bit = 0; 00790 code = 0; 00791 } 00792 00793 // Reset on entering a new ditto frame. This is called after 00794 // parsing a "ditto" header. This is only needed for protocols 00795 // that use distinctive ditto framing. 00796 virtual void rxDittoReset() { } 00797 00798 // Receiver is going idle. This is called any time we get a space 00799 // (IR OFF) that exceeds the general receiver timeout, regardless 00800 // of protocol state. 00801 virtual void rxIdle(IRRecvProIfc *receiver) { } 00802 00803 // Parse a data mark or space. If the symbol is valid, shifts the 00804 // bit into 'code' (the code word under construction) and returns 00805 // true. If the symbol is invalid, returns false. Updates the 00806 // bit counter in 'bit' if this symbol finishes a bit. 00807 virtual bool rxMark(IRRecvProIfc *receiver, uint32_t t) { return false; } 00808 virtual bool rxSpace(IRRecvProIfc *receiver, uint32_t t) { return false; } 00809 00810 // Report the decoded value in our internal register, if it's valid. 00811 // By default, we'll report the code value as stored in 'code', with 00812 // no toggle bit, if the number of bits we've decoded matches the 00813 // expected number of bits as given by nbits(). Subclasses can 00814 // override as needed to do other validation checks; e.g., protocols 00815 // with varying bit lengths will need to check for all of the valid 00816 // bit lengths, and protocols that contain error-checking information 00817 // can validate that. 00818 // 00819 // Unless the protocol subclass overrides the basic pulse handler 00820 // (rxPulse()), this is called when we end the data section of the 00821 // code. 00822 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 00823 { 00824 if (bit == nbits()) 00825 reportCode(receiver, id(), code, bool3::null, ditto); 00826 } 00827 00828 // Encode a universal code value into a bit string in preparation 00829 // for transmission. This should take the code value in state->cmdCode, 00830 // convert it to the string of bits to serially, and store the result 00831 // in state->bitstream. 00832 virtual void codeToBitstream(IRTXState *state) 00833 { 00834 // by default, simply store the code as-is 00835 state->bitstream = state->cmdCode; 00836 state->nbits = nbits(); 00837 } 00838 00839 // Get the next bit for transmission from the bitstream object 00840 int getBit(IRTXState *state) 00841 { 00842 // get the number of bits and the current bit position 00843 int nbits = state->nbits; 00844 int bit = state->bit; 00845 00846 // figure the bit position according to the bit order 00847 int bitpos = (lsbFirst() ? bit : nbits - bit - 1); 00848 00849 // pull out the bit 00850 return int((state->bitstream >> bitpos) & 1); 00851 } 00852 00853 // Set the next bit to '1', optionally incrementing the bit counter 00854 void setBit(bool inc = true) 00855 { 00856 // ignore overflow 00857 int nbits = this->nbits(); 00858 if (bit >= nbits) 00859 return; 00860 00861 // Figure the starting bit position in 'code' for the bit, 00862 // according to whether we're adding them LSB-first or MSB-first. 00863 int bitpos = (lsbFirst() ? bit : nbits - bit - 1); 00864 00865 // mask in the bit 00866 code |= (CodeType(1) << bitpos); 00867 00868 // advance the bit position 00869 if (inc) 00870 ++bit; 00871 } 00872 00873 // Set the next N bits to '1' 00874 void setBits(int n) 00875 { 00876 // ignore overflow 00877 int nbits = this->nbits(); 00878 if (bit + n - 1 >= nbits) 00879 return; 00880 00881 // Figure the starting bit position in 'code' for the bits we're 00882 // setting. If bits arrive LSB-first, the 'bit' counter gives us 00883 // the bit position directly. If bits arrive MSB-first, we need 00884 // to start from the high end instead, so we're starting at 00885 // ((nbits()-1) - bit). However, we want to set multiple bits 00886 // left-to-right in any case, so move our starting position to 00887 // the "end" of the window by moving right n-1 addition bits. 00888 int bitpos = (lsbFirst() ? bit : nbits - bit - n); 00889 00890 // turn that into a bit mask for the first bit 00891 uint64_t mask = (CodeType(1) << bitpos); 00892 00893 // advance our 'bit' counter past the added bits 00894 bit += n; 00895 00896 // set each added bit to 1 00897 for ( ; n != 0 ; --n, mask <<= 1) 00898 code |= mask; 00899 } 00900 00901 // decoding state 00902 uint8_t rxState; 00903 00904 // next bit position 00905 uint8_t bit; 00906 00907 // command code under construction 00908 CodeType code; 00909 }; 00910 00911 // ----------------------------------------------------------------------- 00912 // 00913 // Basic asynchronous encoding 00914 // 00915 // This is essentially the IR equivalent of a wired UART. The transmission 00916 // for a code word is divided into a fixed number of bit time periods of 00917 // fixed length, with each period containing one bit, signified by IR ON for 00918 // 1 or IR OFF for 0. 00919 // 00920 // Simple async coding doesn't have any inherent structure other than the 00921 // bit length. That makes it hard to distinguish from other IR remotes that 00922 // might share the environment, and even from random noise, which is why 00923 // most CE manufacturers use more structured protocols. In practice, remotes 00924 // using this coding are likely have to impose some sort of structure on the 00925 // bits, such as long bit strings, error checking bits, or distinctive prefixes 00926 // or suffixes. The only example of a pure async code I've seen in the wild 00927 // is Lutron, and they do in fact use fairly long codes (36 bits) with set 00928 // prefixes. Their prefixes aren't only distinctive as bit sequences, but 00929 // also in the raw space/mark timing, which makes them effectively serve the 00930 // function that header marks do in most of the structured protocols. 00931 // 00932 template<class CodeType> class IRPAsync: public IRPWithCode<CodeType> 00933 { 00934 public: 00935 IRPAsync() { } 00936 00937 // duration of each bit in microseconds 00938 virtual int tBit() const = 0; 00939 00940 // maximum legal mark in a data section, if applicable 00941 virtual uint32_t maxMark() const { return IRReceiver::MAX_PULSE; } 00942 00943 // Async codes lack the structure of the more typical codes, so we 00944 // use a completely custom pulse handler 00945 virtual void rxPulse(IRRecvProIfc *receiver, uint32_t t, bool mark) 00946 { 00947 uint32_t tRef, tRef2; 00948 switch (this->rxState) 00949 { 00950 case 0: 00951 s0: 00952 // Gap - if this is a long enough space, switch to header mode. 00953 this->rxState = (!mark && t > this->minRxGap() ? 1 : 0); 00954 break; 00955 00956 case 1: 00957 // Header mode. Async protocols don't necessarily have headers, 00958 // but some (e.g., Lutron) do start all codes with a distinctively 00959 // long series of bits. If the subclass defines a header space, 00960 // apply it to sense the start of a code. 00961 if ((tRef = this->tHeaderMark()) == 0) 00962 goto s2; 00963 00964 // we have a defined header mark - make sure this is long enough 00965 tRef2 = tBit(); 00966 if (mark && inRangeOrAbove(t, tRef, tRef2)) 00967 { 00968 // deduct the header time 00969 t = t > tRef ? t - tRef : 0; 00970 00971 // if that leaves us with a single bit time or better, 00972 // treat it as the first data bit 00973 if (inRangeOrAbove(t, tRef2, tRef2)) 00974 goto s2; 00975 00976 // that consumes the whole mark, so just switch to data 00977 // mode starting with the next space 00978 this->rxState = 2; 00979 break; 00980 } 00981 00982 // doesn't look like the start of a code; back to gap mode 00983 goto s0; 00984 00985 case 2: 00986 s2: 00987 // Enter data mode 00988 this->rxReset(); 00989 goto s3; 00990 00991 case 3: 00992 s3: 00993 // Data mode. Process the mark or space as a number of bits. 00994 { 00995 // figure how many bits this symbol represents 00996 int tb = tBit(); 00997 int n = (t + tb/2)/tb; 00998 00999 // figure how many bits remain in the code 01000 int rem = this->nbits() - this->bit; 01001 01002 // check to see if this symbol overflows the bits remaining 01003 if (n > rem) 01004 { 01005 // marks simply can't exceed the bits remaining 01006 if (mark) 01007 goto s0; 01008 01009 // Spaces can exceed the remaining bits, since we can 01010 // have a string of 0 bits followed by a gap between 01011 // codes. Use up the remaining bits as 0's, and apply 01012 // the balance as a gap. 01013 this->bit += rem; 01014 t -= rem*tb; 01015 goto s4; 01016 } 01017 01018 // check if it exceeds the code's maximum mark length 01019 if (mark && t > maxMark()) 01020 goto s0; 01021 01022 // Make sure that it actually looks like an integral 01023 // number of bits. If it's not, take it as a bad code. 01024 if (!inRange(t, n*tb, tb)) 01025 goto s0; 01026 01027 // Add the bits 01028 if (mark) 01029 this->setBits(n); 01030 else 01031 this->bit += n; 01032 01033 // we've consumed the whole interval as bits 01034 t = 0; 01035 01036 // if that's enough bits, we have a decode 01037 if (this->bit == this->nbits()) 01038 goto s4; 01039 01040 // stay in data mode 01041 this->rxState = 3; 01042 } 01043 break; 01044 01045 case 4: 01046 s4: 01047 // done with the code - close it out and start over 01048 this->rxClose(receiver, false); 01049 goto s0; 01050 } 01051 } 01052 01053 // send data 01054 virtual int txDataStep(IRTXState *state) 01055 { 01056 // get the next bit 01057 int b = this->getBit(state); 01058 state->bit++; 01059 01060 // count how many consecutive matching bits follow 01061 int n = 1; 01062 int nbits = state->nbits; 01063 for ( ; state->bit < nbits && this->getBit(state) == b ; 01064 ++n, ++state->bit) ; 01065 01066 // if we're out of bits, advance to the next step 01067 if (state->bit >= nbits) 01068 ++state->step; 01069 01070 // 0 bits are IR OFF and 1 bits are IR ON 01071 state->pin->write(b ? this->pwmDutyCycle() : 0); 01072 01073 // stay on for the number of bits times the time per bit 01074 return n * this->tBit(); 01075 } 01076 }; 01077 01078 01079 // ----------------------------------------------------------------------- 01080 // 01081 // Space-length encoding 01082 // 01083 // This type of encoding uses the lengths of the spaces to encode the bit 01084 // values. Marks are all the same length, and spaces come in two lengths, 01085 // short and long, usually T and 2T for a vendor-specific time T (typically 01086 // on the order of 500us). The short space encodes 0 and the long space 01087 // encodes 1, or vice versa. 01088 // 01089 // The widely used NEC protocol is a space-length encoding, and in practice 01090 // it seems that most of the ad hoc proprietary protocols are also space- 01091 // length encodings, mostly with similar parameters to NEC. 01092 // 01093 template<class CodeType> class IRPSpaceLength: public IRPWithCode<CodeType> 01094 { 01095 public: 01096 IRPSpaceLength() { } 01097 01098 // mark length, in microseconds 01099 virtual int tMark() const = 0; 01100 01101 // 0 and 1 bit space lengths, in microseconds 01102 virtual int tZero() const = 0; 01103 virtual int tOne() const = 0; 01104 01105 // Space-length codings almost always need a mark after the last 01106 // bit, since otherwise the last bit's space (the significant part) 01107 // would just flow into the gap that follows. 01108 virtual uint32_t tStopMark() const { return tMark(); } 01109 01110 // process a mark 01111 virtual bool rxMark(IRRecvProIfc *receiver, uint32_t t) 01112 { 01113 // marks simply delimit spaces in this protocol and thus 01114 // carry no bit information 01115 if (inRange(t, tMark())) 01116 return true; 01117 else 01118 return false; 01119 } 01120 01121 // process a space 01122 virtual bool rxSpace(IRRecvProIfc *receiver, uint32_t t) 01123 { 01124 // a short space represents a '0' bit, a long space is a '1' 01125 if (inRange(t, tZero())) 01126 return this->bit++, true; 01127 else if (inRange(t, tOne())) 01128 return this->setBit(), true; 01129 else 01130 return false; 01131 } 01132 01133 // continue a transmission 01134 virtual int txDataStep(IRTXState *state) 01135 { 01136 // Data section. 01137 if (state->bitstep == 0) 01138 { 01139 // mark - these are fixed length 01140 state->pin->write(this->pwmDutyCycle()); 01141 state->bitstep = 1; 01142 return tMark(); 01143 } 01144 else 01145 { 01146 // space - these are variable length according to the data 01147 state->pin->write(0); 01148 int t = this->getBit(state) ? tOne() : tZero(); 01149 state->bitstep = 0; 01150 01151 // advance to the next bit; stop if we're done 01152 if (++state->bit >= state->nbits) 01153 ++state->step; 01154 01155 // return the space time 01156 return t; 01157 } 01158 } 01159 01160 }; 01161 01162 01163 // ----------------------------------------------------------------------- 01164 // 01165 // Mark-length encoding 01166 // 01167 // This is the inverse of space-length coding. In this scheme, the bit 01168 // values are encoded in the mark length. Spaces are fixed length, and 01169 // marks come in short (0) and long (1) lengths, usually of time T and 2T 01170 // for a protocol-specific time T. 01171 // 01172 // Sony uses this type of encoding. 01173 template<class CodeType> class IRPMarkLength: public IRPWithCode<CodeType> 01174 { 01175 public: 01176 IRPMarkLength() { } 01177 01178 // space length, in microseconds 01179 virtual int tSpace() const = 0; 01180 01181 // 0 and 1 bit mark lengths, in microseconds 01182 virtual int tZero() const = 0; 01183 virtual int tOne() const = 0; 01184 01185 // process a mark 01186 virtual bool rxMark(IRRecvProIfc *receiver, uint32_t t) 01187 { 01188 // a short mark represents a '0' bit, a long space is a '1' 01189 if (inRange(t, tZero())) 01190 this->bit++; 01191 else if (inRange(t, tOne())) 01192 this->setBit(); 01193 else 01194 return false; 01195 return true; 01196 } 01197 01198 // process a space 01199 virtual bool rxSpace(IRRecvProIfc *receiver, uint32_t t) 01200 { 01201 // spaces simply delimit marks in this protocol and carry 01202 // no bit information of their own 01203 return inRange(t, tSpace()); 01204 } 01205 01206 // continue a transmission 01207 virtual int txDataStep(IRTXState *state) 01208 { 01209 // check if we're on a mark (step 0) or space (step 1) 01210 if (state->bitstep == 0) 01211 { 01212 // space - these are variable length according to the data 01213 state->pin->write(this->pwmDutyCycle()); 01214 int t = this->getBit(state) ? tOne() : tZero(); 01215 state->bitstep = 1; 01216 01217 // return the mark time 01218 return t; 01219 } 01220 else 01221 { 01222 // Space - fixed length 01223 state->pin->write(0); 01224 state->bitstep = 0; 01225 01226 // advance to the next bit; stop if we're done 01227 if (++state->bit >= state->nbits) 01228 state->step = 3; 01229 return tSpace(); 01230 } 01231 } 01232 01233 }; 01234 01235 01236 // ----------------------------------------------------------------------- 01237 // 01238 // Manchester coding 01239 // 01240 // This type of coding uses a fixed time per bit, and encodes the bit 01241 // value in a mark/space or space/mark transition within each bit's 01242 // time window. 01243 // 01244 // The decoding process is a little tricky to grap when you're looking 01245 // at just the raw data, because the raw data renders things in terms of 01246 // monolithic marks and spaces of different lengths, whereas the coding 01247 // divides the time axis into even chunks and looks at what's going on 01248 // in each chunk. In terms of the raw data, we can think of it this way. 01249 // Because every bit time window has a transition (mark/space or space/mark) 01250 // in the middle of it, there has to be at least one transition per window. 01251 // There can also be a transition between each window, or not, as needed 01252 // to get the transmitter into the right initial state for the next bit. 01253 // This means that each mark and each space is either T or 2T long, where 01254 // T is the half the bit window time. So we can simply count these units. 01255 // If we see a mark or space of approximate length T, we count one unit; 01256 // if the length is around 2T, we count two units. On each ODD count, we 01257 // look at the state just before the count. If we were in a space just 01258 // before the count, the bit is a 1; if it was a mark, the bit is a 0. 01259 // 01260 // Manchester coding is used in the Philips RC5 and RC6 protocols, which 01261 // are in turn used by most other European CE companies. 01262 template<class CodeType> class IRPManchester: public IRPWithCode<CodeType> 01263 { 01264 public: 01265 IRPManchester() { } 01266 01267 // Half-bit time. This is half of the time interval of one bit, 01268 // so it's equal to the time on each side of the mark/space or 01269 // space/mark transition in the middle of each bit. 01270 virtual int tHalfBit(int bit) const = 0; 01271 01272 // Bit value (0 or 1) of a space-to-mark transition. A mark-to-space 01273 // transition always has the opposite sense. 01274 virtual int spaceToMarkBit() const { return 1; } 01275 inline int markToSpaceBit() const { return !spaceToMarkBit(); } 01276 01277 // reset the decoder state 01278 virtual void rxReset() 01279 { 01280 IRPWithCode<CodeType>::rxReset(); 01281 halfBitPos = 0; 01282 } 01283 01284 // process a mark 01285 virtual bool rxMark(IRRecvProIfc *receiver, uint32_t t) 01286 { 01287 // transitioning from mark to space, so this is a 01288 return processTransition(t, spaceToMarkBit()); 01289 } 01290 01291 // process a space 01292 virtual bool rxSpace(IRRecvProIfc *receiver, uint32_t t) 01293 { 01294 return (processTransition(t, markToSpaceBit())); 01295 } 01296 01297 // Process a space/mark or mark/space transition. Returns true on 01298 // success, false on failure. 01299 bool processTransition(uint32_t t, int bitval) 01300 { 01301 // If this time is close to zero, ignore it. 01302 int thb = tHalfBit(this->bit); 01303 if (t < ((thb*toleranceShl8) >> 8)) 01304 return true; 01305 01306 // If the current time is the middle of a bit, the transition 01307 // specifies a bit value, so set the bit. Transitions between 01308 // bits are just clocking. 01309 if (halfBitPos == 1) 01310 { 01311 if (bitval) 01312 { 01313 // set the bit, but keep the bit counter where it is, since 01314 // we manage it ourselves 01315 this->setBit(); 01316 this->bit--; 01317 } 01318 } 01319 01320 // Advance by the time interval. Check that we have at least one 01321 // half-bit interval to work with. 01322 if (t < ((thb * (256 - toleranceShl8)) >> 8)) 01323 return false; 01324 01325 // If we're at a half-bit position, start by advancing to the next 01326 // bit boundary. 01327 if (halfBitPos) 01328 { 01329 // deduct the half-bit time 01330 t = (t > thb ? t - thb : 0); 01331 01332 // advance our position counters to the next bit boundary 01333 halfBitPos = 0; 01334 this->bit++; 01335 01336 // Some subprotocols (e.g., RC6) have variable bit timing, 01337 // so the timing for this bit might be different than for 01338 // the previous bit. Re-fetch the time. 01339 thb = tHalfBit(this->bit); 01340 } 01341 01342 // If we have another half-interval left to process, advance 01343 // to the middle of the current bit. 01344 if (t < ((thb * toleranceShl8) >> 8)) 01345 { 01346 // we already used up the symbol time, so we're done 01347 return true; 01348 } 01349 else if (inRange(t, thb)) 01350 { 01351 // we have another half-bit time to use, so advance to 01352 // the middle of the bit 01353 halfBitPos = true; 01354 return true; 01355 } 01356 else 01357 { 01358 // The time remaining is wrong, so terminate decoding. 01359 // Note that this could simply be because we've reached 01360 // the gap at the end of the code word, in which case we'll 01361 // already have all of the bits stored and will generate 01362 // the finished code value. 01363 return false; 01364 } 01365 } 01366 01367 virtual int txDataStep(IRTXState *state) 01368 { 01369 // Get the current bit 01370 int b = this->getBit(state); 01371 01372 // Determine if this bit uses a space-to-mark or mark-to-space 01373 // transition. It uses a space-to-mark transition if it matches 01374 // the space-to-mark bit. 01375 int stm = (b == spaceToMarkBit()); 01376 01377 // Check to see if we're at the start or middle of the bit 01378 if (state->bitstep == 0) 01379 { 01380 // Start of the current bit. Set the level for the first 01381 // half of the bit. If we're doing a space-to-mark bit, 01382 // the first half is a space, otherwise it's a mark. 01383 state->pin->write(stm ? 0 : this->pwmDutyCycle()); 01384 01385 // leave this on for a half-bit time to get to the 01386 // middle of the bit 01387 state->bitstep = 1; 01388 return tHalfBit(state->bit); 01389 } 01390 else 01391 { 01392 // Middle of the current bit. Set the level for the second 01393 // half of the bit. If we're in a space-to-mark bit, the 01394 // second half is the mark, otherwise it's the space. 01395 state->pin->write(stm ? this->pwmDutyCycle() : 0); 01396 01397 // figure the time to the start of the next bit 01398 int t = tHalfBit(state->bit); 01399 01400 // advance to the start of the next bit 01401 state->bit++; 01402 state->bitstep = 0; 01403 01404 // If the next bit is the inverse of the current bit, it will 01405 // lead in with the same level we're going out with. That 01406 // means we can go straight to the middle of the next bit 01407 // without another interrupt. 01408 if (state->bit < state->nbits && this->getBit(state) != b) 01409 { 01410 // proceed to the middle of the next bit 01411 state->bitstep = 1; 01412 01413 // add the half-bit time 01414 t += tHalfBit(state->bit); 01415 } 01416 01417 // if this was the last bit, advance to the next state 01418 if (state->bit >= state->nbits) 01419 state->step++; 01420 01421 // return the time to the next transition 01422 return t; 01423 } 01424 } 01425 01426 // Current half-bit position. If the last transition was on the 01427 // border between two bits, this is 0. If it was in the middle 01428 // of a bit, this is 1. 01429 uint8_t halfBitPos : 1; 01430 }; 01431 01432 // ----------------------------------------------------------------------- 01433 // 01434 // NEC protocol family. This is one of the more important proprietary 01435 // protocols, since many CE companies use the standard NEC code or a 01436 // variation of it. This class handles the following variations on the 01437 // basic NEC code: 01438 // 01439 // NEC-32: 32-bit payload, 9000us header mark, 4500us header space 01440 // NEC-32X: 32-bit payload, 4500us header mark, 4500us header space 01441 // NEC-48: 48-bit payload, 9000us header mark, 4500us header space 01442 // Pioneer: NEC-32 with address A0..AF + possible "shift" prefixes 01443 // TCL/Roku: NEC-32 with address EAC7 + doubled code XOR 0x8080 01444 // 01445 // Each of the three NEC-nn protocol types comes in two sub-types: one 01446 // that uses "ditto" codes for repeated keys, and one that simply sends 01447 // the same code again on repeats. The ditto code, when used, varies with 01448 // the main protocol type: 01449 // 01450 // NEC-32: 9000us mark + 2250us space + 564us mark 01451 // NEC-32x: 4500us mark + 4500us space + one data bit + 564us mark 01452 // NEC-48: 9000us mark + 2250us space + 564us mark 01453 // Pioneer: no dittos 01454 // TCL/Roku: no dittos 01455 // 01456 // The NEC-32 and NEC-48 dittos can be detected from the header space 01457 // length. The NEC-32x dittos can be detected by the one-bit code length. 01458 // 01459 // All variations of the NEC code are space-length encodings with 564us 01460 // marks between bits, 564us '0' spaces, and 3*564us '1' spaces. All 01461 // variations use a long header mark and a 564us stop mark. With those 01462 // fixed features, there are three main variations: 01463 // 01464 // The bits in the NEC 32-bit codes are structured into four 8-bit fields, 01465 // with the first bit transmitted in the most significant position: 01466 // 01467 // A1 A0 C1 C0 01468 // 01469 // A1 is the high 8 bits of the address, and A0 is the low 8 bits of the 01470 // address. The address specifies a particular type of device, such as 01471 // "NEC VCR" or "Vizio TV". These are assigned by NEC. C1 and C0 form the 01472 // command code, which has a meaning specific to the device type specified 01473 // by the address field. 01474 // 01475 // In the original NEC protocol, the nominal address is in A1, and A0 is 01476 // the 1's complement of A1 (A0 = ~A1), for error checking. This was removed 01477 // in a later revision to expand the address space. Most modern equipment 01478 // uses the newer system, so A0 is typically an independent value in remotes 01479 // you'll find in use today. 01480 // 01481 // In the official version of the protocol, C1 is the nominal command code, 01482 // and C0 = ~C1, for error checking. However, some other manufacturers who 01483 // use the basic NEC protocol, notably Yamaha and Onkyo, violate this by using 01484 // C0 as an independent byte to expand the command space. We therefore don't 01485 // test for the complemented byte, so that we don't reject codes from devices 01486 // that treat it as independent. 01487 // 01488 // Pioneer uses the NEC protocol with two changes. First, the carrier is 01489 // 40kHz instead of 38kHz. The TSOP384xx seems to receive the 40kHz signal 01490 // reliably, so the frequency doesn't matter on receive, but it might matter 01491 // on transmit if the target equipment isn't as tolerant. Second, Pioneer 01492 // sometimes transmits a second code for the same key. In these cases, the 01493 // first code is a sort of "shift" code (shared among many keys), and the 01494 // second code has the actual key-specific meaning. To learn or recognize 01495 // these extended codes, we have to treat the pair of code words as a single 01496 // command. We sense Pioneer codes based on the address field, and use 01497 // special handling when we find a Pioneer address code. 01498 // 01499 // TCL's Roku models (that is, their TVs that contain embedded Roku features) 01500 // use yet another proprietary variant. They use the standard low-level 01501 // protocol elements (PWM freq and bit timing), but they layer a high-level 01502 // protocol variation where every command consists of two 32-bit code words 01503 // in succession. The second code word repeats the first code word, but with 01504 // the last two bytes (the "command field") each XOR'd with 0x80. TCL's codes 01505 // are recognizable by EAC7 in the command field. The repeated code scheme is 01506 // presumably a redundancy check. Dittos aren't used in this scheme. 01507 // 01508 class IRPNEC: public IRPSpaceLength<uint64_t> 01509 { 01510 public: 01511 // code parameters 01512 virtual uint32_t minRxGap() const { return 3400; } 01513 virtual uint32_t txGap(IRTXState *state) const 01514 { return 108000 - state->txTime.read_us(); } 01515 01516 // The post-code transmit gap is special for TCL Roku for the gap between 01517 // the first and second half of the code. These appear to use a fixed 01518 // 37842us gap. The receiver interprets the normal NEC inter-code gap 01519 // of (108ms minus code transmit time) as a gap between repeats of the 01520 // code rather than as half-codes. 01521 virtual uint32_t txPostGap(IRTXState *state) const 01522 { 01523 // Check for TCL Roku models on even reps. An even rep is the 01524 // first half of a code pair, so we need to use the shorter 01525 // post-code gap for these. 01526 if (state->protocolId == IRPRO_TCLROKU 01527 && (state->rep == 0 || state->rep == 2)) 01528 return 37842; 01529 01530 // use the standard NEC timing for others 01531 return txGap(state); 01532 } 01533 01534 // space length encoding parameters 01535 virtual int tMark() const { return 560; } 01536 virtual int tZero() const { return 560; } 01537 virtual int tOne() const { return 1680; } 01538 01539 // PWM period is 40kHz for Pioneer, 38kHz for everyone else 01540 virtual float pwmPeriod(IRTXState *state) const 01541 { 01542 return state->protocolId == IRPRO_PIONEER ? 25.0e-6f : 26.31578947e-6f; 01543 } 01544 01545 // For Pioneer, we have to send two codes if we have a shift field. 01546 // For TCL Roku models, we always send two codes. 01547 virtual int txMinReps(IRTXState *state) const 01548 { 01549 if (state->protocolId == IRPRO_PIONEER 01550 && (state->cmdCode & 0xFFFF0000) != 0) 01551 return 2; 01552 else if (state->protocolId == IRPRO_TCLROKU) 01553 return 2; 01554 else 01555 return 1; 01556 } 01557 01558 // get the protocol to report for a given data packet bit count 01559 virtual int necPro(int bits) const = 0; 01560 01561 // close out a received bitstream 01562 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 01563 { 01564 // Check the bit count. If nbits() says we can accept 48 bits, 01565 // accept 48 bits, otherwise only accept 32. 01566 if (bit == 32 || (nbits() >= 48 && bit == 48)) 01567 { 01568 uint64_t codeOut; 01569 if (ditto) 01570 { 01571 // report 0 for dittos 01572 codeOut = 0; 01573 } 01574 else 01575 { 01576 // Put the bytes in the right order. The bits are LSB-first, but 01577 // we want the bytes to be ordered within the uint32 as (high to 01578 // low) A0 A1 C0 C1. 01579 uint8_t c1 = uint8_t((code >> 24) & 0xff); 01580 uint8_t c0 = uint8_t((code >> 16) & 0xff); 01581 uint8_t a1 = uint8_t((code >> 8) & 0xff); 01582 uint8_t a0 = uint8_t(code & 0xff); 01583 codeOut = (uint64_t(a0) << 24) | (uint64_t(a1) << 16) 01584 | (uint64_t(c0) << 8) | uint64_t(c1); 01585 01586 // If it's a 48-bit code, add the additional 16 bits for E0 E1 01587 // (the extended command code) at the low end. 01588 if (bit == 48) 01589 { 01590 // get the E1 and E0 bytes from the top end of the code 01591 uint8_t e1 = uint8_t((code >> 40) & 0xff); 01592 uint8_t e0 = uint8_t((code >> 32) & 0xff); 01593 01594 // insert them at the low end in E0 E1 order 01595 codeOut <<= 16; 01596 codeOut |= (uint64_t(e0) << 8) | uint64_t(e1); 01597 } 01598 } 01599 01600 // report it 01601 reportCode(receiver, necPro(bit), codeOut, bool3::null, ditto); 01602 } 01603 } 01604 01605 // convert a code to a bitstream for sending 01606 virtual void codeToBitstream(IRTXState *state) 01607 { 01608 if (state->protocolId == IRPRO_PIONEER) 01609 { 01610 // Check if we have an extended code 01611 uint32_t c; 01612 if ((state->cmdCode & 0xFFFF0000) != 0) 01613 { 01614 // Extended code. These are transmitted as two codes in 01615 // a row, one for the shift code in the high 16 bits, and 01616 // one for the subcode in the low 16 bits. Transmit the 01617 // shift code on even reps and the subcode on odd reps. 01618 if (state->rep == 0 || state->rep == 2) 01619 { 01620 // even rep - use the shift code 01621 c = (state->cmdCode >> 16) & 0xFFFF; 01622 01623 // wrap back to rep 0 on rep 2 01624 state->rep = 0; 01625 } 01626 else 01627 { 01628 // odd rep - use the subcode 01629 c = state->cmdCode & 0xFFFF; 01630 } 01631 } 01632 else 01633 { 01634 // it's a single-part code 01635 c = state->cmdCode; 01636 } 01637 01638 // encode it in the 32-bit original NEC format with the address 01639 // and command byte complemented 01640 uint8_t a0 = uint8_t((c >> 8) & 0xff); 01641 uint8_t a1 = uint8_t(~a0); 01642 uint8_t c0 = uint8_t(c & 0xff); 01643 uint8_t c1 = uint8_t(~c0); 01644 state->bitstream = (uint64_t(c1) << 24) | (uint64_t(c0) << 16) 01645 | (uint64_t(a1) << 8) | uint64_t(a0); 01646 state->nbits = 32; 01647 01648 // Pioneer *can't* use NEC dittos even if the caller thinks we 01649 // should, because that breaks the shift-code model Pioneer uses 01650 state->dittos = false; 01651 } 01652 else if (state->protocolId == IRPRO_TCLROKU) 01653 { 01654 // TCL Roku models use doubled code words. The second code 01655 // word in the pair is always the same as the first with 01656 // the two bytes of the command field XOR'd with 0x80. 01657 uint32_t c; 01658 if (state->rep == 0 || state->rep == 2) 01659 { 01660 // even rep - use the nominal command code 01661 c = state->cmdCode; 01662 01663 // wrap back to rep 0 on rep 2 01664 state->rep = 0; 01665 } 01666 else 01667 { 01668 // odd rep - use the code XOR'd with 0x8080 01669 c = state->cmdCode ^ 0x8080; 01670 } 01671 01672 // use the normal NEC32 encoding, substituting the possibly 01673 // modified code field 'c' we calculated above 01674 uint32_t orig = uint32_t(state->cmdCode); 01675 uint8_t a0 = uint8_t((orig >> 24) & 0xff); 01676 uint8_t a1 = uint8_t((orig >> 16) & 0xff); 01677 uint8_t c0 = uint8_t((c >> 8) & 0xff); 01678 uint8_t c1 = uint8_t(c & 0xff); 01679 state->bitstream = 01680 (uint64_t(c1) << 24) | (uint64_t(c0) << 16) 01681 | (uint64_t(a1) << 8) | uint64_t(a0); 01682 state->nbits = 32; 01683 01684 // this protocol doesn't use dittos 01685 state->dittos = false; 01686 } 01687 else if (state->protocolId == IRPRO_NEC48) 01688 { 01689 // NEC 48-bit code. We store the bytes in the universal 01690 // representation in order A0 A1 C0 C1 E0 E1. Reverse this 01691 // order for transmission. 01692 uint64_t code = state->cmdCode; 01693 uint8_t a0 = uint8_t((code >> 40) & 0xff); 01694 uint8_t a1 = uint8_t((code >> 32) & 0xff); 01695 uint8_t c0 = uint8_t((code >> 24) & 0xff); 01696 uint8_t c1 = uint8_t((code >> 16)& 0xff); 01697 uint8_t e0 = uint8_t((code >> 8) & 0xff); 01698 uint8_t e1 = uint8_t((code) & 0xff); 01699 state->bitstream = 01700 (uint64_t(e1) << 40) | (uint64_t(e0) << 32) 01701 | (uint64_t(c1) << 24) | (uint64_t(c0) << 16) 01702 | (uint64_t(a1) << 8) | uint64_t(a0); 01703 state->nbits = 48; 01704 } 01705 else 01706 { 01707 // NEC 32-bit code. The universal representation stores 01708 // the bytes in order A0 A1 C0 C1. For transmission, we 01709 // need to reverse this to C1 C0 A1 A0. 01710 uint32_t code = uint32_t(state->cmdCode); 01711 uint8_t a0 = uint8_t((code >> 24) & 0xff); 01712 uint8_t a1 = uint8_t((code >> 16) & 0xff); 01713 uint8_t c0 = uint8_t((code >> 8) & 0xff); 01714 uint8_t c1 = uint8_t(code & 0xff); 01715 state->bitstream = 01716 (uint64_t(c1) << 24) | (uint64_t(c0) << 16) 01717 | (uint64_t(a1) << 8) | uint64_t(a0); 01718 state->nbits = 32; 01719 } 01720 } 01721 01722 // NEC uses a special "ditto" code for repeats. The ditto consists 01723 // of the normal header mark, half a header space, a regular data 01724 // mark. After this, a standard inter-message gap follows, and then 01725 // we repeat the ditto as long as the button is held down. 01726 virtual int txDittoStep(IRTXState *state) 01727 { 01728 // send the ditto 01729 uint32_t t; 01730 switch (state->step) 01731 { 01732 case 0: 01733 // Ditto header mark 01734 state->step++; 01735 state->pin->write(pwmDutyCycle()); 01736 01737 // use the special ditto mark timing if it's different; 0 means 01738 // that we use the same timing as the standard data frame header 01739 return (t = tDittoMark()) != 0 ? t : tHeaderMark(); 01740 01741 case 1: 01742 // Ditto header space 01743 state->step++; 01744 state->pin->write(0); 01745 01746 // use the special ditto timing if it's different 01747 return (t = tDittoSpace()) != 0 ? t : tHeaderSpace(); 01748 01749 case 2: 01750 // Data section. NEC-32X sends one data bit. The others 01751 // send no data bits, so go straight to the stop bit. 01752 if (state->protocolId == IRPRO_NEC32X && state->bit == 0) 01753 return txDataStep(state); 01754 01755 // for others, fall through to the stop mark 01756 state->step++; 01757 // FALL THROUGH... 01758 01759 case 3: 01760 // stop mark 01761 state->step++; 01762 state->pin->write(pwmDutyCycle()); 01763 return tMark(); 01764 01765 case 4: 01766 // send a gap 01767 state->step++; 01768 state->pin->write(0); 01769 return 108000 - state->txTime.read_us(); 01770 01771 default: 01772 // done 01773 return txEnd(state); 01774 } 01775 } 01776 01777 }; 01778 01779 // NEC-32, NEC-48, Pioneer, and TCL TVs with embedded Roku 01780 class IRPNEC_32_48: public IRPNEC 01781 { 01782 public: 01783 IRPNEC_32_48() 01784 { 01785 pioneerPrvCode = 0; 01786 tclRokuPrvCode = 0; 01787 } 01788 01789 // name and ID 01790 virtual const char *name() const { return "NEC"; } 01791 virtual int id() const { return IRPRO_NEC32; } 01792 virtual int necPro(int bits) const 01793 { 01794 return bits == 48 ? IRPRO_NEC48 : IRPRO_NEC32; 01795 } 01796 01797 // we encode several protocols 01798 virtual bool isSenderFor(int pro) const 01799 { 01800 return pro == IRPRO_NEC32 01801 || pro == IRPRO_NEC48 01802 || pro == IRPRO_PIONEER 01803 || pro == IRPRO_TCLROKU; 01804 } 01805 01806 // NEC-32 and NEC-48 use the same framing 01807 virtual uint32_t tHeaderMark() const { return 9000; } 01808 virtual uint32_t tHeaderSpace() const { return 4500; } 01809 virtual uint32_t tDittoMark() const { return 9000; } 01810 virtual uint32_t tDittoSpace() const { return 2250; } 01811 virtual int nbits() const { return 48; } 01812 01813 // receiver format descriptor 01814 // decode, check, and report a code value 01815 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 01816 { 01817 // If we're in Pioneer mode, use special handling 01818 if (isPioneerCode()) 01819 { 01820 rxClosePioneer(receiver); 01821 return; 01822 } 01823 01824 // The new code isn't a pioneer code, so if we had a Pioneer 01825 // code stashed, it doesn't have a second half. Report is as 01826 // a standalone code. 01827 if (pioneerPrvCode != 0) 01828 { 01829 reportPioneerFormat(receiver, pioneerPrvCode); 01830 pioneerPrvCode = 0; 01831 } 01832 01833 // If we're in TCL/Roku mode, use special handling 01834 if (isTCLRokuCode()) 01835 { 01836 rxCloseTCLRoku(receiver); 01837 return; 01838 } 01839 01840 // The new code isn't a TCL/Roku code, so if we had a first half 01841 // code stashed, it doesn't have a second half forthcoming. Report 01842 // it as a standalone code. 01843 if (tclRokuPrvCode != 0) 01844 { 01845 reportTCLRokuFormat(receiver, tclRokuPrvCode); 01846 tclRokuPrvCode = 0; 01847 } 01848 01849 // use the generic NEC handling 01850 IRPNEC::rxClose(receiver, ditto); 01851 } 01852 01853 virtual void rxIdle(IRRecvProIfc *receiver) 01854 { 01855 // if we have a stashed prior Pioneer code, close it out, since 01856 // no more codes are forthcoming 01857 if (pioneerPrvCode != 0) 01858 { 01859 reportPioneerFormat(receiver, pioneerPrvCode); 01860 pioneerPrvCode = 0; 01861 } 01862 01863 // likewise for TCL/Roku stashed prior codes 01864 if (tclRokuPrvCode != 0) 01865 { 01866 reportTCLRokuFormat(receiver, tclRokuPrvCode); 01867 tclRokuPrvCode = 0; 01868 } 01869 } 01870 01871 // close out a Pioneer code 01872 virtual void rxClosePioneer(IRRecvProIfc *receiver) 01873 { 01874 // Check to see if we have a valid previous code and/or 01875 // a valid new code. 01876 if (pioneerPrvCode != 0) 01877 { 01878 // We have a stashed Pioneer code plus the new one. If 01879 // they're different, we must have an extended code with 01880 // a "shift" prefix. 01881 if (pioneerPrvCode != code) 01882 { 01883 // distinct code - it's an extended code with a shift 01884 reportPioneerFormat(receiver, pioneerPrvCode, code); 01885 } 01886 else 01887 { 01888 // same code - it's just a repeat, so report it twice 01889 reportPioneerFormat(receiver, code); 01890 reportPioneerFormat(receiver, code); 01891 } 01892 01893 // we've now consumed the previous code 01894 pioneerPrvCode = 0; 01895 } 01896 else 01897 { 01898 // There's no stashed code. Don't report the new one yet, 01899 // since it might be a "shift" prefix. Stash it until we 01900 // find out if another code follows. 01901 pioneerPrvCode = code; 01902 bit = 0; 01903 code = 0; 01904 } 01905 } 01906 01907 // determine if we have Pioneer address 01908 bool isPioneerCode() 01909 { 01910 // pull out the command and address fields 01911 uint8_t c1 = uint8_t((code >> 24) & 0xff); 01912 uint8_t c0 = uint8_t((code >> 16) & 0xff); 01913 uint8_t a1 = uint8_t((code >> 8) & 0xff); 01914 uint8_t a0 = uint8_t(code & 0xff); 01915 01916 // Pioneer uses device codes A0..AF, with A1 complemented, and 01917 // uses only the 32-bit code format. Pioneer also always uses 01918 // a complemented C0-C1 pair. 01919 return bit == 32 01920 && (a0 >= 0xA0 && a0 <= 0xAF) 01921 && a0 == uint8_t(~a1) 01922 && c0 == uint8_t(~c1); 01923 } 01924 01925 // Report a code in Pioneer format. This takes the first address 01926 // byte and combines it with the first command byte to form a 16-bit 01927 // code. Pioneer writes codes in this format because the second 01928 // address and command bytes are always the complements of the first, 01929 // so they contain no information, so it makes the codes more readable 01930 // for human consumption to drop the redundant bits. 01931 void reportPioneerFormat(IRRecvProIfc *receiver, uint32_t code) 01932 { 01933 uint8_t a0 = uint8_t(code & 0xff); 01934 uint8_t c0 = uint8_t((code >> 16) & 0xff); 01935 reportCode(receiver, IRPRO_PIONEER, (uint64_t(a0) << 8) | c0, 01936 bool3::null, bool3::null); 01937 } 01938 01939 // Report an extended two-part code in Pioneer format. code1 is the 01940 // first code received (the "shift" prefix code), and code2 is the 01941 // second (the key-specific subcode). We'll convert each code to 01942 // the standard Pioneer 16-bit format (<address>:<key>), then pack 01943 // the two into a 32-bit int with the shift code in the high half. 01944 void reportPioneerFormat(IRRecvProIfc *receiver, uint32_t code1, uint32_t code2) 01945 { 01946 uint8_t a1 = code1 & 0xff; 01947 uint8_t c1 = (code1 >> 16) & 0xff; 01948 uint8_t a2 = code2 & 0xff; 01949 uint8_t c2 = (code2 >> 16) & 0xff; 01950 reportCode( 01951 receiver, IRPRO_PIONEER, 01952 (uint64_t(a1) << 24) | (uint64_t(c1) << 16) 01953 | (uint64_t(a2) << 8) | c2, 01954 bool3::null, bool3::null); 01955 } 01956 01957 // The previous Pioneer code value. This is used in decoding Pioneer 01958 // codes, since some keys in the Pioneer scheme send a "shift" prefix 01959 // code plus a subcode. 01960 uint32_t pioneerPrvCode; 01961 01962 // close out a TCL/Roku code 01963 virtual void rxCloseTCLRoku(IRRecvProIfc *receiver) 01964 { 01965 // Check to see if we have a valid previous code and/or 01966 // a valid new code. 01967 if (tclRokuPrvCode != 0) 01968 { 01969 // We have a stashed code for the TCL/Roku double-code-word 01970 // scheme. If this one matches the previous one with the 01971 // "command field" bytes XOR'd with 0x80, it's the second 01972 // code in the pair. Otherwise it must be a new code. 01973 if (tclRokuPrvCode == code ^ 0x80800000) 01974 { 01975 // it's the matching code from the pair - report it as one code 01976 reportTCLRokuFormat(receiver, tclRokuPrvCode); 01977 } 01978 else 01979 { 01980 // it's not a match, so it must be a distinct code - report 01981 // the two codes separately 01982 reportTCLRokuFormat(receiver, tclRokuPrvCode); 01983 reportTCLRokuFormat(receiver, code); 01984 } 01985 01986 // we've now consumed the previous code 01987 tclRokuPrvCode = 0; 01988 } 01989 else 01990 { 01991 // There's no stashed code. Don't report the new one yet, since 01992 // it might be the first of a pair. 01993 tclRokuPrvCode = code; 01994 bit = 0; 01995 code = 0; 01996 } 01997 } 01998 01999 // Report a code in TCL/Roku format. This just uses the standard NEC 02000 // reporting. 02001 void reportTCLRokuFormat(IRRecvProIfc *receiver, uint32_t code) 02002 { 02003 // put the bytes in the reporting order for NEC: A0 A1 C0 C1 02004 uint8_t c1 = uint8_t((code >> 24) & 0xff); 02005 uint8_t c0 = uint8_t((code >> 16) & 0xff); 02006 uint8_t a1 = uint8_t((code >> 8) & 0xff); 02007 uint8_t a0 = uint8_t(code & 0xff); 02008 uint64_t codeOut = (uint64_t(a0) << 24) | (uint64_t(a1) << 16) 02009 | (uint64_t(c0) << 8) | uint64_t(c1); 02010 02011 // report it 02012 reportCode(receiver, IRPRO_TCLROKU, codeOut, bool3::null, bool3::null); 02013 } 02014 02015 // determine if we have a TCL/Roku address 02016 bool isTCLRokuCode() 02017 { 02018 // It's a TCL/Roku model if the address field is EA C7 02019 return (code & 0xFFFF) == 0xC7EA; 02020 } 02021 02022 // The previous TCL/Roku code value. All codes in this protocol use 02023 // doubled code words, so we keep track of the first word here. 02024 uint32_t tclRokuPrvCode; 02025 }; 02026 02027 // NEC-32x. This is a minor variation on the standard NEC-32 protocol, 02028 // with a slightly different header signature and a different ditto 02029 // pattern. 02030 class IRPNEC_32x: public IRPNEC 02031 { 02032 public: 02033 virtual int id() const { return IRPRO_NEC32X; } 02034 virtual const char *name() const { return "NEC32x"; } 02035 virtual int necPro(int bits) const { return IRPRO_NEC32X; } 02036 02037 virtual int nbits() const { return 32; } 02038 virtual uint32_t tHeaderMark() const { return 4500; } 02039 virtual uint32_t tHeaderSpace() const { return 4500; } 02040 02041 // Close out the code. NEC-32x has an unusual variation of the 02042 // NEC ditto: it uses the same header as a regular code, but only 02043 // has a 1-bit payload. 02044 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02045 { 02046 if (bit == 1) 02047 reportCode(receiver, IRPRO_NEC32X, code, bool3::null, true); 02048 else 02049 IRPNEC::rxClose(receiver, ditto); 02050 } 02051 02052 }; 02053 02054 // ----------------------------------------------------------------------- 02055 // 02056 // Kaseikyo protocol handler. Like NEC, this is a quasi industry standard 02057 // used by many companies. Unlike NEC, it seems to be used consistently 02058 // by just about everyone. There are only two main variations: a 48-bit 02059 // coding and a 56-bit coding. 02060 // 02061 // For all versions, the first 16 bits in serial order provide an OEM ID. 02062 // We use this to report manufacturer-specific protocol IDs, even though 02063 // the low-level coding is the same for all of them. Differentiating 02064 // by manufacturer is mostly for cosmetic reasons, so that human users 02065 // looking at learned codes or looking for codes to program will see 02066 // names matching their equipment. In some cases it's also useful for 02067 // interpreting the internal data fields within the bit string; some 02068 // OEMs use checksums or other fields that clients might want to 02069 // interpret. 02070 // 02071 class IRPKaseikyo: public IRPSpaceLength<uint64_t> 02072 { 02073 public: 02074 IRPKaseikyo() { } 02075 02076 // name and ID 02077 virtual const char *name() const { return "Kaseikyo"; } 02078 virtual int id() const { return IRPRO_KASEIKYO48; } 02079 02080 // we handle all of the OEM-specific protocols 02081 virtual bool isSenderFor(int id) const 02082 { 02083 switch (id) 02084 { 02085 case IRPRO_KASEIKYO48: 02086 case IRPRO_KASEIKYO56: 02087 case IRPRO_DENONK: 02088 case IRPRO_FUJITSU48: 02089 case IRPRO_FUJITSU56: 02090 case IRPRO_JVC48: 02091 case IRPRO_JVC56: 02092 case IRPRO_MITSUBISHIK: 02093 case IRPRO_PANASONIC48: 02094 case IRPRO_PANASONIC56: 02095 case IRPRO_SHARPK: 02096 case IRPRO_TEACK: 02097 return true; 02098 02099 default: 02100 return false; 02101 } 02102 } 02103 02104 // code boundary parameters 02105 virtual uint32_t tHeaderMark() const { return 3500; } 02106 virtual uint32_t tHeaderSpace() const { return 1750; } 02107 virtual uint32_t minRxGap() const { return 2500; } 02108 virtual uint32_t txGap(IRTXState *state) const { return 173000; } 02109 02110 // space length coding 02111 virtual int nbits() const { return 56; } 02112 virtual int tMark() const { return 420; } 02113 virtual int tZero() const { return 420; } 02114 virtual int tOne() const { return 1300; } 02115 02116 // protocol/OEM mappings 02117 struct OEMMap 02118 { 02119 uint16_t oem; 02120 uint8_t pro; 02121 uint8_t bits; 02122 }; 02123 static const OEMMap oemMap[]; 02124 static const int nOemMap; 02125 02126 // close code reception 02127 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02128 { 02129 // it must be a 48-bit or 56-bit code 02130 if (bit != 48 && bit != 56) 02131 return; 02132 02133 // pull out the OEM code in the low 16 bits 02134 int oem = int(code & 0xFFFF); 02135 02136 // Find the protocol based on the OEM 02137 uint8_t pro = bit == 48 ? IRPRO_KASEIKYO48 : IRPRO_KASEIKYO56; 02138 for (int i = 0 ; i < nOemMap ; ++i) 02139 { 02140 if (oemMap[i].oem == oem && oemMap[i].bits == bit) 02141 { 02142 pro = oemMap[i].pro; 02143 break; 02144 } 02145 } 02146 02147 // report the code 02148 reportCode(receiver, pro, code, bool3::null, bool3::null); 02149 } 02150 02151 virtual void codeToBitstream(IRTXState *state) 02152 { 02153 // presume no OEM and a 48-bit version of the protocol 02154 uint16_t oem = 0; 02155 state->nbits = 48; 02156 02157 // find the protocol variation in the table 02158 for (int i = 0 ; i < nOemMap ; ++i) 02159 { 02160 if (state->protocolId == oemMap[i].pro) 02161 { 02162 state->nbits = oemMap[i].bits; 02163 oem = oemMap[i].oem; 02164 break; 02165 } 02166 } 02167 02168 // if we found a non-zero OEM code, and it doesn't match the 02169 // low-order 16 data bits, replace the OEM code in the data 02170 uint64_t code = state->cmdCode; 02171 if (oem != 0 && int(code & 0xFFFF) != oem) 02172 code = (code & ~0xFFFFLL) | oem; 02173 02174 // store the code (with possibly updated OEM coding) 02175 state->bitstream = code; 02176 } 02177 }; 02178 02179 // ----------------------------------------------------------------------- 02180 // 02181 // Philips RC5 protocol handler. This (along with RC6) is a quasi industry 02182 // standard among European CE companies, so this protocol gives us 02183 // compatibility with many devices from companies besides Philips. 02184 // 02185 // RC5 is a 14-bit Manchester-coded protocol. '1' bits are encoded as 02186 // low->high transitions. 02187 // 02188 // The 14 bits of the command are internally structured as follows: 02189 // 02190 // S F T AAAAA CCCCCC 02191 // 02192 // S = "start bit". Always 1. We omit this from the reported code 02193 // since it's always the same. 02194 // 02195 // F = "field bit", which selects a default (1) or extended (0) set of 02196 // commands. Note the reverse sensing, with '1' being the default. 02197 // This is because this position was a second stop bit in the original 02198 // code, always set to '1'. When Philips repurposed the bit as the 02199 // field code in a later version of the protocol, they used '1' as 02200 // the default for compatibility with older devices. We pass the bit 02201 // through as-is to the universal representation, so be aware that 02202 // you might have to flip it to match some published code tables. 02203 // 02204 // T = "toggle bit". This changes on each successive key press, to allow 02205 // the receiver to distinguish pressing the same key twice from auto- 02206 // repeat due to holding down the key. 02207 // 02208 // A = "address", most significant bit first; specifies which type of 02209 // device the command is for (e.g., "TV", "VCR", etc). The meanings 02210 // of the possible numeric values are arbitrarily assigned by Philips; 02211 // you can Philips; you can find tables online (e.g., at Wikipedia) 02212 // with the published assignments. 02213 // 02214 // C = "command", most significant bit first; the command code. The 02215 // meaning of the command code varies according to the type of device 02216 // in the address field. Published tables with the standard codes can 02217 // be found online. 02218 // 02219 // Note that this protocol doesn't have a "header" per se; it just starts 02220 // in directly with the first bit. As soon as we see a long enough gap, 02221 // we're ready for the start bit. 02222 // 02223 class IRPRC5: public IRPManchester<uint16_t> 02224 { 02225 public: 02226 IRPRC5() { } 02227 02228 // name and ID 02229 virtual const char *name() const { return "Philips RC5"; } 02230 virtual int id() const { return IRPRO_RC5; } 02231 02232 // code parameters 02233 virtual float pwmPeriod(IRTXState *state) const { return 27.7777778e-6; } // 36kHz 02234 virtual uint32_t minRxGap() const { return 3600; } 02235 virtual uint32_t txGap(IRTXState *state) const { return 114000; } 02236 virtual bool lsbFirst() const { return false; } 02237 virtual int nbits() const { return 14; } 02238 02239 // RC5 has no header; the start bit immediately follows the gap 02240 virtual uint32_t tHeaderMark() const { return 0; } 02241 virtual uint32_t tHeaderSpace() const { return 0; } 02242 02243 // Manchester coding parameters 02244 virtual int tHalfBit(int bit) const { return 1778/2; } 02245 02246 // After the gap, the start of the next mark is in the middle 02247 // of the start bit. A '1' start bit always follows the gap, 02248 // and a '1' bit is represented by a space-to-mark transition, 02249 // so the end of the gap is the middle of the start bit. 02250 virtual void rxReset() 02251 { 02252 IRPManchester<uint16_t>::rxReset(); 02253 halfBitPos = 1; 02254 } 02255 02256 virtual void codeToBitstream(IRTXState *state) 02257 { 02258 // add the start bit and toggle bit to the command code 02259 state->nbits = nbits(); 02260 state->bitstream = (state->cmdCode & DataMask) | StartMask; 02261 if (state->toggle) 02262 state->bitstream |= ToggleMask; 02263 } 02264 02265 // report the code 02266 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02267 { 02268 // make sure we have the full code 02269 if (bit == 14) 02270 { 02271 // Pull out the toggle bit to report separately, and zero it 02272 // out in the code word, so that a given key always reports 02273 // the same code value. Also zero out the start bit, since 02274 // it's really just structural. 02275 reportCode( 02276 receiver, id(), 02277 code & ~(StartMask | ToggleMask), 02278 (code & ToggleMask) != 0, bool3::null); 02279 } 02280 } 02281 02282 // masks for the internal fields 02283 static const int CmdMask = 0x3F; 02284 static const int AddrMask = 0x1F << 6; 02285 static const int ToggleMask = 1 << 11; 02286 static const int FieldMask = 1 << 12; 02287 static const int StartMask = 1 << 13; 02288 static const int DataMask = FieldMask | AddrMask | CmdMask; 02289 }; 02290 02291 // ----------------------------------------------------------------------- 02292 // 02293 // RC6 protocol handler. This (along with RC5) is a quasi industry 02294 // standard among European CE companies, so this protocol gives us 02295 // compatibility with many devices from companies besides Philips. 02296 // 02297 // RC6 is a 21-bit Manchester-coded protocol. '1' bits are coded as 02298 // High->Low transitions. The bits are nominally structured into 02299 // fields as follows: 02300 // 02301 // S FFF T AAAAAAAA CCCCCCCC 02302 // 02303 // The fields are: 02304 // 02305 // S = start bit; always 1. We omit this from the reported value since 02306 // it's always the same. 02307 // 02308 // F = "field". These bits are used to select different command sets, 02309 // so they're effectively three additional bits (added as the three 02310 // most significant bits) for the command code. 02311 // 02312 // A = "address", specifying the type of device the command is for. 02313 // This has the same meanings as the address field in RC5. 02314 // 02315 // C = "command". The command code, specific to the device type 02316 // in the address field. 02317 // 02318 // As with all protocols, we don't reproduce the internal field structure 02319 // in the decoded command value; we simply pack all of the bits into a 02320 // 18-bit integer, in the order shown above, field bits at the high end. 02321 // (We omit the start bit, since it's a fixed element that's more properly 02322 // part of the protocol than part of the code.) 02323 // 02324 // Note that this protocol contains an odd exception to normal Manchester 02325 // coding for the "toggle" bit. This bit has a period 2X that of the other 02326 // bits. 02327 // 02328 class IRPRC6: public IRPManchester<uint32_t> 02329 { 02330 public: 02331 IRPRC6() { } 02332 02333 // name and ID 02334 virtual const char *name() const { return "Philips RC6"; } 02335 virtual int id() const { return IRPRO_RC6; } 02336 02337 // code parameters 02338 virtual float pwmPeriod(IRTXState *state) const { return 27.7777778e-6; } // 36kHz 02339 virtual uint32_t tHeaderMark() const { return 2695; } 02340 virtual uint32_t tHeaderSpace() const { return 895; } 02341 virtual uint32_t minRxGap() const { return 2650; } 02342 virtual uint32_t txGap(IRTXState *state) const { return 107000; } 02343 virtual bool lsbFirst() const { return false; } 02344 virtual int nbits() const { return 21; } 02345 02346 // Manchester coding parameters 02347 virtual int spaceToMarkBit() const { return 0; } 02348 02349 // RC6 has the weird exception to the bit timing in the Toggle bit, 02350 // which is twice as long as the other bits. The toggle bit is the 02351 // 5th bit (bit==4). 02352 virtual int tHalfBit(int bit) const { return bit == 4 ? 895 : 895/2; } 02353 02354 // create the bit stream for the command code 02355 virtual void codeToBitstream(IRTXState *state) 02356 { 02357 // add the start bit and toggle bit to the command code 02358 state->nbits = nbits(); 02359 state->bitstream = (state->cmdCode & DataMask) | StartMask; 02360 if (state->toggle) 02361 state->bitstream |= ToggleMask; 02362 } 02363 02364 // report the code 02365 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02366 { 02367 // make sure we have the full code 02368 if (bit == nbits()) 02369 { 02370 // Pull out the toggle bit to report separately, and zero it 02371 // out in the code word, so that a given key always reports 02372 // the same code value. Also clear the start bit, since it's 02373 // just structural. 02374 reportCode( 02375 receiver, id(), 02376 code & ~(ToggleMask | StartMask), 02377 (code & ToggleMask) != 0, bool3::null); 02378 } 02379 } 02380 02381 // field masks 02382 static const int CmdMask = 0xFF; 02383 static const int AddrMask = 0xFF << 8; 02384 static const int ToggleMask = 1 << 16; 02385 static const int FieldMask = 0x07 << 17; 02386 static const int StartMask = 1 << 20; 02387 static const int DataMask = FieldMask | AddrMask | CmdMask; 02388 }; 02389 02390 // ----------------------------------------------------------------------- 02391 // 02392 // Ortek MCE remote. This device uses Manchester coding, with either 16 02393 // or 17 bits, depending on which keys are pressed. The low-order 5 bits 02394 // of either code are the device code. The interpretation of the rest of 02395 // the bits depends on the device code. Bits are sent least-significant 02396 // first (little-endian) for interpretation as integer values. 02397 // 02398 // Device code = 0x14 = Mouse. This is a 16-bit code, with the fields 02399 // as follows (low bit first): 02400 // 02401 // DDDDD L R MMMMM CCCC 02402 // 02403 // D = device code (common field for all codes) 02404 // L = left-click (1=pressed, 0=not pressed) 02405 // R = right-click (1=pressed, 0=not pressed) 02406 // M = mouse movement direction. This type of device is a mouse stick 02407 // rather than a mouse, so it gives only the direction of travel rather 02408 // than X/Y motion in pixels. There are 15 increments of motion numbered 02409 // 0 to 15, starting with 0 at North (straight up), going clockwise: 02410 // 0 = N 02411 // 1 = NNE 02412 // 2 = NE 02413 // 3 = ENE 02414 // 4 = E 02415 // 5 = ESE 02416 // 6 = SE 02417 // 7 = SSE 02418 // 8 = S 02419 // 9 = SSW 02420 // 10 = SW 02421 // 11 = WSW 02422 // 12 = W 02423 // 13 = WNW 02424 // 14 = NW 02425 // 15 = NNW 02426 // The MMMMM field contains 0x10 | the value above when a mouse motion 02427 // direction is pressed, so the codes will be 0x10 (N), 0x11 (NNE), ..., 02428 // 0x1F (NNW). These are shifted left by two in the reported function 02429 // code, so you'll actually see 0x40 ... 0x7C. 02430 // C = checksum 02431 // 02432 // There's no equivalent of the "position" code (see device 0x15 below) for 02433 // the mouse commands, so there's no coding for auto-repeat per se. The 02434 // remote does let the receiver when the last key is released, though, by 02435 // sending one code word with the L, R, and M bits all set to zero; this 02436 // apparently signifies "key up". One of these is always sent after the 02437 // last key is released, but it's not sent between auto-repeated codes, 02438 // so if you hold a key down you'll see a sequence of repeating codes 02439 // for that key (or key combination), followed by one "key up" code when 02440 // you release the last key. 02441 // 02442 // Receivers who interpret these codes will probably want to separate the 02443 // L, R, and M bits and treat them separately, rather than treating any given 02444 // combination as a discrete command. The reason is that the L and R bits 02445 // can combine with the mouse movement field when a left-click or right-click 02446 // button is held down while the movement keys are pressed. This can be used 02447 // to perform click-and-drag operations on a GUI. 02448 // 02449 // Device code = 0x15 = MCE buttons. This is a 17-bit code, with the 02450 // fields as follows (low bit first): 02451 // 02452 // DDDDD PP FFFFFF CCCC 02453 // 02454 // D = device code (common field for all codes) 02455 // P = "position code", for sensing repeats: 00=first, 01=middle, 10=last 02456 // F = function (key code) 02457 // C = checksum 02458 // 02459 // The checksum is the 3 + the total number of '1' bits in the other fields 02460 // combined. 02461 // 02462 // We report these codes in our own 16-bit format, with D in the high byte, 02463 // and the function code in the low byte. For the mouse commands (device 0x14), 02464 // the low byte is (high bit to low bit) 0MMMMMRL. For the MCE buttons, the 02465 // low byte is the function code (F). We drop the position code for uniformity 02466 // with other protocols and instead report a synthetic toggle bit, which we 02467 // flip whenever we see a new transmission as signified by P=0. We don't do 02468 // anything with the toggle bit on mouse commands. 02469 // 02470 class IRPOrtekMCE: public IRPManchester<uint32_t> 02471 { 02472 public: 02473 IRPOrtekMCE() 02474 { 02475 toggle = 0; 02476 } 02477 02478 // name and ID 02479 virtual const char *name() const { return "OrtekMCE"; } 02480 virtual int id() const { return IRPRO_ORTEKMCE; } 02481 02482 // code parameters 02483 virtual float pwmPeriod(IRTXState *state) const { return 25.906736e-6; } // 38.6kHz 02484 virtual uint32_t tHeaderMark() const { return 4*480; } 02485 virtual uint32_t tHeaderSpace() const { return 1*480; } 02486 virtual bool headerSpaceToData() const { return true; } 02487 virtual uint32_t minRxGap() const { return 32000; } 02488 virtual uint32_t txGap(IRTXState *state) const { return 40000; } 02489 02490 // We always require a final rep with position code 2, so 02491 // ask for a minimum of 3 reps (0, 1, 2). 02492 virtual int txMinReps(IRTXState *) const { return 3; } 02493 02494 // Manchester coding parameters 02495 virtual int nbits() const { return 17; } 02496 virtual int spaceToMarkBit() const { return 1; } 02497 virtual int tHalfBit(int bit) const { return 480; } 02498 02499 // encode the bitstream for a given code 02500 virtual void codeToBitstream(IRTXState *state) 02501 { 02502 // Set the repeat count. If we're on any repeat > 0 and 02503 // the key is still pressed, reset the repeat counter to 1. 02504 // All repeats are "1" as long as the key is down. As soon 02505 // as the key is up, advance directly to "2", even if there 02506 // was never a "1". Our txMinRep() count is 2, so this will 02507 // ensure that we always transmit that last position 2 code, 02508 // as required by the protocol. 02509 if (state->rep > 0) 02510 state->rep = state->pressed ? 1 : 2; 02511 02512 // Function field and checksum bit position - we'll fill 02513 // these in momentarily according to the device type. 02514 uint32_t f; 02515 int c_shift; 02516 02517 // check the product code to determine the encoding 02518 uint32_t cmdcode = uint32_t(state->cmdCode); 02519 int dev = (int(cmdcode) >> 8) & 0xff; 02520 if (dev == 0x14) 02521 { 02522 // Mouse function: DDDDD L R MMMMM CCCC 02523 // There's no position field in the mouse messages, but 02524 // we always have to send a final message with all bits 02525 // zeroed. Do this when our rep count is 2, indicating 02526 // that this is the final close-out rep. 02527 if (state->rep == 2) 02528 f = 0; 02529 else 02530 f = cmdcode & 0xff; 02531 02532 // the checksum starts at the 12th bit 02533 c_shift = 12; 02534 } 02535 else if (dev == 0x15) 02536 { 02537 // MCE button: DDDDD PP FFFFFF CCCC 02538 02539 // Set the position field to the rep counter 02540 int p = state->rep; 02541 02542 // fill in the function fields: PP FFFFFF 02543 f = (p) | ((cmdcode & 0x3F) << 2); 02544 02545 // the checksum starts at the 13th bit 02546 c_shift = 13; 02547 } 02548 else 02549 { 02550 // unknown device code - just transmit the low byte as given 02551 f = cmdcode & 0xff; 02552 c_shift = 13; 02553 } 02554 02555 // construct the bit vector with the device code in the low 5 02556 // bits and code in the next 7 or 8 bits 02557 uint32_t bitvec = dev | (f << 5); 02558 02559 // figure the checksum: it's the number of '1' bits in 02560 // the rest of the fields, plus 3 02561 uint32_t checksum = 3; 02562 for (uint32_t v = bitvec ; v != 0 ; checksum += (v & 1), v >>= 1) ; 02563 02564 // construct the bit stream 02565 state->bitstream = bitvec | (checksum << c_shift); 02566 state->nbits = c_shift + 4; 02567 } 02568 02569 // report the code value 02570 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02571 { 02572 // common field masks 02573 const uint32_t D_mask = 0x001F; // lowest 5 bits = device code 02574 02575 // pull out the common fields 02576 uint32_t d = code & D_mask; 02577 02578 // assume no post-toggle 02579 bool postToggle = false; 02580 02581 // check for the different code types, and pull out the fields 02582 uint32_t f, c, checkedBits; 02583 if (bit == 16 && d == 0x14) 02584 { 02585 // field masks for the mouse codes 02586 const int F_shift = 5; 02587 const uint32_t F_mask = 0x7f << F_shift; // next 7 bits 02588 const int C_shift = 12; 02589 const uint32_t C_mask = 0x3F << C_shift; // top 4 bits 02590 02591 // separate the fields 02592 f = (code & F_mask) >> F_shift; 02593 c = (code & C_mask) >> C_shift; 02594 checkedBits = code & ~C_mask; 02595 02596 // if the F bits are all zero, take it as a "key up" sequence, 02597 // so flip the toggle bit next time 02598 if (f == 0) 02599 postToggle = true; 02600 } 02601 else if (bit == 17 && d == 0x15) 02602 { 02603 // 17 bits with device code 0x15 = MCE keyboard commands 02604 02605 // field masks for the keyboard codes 02606 const int P_shift = 5; 02607 const uint32_t P_mask = 0x0003 << P_shift; // next 2 bits 02608 const int F_shift = 7; 02609 const uint32_t F_mask = 0x3F << F_shift; // next 6 bits 02610 const int C_shift = 13; 02611 const uint32_t C_mask = 0x0F << C_shift; // top 4 bits 02612 02613 // separate the fields 02614 uint32_t p = (code & P_mask) >> P_shift; 02615 f = (code & F_mask) >> F_shift; 02616 c = (code & C_mask) >> C_shift; 02617 checkedBits = code & ~C_mask; 02618 02619 /// validate the position code - 0,1,2 are valid, 3 is invalid 02620 if (p == (0x03 << P_shift)) 02621 return; 02622 02623 // flip the toggle bit if this is the first frame in a group 02624 // as signified by P=0 02625 if (p == 0) 02626 toggle ^= 1; 02627 } 02628 else 02629 { 02630 // invalid bit length or device code - reject the code 02631 return; 02632 } 02633 02634 // count the '1' bits in the other fields to get the checksum value 02635 int ones = 0; 02636 for ( ; checkedBits != 0 ; checkedBits >>= 1) 02637 ones += (checkedBits & 1); 02638 02639 // check the checksum 02640 if (c != ones + 3) 02641 return; 02642 02643 // rearrange the code into our canonical format and report it 02644 // along with the synthetic toggle 02645 reportCode(receiver, id(), (d << 8) | f, toggle, bool3::null); 02646 02647 // flip the toggle for next time, if desired 02648 if (postToggle) 02649 toggle ^= 1; 02650 } 02651 02652 // synthetic toggle bit 02653 uint8_t toggle : 1; 02654 }; 02655 02656 // ----------------------------------------------------------------------- 02657 // 02658 // Sony protocol handler. Sony uses mark-length encoding, with 02659 // 8, 12, 15, and 20 bit code words. We use a common receiver 02660 // base class that determines how many bits are in the code from 02661 // the input itself, plus separate base classes for each bit size. 02662 // The common receiver class reports the appropriate sub-protocol 02663 // ID for the bit size, so that the appropriate sender class is 02664 // used if we want to transmit the captured code. 02665 // 02666 class IRPSony: public IRPMarkLength<uint32_t> 02667 { 02668 public: 02669 IRPSony() { } 02670 02671 // Name and ID. We handle all of the Sony bit size variations, 02672 // so we use IRPRO_NONE as our nominal ID, but set the actual code 02673 // on a successful receive based on the actual bit size. We 02674 // transmit any of the Sony codes. 02675 virtual const char *name() const { return "Sony"; } 02676 virtual int id() const { return IRPRO_NONE; } 02677 virtual bool isSenderFor(int protocolId) const 02678 { 02679 return protocolId == IRPRO_SONY8 02680 || protocolId == IRPRO_SONY12 02681 || protocolId == IRPRO_SONY15 02682 || protocolId == IRPRO_SONY20; 02683 } 02684 02685 // code boundary parameters 02686 virtual uint32_t tHeaderMark() const { return 2400; } 02687 virtual uint32_t tHeaderSpace() const { return 600; } 02688 virtual uint32_t minRxGap() const { return 5000; } 02689 virtual uint32_t txGap(IRTXState *state) const { return 45000; } 02690 02691 // mark-length coding parameters 02692 virtual int nbits() const { return 20; } // maximum - can also be 8, 12, or 15 02693 virtual int tSpace() const { return 600; } 02694 virtual int tZero() const { return 600; } 02695 virtual int tOne() const { return 1200; } 02696 02697 // Sony requires at least 3 sends per key press 02698 virtual int txMinReps(IRTXState *state) const { return 3; } 02699 02700 // set up the bitstream for a code value 02701 virtual void codeToBitstream(IRTXState *state) 02702 { 02703 // store the code, and set the bit counter according to 02704 // the Sony protocol subtype 02705 state->bitstream = state->cmdCode; 02706 switch (state->protocolId) 02707 { 02708 case IRPRO_SONY8: 02709 state->nbits = 8; 02710 break; 02711 02712 case IRPRO_SONY12: 02713 state->nbits = 12; 02714 break; 02715 02716 case IRPRO_SONY15: 02717 state->nbits = 15; 02718 break; 02719 02720 case IRPRO_SONY20: 02721 default: 02722 state->nbits = 20; 02723 break; 02724 } 02725 } 02726 02727 // report a code value 02728 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02729 { 02730 // we have a valid code if we have 8, 12, 15, or 20 bits 02731 switch (bit) 02732 { 02733 case 8: 02734 reportCode(receiver, IRPRO_SONY8, code, bool3::null, bool3::null); 02735 break; 02736 02737 case 12: 02738 reportCode(receiver, IRPRO_SONY12, code, bool3::null, bool3::null); 02739 break; 02740 02741 case 15: 02742 reportCode(receiver, IRPRO_SONY15, code, bool3::null, bool3::null); 02743 break; 02744 02745 case 20: 02746 reportCode(receiver, IRPRO_SONY20, code, bool3::null, bool3::null); 02747 break; 02748 } 02749 } 02750 }; 02751 02752 // ----------------------------------------------------------------------- 02753 // 02754 // Denon protocol handler. Denon uses a 15-bit space-length coding, but 02755 // has two unusual features. First, it has no header; it just starts 02756 // right off with the data bits. Second, every code is transmitted a 02757 // second time, starting 65ms after the start of the first iteration, 02758 // with the "F" and "S" fields inverted: 02759 // 02760 // DDDDD FFFFFFFF SS 02761 // 02762 // D = device code 02763 // F = function code (key) 02764 // S = sequence; 0 for the first half of the pair, 1 for the second half 02765 // 02766 // The first half-code is transmitted with the actual function code and 02767 // SS=00; the second half uses ~F (1's complement of the function code) 02768 // and SS=11. 02769 // 02770 // Many learning remotes get this wrong, only learning one or the other 02771 // half. That's understandable, since learning remotes often only collect 02772 // the raw bit codes and thus wouldn't be able to detect the multi-code 02773 // structure. It's a little less forgiveable that some pre-programmed 02774 // universal remotes, such as the Logitech Harmony series, also miss the 02775 // half-code nuance. To be robust against errant remotes, we'll accept 02776 // and report lone half-codes, and we'll invert the F bits if the S bits 02777 // indicate that a second half was learned, to ensure that the client sees 02778 // the correct version of the codes. We'll also internally track the 02779 // pairs so that, when we *do* see a properly formed inverted pair, we'll 02780 // only report one code out of the pair to the client. 02781 // 02782 class IRPDenon: public IRPSpaceLength<uint16_t> 02783 { 02784 public: 02785 IRPDenon() 02786 { 02787 prvCode = 0; 02788 } 02789 02790 // name and ID 02791 virtual const char *name() const { return "Denon"; } 02792 virtual int id() const { return IRPRO_DENON; } 02793 02794 // Code parameters. The Denon protocol has no header; it just 02795 // jumps right in with the first bit. 02796 virtual uint32_t tHeaderMark() const { return 0; } 02797 virtual uint32_t tHeaderSpace() const { return 0; } 02798 virtual uint32_t minRxGap() const { return 30000; } 02799 02800 // use a short gap between paired complemented codes, and a longer 02801 // gap between repeats 02802 virtual uint32_t txGap(IRTXState *state) const { return 165000; } 02803 virtual uint32_t txPostGap(IRTXState *state) const 02804 { 02805 if (state->rep == 0) 02806 { 02807 // Even rep - this is the gap between the two halves of a 02808 // complemented pair. The next code starts 65ms from the 02809 // *start* of the last code, so the gap is 65ms minus the 02810 // transmission time for the last code. 02811 return 65000 - state->txTime.read_us(); 02812 } 02813 else 02814 { 02815 // odd rep - use the normal long gap 02816 return 165000; 02817 } 02818 } 02819 02820 // on idle, clear the previous code 02821 virtual void rxIdle(IRRecvProIfc *receiver) 02822 { 02823 prvCode = 0; 02824 } 02825 02826 // space length coding 02827 virtual int nbits() const { return 15; } 02828 virtual int tMark() const { return 264; } 02829 virtual int tZero() const { return 792; } 02830 virtual int tOne() const { return 1848; } 02831 02832 // handle a space 02833 virtual bool rxSpace(IRRecvProIfc *receiver, uint32_t t) 02834 { 02835 // If this space is longer than the standard space between 02836 // adjacent codes, clear out any previous first-half code 02837 // we've stored. Complementary pairs have to be transmitted 02838 // with the standard inter-code gap between them. Anything 02839 // longer must be separate key presses. 02840 if (t > 65000) 02841 prvCode = 0; 02842 02843 // do the normal work 02844 return IRPSpaceLength<uint16_t>::rxSpace(receiver, t); 02845 } 02846 02847 // always send twice, once for the base version, once for the 02848 // inverted follow-up version 02849 virtual int txMinReps(IRTXState *state) const { return 2; } 02850 02851 // encode the bitstream 02852 virtual void codeToBitstream(IRTXState *state) 02853 { 02854 // If we're on an even repetition, just send the code 02855 // exactly as given. If we're on an odd repetition, 02856 // send the inverted F and S fields. 02857 state->nbits = 15; 02858 if (state->rep == 0 || state->rep == 2) 02859 { 02860 // even rep - send the base version 02861 state->bitstream = state->cmdCode & (D_mask | F_mask); 02862 02863 // If we're on rep 2, switch back to rep 0. This will 02864 // combine with our minimum rep count of 2 to ensure that 02865 // we always transmit an even number of copies. Note that 02866 // this loop terminates: when the key is up after we finish 02867 // rep 1, the rep counter will advance to 2 and we'll stop. 02868 // We'll only reset the rep counter here if we actually 02869 // enter rep 2, which means the key is still down at that 02870 // point, which means that we'll have to go at least another 02871 // two iterations. 02872 state->rep = 0; 02873 } 02874 else 02875 { 02876 // odd rep - invert the F field, and use all 1's in the S field 02877 uint32_t d = state->cmdCode & D_mask; 02878 uint32_t f = (~state->cmdCode) & F_mask; 02879 state->bitstream = d | f | S_mask; 02880 } 02881 } 02882 02883 // report the code 02884 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 02885 { 02886 // If this code matches the inverted prior code, we have 02887 // a matching pair. Otherwise, hang onto this code until 02888 // next time. 02889 if (bit == 15) 02890 { 02891 // get the current and previous code's subfields 02892 uint16_t curD = code & D_mask; 02893 uint16_t curF = code & F_mask; 02894 uint16_t curS = code & S_mask; 02895 uint16_t prvD = prvCode & D_mask; 02896 uint16_t prvF = prvCode & F_mask; 02897 uint16_t prvS = prvCode & S_mask; 02898 02899 // check to see if the current FS fields match the inverted 02900 // prior FS fields, to make up a complementary pair as required 02901 // by the protocol 02902 if (curD == prvD && curF == (~prvF & F_mask) && curS == (~prvS & S_mask)) 02903 { 02904 // The current code is the second half of the first code. 02905 // Don't report the current code, since it's just an 02906 // error-checking copy of the previous code, which we already 02907 // reported. We've now seen both halves, so clear the 02908 // previous code. 02909 prvCode = 0; 02910 } 02911 else 02912 { 02913 // This isn't the second half of an complementary pair, so 02914 // report it as a separate code. If the 'S' bits are 1's 02915 // in this code, it's the second half of the pair, so invert 02916 // the F and S fields to get the original code. It might 02917 // have been sent by a learning remote or universal remote 02918 // that only captured the second half field. 02919 if (curS == S_mask) 02920 { 02921 // The S bits are 1's, so it's a second-half code. We 02922 // don't have a matching first-half code, so either the 02923 // first half was lost, or it was never transmitted. In 02924 // either case, reconstruct the original first-half code 02925 // by inverting the F bits and clearing the S bits. 02926 reportCode(receiver, id(), curD | (~curF & F_mask), 02927 bool3::null, bool3::null); 02928 02929 // Forget any previous code. This is a second-half 02930 // code, so if we do get a first-half code after this, 02931 // it's a brand new key press or repetition. 02932 prvCode = 0; 02933 } 02934 else if (curS == 0) 02935 { 02936 // The S bits are 0, so it's a first-half code. Report 02937 // the code, and save it for next time, so that we can 02938 // check the next code to see if it's the second half. 02939 reportCode(receiver, id(), code, bool3::null, bool3::null); 02940 prvCode = code; 02941 } 02942 else 02943 { 02944 // The S bits are invalid, so this isn't a valid code. 02945 // Clear out any previous code and reject this one. 02946 prvCode = 0; 02947 } 02948 } 02949 } 02950 else 02951 { 02952 // we seem to have an invalid code; clear out any 02953 // previous code so we can start from scratch 02954 prvCode = 0; 02955 } 02956 } 02957 02958 // stored first code - we hang onto this until we see the 02959 // inverted second copy, so that we can verify a matching pair 02960 uint16_t prvCode; 02961 02962 // masks for the subfields 02963 static const int F_shift = 5; 02964 static const int S_shift = 13; 02965 static const uint16_t D_mask = 0x1F; 02966 static const uint16_t F_mask = 0x00FF << F_shift; 02967 static const uint16_t S_mask = 0x0003 << S_shift; 02968 }; 02969 02970 // ----------------------------------------------------------------------- 02971 // 02972 // Samsung 20-bit protocol handler. This is a simple space-length 02973 // encoding. 02974 // 02975 class IRPSamsung20: public IRPSpaceLength<uint32_t> 02976 { 02977 public: 02978 IRPSamsung20() { } 02979 02980 // name and ID 02981 virtual const char *name() const { return "Samsung20"; } 02982 virtual int id() const { return IRPRO_SAMSUNG20; } 02983 02984 // code parameters 02985 virtual float pwmPeriod(IRTXState *state) const { return 26.0416667e-6; } // 38.4 kHz 02986 virtual uint32_t tHeaderMark() const { return 8*564; } 02987 virtual uint32_t tHeaderSpace() const { return 8*564; } 02988 virtual uint32_t minRxGap() const { return 2*564; } 02989 virtual uint32_t txGap(IRTXState *state) const { return 118000; } 02990 02991 // space length coding 02992 virtual int nbits() const { return 20; } 02993 virtual int tMark() const { return 1*564; } 02994 virtual int tZero() const { return 1*564; } 02995 virtual int tOne() const { return 3*564; } 02996 }; 02997 02998 // Samsung 36-bit protocol. This is similar to the NEC protocol, 02999 // with different header timing. 03000 class IRPSamsung36: public IRPSpaceLength<uint32_t> 03001 { 03002 public: 03003 IRPSamsung36() { } 03004 03005 // name and ID 03006 virtual const char *name() const { return "Samsung36"; } 03007 virtual int id() const { return IRPRO_SAMSUNG36; } 03008 03009 // code parameters 03010 virtual uint32_t tHeaderMark() const { return 9*500; } 03011 virtual uint32_t tHeaderSpace() const { return 9*500; } 03012 virtual uint32_t minRxGap() const { return 40000; } 03013 virtual uint32_t txGap(IRTXState *state) const { return 118000; } 03014 03015 // space length coding 03016 virtual int nbits() const { return 36; } 03017 virtual int tMark() const { return 1*500; } 03018 virtual int tZero() const { return 1*500; } 03019 virtual int tOne() const { return 3*500; } 03020 }; 03021 03022 // ----------------------------------------------------------------------- 03023 // 03024 // Lutron lights, fans, and home automation. Lutron uses a simple async 03025 // bit coding: a 2280us space represents '0', a 2280us mark represents '1'. 03026 // Each code consists of 36 bits. The start of a code word is indicated 03027 // by a space of at least 4*2280us followed by a mark of at least 8*2280us. 03028 // The mark amounts to 8 consecutive '1' bits, which Lutron includes as 03029 // digits in the published codes, so we'll include them in our representation 03030 // as well (as opposed to treating them as a separate header). 03031 03032 // These raw 36-bit strings use an internal error correction coding. This 03033 // isn't mentioned in Lutron's technical documentation (they just list the 03034 // raw async bit strings), but there's a description on the Web (see, e.g., 03035 // hifi-remote.com; the original source is unclear). According to that, 03036 // you start with a pair of 8-bit values (device code + function). Append 03037 // two parity bits for even parity in each byte. This gives us 18 bits. 03038 // Divide the 18 bits into 6 groups of 3 bits. For each 3-bit group, take 03039 // the binary value (0..7), recode the bits as a reflected Gray code for 03040 // the same number (e.g., '111' binary = 7 = '100' Gray coded), then add 03041 // an even parity bit. We now have 24 bits. Concatenate these together 03042 // and sandwich them between the 8 x '1' start bits and the 4 x '0' stop 03043 // bits, and we have the 36-bit async bit stream. 03044 // 03045 // The error correction code lets us apply at least one validation check: 03046 // we can verify that each 4-bit group in the raw data has odd parity. If 03047 // it doesn't, we'll reject the code. Ideally, we could also reverse the 03048 // whole coding scheme above and check the two even parity bits for the 03049 // recovered bytes. Unfortunately, I haven't figured out how to do this; 03050 // taking the Web description of the coding at face value doesn't yield 03051 // correct parity bits for all of the codes published by Lutron. Either 03052 // the description is wrong, or I'm just misinterpreting it (which is 03053 // likely given that it's pretty sketchy). 03054 // 03055 class IRPLutron: public IRPAsync<uint32_t> 03056 { 03057 public: 03058 IRPLutron() { } 03059 03060 // name and ID 03061 virtual const char *name() const { return "Lutron"; } 03062 virtual int id() const { return IRPRO_LUTRON; } 03063 03064 // carrier is 40kHz 03065 virtual float pwmPeriod(IRTXState *state) const { return 25.0e-6f; } // 40kHz 03066 03067 // All codes end with 4 '0' bits, which is as much of a gap as these 03068 // remotes provide. 03069 virtual uint32_t minRxGap() const { return 2280*4 - 700; } 03070 virtual uint32_t txGap(IRTXState *state) const { return 2280*16; } 03071 03072 // Lutron doesn't have a formal header, but there's a long mark at 03073 // the beginning of every code. 03074 virtual uint32_t tHeaderMark() const { return 2280*8; } 03075 virtual uint32_t tHeaderSpace() const { return 0; } 03076 03077 // Bit code parameters. The Lutron codes are 36-bits, each bit 03078 // is 2280us long, and bits are stored MSB first. 03079 virtual bool lsbFirst() const { return false; } 03080 virtual int nbits() const { return 24; } 03081 virtual int tBit() const { return 2280; } 03082 03083 // Validate a received code value, using the decoding process 03084 // described above. The code value given here is the low-order 03085 // 32 bits of the 36-bit code, with the initial FF stripped. 03086 bool rxValidate(uint32_t c) 03087 { 03088 // the low 4 bits must be zeroes 03089 if ((c & 0x0000000F) != 0) 03090 return false; 03091 03092 // drop the 4 '0' bits at the end 03093 c >>= 4; 03094 03095 // parity table for 4-bit values 0x00..0x0F - 0=even, 1=odd 03096 static const int parity[] = { 03097 0, 1, 1, 0, // 0, 1, 2, 3 03098 1, 0, 0, 1, // 4, 5, 6, 7 03099 1, 0, 0, 1, // 8, 9, A, B 03100 0, 1, 1, 0 // C, D, E, F 03101 }; 03102 03103 // add up the number of groups with odd parity 03104 int odds = 0; 03105 for (int i = 0 ; i < 6 ; ++i, c >>= 4) 03106 odds += parity[c & 0x0F]; 03107 03108 // we need all 6 groups to have odd parity 03109 return odds == 6; 03110 } 03111 03112 // Return the code. Lutron's code tables have all codes starting 03113 // with FF (8 consecutive '1' bits), but we treat this as a header, 03114 // since it can never occur within a code, so it doesn't show up 03115 // in the bit string. We also count the tailing four '0' bits, 03116 // which Lutron also encodes in each string, as a gap. So we only 03117 // actually capture 24 data bits. To make our codes match the 03118 // Lutron tables, add the structural bits back in for reporting. 03119 virtual void rxClose(IRRecvProIfc *receiver, bool ditto) 03120 { 03121 if (bit == 24) 03122 { 03123 uint64_t report = 0xFF0000000LL | (code << 4); 03124 if (rxValidate(report)) 03125 reportCode(receiver, id(), report, bool3::null, bool3::null); 03126 } 03127 } 03128 03129 // For transmission, convert back to the 24 data bits, stripping out 03130 // the structural leading 8 '1' bits and trailing 4 '0' bits. 03131 virtual void codeToBitstream(IRTXState *state) 03132 { 03133 state->bitstream = (state->cmdCode >> 4) & 0x00FFFFFF; 03134 state->nbits = 24; 03135 } 03136 }; 03137 03138 // ------------------------------------------------------------------------- 03139 // 03140 // Protocol singletons. We combine these into a container structure 03141 // so that we can allocate the whole set of protocol handlers in one 03142 // 'new'. 03143 // 03144 struct IRProtocols 03145 { 03146 #define IR_PROTOCOL_RXTX(cls) cls s_##cls; 03147 #include "IRProtocolList.h" 03148 }; 03149 03150 #endif
Generated on Wed Jul 13 2022 03:30:10 by 1.7.2