| 1 | /* IRrecvPCI.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 implements the IRrecvPCI receiver class which uses pin change interrupts to |
|---|
| 6 | * poll the input pin. This class gives more accurate results than the 50us timer based |
|---|
| 7 | * IRrecv but it has other limitations. It uses the Arduino "attachInterrupt()" function |
|---|
| 8 | * which can conflict with other libraries. Also unless you use an external buffer and |
|---|
| 9 | * enable auto resume this receiver occasionally fails to receive the second of 2 quickly |
|---|
| 10 | * sent frames. If you do not have sufficient RAM for a second buffer you should consider |
|---|
| 11 | * using the other two available receiver classes. |
|---|
| 12 | * |
|---|
| 13 | * This receiver is based in part on Arduino firmware for use with AnalysIR IR signal analysis |
|---|
| 14 | * software for Windows PCs. Many thanks to the people at http://analysir.com for their |
|---|
| 15 | * assistance in developing this section of code. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #include "IRLibRecvPCI.h" |
|---|
| 19 | #include "IRLibHardware.h" //needed for IRLib_didIROut |
|---|
| 20 | |
|---|
| 21 | void IRrecvPCI_Handler();//prototype for interrupt handler |
|---|
| 22 | |
|---|
| 23 | /* Note that the constructor is passed the interrupt number rather than the pin number. |
|---|
| 24 | * WARNING: These interrupt numbers which are passed to attachInterrupt() are not |
|---|
| 25 | * necessarily identical to the interrupt numbers in the datasheet of the processor chip |
|---|
| 26 | * you are using. Rather it is a numbering system to the attachInterrupt() Arduino |
|---|
| 27 | * function. For more information on attachInterrupt see |
|---|
| 28 | * http://arduino.cc/en/Reference/AttachInterrupt |
|---|
| 29 | */ |
|---|
| 30 | IRrecvPCI::IRrecvPCI(uint8_t pin) { |
|---|
| 31 | init(); |
|---|
| 32 | intrNum=digitalPinToInterrupt(pin); |
|---|
| 33 | recvGlobal.recvPin=pin; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /* Initializes receiving and attaches the pin change interrupt handler. Call this to |
|---|
| 37 | * initialize it again to resume receiving after completing the decoding. Previous versions |
|---|
| 38 | * of IRLib had a "resume" method that you should use this in either initializing or resuming. |
|---|
| 39 | */ |
|---|
| 40 | void IRrecvPCI::enableIRIn(void) { |
|---|
| 41 | //See comments on IRrecv::enableIRIn in "IRLibRecv.cpp" for explanation of this logic |
|---|
| 42 | if(recvGlobal.newDataAvailable) |
|---|
| 43 | return; |
|---|
| 44 | recvGlobal.decoderWantsData=true;//otherwise he wouldn't have called |
|---|
| 45 | if( (recvGlobal.currentState==STATE_FINISHED) || IRLib_didIROut ) { |
|---|
| 46 | IRrecvBase::enableIRIn(); |
|---|
| 47 | recvGlobal.timer=micros(); |
|---|
| 48 | attachInterrupt(intrNum, IRrecvPCI_Handler, CHANGE); |
|---|
| 49 | } |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | /* Even when not sampling inputs and recording pulse timing, the ISR remains active |
|---|
| 53 | * continues to interrupt every 50us however it remains in a do-nothing state. If |
|---|
| 54 | * the user wants to truly shut down the ISR they can call this method. |
|---|
| 55 | */ |
|---|
| 56 | void IRrecvPCI::disableIRIn(void) { |
|---|
| 57 | detachInterrupt(intrNum); |
|---|
| 58 | IRrecvBase::disableIRIn(); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /* Returns true if a frame of data is available in the buffer. Most of the |
|---|
| 62 | * handling is done by the base method. Because the ISR only is active when the pin |
|---|
| 63 | * is changing, it may not recognize the long gap at the end of the frame so we need to |
|---|
| 64 | * test to see if we had a long gap. Because the ISR is active, we need to ensure |
|---|
| 65 | * that it doesn't change any variables while we are in the middle of accessing them. |
|---|
| 66 | * Unfortunately the ATOMIC_BLOCK macro that we used to use is not supported for |
|---|
| 67 | * SAMD 21 platforms so we have to use "noInterrupts();" and "interrupts();" |
|---|
| 68 | * This is only necessary for multibyte variables. |
|---|
| 69 | */ |
|---|
| 70 | bool IRrecvPCI::getResults(void) { |
|---|
| 71 | if(recvGlobal.newDataAvailable) { |
|---|
| 72 | IRrecvBase::getResults(); |
|---|
| 73 | return true; |
|---|
| 74 | } |
|---|
| 75 | //ISR hasn't detected end of frame so if running we will take a peek |
|---|
| 76 | if(recvGlobal.currentState==STATE_RUNNING) { |
|---|
| 77 | //Only check for timeout if it is a SPACE |
|---|
| 78 | if(digitalRead(recvGlobal.recvPin)) {//pin high means SPACE |
|---|
| 79 | uint32_t changeTime; //time of the last change |
|---|
| 80 | noInterrupts (); //Ensure atomic access of volatile variable |
|---|
| 81 | changeTime=recvGlobal.timer; |
|---|
| 82 | interrupts(); //restore interrupts |
|---|
| 83 | if( (micros()-changeTime) > recvGlobal.frameTimeout) { |
|---|
| 84 | IRLib_IRrecvComplete(3); |
|---|
| 85 | IRrecvBase::getResults(); |
|---|
| 86 | return true; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | return false; |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | /* This is the interrupt handler used by this class. It is called every time the input |
|---|
| 94 | * pin changes from high to low or from low to high. The initial state of the state machine |
|---|
| 95 | * is STATE_READY_TO_BEGIN. It waits until it sees a MARK before it switches to |
|---|
| 96 | * STATE_RUNNING. Each subsequent interrupt it computes the time since the previous |
|---|
| 97 | * interrupt. If it notices a SPACE is especially long then it calls IRLib_IRrecvComplete |
|---|
| 98 | * which sets the proper flags to say that we had received data and implements |
|---|
| 99 | * auto resume if enabled. The state STATE_FINISHED means that interrupts are continuing |
|---|
| 100 | * however the receiver isn't ready for more data. |
|---|
| 101 | */ |
|---|
| 102 | void IRrecvPCI_Handler(){ |
|---|
| 103 | uint32_t volatile changeTime=micros(); |
|---|
| 104 | uint32_t deltaTime=changeTime-recvGlobal.timer; |
|---|
| 105 | switch(recvGlobal.currentState) { |
|---|
| 106 | case STATE_FINISHED: return; |
|---|
| 107 | case STATE_RUNNING: |
|---|
| 108 | IRLib_doBlink(); |
|---|
| 109 | if( !(recvGlobal.recvLength & 1) ){ |
|---|
| 110 | if (deltaTime>recvGlobal.frameTimeout) { |
|---|
| 111 | IRLib_IRrecvComplete(1);//all finished, reset and possibly do auto resume |
|---|
| 112 | return;//don't record final space |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | break; |
|---|
| 116 | case STATE_READY_TO_BEGIN: |
|---|
| 117 | if(digitalRead(recvGlobal.recvPin)) {//pin high means SPACE |
|---|
| 118 | return;//don't start until we get a MARK |
|---|
| 119 | } else { |
|---|
| 120 | recvGlobal.currentState=STATE_RUNNING; |
|---|
| 121 | } |
|---|
| 122 | break; |
|---|
| 123 | }; |
|---|
| 124 | recvGlobal.recvBuffer[recvGlobal.recvLength]=deltaTime; |
|---|
| 125 | recvGlobal.timer=changeTime; |
|---|
| 126 | if(++recvGlobal.recvLength>=RECV_BUF_LENGTH) {//buffer overflows so we quit |
|---|
| 127 | IRLib_IRrecvComplete(2); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|