source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLibRecvBase.cpp @ 19

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

Setup project structure

File size: 9.7 KB
Line 
1/* IRrecvBase.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 * This module contains the base class classes for receiving IR signals. You will not create
6 * instances of this class, rather it is used as a base class for the 3 different
7 * receiver classes. These are the original IRrecv which uses a 50us timer interrupt.
8 * The IRrecvPCI version that uses pin change interrupts and IRrecvLoop which uses no timers
9 * or interrupts. Each of these has its own .h and .cpp file.
10 *
11 * The IRrecvLoop files are in this folder.  However the other two receivers in their own
12 * separate libraries. That is because their interrupt service routines can conflict
13 * with other ISRs. The ISRs conflict would occur even if you did not create an instance
14 * of their receiver classes. Similarly the frequency detection receiver which also uses
15 * interrupts is also in a separate folder IRLibFreq.
16 */
17
18#include "IRLibRecvBase.h"
19#include "IRLibHardware.h"
20
21/* This structure contains all of the global variables used by the ISRs to communicate
22 * with the receiver and decoder objects. You cannot pass parameters to an ISR so
23 * these values must be global. The fields are defined in IRLibRecvBase.h
24 */
25recvGlobal_t recvGlobal;
26
27// The base constructor receives the pin number
28IRrecvBase::IRrecvBase(uint8_t recvPin) {
29  recvGlobal.recvPin = recvPin;
30  init();
31}
32
33// Initialization common to all three receiver classes
34void IRrecvBase::init(void) {
35//These first two lines would normally be done by the decoder
36//however in rare circumstances there is no decoder.
37  recvGlobal.decoderWantsData=false; //turned on by enableIRIn.
38  recvGlobal.decodeBuffer=recvGlobal.recvBuffer;//default buffer
39        recvGlobal.enableAutoResume=false;
40  recvGlobal.frameTimeout=DEFAULT_FRAME_TIMEOUT;
41  recvGlobal.frameTimeoutTicks=recvGlobal.frameTimeout/USEC_PER_TICK;
42  markExcess=DEFAULT_MARK_EXCESS;
43  recvGlobal.newDataAvailable=false;
44  recvGlobal.enableBlinkLED=false;
45  recvGlobal.currentState=STATE_FINISHED;//i.e. Not running.
46}
47
48// Turn on or off a blinking LED which indicates signal received. Usually pin 13.
49void blink13(bool enableBlinkLED){
50#if (BLINKLED>0)
51  pinMode(BLINKLED,OUTPUT);
52  recvGlobal.enableBlinkLED=enableBlinkLED;
53#endif
54}
55
56/* Any receiver class must implement a getResults method that will return true when
57 * a complete frame of data has been received. When your getResults determines that
58 * the frame is complete, it must guarantee that there will be no further changes to
59 * the data in the buffer or the length value. It can do that by either disabling
60 * interrupts or putting the ISR in a state that ensures it will not
61 * change those values. Then it must then call IRrecvBase::getResults. This base method
62 * will then will perform some math on the values and copy them to the decodeBuffer.
63 * Some receivers provide results in recvBuffer measured in ticks of some number of
64 * microseconds while others return results in actual microseconds. If you use ticks then
65 * you should pass a multiplier value in timePerTicks.
66 * NOTE: Only call the base method if newDataAvailable is true.
67 */
68bool IRrecvBase::getResults(const uint16_t timePerTick) {
69//Conceptually the loop below copies data from the receiver buffer to the decode buffer
70//while performing some math. In some instances, the source and destination are the same.
71//The decoder has already set up decodeBuffer to point to the proper destination.
72//Here we need to figure out the source. If didAutoResume is true then the ISR has already
73//copied the data to decodeBuffer so it is both source and destination. In all
74//other circumstances the source is always recvGlobal.recvBuffer.
75  recvGlobal.newDataAvailable=false;
76  volatile uint16_t *Source;
77  //DEBUG_VALUE("recvGlobal.didAutoResume",recvGlobal.didAutoResume);
78  if(recvGlobal.didAutoResume) {
79    Source=recvGlobal.decodeBuffer;
80    recvGlobal.didAutoResume=false;
81  } else {
82    Source=recvGlobal.recvBuffer;
83    recvGlobal.decodeLength=recvGlobal.recvLength;//if auto resumed, was already copied
84  }
85//If the receiver counts time intervals rather than actual microseconds we will multiply
86//the data by timePerTick. It also adjusts the data by adding or subtracting the
87//markExcess value. See the documentation on markExcess for details.
88  for(uint8_t i=0; i<recvGlobal.decodeLength; i++) {
89    recvGlobal.decodeBuffer[i]=Source[i]*timePerTick + ( (i % 2)? -markExcess:markExcess);
90  }
91  //Now that the decoder has its data it doesn't want any more until it tells you.
92  //It will do so by calling enableIRIn.
93  recvGlobal.decoderWantsData=false;
94  return true;
95}
96
97/* The user calls enableIRIn each time it is ready to receive data. Previously
98 * this was only used to initialize the receiver once and subsequent calls were made
99 * to a method called "resume". However it was confusing because you had to call
100 * enableIRIn after a send because any sending would disable IRIn. It's simpler just to
101 * always use enableIRIn even though parts of it are slightly redundant.
102 * The interrupt driven receivers call this before enabling interrupts.
103 * See the comments on IRrecv::enableIRIn() in IRLibRecv.cpp regarding auto resume.
104 */
105void IRrecvBase::enableIRIn(void) { 
106//some IR receiver datesheets recommend pull-up resistors
107  pinMode(recvGlobal.recvPin, INPUT_PULLUP);
108  recvGlobal.recvLength = 0;
109  recvGlobal.currentState = STATE_READY_TO_BEGIN;
110  IRLib_didIROut=false;//We reinitialized so reset until somebody does more output.
111}
112
113/* Even when not receiving data or waiting to receive data, the ISR may remain active
114 * but remains in a do-nothing state. If the user wants to truly shut down the ISR
115 * they can call this method. The derived method should disable the ISR and then call
116 * this base method to the turn everything off.
117 */
118void IRrecvBase::disableIRIn(void) {
119  recvGlobal.decoderWantsData=false;
120  recvGlobal.didAutoResume=false;
121  recvGlobal.currentState=STATE_FINISHED;//i.e. Not running.
122}
123
124/*
125 * Normally recvGlobal.decodeBuffer points to recvGlobal.recvBuffer and therefore
126 * decoding uses the same buffer as receiving. However you may want to resume
127 * receiving while still decoding. To do so must specify a separate buffer for decoding.
128 * You will declare the buffer as "uint16_t myBuffer[RECV_BUF_LENGHT];" in your sketch
129 * then pass its address using the method below. Then IRrecvBase::getResults() will copy
130 * timing values from its buffer to yours. The receiver will then automatically resume.
131 * The receiver will not overwrite your buffer unless you have called enableIRIn()
132 * to tell it that you have finished your decoding. In other words auto resume will only
133 * occur once until you again call enableIRIn().
134 */
135void IRrecvBase::enableAutoResume(uint16_t *P){
136  recvGlobal.decodeBuffer=(volatile uint16_t*)P;
137  recvGlobal.enableAutoResume=true;
138};
139
140// This had to be a method so that IRrecv::setFrameTimeout can compute
141// frameTimeoutTicks.
142void IRrecvBase::setFrameTimeout(uint16_t frameTimeout) {
143  recvGlobal.frameTimeout=frameTimeout;
144}
145
146/*********************
147 *
148 * The remaining functions below are not part of any class or object. They are global
149 * so they can be called by the ISRs or for other reasons are not really tied to any
150 * class or object.
151 *
152 *********************/
153
154/*
155 * This function is called by both the 50us and PCI ISR in one of two circumstances:
156 * 1) The SPACE was long enough that we are sure the frame is over and ready to process.
157 * 2) The buffer overflowed we have to quit. The parameter is for debugging purposes only.
158 */
159void IRLib_IRrecvComplete(uint8_t Reason) {
160  // Here we are finished. Let the world know there is new data available.
161  recvGlobal.newDataAvailable=true;
162  recvGlobal.currentState=STATE_FINISHED;//this essentially pauses the receiver ISR
163  //Now we need to see if we want to auto resume. We can only do that if it is enabled and
164  //if the user is finished using the buffer from the previous capture and wants more data.
165  //DEBUG_VALUE ("Reason completed", Reason)
166  if (recvGlobal.enableAutoResume && recvGlobal.decoderWantsData) {
167                //Here we do the actual auto resume. We will copy the data using memcpy because it
168    //should be very quick. Any calculations will be handled by the getResults method but
169    //not here.
170    memcpy((void *)recvGlobal.decodeBuffer, (const void*)recvGlobal.recvBuffer,recvGlobal.recvLength*sizeof(uint16_t));
171                recvGlobal.decodeLength=recvGlobal.recvLength;
172                // We have just copied the data to the decoder so it's not going to want more until
173                // it tells us that it is ready for more.
174                recvGlobal.decoderWantsData=false;
175    // Tell getResults that we auto resumed therefore the data has been copied but
176    // still needs the math done.
177    recvGlobal.didAutoResume=true;
178                // Now we need to reset the index to the beginning and restart the state machine.
179                recvGlobal.recvLength=0;
180    //While we were doing the copy, the 50 us interrupt continued but the state machine
181    //was paused in the STATE_FINISHED. Now we actually turn it back on till you get to
182    //actually receive data.
183    recvGlobal.currentState= STATE_READY_TO_BEGIN;
184  }
185}
186
187/* If your hardware is set up to do both output and input but your particular sketch
188 * doesn't do any output, this method will ensure that your output pin is low
189 * and doesn't turn on your IR LED or any output circuit.
190 */
191void IRLib_NoOutput (void) {
192#if defined(IR_SEND_PWM_PIN)
193 pinMode(IR_SEND_PWM_PIN, OUTPUT); 
194 digitalWrite(IR_SEND_PWM_PIN, LOW); // When not sending PWM, we want it low   
195#endif
196}
197       
198/* Do the actual blinking off and on of the indicator LED. Called by the various
199 * receiver ISRs
200 */
201void IRLib_doBlink(void) {
202  if (recvGlobal.enableBlinkLED) {
203    if(recvGlobal.recvLength & 1) {
204      BLINKLED_ON();  // turn pin 13 LED on
205    } 
206    else {
207      BLINKLED_OFF();  // turn pin 13 LED off
208    }
209  }
210}
Note: See TracBrowser for help on using the repository browser.