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

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

Update Arduino-IDE to 1.8.10

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