source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLibDecodeBase.cpp @ 3

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

Setup project structure

File size: 6.8 KB
Line 
1/* IRLibDecodeBase.cpp
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 * This module contains the base classes for decoding. You will not create instances
7 * of these classes, rather you will use them as base classes in creating derived
8 * protocol specific decoders.
9 */
10#include "IRLibDecodeBase.h"
11#include "IRLibHardware.h"
12
13IRdecodeBase::IRdecodeBase(void) {
14  recvGlobal.decoderWantsData=false; //turned on by enableIRIn.
15  recvGlobal.decodeBuffer=recvGlobal.recvBuffer;//default buffer
16  ignoreHeader=false;
17  resetDecoder();
18};
19
20/*
21 * Reinitialize the decoder clearing out previous data.
22 */
23void IRdecodeBase::resetDecoder(void) {
24  protocolNum= UNKNOWN;
25  value=0;
26  address=0;
27  bits=0;
28};
29void IRdecodeBase::dumpResults(bool verbose) {
30  int i;uint32_t Extent;int interval;
31  if((protocolNum>89) || (protocolNum<=LAST_PROTOCOL)) {
32    Serial.print(F("Decoded ")); Serial.print(Pnames(protocolNum));
33    Serial.print(F("(")); Serial.print(protocolNum,DEC);
34    Serial.print(F("): Value:")); Serial.print(value, HEX);
35    Serial.print(F(" Adrs:" )); Serial.print(address, HEX);
36  };
37  Serial.print(F(" ("));  Serial.print(bits, DEC); Serial.print(F(" bits) "));
38  if(recvGlobal.didAutoResume) Serial.print(F("Auto Resumed"));
39  Serial.println();
40  if(!verbose)
41    return;
42  Serial.print(F("Raw samples(")); Serial.print(recvGlobal.decodeLength, DEC);
43  Serial.print(F("): Gap:")); Serial.println(recvGlobal.decodeBuffer[0], DEC);
44  Serial.print(F("  Head: m")); Serial.print(recvGlobal.decodeBuffer[1], DEC);
45  Serial.print(F("  s")); Serial.println(recvGlobal.decodeBuffer[2], DEC);
46  int LowSpace= 32767; int LowMark=  32767;
47  int HiSpace=0; int HiMark=  0;
48  Extent=recvGlobal.decodeBuffer[1]+recvGlobal.decodeBuffer[2];
49  for (i = 3; i < recvGlobal.decodeLength; i++) {
50    Extent+=(interval= recvGlobal.decodeBuffer[i]);
51    if (i % 2) {
52      LowMark=min(LowMark, interval);  HiMark=max(HiMark, interval);
53      Serial.print(i/2-1,DEC);  Serial.print(F(":m"));
54    } 
55    else {
56       if(interval>0)LowSpace=min(LowSpace, interval);  HiSpace=max (HiSpace, interval);
57       Serial.print(F(" s"));
58    }
59    Serial.print(interval, DEC);
60    int j=i-1;
61    if ((j % 2)==1)Serial.print(F("\t"));
62    if ((j % 4)==1)Serial.print(F("\t "));
63    if ((j % 8)==1)Serial.println();
64    if ((j % 32)==1)Serial.println();
65  }
66  Serial.println();
67  Serial.print(F("Extent="));  Serial.println(Extent,DEC);
68  Serial.print(F("Mark  min:")); Serial.print(LowMark,DEC);Serial.print(F("\t max:")); Serial.println(HiMark,DEC);
69  Serial.print(F("Space min:")); Serial.print(LowSpace,DEC);Serial.print(F("\t max:")); Serial.println(HiSpace,DEC);
70  Serial.println();
71}
72
73/* We use a generic routine because most protocols have the same basic structure.
74 * Previous versions of this method would handle protocols with variable marks
75 * or variable spaces. However we have discovered that only Sony protocol uses
76 * variable marks so we have stripped out that portion of the code. This changes
77 * the number of necessary parameters. We no longer need markOne and markZero
78 * because they are both the same which we will pass in markData. Note this new
79 * version will handle up to 48 bits putting the most significant 16 bits in
80 * "this.address" in the least significant 32 bits in "this.data". We could have
81 * allowed for 64 bit but we have not seen generic protocols that large.
82 */
83bool IRdecodeBase::decodeGeneric(uint8_t expectedLength, 
84      uint16_t headMark, uint16_t headSpace,  uint16_t markData, 
85      uint16_t spaceOne, uint16_t spaceZero) {
86   resetDecoder();//This used to be in the receiver getResults.
87// If "expectedLenght" or "headMark" or "headSpace" are zero or if "ignoreHeader"
88// is true then don't perform these tests. This is because some protocols need
89// to do their own custom header work.
90  uint64_t data = 0; 
91  bufIndex_t Max=recvGlobal.decodeLength-1; 
92  if (expectedLength) {
93    if (recvGlobal.decodeLength != expectedLength) return RAW_COUNT_ERROR;
94  }
95  if(!ignoreHeader) {
96    if (headMark) {
97      if (!MATCH(recvGlobal.decodeBuffer[1],headMark)) return HEADER_MARK_ERROR(headMark);
98    }
99  }
100  if (headSpace) {
101    if (!MATCH(recvGlobal.decodeBuffer[2],headSpace)) return HEADER_SPACE_ERROR(headSpace);
102  }
103  offset=3;//skip initial gap plus two header items
104  while (offset < Max) {
105    if (!MATCH (recvGlobal.decodeBuffer[offset],markData)) return DATA_MARK_ERROR(markData);
106    offset++;
107    if (MATCH(recvGlobal.decodeBuffer[offset],spaceOne)) {
108      data = (data << 1) | 1;
109    } 
110    else if (MATCH (recvGlobal.decodeBuffer[offset],spaceZero)) {
111      data <<= 1;
112    } 
113    else return DATA_SPACE_ERROR(spaceZero);
114    offset++;
115  }
116  bits = (offset - 1) / 2 -1;//didn't encode stop bit
117  // Success
118  value = (uint32_t)data;           //low order 32 bits
119  address = (uint16_t) (data>>32);  //high order 16 bits
120  return true;
121}
122
123/*
124 * These MATCH methods used to be macros but we saved nearly
125 * 800 bytes of program space by making them actual methods.
126 */
127 
128bool IRdecodeBase::MATCH(int16_t val,int16_t expected){
129#ifdef IRLIB_USE_PERCENT
130  return (val >= (uint16_t)(expected*(1.0-PERCENT_TOLERANCE/100.0)))
131      && (val <= (uint16_t)(expected*(1.0+PERCENT_TOLERANCE/100.0))); 
132#else
133  return  ABS_MATCH(val,expected,DEFAULT_ABS_TOLERANCE);
134#endif
135}
136bool IRdecodeBase::ABS_MATCH(int16_t val,int16_t expected,int16_t tolerance){
137  return  (val >= (expected-tolerance)) && (val <= (expected+tolerance));
138}
139
140/*
141 * The RC5 and RC6 and similar protocols used phase encoding and leave a
142 * routine to extract zeros and ones. This routine gets one undecoded
143 * level at a time from the raw buffer. personally The RC5/6 decoding
144 * is easier if the data is broken into time intervals.
145 * E.g. if the buffer has MARK for 2 time intervals and SPACE for 1,
146 * successive calls to getRClevel will return MARK, MARK, SPACE.
147 * The variables "offset" and "used" are updated to keep track of the
148 * current position. The variable "t1" is the time interval for a single
149 * bit in microseconds. Returns ERROR if the measured time interval is
150 * not a multiple of "t1".
151 */
152IRdecodeRC::RCLevel IRdecodeRC::getRClevel(uint8_t *used, const uint16_t t1) {
153  if (offset >= recvGlobal.decodeLength) {
154    // After end of recorded buffer, assume SPACE.
155    return SPACE;
156  }
157  uint16_t width = recvGlobal.decodeBuffer[offset];
158  IRdecodeRC::RCLevel val;
159  if ((offset) % 2) val=MARK; else val=SPACE;
160  uint8_t avail;
161  if (MATCH(width, t1)) {
162    avail = 1;
163  } 
164  else if (MATCH(width, 2*t1)) {
165    avail = 2;
166  } 
167  else if (MATCH(width, 3*t1)) {
168    avail = 3;
169  } else {
170    if((ignoreHeader) && (offset==1) && (width<t1)){
171      avail =1;
172    } else {
173      return ERROR;
174    }
175  }
176  (*used)++;
177  if (*used >= avail) {
178    *used = 0;
179    (offset)++;
180  }
181  return val;   
182}
Note: See TracBrowser for help on using the repository browser.