mirror of
https://github.com/Theaninova/Arduino-IRremote.git
synced 2025-12-11 08:56:14 +00:00
Bit more code cleanup
This commit is contained in:
82
IRremote.cpp
82
IRremote.cpp
@@ -31,50 +31,66 @@
|
||||
// ...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)
|
||||
{
|
||||
DBG_PRINT("Testing: ");
|
||||
DBG_PRINT(TICKS_LOW(desired), DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINT(measured, DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINTLN(TICKS_HIGH(desired), DEC);
|
||||
DBG_PRINT("Testing: ");
|
||||
DBG_PRINT(TICKS_LOW(desired), DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINT(measured, DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
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)
|
||||
{
|
||||
DBG_PRINT("Testing mark ");
|
||||
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
|
||||
DBG_PRINT(" vs ");
|
||||
DBG_PRINT(desired_us, DEC);
|
||||
DBG_PRINT(": ");
|
||||
DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINT(measured_ticks, DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINTLN(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
|
||||
DBG_PRINT("Testing mark ");
|
||||
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
|
||||
DBG_PRINT(" vs ");
|
||||
DBG_PRINT(desired_us, DEC);
|
||||
DBG_PRINT(": ");
|
||||
DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINT(measured_ticks, DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_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)));
|
||||
return ((measured_ticks >= TICKS_LOW (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)
|
||||
{
|
||||
DBG_PRINT("Testing space ");
|
||||
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
|
||||
DBG_PRINT(" vs ");
|
||||
DBG_PRINT(desired_us, DEC);
|
||||
DBG_PRINT(": ");
|
||||
DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINT(measured_ticks, DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINTLN(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
|
||||
DBG_PRINT("Testing space ");
|
||||
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
|
||||
DBG_PRINT(" vs ");
|
||||
DBG_PRINT(desired_us, DEC);
|
||||
DBG_PRINT(": ");
|
||||
DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_PRINT(measured_ticks, DEC);
|
||||
DBG_PRINT(" <= ");
|
||||
DBG_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)));
|
||||
return ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS))
|
||||
&& (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS)));
|
||||
}
|
||||
|
||||
18
IRremote.h
18
IRremote.h
@@ -1,3 +1,6 @@
|
||||
#ifndef IRremote_h
|
||||
#define IRremote_h
|
||||
|
||||
#define DEBUG
|
||||
#undef DEBUG
|
||||
|
||||
@@ -16,10 +19,7 @@ int MATCH (int measured, int desired) ;
|
||||
# define DBG_PRINTLN(...)
|
||||
#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 DECODE_NEC
|
||||
#define SEND_WHYNTER
|
||||
@@ -60,9 +60,6 @@ int MATCH (int measured, int desired) ;
|
||||
* Whynter A/C ARC-110WD added by Francesco Meschia
|
||||
*/
|
||||
|
||||
#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.
|
||||
@@ -71,6 +68,7 @@ int MATCH (int measured, int desired) ;
|
||||
//#define DEBUG
|
||||
// #define TEST
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
enum decode_type_t {
|
||||
UNKNOWN = -1,
|
||||
UNUSED = 0,
|
||||
@@ -90,6 +88,7 @@ enum decode_type_t {
|
||||
AIWA_RC_T501 = 14,
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Results returned from the decoder
|
||||
class decode_results {
|
||||
public:
|
||||
@@ -104,9 +103,11 @@ public:
|
||||
int rawlen; // Number of records in rawbuf.
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Decoded value for NEC when a repeat code is received
|
||||
#define REPEAT 0xffffffff
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// main class for receiving IR
|
||||
class IRrecv
|
||||
{
|
||||
@@ -163,6 +164,7 @@ private:
|
||||
|
||||
} ;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Only used for testing; can remove virtual for shorter code
|
||||
#ifdef TEST
|
||||
#define VIRTUAL virtual
|
||||
@@ -170,6 +172,7 @@ private:
|
||||
#define VIRTUAL
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class IRsend
|
||||
{
|
||||
public:
|
||||
@@ -217,6 +220,7 @@ public:
|
||||
VIRTUAL void space(int usec);
|
||||
} ;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Some useful constants
|
||||
|
||||
#define USECPERTICK 50 // microseconds per clock interrupt tick
|
||||
|
||||
@@ -17,18 +17,21 @@
|
||||
#ifndef IRremoteint_h
|
||||
#define IRremoteint_h
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#ifdef IR_GLOBAL
|
||||
# define EXTERN
|
||||
#else
|
||||
# define EXTERN extern
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// define which timer to use
|
||||
//
|
||||
// Uncomment the timer you wish to use on your board. If you
|
||||
@@ -78,18 +81,18 @@
|
||||
#define IR_USE_TIMER2 // tx = pin 3
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#ifdef F_CPU
|
||||
#define SYSCLOCK F_CPU // main Arduino clock
|
||||
#else
|
||||
#define SYSCLOCK 16000000 // main Arduino clock
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#define ERR 0
|
||||
#define DECODED 1
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// defines for setting and clearing register bits
|
||||
#ifndef cbi
|
||||
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
||||
@@ -98,6 +101,7 @@
|
||||
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Pulse parms are *50-100 for the Mark and *50+100 for the space
|
||||
// First MARK is the one after the long gap
|
||||
// pulse parameters in usec
|
||||
|
||||
100
irISR.cpp
100
irISR.cpp
@@ -14,63 +14,61 @@
|
||||
//
|
||||
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
|
||||
if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_STOP ; // Buffer overflow
|
||||
irparams.timer++; // One more 50us tick
|
||||
if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_STOP ; // Buffer overflow
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
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;
|
||||
} 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_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_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_STOP: // waiting, measuring gap
|
||||
if (irdata == MARK) irparams.timer = 0 ; // reset gap timer
|
||||
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;
|
||||
|
||||
if (irparams.blinkflag) {
|
||||
if (irdata == MARK) BLINKLED_ON() ; // turn pin 13 LED on
|
||||
else BLINKLED_OFF() ; // turn pin 13 LED off
|
||||
}
|
||||
} else if (irparams.timer > GAP_TICKS) { // SPACE
|
||||
// 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) 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
42
irRecv.cpp
42
irRecv.cpp
@@ -51,18 +51,6 @@ void IRrecv::resume ( )
|
||||
irparams.rawlen = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//+=============================================================================
|
||||
// Decodes the received IR message
|
||||
// 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)
|
||||
{
|
||||
if (newval < oldval * .8) return 0 ;
|
||||
else if (oldval < newval * .8) return 2 ;
|
||||
else return 1 ;
|
||||
if (newval < oldval * .8) return 0 ;
|
||||
else if (oldval < newval * .8) return 2 ;
|
||||
else return 1 ;
|
||||
}
|
||||
|
||||
//+=============================================================================
|
||||
@@ -178,18 +166,18 @@ int IRrecv::compare (unsigned int oldval, unsigned int newval)
|
||||
|
||||
long IRrecv::decodeHash (decode_results *results)
|
||||
{
|
||||
// Require at least 6 samples to prevent triggering on noise
|
||||
if (results->rawlen < 6) return false ;
|
||||
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;
|
||||
}
|
||||
// Require at least 6 samples to prevent triggering on noise
|
||||
if (results->rawlen < 6) return false ;
|
||||
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;
|
||||
results->value = hash;
|
||||
results->bits = 32;
|
||||
results->decode_type = UNKNOWN;
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user