|
| 1 | +//****************************************************************************** |
| 2 | +// IRremote |
| 3 | +// Version 2.0.1 June, 2015 |
| 4 | +// Copyright 2009 Ken Shirriff |
| 5 | +// For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html |
| 6 | +// |
| 7 | +// Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers |
| 8 | +// Modified by Mitra Ardron <mitra@mitra.biz> |
| 9 | +// Added Sanyo and Mitsubishi controllers |
| 10 | +// Modified Sony to spot the repeat codes that some Sony's send |
| 11 | +// |
| 12 | +// Interrupt code based on NECIRrcv by Joe Knapp |
| 13 | +// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 |
| 14 | +// Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ |
| 15 | +// |
| 16 | +// JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post) |
| 17 | +// LG added by Darryl Smith (based on the JVC protocol) |
| 18 | +// Whynter A/C ARC-110WD added by Francesco Meschia |
| 19 | +//****************************************************************************** |
| 20 | + |
| 21 | +// Defining IR_GLOBAL here allows us to declare the instantiation of global variables |
| 22 | +#define IR_GLOBAL |
| 23 | +# include "IRremote.h" |
| 24 | +# include "IRremoteInt.h" |
| 25 | +#undef IR_GLOBAL |
| 26 | + |
| 27 | +#ifdef HAS_AVR_INTERRUPT_H |
| 28 | +#include <avr/interrupt.h> |
| 29 | +#endif |
| 30 | + |
| 31 | + |
| 32 | +//+============================================================================= |
| 33 | +// The match functions were (apparently) originally MACROs to improve code speed |
| 34 | +// (although this would have bloated the code) hence the names being CAPS |
| 35 | +// A later release implemented debug output and so they needed to be converted |
| 36 | +// to functions. |
| 37 | +// I tried to implement a dual-compile mode (DEBUG/non-DEBUG) but for some |
| 38 | +// reason, no matter what I did I could not get them to function as macros again. |
| 39 | +// I have found a *lot* of bugs in the Arduino compiler over the last few weeks, |
| 40 | +// and I am currently assuming that one of these bugs is my problem. |
| 41 | +// I may revisit this code at a later date and look at the assembler produced |
| 42 | +// in a hope of finding out what is going on, but for now they will remain as |
| 43 | +// functions even in non-DEBUG mode |
| 44 | +// |
| 45 | +int MATCH (int measured, int desired) |
| 46 | +{ |
| 47 | + DBG_PRINT(F("Testing: ")); |
| 48 | + DBG_PRINT(TICKS_LOW(desired), DEC); |
| 49 | + DBG_PRINT(F(" <= ")); |
| 50 | + DBG_PRINT(measured, DEC); |
| 51 | + DBG_PRINT(F(" <= ")); |
| 52 | + DBG_PRINT(TICKS_HIGH(desired), DEC); |
| 53 | + |
| 54 | + bool passed = ((measured >= TICKS_LOW(desired)) && (measured <= TICKS_HIGH(desired))); |
| 55 | + if (passed) |
| 56 | + DBG_PRINTLN(F("?; passed")); |
| 57 | + else |
| 58 | + DBG_PRINTLN(F("?; FAILED")); |
| 59 | + return passed; |
| 60 | +} |
| 61 | + |
| 62 | +//+======================================================== |
| 63 | +// Due to sensor lag, when received, Marks tend to be 100us too long |
| 64 | +// |
| 65 | +int MATCH_MARK (int measured_ticks, int desired_us) |
| 66 | +{ |
| 67 | + DBG_PRINT(F("Testing mark (actual vs desired): ")); |
| 68 | + DBG_PRINT(measured_ticks * USECPERTICK, DEC); |
| 69 | + DBG_PRINT(F("us vs ")); |
| 70 | + DBG_PRINT(desired_us, DEC); |
| 71 | + DBG_PRINT("us"); |
| 72 | + DBG_PRINT(": "); |
| 73 | + DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS) * USECPERTICK, DEC); |
| 74 | + DBG_PRINT(F(" <= ")); |
| 75 | + DBG_PRINT(measured_ticks * USECPERTICK, DEC); |
| 76 | + DBG_PRINT(F(" <= ")); |
| 77 | + DBG_PRINT(TICKS_HIGH(desired_us + MARK_EXCESS) * USECPERTICK, DEC); |
| 78 | + |
| 79 | + bool passed = ((measured_ticks >= TICKS_LOW (desired_us + MARK_EXCESS)) |
| 80 | + && (measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS))); |
| 81 | + if (passed) |
| 82 | + DBG_PRINTLN(F("?; passed")); |
| 83 | + else |
| 84 | + DBG_PRINTLN(F("?; FAILED")); |
| 85 | + return passed; |
| 86 | +} |
| 87 | + |
| 88 | +//+======================================================== |
| 89 | +// Due to sensor lag, when received, Spaces tend to be 100us too short |
| 90 | +// |
| 91 | +int MATCH_SPACE (int measured_ticks, int desired_us) |
| 92 | +{ |
| 93 | + DBG_PRINT(F("Testing space (actual vs desired): ")); |
| 94 | + DBG_PRINT(measured_ticks * USECPERTICK, DEC); |
| 95 | + DBG_PRINT(F("us vs ")); |
| 96 | + DBG_PRINT(desired_us, DEC); |
| 97 | + DBG_PRINT("us"); |
| 98 | + DBG_PRINT(": "); |
| 99 | + DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS) * USECPERTICK, DEC); |
| 100 | + DBG_PRINT(F(" <= ")); |
| 101 | + DBG_PRINT(measured_ticks * USECPERTICK, DEC); |
| 102 | + DBG_PRINT(F(" <= ")); |
| 103 | + DBG_PRINT(TICKS_HIGH(desired_us - MARK_EXCESS) * USECPERTICK, DEC); |
| 104 | + |
| 105 | + bool passed = ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS)) |
| 106 | + && (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS))); |
| 107 | + if (passed) |
| 108 | + DBG_PRINTLN(F("?; passed")); |
| 109 | + else |
| 110 | + DBG_PRINTLN(F("?; FAILED")); |
| 111 | + return passed; |
| 112 | +} |
| 113 | + |
| 114 | +//+============================================================================= |
| 115 | +// Interrupt Service Routine - Fires every 50uS |
| 116 | +// TIMER2 interrupt code to collect raw data. |
| 117 | +// Widths of alternating SPACE, MARK are recorded in rawbuf. |
| 118 | +// Recorded in ticks of 50uS [microseconds, 0.000050 seconds] |
| 119 | +// 'rawlen' counts the number of entries recorded so far. |
| 120 | +// First entry is the SPACE between transmissions. |
| 121 | +// As soon as a the first [SPACE] entry gets long: |
| 122 | +// Ready is set; State switches to IDLE; Timing of SPACE continues. |
| 123 | +// As soon as first MARK arrives: |
| 124 | +// Gap width is recorded; Ready is cleared; New logging starts |
| 125 | +// |
| 126 | +ISR (TIMER_INTR_NAME) |
| 127 | +{ |
| 128 | + TIMER_RESET; |
| 129 | + |
| 130 | + // Read if IR Receiver -> SPACE [xmt LED off] or a MARK [xmt LED on] |
| 131 | + // digitalRead() is very slow. Optimisation is possible, but makes the code unportable |
| 132 | + uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); |
| 133 | + |
| 134 | + irparams.timer++; // One more 50uS tick |
| 135 | + if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_OVERFLOW ; // Buffer overflow |
| 136 | + |
| 137 | + switch(irparams.rcvstate) { |
| 138 | + //...................................................................... |
| 139 | + case STATE_IDLE: // In the middle of a gap |
| 140 | + if (irdata == MARK) { |
| 141 | + if (irparams.timer < GAP_TICKS) { // Not big enough to be a gap. |
| 142 | + irparams.timer = 0; |
| 143 | + |
| 144 | + } else { |
| 145 | + // Gap just ended; Record duration; Start recording transmission |
| 146 | + irparams.overflow = false; |
| 147 | + irparams.rawlen = 0; |
| 148 | + irparams.rawbuf[irparams.rawlen++] = irparams.timer; |
| 149 | + irparams.timer = 0; |
| 150 | + irparams.rcvstate = STATE_MARK; |
| 151 | + } |
| 152 | + } |
| 153 | + break; |
| 154 | + //...................................................................... |
| 155 | + case STATE_MARK: // Timing Mark |
| 156 | + if (irdata == SPACE) { // Mark ended; Record time |
| 157 | + irparams.rawbuf[irparams.rawlen++] = irparams.timer; |
| 158 | + irparams.timer = 0; |
| 159 | + irparams.rcvstate = STATE_SPACE; |
| 160 | + } |
| 161 | + break; |
| 162 | + //...................................................................... |
| 163 | + case STATE_SPACE: // Timing Space |
| 164 | + if (irdata == MARK) { // Space just ended; Record time |
| 165 | + irparams.rawbuf[irparams.rawlen++] = irparams.timer; |
| 166 | + irparams.timer = 0; |
| 167 | + irparams.rcvstate = STATE_MARK; |
| 168 | + |
| 169 | + } else if (irparams.timer > GAP_TICKS) { // Space |
| 170 | + // A long Space, indicates gap between codes |
| 171 | + // Flag the current code as ready for processing |
| 172 | + // Switch to STOP |
| 173 | + // Don't reset timer; keep counting Space width |
| 174 | + irparams.rcvstate = STATE_STOP; |
| 175 | + } |
| 176 | + break; |
| 177 | + //...................................................................... |
| 178 | + case STATE_STOP: // Waiting; Measuring Gap |
| 179 | + if (irdata == MARK) irparams.timer = 0 ; // Reset gap timer |
| 180 | + break; |
| 181 | + //...................................................................... |
| 182 | + case STATE_OVERFLOW: // Flag up a read overflow; Stop the State Machine |
| 183 | + irparams.overflow = true; |
| 184 | + irparams.rcvstate = STATE_STOP; |
| 185 | + break; |
| 186 | + } |
| 187 | + |
| 188 | +#ifdef BLINKLED |
| 189 | + // If requested, flash LED while receiving IR data |
| 190 | + if (irparams.blinkflag) { |
| 191 | + if (irdata == MARK) |
| 192 | + if (irparams.blinkpin) digitalWrite(irparams.blinkpin, HIGH); // Turn user defined pin LED on |
| 193 | + else BLINKLED_ON() ; // if no user defined LED pin, turn default LED pin for the hardware on |
| 194 | + else if (irparams.blinkpin) digitalWrite(irparams.blinkpin, LOW); // Turn user defined pin LED on |
| 195 | + else BLINKLED_OFF() ; // if no user defined LED pin, turn default LED pin for the hardware on |
| 196 | + } |
| 197 | +#endif // BLINKLED |
| 198 | +} |
0 commit comments