source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLib_P03_RC5.h

Last change on this file was 3, checked in by markus, 7 years ago

Setup project structure

File size: 4.2 KB
Line 
1/* IRLib_P03_RC5.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 RC5 protocol was invented by Phillips but is used by wide variety of manufacturers.
7 * It uses a phase encoding of data bits. A space/mark pair indicates "1"
8 * and a mark/space indicates a "0". It begins with a single "1" bit which is not encoded
9 * in the data. The second highest order data bit is a toggle bit that indicates individual
10 * keypresses. You must toggle this bit yourself when sending data. The protocol uses 36 kHz
11 * modulation however 38 kHz receivers can typically receive the codes okay.
12 * There are three supported varieties as follows:
13 * RC5          13 bits, 36 kHz (default, most common variety.
14 * RC5-F7       14 bits, 36 kHz
15 * RC5-F7-57    14 bits, 57 kHz
16 * There's also a 19 bit variety called RC5x that is not supported here but may be a separate
17 * module eventually.
18 */
19
20#ifndef IRLIB_PROTOCOL_03_H
21#define IRLIB_PROTOCOL_03_H
22#define IR_SEND_PROTOCOL_03             case 03: if(khz==38)khz=36; IRsendRC5::send(data,data2,khz); break;
23#define IR_DECODE_PROTOCOL_03   if(IRdecodeRC5::decode()) return true;
24#ifdef IRLIB_HAVE_COMBO
25        #define PV_IR_DECODE_PROTOCOL_03 ,public virtual IRdecodeRC5
26        #define PV_IR_SEND_PROTOCOL_03   ,public virtual IRsendRC5
27#else
28        #define PV_IR_DECODE_PROTOCOL_03  public virtual IRdecodeRC5
29        #define PV_IR_SEND_PROTOCOL_03    public virtual IRsendRC5
30#endif
31#define RC5_T1          889
32
33#ifdef IRLIBSENDBASE_H
34
35class IRsendRC5: public virtual IRsendBase {
36  public:
37    void send(uint32_t data, uint8_t nBits=13, uint8_t kHz=36) {
38      if(nBits==0)nBits=13;
39      if(kHz==0)kHz=36;
40      enableIROut(kHz);
41      data = data << (32 - nBits);
42      extent=0;
43      mark(RC5_T1); // First start bit
44//Note: Original IRremote library incorrectly assumed second bit was
45//always a "1". Bit patterns from this decoder are not backward compatible
46//with patterns produced by the original library. Uncomment the following two
47//lines to maintain backward compatibility.
48      //space(RC5_T1); // Second start bit
49      //mark(RC5_T1); // Second start bit
50      for (uint8_t i = 0; i < nBits; i++) {
51        if (data & TOPBIT) {
52          space(RC5_T1); mark(RC5_T1);// 1 is space, then mark
53        } else {
54          mark(RC5_T1);  space(RC5_T1);// 0 is mark, then space
55        }
56        data <<= 1;
57      }
58      space(114000-extent); // Turn off at end
59    }
60};
61#endif  //IRLIBSENDBASE_H
62
63#ifdef IRLIBDECODEBASE_H
64
65/* Note this decoder is a derived class from the IRdecodeRC base class
66 * rather than IRdecodeBase. The base class defines the method "getRClevel"
67 * which is common to both RC5 and RC6 protocols. It facilitates the decoding
68 * of phase encoded data.
69 */
70
71class IRdecodeRC5: public virtual IRdecodeRC {
72  public:
73    virtual bool decode(void) {
74      IRLIB_ATTEMPT_MESSAGE(F("RC5"));
75      resetDecoder();//This used to be in the receiver getResults.
76      if (recvGlobal.decodeLength < 13) return RAW_COUNT_ERROR;
77      offset = 1; // Skip gap space
78      data = 0;
79      used = 0;
80      // Get start bits
81      if (getRClevel(&used, RC5_T1) != MARK) return HEADER_MARK_ERROR(RC5_T1);
82//Note: Original IRremote library incorrectly assumed second bit was always
83// a "1". Bit patterns from this decoder are not backward compatible with
84// patterns produced by original library. Uncomment the following two lines
85// to maintain backward compatibility.
86      //if (getRClevel(&used, RC5_T1) != SPACE) return HEADER_SPACE_ERROR(RC5_T1);
87      //if (getRClevel(&used, RC5_T1) != MARK) return HEADER_MARK_ERROR(RC5_T1);
88      for (nBits = 0; offset < recvGlobal.decodeLength; nBits++) {
89        RCLevel levelA = getRClevel(&used, RC5_T1); 
90        RCLevel levelB = getRClevel(&used, RC5_T1);
91        if (levelA == SPACE && levelB == MARK) {
92          data = (data << 1) | 1;   // 1 bit
93        } else if (levelA == MARK && levelB == SPACE) {
94          data <<= 1;               // zero bit
95        } else return DATA_MARK_ERROR(RC5_T1);
96      }
97      // Success
98      bits = nBits;
99      value = data;
100      protocolNum = RC5;
101      return true;
102    }
103};
104#endif //IRLIBDECODEBASE_H
105
106#define IRLIB_HAVE_COMBO
107
108#endif //IRLIB_PROTOCOL_03_H
Note: See TracBrowser for help on using the repository browser.