source: trunk/Arduino/libraries/Adafruit_Circuit_Playground/utility/IRLibGlobals.h @ 26

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

Setup project structure

File size: 7.7 KB
Line 
1/* IRLibGlobals.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 * This file replaces the previous IRLibRData.h file. It contains definition of global
6 * items which are used by both the receiver and decoding classes. They have to be
7 * declared global in scope because they are accessed by the ISR and you cannot pass
8 * parameters nor objects to an ISR routine.
9 *
10 * In general, applications would not include this file. Unless you are creating your own
11 * customized receiver class apart from the provided IRrcev, IRrcevPCI, or IRrcevLoop
12 * classes, you can ignore the contents of this file.
13 * The RECV_BUF_LENGTH is the only item you would ever modify unless you are completely
14 * rewriting how the system works in general.
15 */
16#ifndef IRLibGlobals_h
17#define IRLibGlobals_h
18#include <Arduino.h>
19
20/* Timing data is stored in a buffer by the receiver object. It is an array of
21 * uint16_t that should be at least 100 entries as defined by this default below.
22 * However some IR sequences will require longer buffers especially those used for
23 * air conditioner controls. In general we recommend you keep this value below 255
24 * so that the index into the array can remain 8 bits. This library can handle larger
25 * arrays however it will make your code longer in addition to taking more RAM.
26 */
27#define RECV_BUF_LENGTH 100
28#if (RECV_BUF_LENGTH > 255)
29        typedef uint16_t bufIndex_t;
30#else
31        typedef uint8_t bufIndex_t;
32#endif
33
34// Receiver states. This previously was enum but changed it to uint8_t
35// to guarantee it was a single atomic 8-bit value.
36#define  STATE_UNKNOWN 0
37#define  STATE_READY_TO_BEGIN 1
38#define  STATE_TIMING_MARK 2
39#define  STATE_TIMING_SPACE 3
40#define  STATE_FINISHED 4
41#define  STATE_RUNNING 5
42typedef uint8_t  currentState_t;
43/* The structure contains information used by the ISR routines. Because we cannot
44 * pass parameters to an ISR, it must be global. Values which can be changed by
45 * the ISR but are accessed outside the ISR must be volatile.
46 */
47typedef struct {
48//These next 4 fields are initialized by the receiver class and unlikely to change once
49//they have been initialized. Only change them on the fly at your own peril.
50  uint8_t recvPin;    // pin number or interrupt number for receiver
51  bool enableAutoResume;        //ISR can copy data to decodeBuffer and restart by itself
52  uint16_t frameTimeout; //Maximum length of a SPACE before we decide the frame has ended
53//Used by IRrecv only
54  uint16_t frameTimeoutTicks;// = frameTimeout/USEC_PER_TICKS
55  bool enableBlinkLED;
56 
57//These fields are both read and written inside and outside ISRs. Must be volatile.
58  volatile bool decoderWantsData;       //tells ISR previous decode is finished. Buffer available.
59  volatile bool newDataAvailable; //ISR flag informs getResults that data is available.
60  volatile bool didAutoResume;    //ISR tells getResults we already copied, just do math.
61//The buffers are filled with timing values by the ISRs. The recvLength is the number of
62//entries when the frame is complete. See the note at the end of this file about buffers.
63  volatile uint16_t recvBuffer[RECV_BUF_LENGTH];
64//  volatile uint16_t* recvBuffer;
65  volatile bufIndex_t recvLength;
66//These next two fields are how the receiver communicates with the decoder. Previously
67//this was accomplished by passing to the receiver a pointer to the decoder in getResults.
68//However with auto resume we now may need to communicate before getResults is called.
69//The decoderBuffer pointer is maintained by the decoder. It points to where the
70//decoder wants the data put by the receiver. It will point to either recvGlobal.recvBuffer
71//or an external buffer provided by the user via useExtnBuf. The decodeLength
72//is set by the receiver telling the decoder the data length.
73  volatile uint16_t* decodeBuffer;
74  volatile bufIndex_t decodeLength;
75//This field accumulates the elapsed time of a MARK or SPACE. IRrecv uses it only inside
76//the ISR however IRrecvPCI accesses it outside the ISR. Therefore it is volatile
77//and because it is multi-byte it will need atomic guards when accessed outside the ISR.
78  volatile uint32_t timer;     // state timer, counts 50uS ticks.(and other uses)
79//Used by both IRrecv and IRrecvPCI.
80  volatile currentState_t currentState;   // state machine  Legal values defined above.
81} 
82recvGlobal_t;
83extern  recvGlobal_t recvGlobal; //declared in IRLibRecvBase.cpp
84
85/****  NOTE CONCERNING BUFFER NAMES AND VOLATILITY
86 * In versions of IRLib prior to 2.0 we had the capability to declare an external
87 * buffer so that the receiver could resume receiving while the decoding was taking place.
88 * However it was up to the decoder to notice that the data had been received via a call to
89 * getResults() and it had to tell the receiver to resume. Starting with version 2.0
90 * it is now possible that the receiver can automatically copy the data to the external
91 * buffer and auto resume. Rather than using names like rawbuf1 and rawbuf2 we have come
92 * up with a new naming system. Also irparams wasn't the most descriptive name
93 * ever conceived. Here is our new naming system which we hope is slightly less confusing.
94 *
95 * First irparams was a structure that contained global variables used by the receiver ISR.
96 * They should not be part of the receiver class because you can't pass parameters
97 * to an ISR. We think recvGlobal is a better name.
98 *
99 * Previously the ISR did not need access to any external buffer so it only needed one
100 * buffer and one length variable. If there was an external buffer, only the decoder
101 * needed to access it. Now the receiver and ISR needs access to that external buffer
102 * in order to implement auto resume. Therefore the extra buffer needs to be linked
103 * to recvGlobal as well. The receiver puts the data in recvGlobal.recvBuffer and it's
104 * length is in recvGlobal.recvLength. It is for the receiver's internal use.
105 * Additionally we have "decodeBuffer" and "decodeLength" which
106 * are defined as where the decoder wants the receiver to put its data. The decoder
107 * decides where decodeBuffer points. The receiver puts the data at that address
108 * and has no idea where it points. The receiver puts the length in decodeLength.
109 * It is the receiver's responsibility to copy the data from recvBuffer to decodeBuffer.
110 * The math is always done in getResults however deciding when to do the copying is
111 * a bit more complicated. If we are doing auto resume, we do a bufcpy to copy as
112 * quickly as possible and defer the math until the actual poll to getResults.
113 * The getResults needs to know whether it should copy or just do the math in place.
114 * If you determine that by the flag didAutoResume.
115 *
116 * Now we tackle the volatile issue of "volatile". Aptly named because it starts flame wars.
117 * In general the rule is, if data is to be accessed inside and outside an ISR you need to
118 * add the volatile qualifier to its definition. This ensures that the compiler does not
119 * optimize the code in such a way that a bad copy of a variable might be used. It forces
120 * the compiler to fetch the data every time he uses it and not to store it in a register.
121 * We are going to presume that our ISRs never interrupt themselves. I believe the volatile
122 * qualifier may not be necessary because the decoder is only accessing the data once the
123 * receiver is finished working with it. The only items that really need to be volatile
124 * are some of the flags. However making the buffer volatile does not seem to measurably
125 * slowdown the decoding process (I ran some tests). So as part of "good programming
126 * practices we are marking the buffers and lengths as volatile just in case. We need not
127 * use atomic blocks to access the buffer except in very rare cases.
128 */ 
129#define DEBUG_VALUE(l, v) Serial.print(l); Serial.print(":"); Serial.println(v,DEC);
130#endif
Note: See TracBrowser for help on using the repository browser.