Skip to content

Commit a1cf782

Browse files
committed
Bit more code cleanup
1 parent 88e243f commit a1cf782

File tree

5 files changed

+133
-123
lines changed

5 files changed

+133
-123
lines changed

IRremote.cpp

+49-33
Original file line numberDiff line numberDiff line change
@@ -31,50 +31,66 @@
3131
// ...but every time i reduce these functions down to macros, the decoders stop working!!??
3232

3333
//+=============================================================================
34+
// The match functions were (apparently) originally MACROs to improve code speed
35+
// (although this would have bloated the code size) hence the names being CAPS
36+
// A later release implemented debug output and so they needed to be converted
37+
// to functions.
38+
// I tried to implement a dual-compile mode (DEBUG/non-DEBUG) but for some
39+
// reason, no matter what I did I could not get them to function as macros again.
40+
// I have found a *lot* of bugs in the Arduino compiler over the last few weeks,
41+
// and I am currently assuming that one of these bugs is my problem.
42+
// I may revisit this code at a later date and look at the assembler produced
43+
// in a hope of finding out what is going on, but for now they will remain as
44+
// functions even in non-DEBUG mode
45+
//
3446
int MATCH (int measured, int desired)
3547
{
36-
DBG_PRINT("Testing: ");
37-
DBG_PRINT(TICKS_LOW(desired), DEC);
38-
DBG_PRINT(" <= ");
39-
DBG_PRINT(measured, DEC);
40-
DBG_PRINT(" <= ");
41-
DBG_PRINTLN(TICKS_HIGH(desired), DEC);
48+
DBG_PRINT("Testing: ");
49+
DBG_PRINT(TICKS_LOW(desired), DEC);
50+
DBG_PRINT(" <= ");
51+
DBG_PRINT(measured, DEC);
52+
DBG_PRINT(" <= ");
53+
DBG_PRINTLN(TICKS_HIGH(desired), DEC);
4254

43-
return ((measured >= TICKS_LOW(desired)) && (measured <= TICKS_HIGH(desired)));
55+
return ((measured >= TICKS_LOW(desired)) && (measured <= TICKS_HIGH(desired)));
4456
}
4557

46-
//+=============================================================================
58+
//+========================================================
59+
// Marks tend to be 100us too long when received due to sensor lag.
60+
//
4761
int MATCH_MARK (int measured_ticks, int desired_us)
4862
{
49-
DBG_PRINT("Testing mark ");
50-
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
51-
DBG_PRINT(" vs ");
52-
DBG_PRINT(desired_us, DEC);
53-
DBG_PRINT(": ");
54-
DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
55-
DBG_PRINT(" <= ");
56-
DBG_PRINT(measured_ticks, DEC);
57-
DBG_PRINT(" <= ");
58-
DBG_PRINTLN(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
63+
DBG_PRINT("Testing mark ");
64+
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
65+
DBG_PRINT(" vs ");
66+
DBG_PRINT(desired_us, DEC);
67+
DBG_PRINT(": ");
68+
DBG_PRINT(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
69+
DBG_PRINT(" <= ");
70+
DBG_PRINT(measured_ticks, DEC);
71+
DBG_PRINT(" <= ");
72+
DBG_PRINTLN(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
5973

60-
return ((measured_ticks >= TICKS_LOW (desired_us + MARK_EXCESS))
61-
&& (measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS)));
74+
return ((measured_ticks >= TICKS_LOW (desired_us + MARK_EXCESS))
75+
&& (measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS)));
6276
}
6377

64-
//+=============================================================================
78+
//+========================================================
79+
// Spaces tend to be 100us too short when received due to sensor lag.
80+
//
6581
int MATCH_SPACE (int measured_ticks, int desired_us)
6682
{
67-
DBG_PRINT("Testing space ");
68-
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
69-
DBG_PRINT(" vs ");
70-
DBG_PRINT(desired_us, DEC);
71-
DBG_PRINT(": ");
72-
DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
73-
DBG_PRINT(" <= ");
74-
DBG_PRINT(measured_ticks, DEC);
75-
DBG_PRINT(" <= ");
76-
DBG_PRINTLN(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
83+
DBG_PRINT("Testing space ");
84+
DBG_PRINT(measured_ticks * USECPERTICK, DEC);
85+
DBG_PRINT(" vs ");
86+
DBG_PRINT(desired_us, DEC);
87+
DBG_PRINT(": ");
88+
DBG_PRINT(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
89+
DBG_PRINT(" <= ");
90+
DBG_PRINT(measured_ticks, DEC);
91+
DBG_PRINT(" <= ");
92+
DBG_PRINTLN(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
7793

78-
return ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS))
79-
&& (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS)));
94+
return ((measured_ticks >= TICKS_LOW (desired_us - MARK_EXCESS))
95+
&& (measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS)));
8096
}

IRremote.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef IRremote_h
2+
#define IRremote_h
3+
14
#define DEBUG
25
#undef DEBUG
36

@@ -16,10 +19,7 @@ int MATCH (int measured, int desired) ;
1619
# define DBG_PRINTLN(...)
1720
#endif
1821

19-
///int MATCH (int measured, int desired);
20-
///int MATCH_MARK (int measured_ticks, int desired_us);
21-
///int MATCH_SPACE (int measured_ticks, int desired_us);
22-
22+
//------------------------------------------------------------------------------
2323
#define SEND_NEC
2424
#define DECODE_NEC
2525
#define SEND_WHYNTER
@@ -60,9 +60,6 @@ int MATCH (int measured, int desired) ;
6060
* Whynter A/C ARC-110WD added by Francesco Meschia
6161
*/
6262

63-
#ifndef IRremote_h
64-
#define IRremote_h
65-
6663
// The following are compile-time library options.
6764
// If you change them, recompile the library.
6865
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
@@ -71,6 +68,7 @@ int MATCH (int measured, int desired) ;
7168
//#define DEBUG
7269
// #define TEST
7370

71+
//------------------------------------------------------------------------------
7472
enum decode_type_t {
7573
UNKNOWN = -1,
7674
UNUSED = 0,
@@ -90,6 +88,7 @@ enum decode_type_t {
9088
AIWA_RC_T501 = 14,
9189
};
9290

91+
//------------------------------------------------------------------------------
9392
// Results returned from the decoder
9493
class decode_results {
9594
public:
@@ -104,9 +103,11 @@ class decode_results {
104103
int rawlen; // Number of records in rawbuf.
105104
};
106105

106+
//------------------------------------------------------------------------------
107107
// Decoded value for NEC when a repeat code is received
108108
#define REPEAT 0xffffffff
109109

110+
//------------------------------------------------------------------------------
110111
// main class for receiving IR
111112
class IRrecv
112113
{
@@ -163,13 +164,15 @@ class IRrecv
163164

164165
} ;
165166

167+
//------------------------------------------------------------------------------
166168
// Only used for testing; can remove virtual for shorter code
167169
#ifdef TEST
168170
#define VIRTUAL virtual
169171
#else
170172
#define VIRTUAL
171173
#endif
172174

175+
//------------------------------------------------------------------------------
173176
class IRsend
174177
{
175178
public:
@@ -217,6 +220,7 @@ class IRsend
217220
VIRTUAL void space(int usec);
218221
} ;
219222

223+
//------------------------------------------------------------------------------
220224
// Some useful constants
221225

222226
#define USECPERTICK 50 // microseconds per clock interrupt tick

IRremoteInt.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717
#ifndef IRremoteint_h
1818
#define IRremoteint_h
1919

20+
//------------------------------------------------------------------------------
2021
#ifdef IR_GLOBAL
2122
# define EXTERN
2223
#else
2324
# define EXTERN extern
2425
#endif
2526

27+
//------------------------------------------------------------------------------
2628
#if defined(ARDUINO) && ARDUINO >= 100
2729
#include <Arduino.h>
2830
#else
2931
#include <WProgram.h>
3032
#endif
3133

34+
//------------------------------------------------------------------------------
3235
// define which timer to use
3336
//
3437
// Uncomment the timer you wish to use on your board. If you
@@ -78,18 +81,18 @@
7881
#define IR_USE_TIMER2 // tx = pin 3
7982
#endif
8083

81-
82-
84+
//------------------------------------------------------------------------------
8385
#ifdef F_CPU
8486
#define SYSCLOCK F_CPU // main Arduino clock
8587
#else
8688
#define SYSCLOCK 16000000 // main Arduino clock
8789
#endif
8890

91+
//------------------------------------------------------------------------------
8992
#define ERR 0
9093
#define DECODED 1
9194

92-
95+
//------------------------------------------------------------------------------
9396
// defines for setting and clearing register bits
9497
#ifndef cbi
9598
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
@@ -98,6 +101,7 @@
98101
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
99102
#endif
100103

104+
//------------------------------------------------------------------------------
101105
// Pulse parms are *50-100 for the Mark and *50+100 for the space
102106
// First MARK is the one after the long gap
103107
// pulse parameters in usec

irISR.cpp

+49-51
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,61 @@
1414
//
1515
ISR (TIMER_INTR_NAME)
1616
{
17-
TIMER_RESET;
17+
TIMER_RESET;
1818

19-
uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
19+
uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
2020

21-
irparams.timer++; // One more 50us tick
22-
if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_STOP ; // Buffer overflow
21+
irparams.timer++; // One more 50us tick
22+
if (irparams.rawlen >= RAWBUF) irparams.rcvstate = STATE_STOP ; // Buffer overflow
2323

24-
switch(irparams.rcvstate) {
25-
case STATE_IDLE: // In the middle of a gap
26-
if (irdata == MARK) {
27-
if (irparams.timer < GAP_TICKS) {
28-
// Not big enough to be a gap.
29-
irparams.timer = 0;
30-
}
31-
else {
32-
// gap just ended, record duration and start recording transmission
33-
irparams.rawlen = 0;
34-
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
35-
irparams.timer = 0;
36-
irparams.rcvstate = STATE_MARK;
37-
}
38-
}
39-
break;
24+
switch(irparams.rcvstate) {
25+
case STATE_IDLE: // In the middle of a gap
26+
if (irdata == MARK) {
27+
if (irparams.timer < GAP_TICKS) {
28+
// Not big enough to be a gap.
29+
irparams.timer = 0;
4030

41-
case STATE_MARK: // timing MARK
42-
if (irdata == SPACE) { // MARK ended, record time
43-
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
44-
irparams.timer = 0;
45-
irparams.rcvstate = STATE_SPACE;
46-
}
47-
break;
31+
} else {
32+
// gap just ended, record duration and start recording transmission
33+
irparams.rawlen = 0;
34+
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
35+
irparams.timer = 0;
36+
irparams.rcvstate = STATE_MARK;
37+
}
38+
}
39+
break;
4840

49-
case STATE_SPACE: // timing SPACE
50-
if (irdata == MARK) { // SPACE just ended, record it
51-
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
52-
irparams.timer = 0;
53-
irparams.rcvstate = STATE_MARK;
54-
}
55-
else { // SPACE
56-
if (irparams.timer > GAP_TICKS) {
57-
// big SPACE, indicates gap between codes
58-
// Mark current code as ready for processing
59-
// Switch to STOP
60-
// Don't reset timer; keep counting space width
61-
irparams.rcvstate = STATE_STOP;
62-
}
63-
}
64-
break;
41+
case STATE_MARK: // timing MARK
42+
if (irdata == SPACE) { // MARK ended, record time
43+
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
44+
irparams.timer = 0;
45+
irparams.rcvstate = STATE_SPACE;
46+
}
47+
break;
6548

66-
case STATE_STOP: // waiting, measuring gap
67-
if (irdata == MARK) irparams.timer = 0 ; // reset gap timer
68-
break;
69-
}
49+
case STATE_SPACE: // timing SPACE
50+
if (irdata == MARK) { // SPACE just ended, record it
51+
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
52+
irparams.timer = 0;
53+
irparams.rcvstate = STATE_MARK;
7054

71-
if (irparams.blinkflag) {
72-
if (irdata == MARK) BLINKLED_ON() ; // turn pin 13 LED on
73-
else BLINKLED_OFF() ; // turn pin 13 LED off
74-
}
55+
} else if (irparams.timer > GAP_TICKS) { // SPACE
56+
// big SPACE, indicates gap between codes
57+
// Mark current code as ready for processing
58+
// Switch to STOP
59+
// Don't reset timer; keep counting space width
60+
irparams.rcvstate = STATE_STOP;
61+
}
62+
break;
63+
64+
case STATE_STOP: // waiting, measuring gap
65+
if (irdata == MARK) irparams.timer = 0 ; // reset gap timer
66+
break;
67+
}
68+
69+
if (irparams.blinkflag) {
70+
if (irdata == MARK) BLINKLED_ON() ; // turn pin 13 LED on
71+
else BLINKLED_OFF() ; // turn pin 13 LED off
72+
}
7573
}
7674

0 commit comments

Comments
 (0)