From acca27b698676db579b091c266b77467e701a974 Mon Sep 17 00:00:00 2001 From: Ken Shirriff Date: Sat, 15 May 2010 15:19:55 -0700 Subject: [PATCH] Fix CRLF issues. Many problems happen with git due to some machines liking CRLF at the end of lines, and others linking CR. To try to straighten this out, I'm using Unix-style LF (\n) as the line endings. To make sure your repository remains consistent, try: $ git config --global core.autocrlf input For details, see: http://help.github.com/dealing-with-lineendings/ --- IRremote.cpp | 1452 ++++++++++++++-------------- IRremote.h | 202 ++-- IRremoteInt.h | 258 ++--- examples/IRrecord/IRrecord.pde | 7 - examples/IRrecvDemo/IRrecvDemo.pde | 2 +- examples/IRrecvDump/IRrecvDump.pde | 2 +- examples/IRrelay/IRrelay.pde | 2 +- examples/IRsendDemo/IRsendDemo.pde | 1 - examples/IRtest/IRtest.pde | 380 ++++---- 9 files changed, 1149 insertions(+), 1157 deletions(-) diff --git a/IRremote.cpp b/IRremote.cpp index d91fb1b..0c3974a 100644 --- a/IRremote.cpp +++ b/IRremote.cpp @@ -1,726 +1,726 @@ -/* - * IRremote - * Version 0.11 August, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ - -#include "IRremote.h" -#include "IRremoteInt.h" - -// Provides ISR -#include - -volatile irparams_t irparams; - -// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging. -// To use them, set DEBUG in IRremoteInt.h -// Normally macros are used for efficiency -#ifdef DEBUG -int MATCH(int measured, int desired) { - Serial.print("Testing: "); - Serial.print(TICKS_LOW(desired), DEC); - Serial.print(" <= "); - Serial.print(measured, DEC); - Serial.print(" <= "); - Serial.println(TICKS_HIGH(desired), DEC); - return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired); -} - -int MATCH_MARK(int measured_ticks, int desired_us) { - Serial.print("Testing mark "); - Serial.print(measured_ticks * USECPERTICK, DEC); - Serial.print(" vs "); - Serial.print(desired_us, DEC); - Serial.print(": "); - Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC); - Serial.print(" <= "); - Serial.print(measured_ticks, DEC); - Serial.print(" <= "); - Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC); - return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS); -} - -int MATCH_SPACE(int measured_ticks, int desired_us) { - Serial.print("Testing space "); - Serial.print(measured_ticks * USECPERTICK, DEC); - Serial.print(" vs "); - Serial.print(desired_us, DEC); - Serial.print(": "); - Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC); - Serial.print(" <= "); - Serial.print(measured_ticks, DEC); - Serial.print(" <= "); - Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC); - return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS); -} -#endif - -void IRsend::sendNEC(unsigned long data, int nbits) -{ - enableIROut(38); - mark(NEC_HDR_MARK); - space(NEC_HDR_SPACE); - for (int i = 0; i < nbits; i++) { - if (data & TOPBIT) { - mark(NEC_BIT_MARK); - space(NEC_ONE_SPACE); - } - else { - mark(NEC_BIT_MARK); - space(NEC_ZERO_SPACE); - } - data <<= 1; - } - mark(NEC_BIT_MARK); - space(0); -} - -void IRsend::sendSony(unsigned long data, int nbits) { - enableIROut(40); - mark(SONY_HDR_MARK); - space(SONY_HDR_SPACE); - data = data << (32 - nbits); - for (int i = 0; i < nbits; i++) { - if (data & TOPBIT) { - mark(SONY_ONE_MARK); - space(SONY_HDR_SPACE); - } - else { - mark(SONY_ZERO_MARK); - space(SONY_HDR_SPACE); - } - data <<= 1; - } -} - -void IRsend::sendRaw(unsigned int buf[], int len, int hz) -{ - enableIROut(hz); - for (int i = 0; i < len; i++) { - if (i & 1) { - space(buf[i]); - } - else { - mark(buf[i]); - } - } - space(0); // Just to be sure -} - -// Note: first bit must be a one (start bit) -void IRsend::sendRC5(unsigned long data, int nbits) -{ - enableIROut(36); - data = data << (32 - nbits); - mark(RC5_T1); // First start bit - space(RC5_T1); // Second start bit - mark(RC5_T1); // Second start bit - for (int i = 0; i < nbits; i++) { - if (data & TOPBIT) { - space(RC5_T1); // 1 is space, then mark - mark(RC5_T1); - } - else { - mark(RC5_T1); - space(RC5_T1); - } - data <<= 1; - } - space(0); // Turn off at end -} - -// Caller needs to take care of flipping the toggle bit -void IRsend::sendRC6(unsigned long data, int nbits) -{ - enableIROut(36); - data = data << (32 - nbits); - mark(RC6_HDR_MARK); - space(RC6_HDR_SPACE); - mark(RC6_T1); // start bit - space(RC6_T1); - int t; - for (int i = 0; i < nbits; i++) { - if (i == 3) { - // double-wide trailer bit - t = 2 * RC6_T1; - } - else { - t = RC6_T1; - } - if (data & TOPBIT) { - mark(t); - space(t); - } - else { - space(t); - mark(t); - } - - data <<= 1; - } - space(0); // Turn off at end -} - -void IRsend::mark(int time) { - // Sends an IR mark for the specified number of microseconds. - // The mark output is modulated at the PWM frequency. - TCCR2A |= _BV(COM2B1); // Enable pin 3 PWM output - delayMicroseconds(time); -} - -/* Leave pin off for time (given in microseconds) */ -void IRsend::space(int time) { - // Sends an IR space for the specified number of microseconds. - // A space is no output, so the PWM output is disabled. - TCCR2A &= ~(_BV(COM2B1)); // Disable pin 3 PWM output - delayMicroseconds(time); -} - -void IRsend::enableIROut(int khz) { - // Enables IR output. The khz value controls the modulation frequency in kilohertz. - // The IR output will be on pin 3 (OC2B). - // This routine is designed for 36-40KHz; if you use it for other values, it's up to you - // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) - // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B - // controlling the duty cycle. - // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) - // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. - // A few hours staring at the ATmega documentation and this will all make sense. - // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details. - - - // Disable the Timer2 Interrupt (which is used for receiving IR) - TIMSK2 &= ~_BV(TOIE2); //Timer2 Overflow Interrupt - - pinMode(3, OUTPUT); - digitalWrite(3, LOW); // When not sending PWM, we want it low - - // COM2A = 00: disconnect OC2A - // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted - // WGM2 = 101: phase-correct PWM with OCRA as top - // CS2 = 000: no prescaling - TCCR2A = _BV(WGM20); - TCCR2B = _BV(WGM22) | _BV(CS20); - - // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A. - OCR2A = SYSCLOCK / 2 / khz / 1000; - OCR2B = OCR2A / 3; // 33% duty cycle -} - -IRrecv::IRrecv(int recvpin) -{ - irparams.recvpin = recvpin; - irparams.blinkflag = 0; -} - -// initialization -void IRrecv::enableIRIn() { - // setup pulse clock timer interrupt - TCCR2A = 0; // normal mode - - //Prescale /8 (16M/8 = 0.5 microseconds per tick) - // Therefore, the timer interval can range from 0.5 to 128 microseconds - // depending on the reset value (255 to 0) - cbi(TCCR2B,CS22); - sbi(TCCR2B,CS21); - cbi(TCCR2B,CS20); - - //Timer2 Overflow Interrupt Enable - sbi(TIMSK2,TOIE2); - - RESET_TIMER2; - - sei(); // enable interrupts - - // initialize state machine variables - irparams.rcvstate = STATE_IDLE; - irparams.rawlen = 0; - - - // set pin modes - pinMode(irparams.recvpin, INPUT); -} - -// enable/disable blinking of pin 13 on IR processing -void IRrecv::blink13(int blinkflag) -{ - irparams.blinkflag = blinkflag; - if (blinkflag) - pinMode(BLINKLED, OUTPUT); -} - -// TIMER2 interrupt code to collect raw data. -// Widths of alternating SPACE, MARK are recorded in rawbuf. -// Recorded in ticks of 50 microseconds. -// rawlen counts the number of entries recorded so far. -// First entry is the SPACE between transmissions. -// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues. -// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts -ISR(TIMER2_OVF_vect) -{ - RESET_TIMER2; - - uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); - - irparams.timer++; // One more 50us tick - if (irparams.rawlen >= RAWBUF) { - // Buffer overflow - irparams.rcvstate = STATE_STOP; - } - switch(irparams.rcvstate) { - case STATE_IDLE: // In the middle of a gap - if (irdata == MARK) { - if (irparams.timer < GAP_TICKS) { - // Not big enough to be a gap. - irparams.timer = 0; - } - else { - // gap just ended, record duration and start recording transmission - irparams.rawlen = 0; - irparams.rawbuf[irparams.rawlen++] = irparams.timer; - irparams.timer = 0; - irparams.rcvstate = STATE_MARK; - } - } - break; - case STATE_MARK: // timing MARK - if (irdata == SPACE) { // MARK ended, record time - irparams.rawbuf[irparams.rawlen++] = irparams.timer; - irparams.timer = 0; - irparams.rcvstate = STATE_SPACE; - } - break; - case STATE_SPACE: // timing SPACE - if (irdata == MARK) { // SPACE just ended, record it - irparams.rawbuf[irparams.rawlen++] = irparams.timer; - irparams.timer = 0; - irparams.rcvstate = STATE_MARK; - } - else { // SPACE - if (irparams.timer > GAP_TICKS) { - // big SPACE, indicates gap between codes - // Mark current code as ready for processing - // Switch to STOP - // Don't reset timer; keep counting space width - irparams.rcvstate = STATE_STOP; - } - } - break; - case STATE_STOP: // waiting, measuring gap - if (irdata == MARK) { // reset gap timer - irparams.timer = 0; - } - break; - } - - if (irparams.blinkflag) { - if (irdata == MARK) { - PORTB |= B00100000; // turn pin 13 LED on - } - else { - PORTB &= B11011111; // turn pin 13 LED off - } - } -} - -void IRrecv::resume() { - irparams.rcvstate = STATE_IDLE; - irparams.rawlen = 0; -} - - - -// Decodes the received IR message -// Returns 0 if no data ready, 1 if data ready. -// Results of decoding are stored in results -int IRrecv::decode(decode_results *results) { - results->rawbuf = irparams.rawbuf; - results->rawlen = irparams.rawlen; - if (irparams.rcvstate != STATE_STOP) { - return ERR; - } -#ifdef DEBUG - Serial.println("Attempting NEC decode"); -#endif - if (decodeNEC(results)) { - return DECODED; - } -#ifdef DEBUG - Serial.println("Attempting Sony decode"); -#endif - if (decodeSony(results)) { - return DECODED; - } -#ifdef DEBUG - Serial.println("Attempting RC5 decode"); -#endif - if (decodeRC5(results)) { - return DECODED; - } -#ifdef DEBUG - Serial.println("Attempting RC6 decode"); -#endif - if (decodeRC6(results)) { - return DECODED; - } - // decodeHash returns a hash on any input. - // Thus, it needs to be last in the list. - // If you add any decodes, add them before this. - if (decodeHash(results)) { - return DECODED; - } - // Throw away and start over - resume(); - return ERR; -} - -long IRrecv::decodeNEC(decode_results *results) { - long data = 0; - int offset = 1; // Skip first space - // Initial mark - if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) { - return ERR; - } - offset++; - // Check for repeat - if (irparams.rawlen == 4 && - MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) && - MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) { - results->bits = 0; - results->value = REPEAT; - results->decode_type = NEC; - return DECODED; - } - if (irparams.rawlen < 2 * NEC_BITS + 4) { - return ERR; - } - // Initial space - if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) { - return ERR; - } - offset++; - for (int i = 0; i < NEC_BITS; i++) { - if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) { - return ERR; - } - offset++; - if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) { - data = (data << 1) | 1; - } - else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) { - data <<= 1; - } - else { - return ERR; - } - offset++; - } - // Success - results->bits = NEC_BITS; - results->value = data; - results->decode_type = NEC; - return DECODED; -} - -long IRrecv::decodeSony(decode_results *results) { - long data = 0; - if (irparams.rawlen < 2 * SONY_BITS + 2) { - return ERR; - } - int offset = 1; // Skip first space - // Initial mark - if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) { - return ERR; - } - offset++; - - while (offset + 1 < irparams.rawlen) { - if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) { - break; - } - offset++; - if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) { - data = (data << 1) | 1; - } - else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) { - data <<= 1; - } - else { - return ERR; - } - offset++; - } - - // Success - results->bits = (offset - 1) / 2; - if (results->bits < 12) { - results->bits = 0; - return ERR; - } - results->value = data; - results->decode_type = SONY; - return DECODED; -} - -// Gets one undecoded level at a time from the raw buffer. -// The RC5/6 decoding is easier if the data is broken into time intervals. -// E.g. if the buffer has MARK for 2 time intervals and SPACE for 1, -// successive calls to getRClevel will return MARK, MARK, SPACE. -// offset and used are updated to keep track of the current position. -// t1 is the time interval for a single bit in microseconds. -// Returns -1 for error (measured time interval is not a multiple of t1). -int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) { - if (*offset >= results->rawlen) { - // After end of recorded buffer, assume SPACE. - return SPACE; - } - int width = results->rawbuf[*offset]; - int val = ((*offset) % 2) ? MARK : SPACE; - int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS; - - int avail; - if (MATCH(width, t1 + correction)) { - avail = 1; - } - else if (MATCH(width, 2*t1 + correction)) { - avail = 2; - } - else if (MATCH(width, 3*t1 + correction)) { - avail = 3; - } - else { - return -1; - } - - (*used)++; - if (*used >= avail) { - *used = 0; - (*offset)++; - } -#ifdef DEBUG - if (val == MARK) { - Serial.println("MARK"); - } - else { - Serial.println("SPACE"); - } -#endif - return val; -} - -long IRrecv::decodeRC5(decode_results *results) { - if (irparams.rawlen < MIN_RC5_SAMPLES + 2) { - return ERR; - } - int offset = 1; // Skip gap space - long data = 0; - int used = 0; - // Get start bits - if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; - if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR; - if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; - int nbits; - for (nbits = 0; offset < irparams.rawlen; nbits++) { - int levelA = getRClevel(results, &offset, &used, RC5_T1); - int levelB = getRClevel(results, &offset, &used, RC5_T1); - if (levelA == SPACE && levelB == MARK) { - // 1 bit - data = (data << 1) | 1; - } - else if (levelA == MARK && levelB == SPACE) { - // zero bit - data <<= 1; - } - else { - return ERR; - } - } - - // Success - results->bits = nbits; - results->value = data; - results->decode_type = RC5; - return DECODED; -} - -long IRrecv::decodeRC6(decode_results *results) { - if (results->rawlen < MIN_RC6_SAMPLES) { - return ERR; - } - int offset = 1; // Skip first space - // Initial mark - if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) { - return ERR; - } - offset++; - if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) { - return ERR; - } - offset++; - long data = 0; - int used = 0; - // Get start bit (1) - if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR; - if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR; - int nbits; - for (nbits = 0; offset < results->rawlen; nbits++) { - int levelA, levelB; // Next two levels - levelA = getRClevel(results, &offset, &used, RC6_T1); - if (nbits == 3) { - // T bit is double wide; make sure second half matches - if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR; - } - levelB = getRClevel(results, &offset, &used, RC6_T1); - if (nbits == 3) { - // T bit is double wide; make sure second half matches - if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR; - } - if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5 - // 1 bit - data = (data << 1) | 1; - } - else if (levelA == SPACE && levelB == MARK) { - // zero bit - data <<= 1; - } - else { - return ERR; // Error - } - } - // Success - results->bits = nbits; - results->value = data; - results->decode_type = RC6; - return DECODED; -} - -/* ----------------------------------------------------------------------- - * hashdecode - decode an arbitrary IR code. - * Instead of decoding using a standard encoding scheme - * (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value. - * - * The algorithm: look at the sequence of MARK signals, and see if each one - * is shorter (0), the same length (1), or longer (2) than the previous. - * Do the same with the SPACE signals. Hszh the resulting sequence of 0's, - * 1's, and 2's to a 32-bit value. This will give a unique value for each - * different code (probably), for most code systems. - * - * http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html - */ - -// Compare two tick values, returning 0 if newval is shorter, -// 1 if newval is equal, and 2 if newval is longer -// Use a tolerance of 20% -int IRrecv::compare(unsigned int oldval, unsigned int newval) { - if (newval < oldval * .8) { - return 0; - } - else if (oldval < newval * .8) { - return 2; - } - else { - return 1; - } -} - -// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param -#define FNV_PRIME_32 16777619 -#define FNV_BASIS_32 2166136261 - -/* Converts the raw code values into a 32-bit hash code. - * Hopefully this code is unique for each button. - * This isn't a "real" decoding, just an arbitrary value. - */ -long IRrecv::decodeHash(decode_results *results) { - // Require at least 6 samples to prevent triggering on noise - if (results->rawlen < 6) { - return ERR; - } - long hash = FNV_BASIS_32; - for (int i = 1; i+2 < results->rawlen; i++) { - int value = compare(results->rawbuf[i], results->rawbuf[i+2]); - // Add value into the hash - hash = (hash * FNV_PRIME_32) ^ value; - } - results->value = hash; - results->bits = 32; - results->decode_type = UNKNOWN; - return DECODED; -} - -/* Sharp and DISH support by Todd Treece - -The Dish send function needs to be repeated 4 times and the Sharp function -has the necessary repeats built in. I know that it's not consistent, -but I don't have the time to update my code. - -Here are the LIRC files that I found that seem to match the remote codes -from the oscilloscope: - -Sharp LCD TV: -http://lirc.sourceforge.net/remotes/sharp/GA538WJSA - -DISH NETWORK (echostar 301): -http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx - -For the DISH codes, only send the last for characters of the hex. -i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the -linked LIRC file. -*/ - -void IRsend::sendSharp(unsigned long data, int nbits) { - unsigned long invertdata = data ^ SHARP_TOGGLE_MASK; - enableIROut(38); - for (int i = 0; i < nbits; i++) { - if (data & 0x4000) { - mark(SHARP_BIT_MARK); - space(SHARP_ONE_SPACE); - } - else { - mark(SHARP_BIT_MARK); - space(SHARP_ZERO_SPACE); - } - data <<= 1; - } - - mark(SHARP_BIT_MARK); - space(SHARP_ZERO_SPACE); - delay(46); - for (int i = 0; i < nbits; i++) { - if (invertdata & 0x4000) { - mark(SHARP_BIT_MARK); - space(SHARP_ONE_SPACE); - } - else { - mark(SHARP_BIT_MARK); - space(SHARP_ZERO_SPACE); - } - invertdata <<= 1; - } - mark(SHARP_BIT_MARK); - space(SHARP_ZERO_SPACE); - delay(46); -} - -void IRsend::sendDISH(unsigned long data, int nbits) -{ - enableIROut(56); - mark(DISH_HDR_MARK); - space(DISH_HDR_SPACE); - for (int i = 0; i < nbits; i++) { - if (data & DISH_TOP_BIT) { - mark(DISH_BIT_MARK); - space(DISH_ONE_SPACE); - } - else { - mark(DISH_BIT_MARK); - space(DISH_ZERO_SPACE); - } - data <<= 1; - } -} +/* + * IRremote + * Version 0.11 August, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + */ + +#include "IRremote.h" +#include "IRremoteInt.h" + +// Provides ISR +#include + +volatile irparams_t irparams; + +// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging. +// To use them, set DEBUG in IRremoteInt.h +// Normally macros are used for efficiency +#ifdef DEBUG +int MATCH(int measured, int desired) { + Serial.print("Testing: "); + Serial.print(TICKS_LOW(desired), DEC); + Serial.print(" <= "); + Serial.print(measured, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired), DEC); + return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired); +} + +int MATCH_MARK(int measured_ticks, int desired_us) { + Serial.print("Testing mark "); + Serial.print(measured_ticks * USECPERTICK, DEC); + Serial.print(" vs "); + Serial.print(desired_us, DEC); + Serial.print(": "); + Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC); + Serial.print(" <= "); + Serial.print(measured_ticks, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC); + return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS); +} + +int MATCH_SPACE(int measured_ticks, int desired_us) { + Serial.print("Testing space "); + Serial.print(measured_ticks * USECPERTICK, DEC); + Serial.print(" vs "); + Serial.print(desired_us, DEC); + Serial.print(": "); + Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC); + Serial.print(" <= "); + Serial.print(measured_ticks, DEC); + Serial.print(" <= "); + Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC); + return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS); +} +#endif + +void IRsend::sendNEC(unsigned long data, int nbits) +{ + enableIROut(38); + mark(NEC_HDR_MARK); + space(NEC_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(NEC_BIT_MARK); + space(NEC_ONE_SPACE); + } + else { + mark(NEC_BIT_MARK); + space(NEC_ZERO_SPACE); + } + data <<= 1; + } + mark(NEC_BIT_MARK); + space(0); +} + +void IRsend::sendSony(unsigned long data, int nbits) { + enableIROut(40); + mark(SONY_HDR_MARK); + space(SONY_HDR_SPACE); + data = data << (32 - nbits); + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + mark(SONY_ONE_MARK); + space(SONY_HDR_SPACE); + } + else { + mark(SONY_ZERO_MARK); + space(SONY_HDR_SPACE); + } + data <<= 1; + } +} + +void IRsend::sendRaw(unsigned int buf[], int len, int hz) +{ + enableIROut(hz); + for (int i = 0; i < len; i++) { + if (i & 1) { + space(buf[i]); + } + else { + mark(buf[i]); + } + } + space(0); // Just to be sure +} + +// Note: first bit must be a one (start bit) +void IRsend::sendRC5(unsigned long data, int nbits) +{ + enableIROut(36); + data = data << (32 - nbits); + mark(RC5_T1); // First start bit + space(RC5_T1); // Second start bit + mark(RC5_T1); // Second start bit + for (int i = 0; i < nbits; i++) { + if (data & TOPBIT) { + space(RC5_T1); // 1 is space, then mark + mark(RC5_T1); + } + else { + mark(RC5_T1); + space(RC5_T1); + } + data <<= 1; + } + space(0); // Turn off at end +} + +// Caller needs to take care of flipping the toggle bit +void IRsend::sendRC6(unsigned long data, int nbits) +{ + enableIROut(36); + data = data << (32 - nbits); + mark(RC6_HDR_MARK); + space(RC6_HDR_SPACE); + mark(RC6_T1); // start bit + space(RC6_T1); + int t; + for (int i = 0; i < nbits; i++) { + if (i == 3) { + // double-wide trailer bit + t = 2 * RC6_T1; + } + else { + t = RC6_T1; + } + if (data & TOPBIT) { + mark(t); + space(t); + } + else { + space(t); + mark(t); + } + + data <<= 1; + } + space(0); // Turn off at end +} + +void IRsend::mark(int time) { + // Sends an IR mark for the specified number of microseconds. + // The mark output is modulated at the PWM frequency. + TCCR2A |= _BV(COM2B1); // Enable pin 3 PWM output + delayMicroseconds(time); +} + +/* Leave pin off for time (given in microseconds) */ +void IRsend::space(int time) { + // Sends an IR space for the specified number of microseconds. + // A space is no output, so the PWM output is disabled. + TCCR2A &= ~(_BV(COM2B1)); // Disable pin 3 PWM output + delayMicroseconds(time); +} + +void IRsend::enableIROut(int khz) { + // Enables IR output. The khz value controls the modulation frequency in kilohertz. + // The IR output will be on pin 3 (OC2B). + // This routine is designed for 36-40KHz; if you use it for other values, it's up to you + // to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.) + // TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B + // controlling the duty cycle. + // There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A) + // To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin. + // A few hours staring at the ATmega documentation and this will all make sense. + // See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details. + + + // Disable the Timer2 Interrupt (which is used for receiving IR) + TIMSK2 &= ~_BV(TOIE2); //Timer2 Overflow Interrupt + + pinMode(3, OUTPUT); + digitalWrite(3, LOW); // When not sending PWM, we want it low + + // COM2A = 00: disconnect OC2A + // COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted + // WGM2 = 101: phase-correct PWM with OCRA as top + // CS2 = 000: no prescaling + TCCR2A = _BV(WGM20); + TCCR2B = _BV(WGM22) | _BV(CS20); + + // The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A. + OCR2A = SYSCLOCK / 2 / khz / 1000; + OCR2B = OCR2A / 3; // 33% duty cycle +} + +IRrecv::IRrecv(int recvpin) +{ + irparams.recvpin = recvpin; + irparams.blinkflag = 0; +} + +// initialization +void IRrecv::enableIRIn() { + // setup pulse clock timer interrupt + TCCR2A = 0; // normal mode + + //Prescale /8 (16M/8 = 0.5 microseconds per tick) + // Therefore, the timer interval can range from 0.5 to 128 microseconds + // depending on the reset value (255 to 0) + cbi(TCCR2B,CS22); + sbi(TCCR2B,CS21); + cbi(TCCR2B,CS20); + + //Timer2 Overflow Interrupt Enable + sbi(TIMSK2,TOIE2); + + RESET_TIMER2; + + sei(); // enable interrupts + + // initialize state machine variables + irparams.rcvstate = STATE_IDLE; + irparams.rawlen = 0; + + + // set pin modes + pinMode(irparams.recvpin, INPUT); +} + +// enable/disable blinking of pin 13 on IR processing +void IRrecv::blink13(int blinkflag) +{ + irparams.blinkflag = blinkflag; + if (blinkflag) + pinMode(BLINKLED, OUTPUT); +} + +// TIMER2 interrupt code to collect raw data. +// Widths of alternating SPACE, MARK are recorded in rawbuf. +// Recorded in ticks of 50 microseconds. +// rawlen counts the number of entries recorded so far. +// First entry is the SPACE between transmissions. +// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues. +// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts +ISR(TIMER2_OVF_vect) +{ + RESET_TIMER2; + + uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); + + irparams.timer++; // One more 50us tick + if (irparams.rawlen >= RAWBUF) { + // Buffer overflow + irparams.rcvstate = STATE_STOP; + } + switch(irparams.rcvstate) { + case STATE_IDLE: // In the middle of a gap + if (irdata == MARK) { + if (irparams.timer < GAP_TICKS) { + // Not big enough to be a gap. + irparams.timer = 0; + } + else { + // gap just ended, record duration and start recording transmission + irparams.rawlen = 0; + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_MARK; + } + } + break; + case STATE_MARK: // timing MARK + if (irdata == SPACE) { // MARK ended, record time + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_SPACE; + } + break; + case STATE_SPACE: // timing SPACE + if (irdata == MARK) { // SPACE just ended, record it + irparams.rawbuf[irparams.rawlen++] = irparams.timer; + irparams.timer = 0; + irparams.rcvstate = STATE_MARK; + } + else { // SPACE + if (irparams.timer > GAP_TICKS) { + // big SPACE, indicates gap between codes + // Mark current code as ready for processing + // Switch to STOP + // Don't reset timer; keep counting space width + irparams.rcvstate = STATE_STOP; + } + } + break; + case STATE_STOP: // waiting, measuring gap + if (irdata == MARK) { // reset gap timer + irparams.timer = 0; + } + break; + } + + if (irparams.blinkflag) { + if (irdata == MARK) { + PORTB |= B00100000; // turn pin 13 LED on + } + else { + PORTB &= B11011111; // turn pin 13 LED off + } + } +} + +void IRrecv::resume() { + irparams.rcvstate = STATE_IDLE; + irparams.rawlen = 0; +} + + + +// Decodes the received IR message +// Returns 0 if no data ready, 1 if data ready. +// Results of decoding are stored in results +int IRrecv::decode(decode_results *results) { + results->rawbuf = irparams.rawbuf; + results->rawlen = irparams.rawlen; + if (irparams.rcvstate != STATE_STOP) { + return ERR; + } +#ifdef DEBUG + Serial.println("Attempting NEC decode"); +#endif + if (decodeNEC(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting Sony decode"); +#endif + if (decodeSony(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting RC5 decode"); +#endif + if (decodeRC5(results)) { + return DECODED; + } +#ifdef DEBUG + Serial.println("Attempting RC6 decode"); +#endif + if (decodeRC6(results)) { + return DECODED; + } + // decodeHash returns a hash on any input. + // Thus, it needs to be last in the list. + // If you add any decodes, add them before this. + if (decodeHash(results)) { + return DECODED; + } + // Throw away and start over + resume(); + return ERR; +} + +long IRrecv::decodeNEC(decode_results *results) { + long data = 0; + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) { + return ERR; + } + offset++; + // Check for repeat + if (irparams.rawlen == 4 && + MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) && + MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) { + results->bits = 0; + results->value = REPEAT; + results->decode_type = NEC; + return DECODED; + } + if (irparams.rawlen < 2 * NEC_BITS + 4) { + return ERR; + } + // Initial space + if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) { + return ERR; + } + offset++; + for (int i = 0; i < NEC_BITS; i++) { + if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) { + return ERR; + } + offset++; + if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) { + data = (data << 1) | 1; + } + else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + // Success + results->bits = NEC_BITS; + results->value = data; + results->decode_type = NEC; + return DECODED; +} + +long IRrecv::decodeSony(decode_results *results) { + long data = 0; + if (irparams.rawlen < 2 * SONY_BITS + 2) { + return ERR; + } + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) { + return ERR; + } + offset++; + + while (offset + 1 < irparams.rawlen) { + if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) { + break; + } + offset++; + if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) { + data = (data << 1) | 1; + } + else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) { + data <<= 1; + } + else { + return ERR; + } + offset++; + } + + // Success + results->bits = (offset - 1) / 2; + if (results->bits < 12) { + results->bits = 0; + return ERR; + } + results->value = data; + results->decode_type = SONY; + return DECODED; +} + +// Gets one undecoded level at a time from the raw buffer. +// The RC5/6 decoding is easier if the data is broken into time intervals. +// E.g. if the buffer has MARK for 2 time intervals and SPACE for 1, +// successive calls to getRClevel will return MARK, MARK, SPACE. +// offset and used are updated to keep track of the current position. +// t1 is the time interval for a single bit in microseconds. +// Returns -1 for error (measured time interval is not a multiple of t1). +int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) { + if (*offset >= results->rawlen) { + // After end of recorded buffer, assume SPACE. + return SPACE; + } + int width = results->rawbuf[*offset]; + int val = ((*offset) % 2) ? MARK : SPACE; + int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS; + + int avail; + if (MATCH(width, t1 + correction)) { + avail = 1; + } + else if (MATCH(width, 2*t1 + correction)) { + avail = 2; + } + else if (MATCH(width, 3*t1 + correction)) { + avail = 3; + } + else { + return -1; + } + + (*used)++; + if (*used >= avail) { + *used = 0; + (*offset)++; + } +#ifdef DEBUG + if (val == MARK) { + Serial.println("MARK"); + } + else { + Serial.println("SPACE"); + } +#endif + return val; +} + +long IRrecv::decodeRC5(decode_results *results) { + if (irparams.rawlen < MIN_RC5_SAMPLES + 2) { + return ERR; + } + int offset = 1; // Skip gap space + long data = 0; + int used = 0; + // Get start bits + if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; + if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR; + if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR; + int nbits; + for (nbits = 0; offset < irparams.rawlen; nbits++) { + int levelA = getRClevel(results, &offset, &used, RC5_T1); + int levelB = getRClevel(results, &offset, &used, RC5_T1); + if (levelA == SPACE && levelB == MARK) { + // 1 bit + data = (data << 1) | 1; + } + else if (levelA == MARK && levelB == SPACE) { + // zero bit + data <<= 1; + } + else { + return ERR; + } + } + + // Success + results->bits = nbits; + results->value = data; + results->decode_type = RC5; + return DECODED; +} + +long IRrecv::decodeRC6(decode_results *results) { + if (results->rawlen < MIN_RC6_SAMPLES) { + return ERR; + } + int offset = 1; // Skip first space + // Initial mark + if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) { + return ERR; + } + offset++; + if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) { + return ERR; + } + offset++; + long data = 0; + int used = 0; + // Get start bit (1) + if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR; + if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR; + int nbits; + for (nbits = 0; offset < results->rawlen; nbits++) { + int levelA, levelB; // Next two levels + levelA = getRClevel(results, &offset, &used, RC6_T1); + if (nbits == 3) { + // T bit is double wide; make sure second half matches + if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR; + } + levelB = getRClevel(results, &offset, &used, RC6_T1); + if (nbits == 3) { + // T bit is double wide; make sure second half matches + if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR; + } + if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5 + // 1 bit + data = (data << 1) | 1; + } + else if (levelA == SPACE && levelB == MARK) { + // zero bit + data <<= 1; + } + else { + return ERR; // Error + } + } + // Success + results->bits = nbits; + results->value = data; + results->decode_type = RC6; + return DECODED; +} + +/* ----------------------------------------------------------------------- + * hashdecode - decode an arbitrary IR code. + * Instead of decoding using a standard encoding scheme + * (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value. + * + * The algorithm: look at the sequence of MARK signals, and see if each one + * is shorter (0), the same length (1), or longer (2) than the previous. + * Do the same with the SPACE signals. Hszh the resulting sequence of 0's, + * 1's, and 2's to a 32-bit value. This will give a unique value for each + * different code (probably), for most code systems. + * + * http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html + */ + +// Compare two tick values, returning 0 if newval is shorter, +// 1 if newval is equal, and 2 if newval is longer +// Use a tolerance of 20% +int IRrecv::compare(unsigned int oldval, unsigned int newval) { + if (newval < oldval * .8) { + return 0; + } + else if (oldval < newval * .8) { + return 2; + } + else { + return 1; + } +} + +// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param +#define FNV_PRIME_32 16777619 +#define FNV_BASIS_32 2166136261 + +/* Converts the raw code values into a 32-bit hash code. + * Hopefully this code is unique for each button. + * This isn't a "real" decoding, just an arbitrary value. + */ +long IRrecv::decodeHash(decode_results *results) { + // Require at least 6 samples to prevent triggering on noise + if (results->rawlen < 6) { + return ERR; + } + long hash = FNV_BASIS_32; + for (int i = 1; i+2 < results->rawlen; i++) { + int value = compare(results->rawbuf[i], results->rawbuf[i+2]); + // Add value into the hash + hash = (hash * FNV_PRIME_32) ^ value; + } + results->value = hash; + results->bits = 32; + results->decode_type = UNKNOWN; + return DECODED; +} + +/* Sharp and DISH support by Todd Treece + +The Dish send function needs to be repeated 4 times and the Sharp function +has the necessary repeats built in. I know that it's not consistent, +but I don't have the time to update my code. + +Here are the LIRC files that I found that seem to match the remote codes +from the oscilloscope: + +Sharp LCD TV: +http://lirc.sourceforge.net/remotes/sharp/GA538WJSA + +DISH NETWORK (echostar 301): +http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx + +For the DISH codes, only send the last for characters of the hex. +i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the +linked LIRC file. +*/ + +void IRsend::sendSharp(unsigned long data, int nbits) { + unsigned long invertdata = data ^ SHARP_TOGGLE_MASK; + enableIROut(38); + for (int i = 0; i < nbits; i++) { + if (data & 0x4000) { + mark(SHARP_BIT_MARK); + space(SHARP_ONE_SPACE); + } + else { + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + } + data <<= 1; + } + + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + delay(46); + for (int i = 0; i < nbits; i++) { + if (invertdata & 0x4000) { + mark(SHARP_BIT_MARK); + space(SHARP_ONE_SPACE); + } + else { + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + } + invertdata <<= 1; + } + mark(SHARP_BIT_MARK); + space(SHARP_ZERO_SPACE); + delay(46); +} + +void IRsend::sendDISH(unsigned long data, int nbits) +{ + enableIROut(56); + mark(DISH_HDR_MARK); + space(DISH_HDR_SPACE); + for (int i = 0; i < nbits; i++) { + if (data & DISH_TOP_BIT) { + mark(DISH_BIT_MARK); + space(DISH_ONE_SPACE); + } + else { + mark(DISH_BIT_MARK); + space(DISH_ZERO_SPACE); + } + data <<= 1; + } +} diff --git a/IRremote.h b/IRremote.h index 9bb514a..ef98ceb 100644 --- a/IRremote.h +++ b/IRremote.h @@ -1,101 +1,101 @@ -/* - * IRremote - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ - -#ifndef IRremote_h -#define IRremote_h - -// The following are compile-time library options. -// If you change them, recompile the library. -// If DEBUG is defined, a lot of debugging output will be printed during decoding. -// TEST must be defined for the IRtest unittests to work. It will make some -// methods virtual, which will be slightly slower, which is why it is optional. -// #define DEBUG -// #define TEST - -// Results returned from the decoder -class decode_results { -public: - int decode_type; // NEC, SONY, RC5, UNKNOWN - unsigned long value; // Decoded value - int bits; // Number of bits in decoded value - volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks - int rawlen; // Number of records in rawbuf. -}; - -// Values for decode_type -#define NEC 1 -#define SONY 2 -#define RC5 3 -#define RC6 4 -#define DISH 5 -#define SHARP 6 -#define UNKNOWN -1 - -// Decoded value for NEC when a repeat code is received -#define REPEAT 0xffffffff - -// main class for receiving IR -class IRrecv -{ -public: - IRrecv(int recvpin); - void blink13(int blinkflag); - int decode(decode_results *results); - void enableIRIn(); - void resume(); -private: - // These are called by decode - int getRClevel(decode_results *results, int *offset, int *used, int t1); - long decodeNEC(decode_results *results); - long decodeSony(decode_results *results); - long decodeRC5(decode_results *results); - long decodeRC6(decode_results *results); - long decodeHash(decode_results *results); - int compare(unsigned int oldval, unsigned int newval); - -} -; - -// Only used for testing; can remove virtual for shorter code -#ifdef TEST -#define VIRTUAL virtual -#else -#define VIRTUAL -#endif - -class IRsend -{ -public: - IRsend() {} - void sendNEC(unsigned long data, int nbits); - void sendSony(unsigned long data, int nbits); - void sendRaw(unsigned int buf[], int len, int hz); - void sendRC5(unsigned long data, int nbits); - void sendRC6(unsigned long data, int nbits); - void sendDISH(unsigned long data, int nbits); - void sendSharp(unsigned long data, int nbits); - // private: - void enableIROut(int khz); - VIRTUAL void mark(int usec); - VIRTUAL void space(int usec); -} -; - -// Some useful constants - -#define USECPERTICK 50 // microseconds per clock interrupt tick -#define RAWBUF 76 // Length of raw duration buffer - -// Marks tend to be 100us too long, and spaces 100us too short -// when received due to sensor lag. -#define MARK_EXCESS 100 - -#endif +/* + * IRremote + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + */ + +#ifndef IRremote_h +#define IRremote_h + +// The following are compile-time library options. +// If you change them, recompile the library. +// If DEBUG is defined, a lot of debugging output will be printed during decoding. +// TEST must be defined for the IRtest unittests to work. It will make some +// methods virtual, which will be slightly slower, which is why it is optional. +// #define DEBUG +// #define TEST + +// Results returned from the decoder +class decode_results { +public: + int decode_type; // NEC, SONY, RC5, UNKNOWN + unsigned long value; // Decoded value + int bits; // Number of bits in decoded value + volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks + int rawlen; // Number of records in rawbuf. +}; + +// Values for decode_type +#define NEC 1 +#define SONY 2 +#define RC5 3 +#define RC6 4 +#define DISH 5 +#define SHARP 6 +#define UNKNOWN -1 + +// Decoded value for NEC when a repeat code is received +#define REPEAT 0xffffffff + +// main class for receiving IR +class IRrecv +{ +public: + IRrecv(int recvpin); + void blink13(int blinkflag); + int decode(decode_results *results); + void enableIRIn(); + void resume(); +private: + // These are called by decode + int getRClevel(decode_results *results, int *offset, int *used, int t1); + long decodeNEC(decode_results *results); + long decodeSony(decode_results *results); + long decodeRC5(decode_results *results); + long decodeRC6(decode_results *results); + long decodeHash(decode_results *results); + int compare(unsigned int oldval, unsigned int newval); + +} +; + +// Only used for testing; can remove virtual for shorter code +#ifdef TEST +#define VIRTUAL virtual +#else +#define VIRTUAL +#endif + +class IRsend +{ +public: + IRsend() {} + void sendNEC(unsigned long data, int nbits); + void sendSony(unsigned long data, int nbits); + void sendRaw(unsigned int buf[], int len, int hz); + void sendRC5(unsigned long data, int nbits); + void sendRC6(unsigned long data, int nbits); + void sendDISH(unsigned long data, int nbits); + void sendSharp(unsigned long data, int nbits); + // private: + void enableIROut(int khz); + VIRTUAL void mark(int usec); + VIRTUAL void space(int usec); +} +; + +// Some useful constants + +#define USECPERTICK 50 // microseconds per clock interrupt tick +#define RAWBUF 76 // Length of raw duration buffer + +// Marks tend to be 100us too long, and spaces 100us too short +// when received due to sensor lag. +#define MARK_EXCESS 100 + +#endif diff --git a/IRremoteInt.h b/IRremoteInt.h index 4325972..74491b8 100644 --- a/IRremoteInt.h +++ b/IRremoteInt.h @@ -1,129 +1,129 @@ -/* - * IRremote - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html - * - * Interrupt code based on NECIRrcv by Joe Knapp - * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 - * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ - */ - -#ifndef IRremoteint_h -#define IRremoteint_h - -#include - -#define CLKFUDGE 5 // fudge factor for clock interrupt overhead -#define CLK 256 // max value for clock (timer 2) -#define PRESCALE 8 // timer2 clock prescale -#define SYSCLOCK 16000000 // main Arduino clock -#define CLKSPERUSEC (SYSCLOCK/PRESCALE/1000000) // timer clocks per microsecond - -#define ERR 0 -#define DECODED 1 - -#define BLINKLED 13 - -// defines for setting and clearing register bits -#ifndef cbi -#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) -#endif -#ifndef sbi -#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) -#endif - -// clock timer reset value -#define INIT_TIMER_COUNT2 (CLK - USECPERTICK*CLKSPERUSEC + CLKFUDGE) -#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT2 - -// pulse parameters in usec -#define NEC_HDR_MARK 9000 -#define NEC_HDR_SPACE 4500 -#define NEC_BIT_MARK 560 -#define NEC_ONE_SPACE 1600 -#define NEC_ZERO_SPACE 560 -#define NEC_RPT_SPACE 2250 - -#define SONY_HDR_MARK 2400 -#define SONY_HDR_SPACE 600 -#define SONY_ONE_MARK 1200 -#define SONY_ZERO_MARK 600 -#define SONY_RPT_LENGTH 45000 - -#define RC5_T1 889 -#define RC5_RPT_LENGTH 46000 - -#define RC6_HDR_MARK 2666 -#define RC6_HDR_SPACE 889 -#define RC6_T1 444 -#define RC6_RPT_LENGTH 46000 - -#define SHARP_BIT_MARK 245 -#define SHARP_ONE_SPACE 1805 -#define SHARP_ZERO_SPACE 795 -#define SHARP_GAP 600000 -#define SHARP_TOGGLE_MASK 0x3FF -#define SHARP_RPT_SPACE 3000 - -#define DISH_HDR_MARK 400 -#define DISH_HDR_SPACE 6100 -#define DISH_BIT_MARK 400 -#define DISH_ONE_SPACE 1700 -#define DISH_ZERO_SPACE 2800 -#define DISH_RPT_SPACE 6200 -#define DISH_TOP_BIT 0x8000 - -#define SHARP_BITS 15 -#define DISH_BITS 16 - -#define TOLERANCE 25 // percent tolerance in measurements -#define LTOL (1.0 - TOLERANCE/100.) -#define UTOL (1.0 + TOLERANCE/100.) - -#define _GAP 5000 // Minimum map between transmissions -#define GAP_TICKS (_GAP/USECPERTICK) - -#define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK)) -#define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1)) - -#ifndef DEBUG -#define MATCH(measured_ticks, desired_us) ((measured_ticks) >= TICKS_LOW(desired_us) && (measured_ticks) <= TICKS_HIGH(desired_us)) -#define MATCH_MARK(measured_ticks, desired_us) MATCH(measured_ticks, (desired_us) + MARK_EXCESS) -#define MATCH_SPACE(measured_ticks, desired_us) MATCH((measured_ticks), (desired_us) - MARK_EXCESS) -// Debugging versions are in IRremote.cpp -#endif - -// receiver states -#define STATE_IDLE 2 -#define STATE_MARK 3 -#define STATE_SPACE 4 -#define STATE_STOP 5 - -// information for the interrupt handler -typedef struct { - uint8_t recvpin; // pin for IR data from detector - uint8_t rcvstate; // state machine - uint8_t blinkflag; // TRUE to enable blinking of pin 13 on IR processing - unsigned int timer; // state timer, counts 50uS ticks. - unsigned int rawbuf[RAWBUF]; // raw data - uint8_t rawlen; // counter of entries in rawbuf -} -irparams_t; - -// Defined in IRremote.cpp -extern volatile irparams_t irparams; - -// IR detector output is active low -#define MARK 0 -#define SPACE 1 - -#define TOPBIT 0x80000000 - -#define NEC_BITS 32 -#define SONY_BITS 12 -#define MIN_RC5_SAMPLES 11 -#define MIN_RC6_SAMPLES 1 - -#endif - +/* + * IRremote + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html + * + * Interrupt code based on NECIRrcv by Joe Knapp + * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556 + * Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/ + */ + +#ifndef IRremoteint_h +#define IRremoteint_h + +#include + +#define CLKFUDGE 5 // fudge factor for clock interrupt overhead +#define CLK 256 // max value for clock (timer 2) +#define PRESCALE 8 // timer2 clock prescale +#define SYSCLOCK 16000000 // main Arduino clock +#define CLKSPERUSEC (SYSCLOCK/PRESCALE/1000000) // timer clocks per microsecond + +#define ERR 0 +#define DECODED 1 + +#define BLINKLED 13 + +// defines for setting and clearing register bits +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +// clock timer reset value +#define INIT_TIMER_COUNT2 (CLK - USECPERTICK*CLKSPERUSEC + CLKFUDGE) +#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT2 + +// pulse parameters in usec +#define NEC_HDR_MARK 9000 +#define NEC_HDR_SPACE 4500 +#define NEC_BIT_MARK 560 +#define NEC_ONE_SPACE 1600 +#define NEC_ZERO_SPACE 560 +#define NEC_RPT_SPACE 2250 + +#define SONY_HDR_MARK 2400 +#define SONY_HDR_SPACE 600 +#define SONY_ONE_MARK 1200 +#define SONY_ZERO_MARK 600 +#define SONY_RPT_LENGTH 45000 + +#define RC5_T1 889 +#define RC5_RPT_LENGTH 46000 + +#define RC6_HDR_MARK 2666 +#define RC6_HDR_SPACE 889 +#define RC6_T1 444 +#define RC6_RPT_LENGTH 46000 + +#define SHARP_BIT_MARK 245 +#define SHARP_ONE_SPACE 1805 +#define SHARP_ZERO_SPACE 795 +#define SHARP_GAP 600000 +#define SHARP_TOGGLE_MASK 0x3FF +#define SHARP_RPT_SPACE 3000 + +#define DISH_HDR_MARK 400 +#define DISH_HDR_SPACE 6100 +#define DISH_BIT_MARK 400 +#define DISH_ONE_SPACE 1700 +#define DISH_ZERO_SPACE 2800 +#define DISH_RPT_SPACE 6200 +#define DISH_TOP_BIT 0x8000 + +#define SHARP_BITS 15 +#define DISH_BITS 16 + +#define TOLERANCE 25 // percent tolerance in measurements +#define LTOL (1.0 - TOLERANCE/100.) +#define UTOL (1.0 + TOLERANCE/100.) + +#define _GAP 5000 // Minimum map between transmissions +#define GAP_TICKS (_GAP/USECPERTICK) + +#define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK)) +#define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1)) + +#ifndef DEBUG +#define MATCH(measured_ticks, desired_us) ((measured_ticks) >= TICKS_LOW(desired_us) && (measured_ticks) <= TICKS_HIGH(desired_us)) +#define MATCH_MARK(measured_ticks, desired_us) MATCH(measured_ticks, (desired_us) + MARK_EXCESS) +#define MATCH_SPACE(measured_ticks, desired_us) MATCH((measured_ticks), (desired_us) - MARK_EXCESS) +// Debugging versions are in IRremote.cpp +#endif + +// receiver states +#define STATE_IDLE 2 +#define STATE_MARK 3 +#define STATE_SPACE 4 +#define STATE_STOP 5 + +// information for the interrupt handler +typedef struct { + uint8_t recvpin; // pin for IR data from detector + uint8_t rcvstate; // state machine + uint8_t blinkflag; // TRUE to enable blinking of pin 13 on IR processing + unsigned int timer; // state timer, counts 50uS ticks. + unsigned int rawbuf[RAWBUF]; // raw data + uint8_t rawlen; // counter of entries in rawbuf +} +irparams_t; + +// Defined in IRremote.cpp +extern volatile irparams_t irparams; + +// IR detector output is active low +#define MARK 0 +#define SPACE 1 + +#define TOPBIT 0x80000000 + +#define NEC_BITS 32 +#define SONY_BITS 12 +#define MIN_RC5_SAMPLES 11 +#define MIN_RC6_SAMPLES 1 + +#endif + diff --git a/examples/IRrecord/IRrecord.pde b/examples/IRrecord/IRrecord.pde index a1cf878..caf86de 100644 --- a/examples/IRrecord/IRrecord.pde +++ b/examples/IRrecord/IRrecord.pde @@ -165,10 +165,3 @@ void loop() { } lastButtonState = buttonState; } - - - - - - - diff --git a/examples/IRrecvDemo/IRrecvDemo.pde b/examples/IRrecvDemo/IRrecvDemo.pde index 14982d8..f7b45b8 100644 --- a/examples/IRrecvDemo/IRrecvDemo.pde +++ b/examples/IRrecvDemo/IRrecvDemo.pde @@ -25,4 +25,4 @@ void loop() { Serial.println(results.value, HEX); irrecv.resume(); // Receive the next value } -} +} diff --git a/examples/IRrecvDump/IRrecvDump.pde b/examples/IRrecvDump/IRrecvDump.pde index 93495b7..7fd52d0 100644 --- a/examples/IRrecvDump/IRrecvDump.pde +++ b/examples/IRrecvDump/IRrecvDump.pde @@ -69,4 +69,4 @@ void loop() { dump(&results); irrecv.resume(); // Receive the next value } -} +} diff --git a/examples/IRrelay/IRrelay.pde b/examples/IRrelay/IRrelay.pde index f333133..046fb5f 100644 --- a/examples/IRrelay/IRrelay.pde +++ b/examples/IRrelay/IRrelay.pde @@ -82,4 +82,4 @@ void loop() { last = millis(); irrecv.resume(); // Receive the next value } -} +} diff --git a/examples/IRsendDemo/IRsendDemo.pde b/examples/IRsendDemo/IRsendDemo.pde index db111cb..a21af31 100644 --- a/examples/IRsendDemo/IRsendDemo.pde +++ b/examples/IRsendDemo/IRsendDemo.pde @@ -23,4 +23,3 @@ void loop() { } } } - diff --git a/examples/IRtest/IRtest.pde b/examples/IRtest/IRtest.pde index b1cb522..4845a4a 100644 --- a/examples/IRtest/IRtest.pde +++ b/examples/IRtest/IRtest.pde @@ -1,190 +1,190 @@ -/* - * IRremote: IRtest unittest - * Version 0.1 July, 2009 - * Copyright 2009 Ken Shirriff - * http://arcfn.com - * - * Note: to run these tests, edit IRremote/IRremote.h to add "#define TEST" - * You must then recompile the library by removing IRremote.o and restarting - * the arduino IDE. - */ - -#include -#include - -// Dumps out the decode_results structure. -// Call this after IRrecv::decode() -// void * to work around compiler issue -//void dump(void *v) { -// decode_results *results = (decode_results *)v -void dump(decode_results *results) { - int count = results->rawlen; - if (results->decode_type == UNKNOWN) { - Serial.println("Could not decode message"); - } - else { - if (results->decode_type == NEC) { - Serial.print("Decoded NEC: "); - } - else if (results->decode_type == SONY) { - Serial.print("Decoded SONY: "); - } - else if (results->decode_type == RC5) { - Serial.print("Decoded RC5: "); - } - else if (results->decode_type == RC6) { - Serial.print("Decoded RC6: "); - } - Serial.print(results->value, HEX); - Serial.print(" ("); - Serial.print(results->bits, DEC); - Serial.println(" bits)"); - } - Serial.print("Raw ("); - Serial.print(count, DEC); - Serial.print("): "); - - for (int i = 0; i < count; i++) { - if ((i % 2) == 1) { - Serial.print(results->rawbuf[i]*USECPERTICK, DEC); - } - else { - Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); - } - Serial.print(" "); - } - Serial.println(""); -} - -IRrecv irrecv(0); -decode_results results; - -class IRsendDummy : -public IRsend -{ -public: - // For testing, just log the marks/spaces -#define SENDLOG_LEN 128 - int sendlog[SENDLOG_LEN]; - int sendlogcnt; - IRsendDummy() : - IRsend() { - } - void reset() { - sendlogcnt = 0; - } - void mark(int time) { - sendlog[sendlogcnt] = time; - if (sendlogcnt < SENDLOG_LEN) sendlogcnt++; - } - void space(int time) { - sendlog[sendlogcnt] = -time; - if (sendlogcnt < SENDLOG_LEN) sendlogcnt++; - } - // Copies the dummy buf into the interrupt buf - void useDummyBuf() { - int last = SPACE; - irparams.rcvstate = STATE_STOP; - irparams.rawlen = 1; // Skip the gap - for (int i = 0 ; i < sendlogcnt; i++) { - if (sendlog[i] < 0) { - if (last == MARK) { - // New space - irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK; - last = SPACE; - } - else { - // More space - irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK; - } - } - else if (sendlog[i] > 0) { - if (last == SPACE) { - // New mark - irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK; - last = MARK; - } - else { - // More mark - irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK; - } - } - } - if (irparams.rawlen % 2) { - irparams.rawlen--; // Remove trailing space - } - } -}; - -IRsendDummy irsenddummy; - -void verify(unsigned long val, int bits, int type) { - irsenddummy.useDummyBuf(); - irrecv.decode(&results); - Serial.print("Testing "); - Serial.print(val, HEX); - if (results.value == val && results.bits == bits && results.decode_type == type) { - Serial.println(": OK"); - } - else { - Serial.println(": Error"); - dump(&results); - } -} - -void testNEC(unsigned long val, int bits) { - irsenddummy.reset(); - irsenddummy.sendNEC(val, bits); - verify(val, bits, NEC); -} -void testSony(unsigned long val, int bits) { - irsenddummy.reset(); - irsenddummy.sendSony(val, bits); - verify(val, bits, SONY); -} -void testRC5(unsigned long val, int bits) { - irsenddummy.reset(); - irsenddummy.sendRC5(val, bits); - verify(val, bits, RC5); -} -void testRC6(unsigned long val, int bits) { - irsenddummy.reset(); - irsenddummy.sendRC6(val, bits); - verify(val, bits, RC6); -} - -void test() { - Serial.println("NEC tests"); - testNEC(0x00000000, 32); - testNEC(0xffffffff, 32); - testNEC(0xaaaaaaaa, 32); - testNEC(0x55555555, 32); - testNEC(0x12345678, 32); - Serial.println("Sony tests"); - testSony(0xfff, 12); - testSony(0x000, 12); - testSony(0xaaa, 12); - testSony(0x555, 12); - testSony(0x123, 12); - Serial.println("RC5 tests"); - testRC5(0xfff, 12); - testRC5(0x000, 12); - testRC5(0xaaa, 12); - testRC5(0x555, 12); - testRC5(0x123, 12); - Serial.println("RC6 tests"); - testRC6(0xfffff, 20); - testRC6(0x00000, 20); - testRC6(0xaaaaa, 20); - testRC6(0x55555, 20); - testRC6(0x12345, 20); -} - -void setup() -{ - Serial.begin(9600); - test(); -} - -void loop() { -} +/* + * IRremote: IRtest unittest + * Version 0.1 July, 2009 + * Copyright 2009 Ken Shirriff + * http://arcfn.com + * + * Note: to run these tests, edit IRremote/IRremote.h to add "#define TEST" + * You must then recompile the library by removing IRremote.o and restarting + * the arduino IDE. + */ + +#include +#include + +// Dumps out the decode_results structure. +// Call this after IRrecv::decode() +// void * to work around compiler issue +//void dump(void *v) { +// decode_results *results = (decode_results *)v +void dump(decode_results *results) { + int count = results->rawlen; + if (results->decode_type == UNKNOWN) { + Serial.println("Could not decode message"); + } + else { + if (results->decode_type == NEC) { + Serial.print("Decoded NEC: "); + } + else if (results->decode_type == SONY) { + Serial.print("Decoded SONY: "); + } + else if (results->decode_type == RC5) { + Serial.print("Decoded RC5: "); + } + else if (results->decode_type == RC6) { + Serial.print("Decoded RC6: "); + } + Serial.print(results->value, HEX); + Serial.print(" ("); + Serial.print(results->bits, DEC); + Serial.println(" bits)"); + } + Serial.print("Raw ("); + Serial.print(count, DEC); + Serial.print("): "); + + for (int i = 0; i < count; i++) { + if ((i % 2) == 1) { + Serial.print(results->rawbuf[i]*USECPERTICK, DEC); + } + else { + Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); + } + Serial.print(" "); + } + Serial.println(""); +} + +IRrecv irrecv(0); +decode_results results; + +class IRsendDummy : +public IRsend +{ +public: + // For testing, just log the marks/spaces +#define SENDLOG_LEN 128 + int sendlog[SENDLOG_LEN]; + int sendlogcnt; + IRsendDummy() : + IRsend() { + } + void reset() { + sendlogcnt = 0; + } + void mark(int time) { + sendlog[sendlogcnt] = time; + if (sendlogcnt < SENDLOG_LEN) sendlogcnt++; + } + void space(int time) { + sendlog[sendlogcnt] = -time; + if (sendlogcnt < SENDLOG_LEN) sendlogcnt++; + } + // Copies the dummy buf into the interrupt buf + void useDummyBuf() { + int last = SPACE; + irparams.rcvstate = STATE_STOP; + irparams.rawlen = 1; // Skip the gap + for (int i = 0 ; i < sendlogcnt; i++) { + if (sendlog[i] < 0) { + if (last == MARK) { + // New space + irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK; + last = SPACE; + } + else { + // More space + irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK; + } + } + else if (sendlog[i] > 0) { + if (last == SPACE) { + // New mark + irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK; + last = MARK; + } + else { + // More mark + irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK; + } + } + } + if (irparams.rawlen % 2) { + irparams.rawlen--; // Remove trailing space + } + } +}; + +IRsendDummy irsenddummy; + +void verify(unsigned long val, int bits, int type) { + irsenddummy.useDummyBuf(); + irrecv.decode(&results); + Serial.print("Testing "); + Serial.print(val, HEX); + if (results.value == val && results.bits == bits && results.decode_type == type) { + Serial.println(": OK"); + } + else { + Serial.println(": Error"); + dump(&results); + } +} + +void testNEC(unsigned long val, int bits) { + irsenddummy.reset(); + irsenddummy.sendNEC(val, bits); + verify(val, bits, NEC); +} +void testSony(unsigned long val, int bits) { + irsenddummy.reset(); + irsenddummy.sendSony(val, bits); + verify(val, bits, SONY); +} +void testRC5(unsigned long val, int bits) { + irsenddummy.reset(); + irsenddummy.sendRC5(val, bits); + verify(val, bits, RC5); +} +void testRC6(unsigned long val, int bits) { + irsenddummy.reset(); + irsenddummy.sendRC6(val, bits); + verify(val, bits, RC6); +} + +void test() { + Serial.println("NEC tests"); + testNEC(0x00000000, 32); + testNEC(0xffffffff, 32); + testNEC(0xaaaaaaaa, 32); + testNEC(0x55555555, 32); + testNEC(0x12345678, 32); + Serial.println("Sony tests"); + testSony(0xfff, 12); + testSony(0x000, 12); + testSony(0xaaa, 12); + testSony(0x555, 12); + testSony(0x123, 12); + Serial.println("RC5 tests"); + testRC5(0xfff, 12); + testRC5(0x000, 12); + testRC5(0xaaa, 12); + testRC5(0x555, 12); + testRC5(0x123, 12); + Serial.println("RC6 tests"); + testRC6(0xfffff, 20); + testRC6(0x00000, 20); + testRC6(0xaaaaa, 20); + testRC6(0x55555, 20); + testRC6(0x12345, 20); +} + +void setup() +{ + Serial.begin(9600); + test(); +} + +void loop() { +}