| 1 | /* IRLib_P04_RC6.h |
|---|
| 2 | * Part of IRLib Library for Arduino receiving, decoding, and sending |
|---|
| 3 | * infrared signals. See COPYRIGHT.txt and LICENSE.txt for more information. |
|---|
| 4 | */ |
|---|
| 5 | /* |
|---|
| 6 | * The RC6 protocol was invented by Phillips but is used by wide variety of manufacturers. |
|---|
| 7 | * Like the Phillips RC5 protocol it uses phased coding however the phase is backwards |
|---|
| 8 | * from RC5. With RC6 a space/mark pair indicates "0" and a mark/space indicates a "1". |
|---|
| 9 | * in the data. The protocol uses 36 kHz modulation however 38 kHz receivers can typically |
|---|
| 10 | * receive the codes okay. |
|---|
| 11 | * The protocol consists of a header followed by a "1" bit which is always on and we do not |
|---|
| 12 | * encode. This is followed by a 3 bit OEM code that is usually 0 or 6. This is followed by |
|---|
| 13 | * special trailer bit whose time is twice that of normal. It all but the 32-bit version, |
|---|
| 14 | * the trailer bit also serves as a toggle bit. The toggle bit changes if the button was |
|---|
| 15 | * pressed and released however remains the same if the button was held. You must toggle |
|---|
| 16 | * this bit yourself when sending data and account for it when interpreting decoded values. |
|---|
| 17 | * Next are the actual data bits. Varieties include 16, 20, 24, and 32 bit versions. |
|---|
| 18 | * Because we encode the 3 OEM bits and the toggle bit, our actual bit lengths are 20, 24, |
|---|
| 19 | * and 28 for the first three varieties. The 32-bit variety is different because it uses |
|---|
| 20 | * OEM bits of 011 followed by a 0 in what is traditionally the toggle bit. |
|---|
| 21 | * Because the 32-bit version is invariant in these first 4 bits, we do not encode them |
|---|
| 22 | * and presume they are always "0110". The 32-bit version uses the highest order of the |
|---|
| 23 | * data bits as a toggle bit. At this time the 16-bit version always uses OEM = 0 and |
|---|
| 24 | * other known varieties always use OEM = 6 but we will only make that presumption for the |
|---|
| 25 | * 32-bit version. Encoding the OEM bits allows for other OEM values to be used without |
|---|
| 26 | * modifying the library. If we ever encounter a 32-bit version with an OEM other than 6 |
|---|
| 27 | * it will require a special modified encoder and decoder. |
|---|
| 28 | * Here is a description of known varieties: |
|---|
| 29 | * RC6-0-16: Original version by Phillips. 16 bits, we encode 20, toggle is 0x00010000 |
|---|
| 30 | * RC6-6-20: Used by some Sky and Sky+ remotes. 20 bits, we encode 24, toggle is 0x00100000 |
|---|
| 31 | * RC6-6-24: Also known as "Replay" protocol. 24 bits, we encode 28, toggle is 0x01000000 |
|---|
| 32 | * RC6-6-32: Also known as "MCE" protocol. 32 bits, we encode 32, toggle is 0x00008000 |
|---|
| 33 | */ |
|---|
| 34 | #ifndef IRLIB_PROTOCOL_04_H |
|---|
| 35 | #define IRLIB_PROTOCOL_04_H |
|---|
| 36 | #define IR_SEND_PROTOCOL_04 case 04: IRsendRC6::send(data,data2); break; |
|---|
| 37 | #define IR_DECODE_PROTOCOL_04 if(IRdecodeRC6::decode()) return true; |
|---|
| 38 | #ifdef IRLIB_HAVE_COMBO |
|---|
| 39 | #define PV_IR_DECODE_PROTOCOL_04 ,public virtual IRdecodeRC6 |
|---|
| 40 | #define PV_IR_SEND_PROTOCOL_04 ,public virtual IRsendRC6 |
|---|
| 41 | #else |
|---|
| 42 | #define PV_IR_DECODE_PROTOCOL_04 public virtual IRdecodeRC6 |
|---|
| 43 | #define PV_IR_SEND_PROTOCOL_04 public virtual IRsendRC6 |
|---|
| 44 | #endif |
|---|
| 45 | #define RC6_HDR_MARK 2666 |
|---|
| 46 | #define RC6_HDR_SPACE 889 |
|---|
| 47 | #define RC6_T1 444 |
|---|
| 48 | |
|---|
| 49 | #ifdef IRLIBSENDBASE_H |
|---|
| 50 | class IRsendRC6: public virtual IRsendBase { |
|---|
| 51 | public: |
|---|
| 52 | void send(uint32_t data, uint8_t nBits=16) { |
|---|
| 53 | if (nBits==0) nBits=16; |
|---|
| 54 | enableIROut(36); |
|---|
| 55 | uint64_t bigData = data; |
|---|
| 56 | if (nBits==32) { |
|---|
| 57 | bigData+=0xc00000000ull;//add OEM value |
|---|
| 58 | nBits=36; |
|---|
| 59 | }; |
|---|
| 60 | bigData=bigData << (64 - nBits); |
|---|
| 61 | extent=0; |
|---|
| 62 | mark(RC6_HDR_MARK); space(RC6_HDR_SPACE); |
|---|
| 63 | mark(RC6_T1); space(RC6_T1);// start bit "1" |
|---|
| 64 | uint16_t t; |
|---|
| 65 | for (uint8_t i = 0; i < nBits; i++) { |
|---|
| 66 | if (i == 3) { |
|---|
| 67 | t = 2 * RC6_T1; // double-wide trailer bit |
|---|
| 68 | } else { |
|---|
| 69 | t = RC6_T1; |
|---|
| 70 | } |
|---|
| 71 | if (bigData & 0x8000000000000000ull) { |
|---|
| 72 | mark(t); space(t);//"1" is a Mark/space |
|---|
| 73 | } else { |
|---|
| 74 | space(t); mark(t);//"0" is a space/Mark |
|---|
| 75 | } |
|---|
| 76 | bigData <<= 1; |
|---|
| 77 | } |
|---|
| 78 | space(107000-extent); // Turn off at end |
|---|
| 79 | } |
|---|
| 80 | }; |
|---|
| 81 | #endif //IRLIBSENDBASE_H |
|---|
| 82 | |
|---|
| 83 | #ifdef IRLIBDECODEBASE_H |
|---|
| 84 | |
|---|
| 85 | /* Note this decoder is a derived class from the IRdecodeRC base |
|---|
| 86 | * class rather than IRdecodeBase. The base class defines the |
|---|
| 87 | * method "getRClevel" which is common to both RC5 and RC6 protocols. |
|---|
| 88 | * It facilitates the decoding of phase encoded data. |
|---|
| 89 | */ |
|---|
| 90 | class IRdecodeRC6: public virtual IRdecodeRC { |
|---|
| 91 | public: |
|---|
| 92 | virtual bool decode(void) { |
|---|
| 93 | IRLIB_ATTEMPT_MESSAGE(F("RC6")); |
|---|
| 94 | resetDecoder();//This used to be in the receiver getResults. |
|---|
| 95 | //Legal lengths range from 24 through 76 we went one bigger just in case. |
|---|
| 96 | if( (recvGlobal.decodeLength < 23) || (recvGlobal.decodeLength > 77) )return RAW_COUNT_ERROR; |
|---|
| 97 | // Initial mark |
|---|
| 98 | if (!ignoreHeader) { |
|---|
| 99 | if (!MATCH(recvGlobal.decodeBuffer[1], RC6_HDR_MARK)) return HEADER_MARK_ERROR(RC6_HDR_MARK); |
|---|
| 100 | } |
|---|
| 101 | if (!MATCH(recvGlobal.decodeBuffer[2], RC6_HDR_SPACE)) return HEADER_SPACE_ERROR(RC6_HDR_SPACE); |
|---|
| 102 | offset=3;//Skip gap and header |
|---|
| 103 | data = 0; |
|---|
| 104 | used = 0; |
|---|
| 105 | // Get start bit (1) |
|---|
| 106 | if (getRClevel(&used, RC6_T1) != MARK) return DATA_MARK_ERROR(RC6_T1); |
|---|
| 107 | if (getRClevel(&used, RC6_T1) != SPACE) return DATA_SPACE_ERROR(RC6_T1); |
|---|
| 108 | for (nBits = 0; offset < recvGlobal.decodeLength; nBits++) { |
|---|
| 109 | RCLevel levelA, levelB; // Next two levels |
|---|
| 110 | levelA = getRClevel(&used, RC6_T1); |
|---|
| 111 | if (nBits == 3) { |
|---|
| 112 | // T bit is double wide; make sure second half matches |
|---|
| 113 | if (levelA != getRClevel(&used, RC6_T1)) return TRAILER_BIT_ERROR(RC6_T1); |
|---|
| 114 | } |
|---|
| 115 | levelB = getRClevel(&used, RC6_T1); |
|---|
| 116 | if (nBits == 3) { |
|---|
| 117 | // T bit is double wide; make sure second half matches |
|---|
| 118 | if (levelB != getRClevel(&used, RC6_T1)) return TRAILER_BIT_ERROR(RC6_T1); |
|---|
| 119 | } |
|---|
| 120 | if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5 |
|---|
| 121 | data = (data << 1) | 1; // 1 bit |
|---|
| 122 | } else if (levelA == SPACE && levelB == MARK) { |
|---|
| 123 | data <<= 1; // zero bit |
|---|
| 124 | } else { |
|---|
| 125 | return DATA_MARK_ERROR(RC6_T1); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | // Success |
|---|
| 129 | if (nBits==36) { |
|---|
| 130 | nBits=32;//OEM & trailer bits are discarded on 32-bit version |
|---|
| 131 | } else { |
|---|
| 132 | //if ( (nBits!=20) || (nBits!=24) || (nBits!=28) ) return BIT_COUNT_ERROR; |
|---|
| 133 | } |
|---|
| 134 | bits = nBits; |
|---|
| 135 | value = data; |
|---|
| 136 | protocolNum = RC6; |
|---|
| 137 | return true; |
|---|
| 138 | } |
|---|
| 139 | }; |
|---|
| 140 | #endif //IRLIBDECODEBASE_H |
|---|
| 141 | |
|---|
| 142 | #define IRLIB_HAVE_COMBO |
|---|
| 143 | |
|---|
| 144 | #endif //IRLIB_PROTOCOL_04_H |
|---|