Bit more code cleanup

This commit is contained in:
Bluechip
2015-06-20 14:42:59 +01:00
parent 88e243fe06
commit a1cf782c44
5 changed files with 131 additions and 121 deletions

View File

@@ -31,50 +31,66 @@
// ...but every time i reduce these functions down to macros, the decoders stop working!!?? // ...but every time i reduce these functions down to macros, the decoders stop working!!??
//+============================================================================= //+=============================================================================
// The match functions were (apparently) originally MACROs to improve code speed
// (although this would have bloated the code size) hence the names being CAPS
// A later release implemented debug output and so they needed to be converted
// to functions.
// I tried to implement a dual-compile mode (DEBUG/non-DEBUG) but for some
// reason, no matter what I did I could not get them to function as macros again.
// I have found a *lot* of bugs in the Arduino compiler over the last few weeks,
// and I am currently assuming that one of these bugs is my problem.
// I may revisit this code at a later date and look at the assembler produced
// in a hope of finding out what is going on, but for now they will remain as
// functions even in non-DEBUG mode
//
int MATCH (int measured, int desired) int MATCH (int measured, int desired)
{ {
DBG_PRINT("Testing: "); DBG_PRINT("Testing: ");
DBG_PRINT(TICKS_LOW(desired), DEC); DBG_PRINT(TICKS_LOW(desired), DEC);
DBG_PRINT(" <= "); DBG_PRINT(" <= ");
DBG_PRINT(measured, DEC); DBG_PRINT(measured, DEC);
DBG_PRINT(" <= "); DBG_PRINT(" <= ");
DBG_PRINTLN(TICKS_HIGH(desired), DEC); DBG_PRINTLN(TICKS_HIGH(desired), DEC);
return ((measured >= TICKS_LOW(desired)) && (measured <= TICKS_HIGH(desired))); return ((measured >= TICKS_LOW(desired)) && (measured <= TICKS_HIGH(desired)));
} }
//+============================================================================= //+========================================================
// Marks tend to be 100us too long when received due to sensor lag.
//
int MATCH_MARK (int measured_ticks, int desired_us) int MATCH_MARK (int measured_ticks, int desired_us)
{ {
DBG_PRINT("Testing mark "); DBG_PRINT("Testing mark ");
DBG_PRINT(measured_ticks * USECPERTICK, DEC); DBG_PRINT(measured_ticks * USECPERTICK, DEC);
DBG_PRINT(" vs "); DBG_PRINT(" vs ");
DBG_PRINT(desired_us, DEC); DBG_PRINT(desired_us, DEC);
DBG_PRINT(": "); DBG_PRINT(": ");
DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS), DEC); DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
DBG_PRINT(" <= "); DBG_PRINT(" <= ");
DBG_PRINT(measured_ticks, DEC); DBG_PRINT(measured_ticks, DEC);
DBG_PRINT(" <= "); DBG_PRINT(" <= ");
DBG_PRINTLN(TICKS_HIGH(desired_us + MARK_EXCESS), DEC); DBG_PRINTLN(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
return ((measured_ticks >= TICKS_LOW (desired_us + MARK_EXCESS)) return ((measured_ticks >= TICKS_LOW (desired_us + MARK_EXCESS))
&& (measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS))); && (measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS)));
} }
//+============================================================================= //+========================================================
// Spaces tend to be 100us too short when received due to sensor lag.
//
int MATCH_SPACE (int measured_ticks, int desired_us) int MATCH_SPACE (int measured_ticks, int desired_us)
{ {
DBG_PRINT("Testing space "); DBG_PRINT("Testing space ");
DBG_PRINT(measured_ticks * USECPERTICK, DEC); DBG_PRINT(measured_ticks * USECPERTICK, DEC);
DBG_PRINT(" vs "); DBG_PRINT(" vs ");
DBG_PRINT(desired_us, DEC); DBG_PRINT(desired_us, DEC);
DBG_PRINT(": "); DBG_PRINT(": ");
DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS), DEC); DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
DBG_PRINT(" <= "); DBG_PRINT(" <= ");
DBG_PRINT(measured_ticks, DEC); DBG_PRINT(measured_ticks, DEC);
DBG_PRINT(" <= "); DBG_PRINT(" <= ");
DBG_PRINTLN(TICKS_HIGH(desired_us - MARK_EXCESS), DEC); DBG_PRINTLN(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
return ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS)) return ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS))
&& (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS))); && (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS)));
} }

View File

@@ -1,3 +1,6 @@
#ifndef IRremote_h
#define IRremote_h
#define DEBUG #define DEBUG
#undef DEBUG #undef DEBUG
@@ -16,10 +19,7 @@ int MATCH (int measured, int desired) ;
# define DBG_PRINTLN(...) # define DBG_PRINTLN(...)
#endif #endif
///int MATCH (int measured, int desired); //------------------------------------------------------------------------------
///int MATCH_MARK (int measured_ticks, int desired_us);
///int MATCH_SPACE (int measured_ticks, int desired_us);
#define SEND_NEC #define SEND_NEC
#define DECODE_NEC #define DECODE_NEC
#define SEND_WHYNTER #define SEND_WHYNTER
@@ -60,9 +60,6 @@ int MATCH (int measured, int desired) ;
* Whynter A/C ARC-110WD added by Francesco Meschia * Whynter A/C ARC-110WD added by Francesco Meschia
*/ */
#ifndef IRremote_h
#define IRremote_h
// The following are compile-time library options. // The following are compile-time library options.
// If you change them, recompile the library. // If you change them, recompile the library.
// If DEBUG is defined, a lot of debugging output will be printed during decoding. // If DEBUG is defined, a lot of debugging output will be printed during decoding.
@@ -71,6 +68,7 @@ int MATCH (int measured, int desired) ;
//#define DEBUG //#define DEBUG
// #define TEST // #define TEST
//------------------------------------------------------------------------------
enum decode_type_t { enum decode_type_t {
UNKNOWN = -1, UNKNOWN = -1,
UNUSED = 0, UNUSED = 0,
@@ -90,6 +88,7 @@ enum decode_type_t {
AIWA_RC_T501 = 14, AIWA_RC_T501 = 14,
}; };
//------------------------------------------------------------------------------
// Results returned from the decoder // Results returned from the decoder
class decode_results { class decode_results {
public: public:
@@ -104,9 +103,11 @@ public:
int rawlen; // Number of records in rawbuf. int rawlen; // Number of records in rawbuf.
}; };
//------------------------------------------------------------------------------
// Decoded value for NEC when a repeat code is received // Decoded value for NEC when a repeat code is received
#define REPEAT 0xffffffff #define REPEAT 0xffffffff
//------------------------------------------------------------------------------
// main class for receiving IR // main class for receiving IR
class IRrecv class IRrecv
{ {
@@ -163,6 +164,7 @@ private:
} ; } ;
//------------------------------------------------------------------------------
// Only used for testing; can remove virtual for shorter code // Only used for testing; can remove virtual for shorter code
#ifdef TEST #ifdef TEST
#define VIRTUAL virtual #define VIRTUAL virtual
@@ -170,6 +172,7 @@ private:
#define VIRTUAL #define VIRTUAL
#endif #endif
//------------------------------------------------------------------------------
class IRsend class IRsend
{ {
public: public:
@@ -217,6 +220,7 @@ public:
VIRTUAL void space(int usec); VIRTUAL void space(int usec);
} ; } ;
//------------------------------------------------------------------------------
// Some useful constants // Some useful constants
#define USECPERTICK 50 // microseconds per clock interrupt tick #define USECPERTICK 50 // microseconds per clock interrupt tick

View File

@@ -17,18 +17,21 @@
#ifndef IRremoteint_h #ifndef IRremoteint_h
#define IRremoteint_h #define IRremoteint_h
//------------------------------------------------------------------------------
#ifdef IR_GLOBAL #ifdef IR_GLOBAL
# define EXTERN # define EXTERN
#else #else
# define EXTERN extern # define EXTERN extern
#endif #endif
//------------------------------------------------------------------------------
#if defined(ARDUINO) && ARDUINO >= 100 #if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h> #include <Arduino.h>
#else #else
#include <WProgram.h> #include <WProgram.h>
#endif #endif
//------------------------------------------------------------------------------
// define which timer to use // define which timer to use
// //
// Uncomment the timer you wish to use on your board. If you // Uncomment the timer you wish to use on your board. If you
@@ -78,18 +81,18 @@
#define IR_USE_TIMER2 // tx = pin 3 #define IR_USE_TIMER2 // tx = pin 3
#endif #endif
//------------------------------------------------------------------------------
#ifdef F_CPU #ifdef F_CPU
#define SYSCLOCK F_CPU // main Arduino clock #define SYSCLOCK F_CPU // main Arduino clock
#else #else
#define SYSCLOCK 16000000 // main Arduino clock #define SYSCLOCK 16000000 // main Arduino clock
#endif #endif
//------------------------------------------------------------------------------
#define ERR 0 #define ERR 0
#define DECODED 1 #define DECODED 1
//------------------------------------------------------------------------------
// defines for setting and clearing register bits // defines for setting and clearing register bits
#ifndef cbi #ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
@@ -98,6 +101,7 @@
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif #endif
//------------------------------------------------------------------------------
// Pulse parms are *50-100 for the Mark and *50+100 for the space // Pulse parms are *50-100 for the Mark and *50+100 for the space
// First MARK is the one after the long gap // First MARK is the one after the long gap
// pulse parameters in usec // pulse parameters in usec

100
irISR.cpp
View File

@@ -14,63 +14,61 @@
// //
ISR (TIMER_INTR_NAME) ISR (TIMER_INTR_NAME)
{ {
TIMER_RESET; TIMER_RESET;
uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin); uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
irparams.timer++; // One more 50us tick irparams.timer++; // One more 50us tick
if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_STOP ; // Buffer overflow if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_STOP ; // Buffer overflow
switch(irparams.rcvstate) { switch(irparams.rcvstate) {
case STATE_IDLE: // In the middle of a gap case STATE_IDLE: // In the middle of a gap
if (irdata == MARK) { if (irdata == MARK) {
if (irparams.timer < GAP_TICKS) { if (irparams.timer < GAP_TICKS) {
// Not big enough to be a gap. // Not big enough to be a gap.
irparams.timer = 0; 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 } else {
if (irdata == SPACE) { // MARK ended, record time // gap just ended, record duration and start recording transmission
irparams.rawbuf[irparams.rawlen++] = irparams.timer; irparams.rawlen = 0;
irparams.timer = 0; irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.rcvstate = STATE_SPACE; irparams.timer = 0;
} irparams.rcvstate = STATE_MARK;
break; }
}
break;
case STATE_SPACE: // timing SPACE case STATE_MARK: // timing MARK
if (irdata == MARK) { // SPACE just ended, record it if (irdata == SPACE) { // MARK ended, record time
irparams.rawbuf[irparams.rawlen++] = irparams.timer; irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0; irparams.timer = 0;
irparams.rcvstate = STATE_MARK; irparams.rcvstate = STATE_SPACE;
} }
else { // SPACE break;
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 case STATE_SPACE: // timing SPACE
if (irdata == MARK) irparams.timer = 0 ; // reset gap timer if (irdata == MARK) { // SPACE just ended, record it
break; irparams.rawbuf[irparams.rawlen++] = irparams.timer;
} irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
if (irparams.blinkflag) { } else if (irparams.timer > GAP_TICKS) { // SPACE
if (irdata == MARK) BLINKLED_ON() ; // turn pin 13 LED on // big SPACE, indicates gap between codes
else BLINKLED_OFF() ; // turn pin 13 LED off // 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) irparams.timer = 0 ; // reset gap timer
break;
}
if (irparams.blinkflag) {
if (irdata == MARK) BLINKLED_ON() ; // turn pin 13 LED on
else BLINKLED_OFF() ; // turn pin 13 LED off
}
} }

View File

@@ -51,18 +51,6 @@ void IRrecv::resume ( )
irparams.rawlen = 0; irparams.rawlen = 0;
} }
//+============================================================================= //+=============================================================================
// Decodes the received IR message // Decodes the received IR message
// Returns 0 if no data ready, 1 if data ready. // Returns 0 if no data ready, 1 if data ready.
@@ -162,9 +150,9 @@ int IRrecv::decode (decode_results *results)
// //
int IRrecv::compare (unsigned int oldval, unsigned int newval) int IRrecv::compare (unsigned int oldval, unsigned int newval)
{ {
if (newval < oldval * .8) return 0 ; if (newval < oldval * .8) return 0 ;
else if (oldval < newval * .8) return 2 ; else if (oldval < newval * .8) return 2 ;
else return 1 ; else return 1 ;
} }
//+============================================================================= //+=============================================================================
@@ -178,18 +166,18 @@ int IRrecv::compare (unsigned int oldval, unsigned int newval)
long IRrecv::decodeHash (decode_results *results) long IRrecv::decodeHash (decode_results *results)
{ {
// Require at least 6 samples to prevent triggering on noise // Require at least 6 samples to prevent triggering on noise
if (results->rawlen < 6) return false ; if (results->rawlen < 6) return false ;
long hash = FNV_BASIS_32; long hash = FNV_BASIS_32;
for (int i = 1; (i + 2) < results->rawlen; i++) { for (int i = 1; (i + 2) < results->rawlen; i++) {
int value = compare(results->rawbuf[i], results->rawbuf[i+2]); int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
// Add value into the hash // Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value; hash = (hash * FNV_PRIME_32) ^ value;
} }
results->value = hash; results->value = hash;
results->bits = 32; results->bits = 32;
results->decode_type = UNKNOWN; results->decode_type = UNKNOWN;
return true; return true;
} }