|
18 | 18 | // Whynter A/C ARC-110WD added by Francesco Meschia
|
19 | 19 | //******************************************************************************
|
20 | 20 |
|
| 21 | +#include <avr/interrupt.h> |
| 22 | + |
21 | 23 | // Defining IR_GLOBAL here allows us to declare the instantiation of global variables
|
22 | 24 | #define IR_GLOBAL
|
23 | 25 | #include "IRremote.h"
|
@@ -88,3 +90,87 @@ int MATCH_SPACE (int measured_ticks, int desired_us)
|
88 | 90 | return ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS))
|
89 | 91 | && (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS)));
|
90 | 92 | }
|
| 93 | + |
| 94 | +//+============================================================================= |
| 95 | +// Interrupt Service Routine - Fires every 50uS |
| 96 | +// TIMER2 interrupt code to collect raw data. |
| 97 | +// Widths of alternating SPACE, MARK are recorded in rawbuf. |
| 98 | +// Recorded in ticks of 50uS [microseconds, 0.000050 seconds] |
| 99 | +// 'rawlen' counts the number of entries recorded so far. |
| 100 | +// First entry is the SPACE between transmissions. |
| 101 | +// As soon as a the first [SPACE] entry gets long: |
| 102 | +// Ready is set; State switches to IDLE; Timing of SPACE continues. |
| 103 | +// As soon as first MARK arrives: |
| 104 | +// Gap width is recorded; Ready is cleared; New logging starts |
| 105 | +// |
| 106 | +ISR (TIMER_INTR_NAME) |
| 107 | +{ |
| 108 | +TIMER_RESET; |
| 109 | + |
| 110 | +// Read if IR Receiver -> SPACE [xmt LED off] or a MARK [xmt LED on] |
| 111 | +// digitalRead() is very slow. Optimisation is possible, but makes the code unportable |
| 112 | +uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); |
| 113 | + |
| 114 | +irparams.timer++; // One more 50uS tick |
| 115 | +if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_OVERFLOW ; // Buffer overflow |
| 116 | + |
| 117 | +switch(irparams.rcvstate) { |
| 118 | +//...................................................................... |
| 119 | +case STATE_IDLE: // In the middle of a gap |
| 120 | +if (irdata == MARK) { |
| 121 | +if (irparams.timer < GAP_TICKS) { // Not big enough to be a gap. |
| 122 | +irparams.timer = 0; |
| 123 | + |
| 124 | +} else { |
| 125 | +// Gap just ended; Record duration; Start recording transmission |
| 126 | +irparams.overflow = false; |
| 127 | +irparams.rawlen = 0; |
| 128 | +irparams.rawbuf[irparams.rawlen++] = irparams.timer; |
| 129 | +irparams.timer = 0; |
| 130 | +irparams.rcvstate = STATE_MARK; |
| 131 | +} |
| 132 | +} |
| 133 | +break; |
| 134 | +//...................................................................... |
| 135 | +case STATE_MARK: // Timing Mark |
| 136 | +if (irdata == SPACE) { // Mark ended; Record time |
| 137 | +irparams.rawbuf[irparams.rawlen++] = irparams.timer; |
| 138 | +irparams.timer = 0; |
| 139 | +irparams.rcvstate = STATE_SPACE; |
| 140 | +} |
| 141 | +break; |
| 142 | +//...................................................................... |
| 143 | +case STATE_SPACE: // Timing Space |
| 144 | +if (irdata == MARK) { // Space just ended; Record time |
| 145 | +irparams.rawbuf[irparams.rawlen++] = irparams.timer; |
| 146 | +irparams.timer = 0; |
| 147 | +irparams.rcvstate = STATE_MARK; |
| 148 | + |
| 149 | +} else if (irparams.timer > GAP_TICKS) { // Space |
| 150 | +// A long Space, indicates gap between codes |
| 151 | +// Flag the current code as ready for processing |
| 152 | +// Switch to STOP |
| 153 | +// Don't reset timer; keep counting Space width |
| 154 | +irparams.rcvstate = STATE_STOP; |
| 155 | +} |
| 156 | +break; |
| 157 | +//...................................................................... |
| 158 | +case STATE_STOP: // Waiting; Measuring Gap |
| 159 | + if (irdata == MARK) irparams.timer = 0 ; // Reset gap timer |
| 160 | + break; |
| 161 | +//...................................................................... |
| 162 | +case STATE_OVERFLOW: // Flag up a read overflow; Stop the State Machine |
| 163 | +irparams.overflow = true; |
| 164 | +irparams.rcvstate = STATE_STOP; |
| 165 | + break; |
| 166 | +} |
| 167 | + |
| 168 | +// If requested, flash LED while receiving IR data |
| 169 | +if (irparams.blinkflag) { |
| 170 | +if (irdata == MARK) |
| 171 | +if (irparams.blinkpin) digitalWrite(irparams.blinkpin, HIGH); // Turn user defined pin LED on |
| 172 | +else BLINKLED_ON() ; // if no user defined LED pin, turn default LED pin for the hardware on |
| 173 | +else if (irparams.blinkpin) digitalWrite(irparams.blinkpin, LOW); // Turn user defined pin LED on |
| 174 | +else BLINKLED_OFF() ; // if no user defined LED pin, turn default LED pin for the hardware on |
| 175 | +} |
| 176 | +} |
0 commit comments