2 * IRremote: IRtest unittest
3 * Version 0.1 July, 2009
4 * Copyright 2009 Ken Shirriff
7 * Note: to run these tests, edit IRremote/IRremote.h to add "#define TEST"
8 * You must then recompile the library by removing IRremote.o and restarting
13 #include <IRremoteInt.h>
15 // Dumps out the decode_results structure.
16 // Call this after IRrecv::decode()
17 // void * to work around compiler issue
18 //void dump(void *v) {
19 // decode_results *results = (decode_results *)v
20 void dump(decode_results *results) {
21 int count = results->rawlen;
22 if (results->decode_type == UNKNOWN) {
23 Serial.println("Could not decode message");
26 if (results->decode_type == NEC) {
27 Serial.print("Decoded NEC: ");
29 else if (results->decode_type == SONY) {
30 Serial.print("Decoded SONY: ");
32 else if (results->decode_type == RC5) {
33 Serial.print("Decoded RC5: ");
35 else if (results->decode_type == RC6) {
36 Serial.print("Decoded RC6: ");
38 Serial.print(results->value, HEX);
40 Serial.print(results->bits, DEC);
41 Serial.println(" bits)");
43 Serial.print("Raw (");
44 Serial.print(count, DEC);
47 for (int i = 0; i < count; i++) {
49 Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
52 Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
60 decode_results results;
66 // For testing, just log the marks/spaces
67 #define SENDLOG_LEN 128
68 int sendlog[SENDLOG_LEN];
77 sendlog[sendlogcnt] = time;
78 if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
80 void space(int time) {
81 sendlog[sendlogcnt] = -time;
82 if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
84 // Copies the dummy buf into the interrupt buf
87 irparams.rcvstate = STATE_STOP;
88 irparams.rawlen = 1; // Skip the gap
89 for (int i = 0 ; i < sendlogcnt; i++) {
93 irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK;
98 irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK;
101 else if (sendlog[i] > 0) {
104 irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK;
109 irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK;
113 if (irparams.rawlen % 2) {
114 irparams.rawlen--; // Remove trailing space
119 IRsendDummy irsenddummy;
121 void verify(unsigned long val, int bits, int type) {
122 irsenddummy.useDummyBuf();
123 irrecv.decode(&results);
124 Serial.print("Testing ");
125 Serial.print(val, HEX);
126 if (results.value == val && results.bits == bits && results.decode_type == type) {
127 Serial.println(": OK");
130 Serial.println(": Error");
135 void testNEC(unsigned long val, int bits) {
137 irsenddummy.sendNEC(val, bits);
138 verify(val, bits, NEC);
140 void testSony(unsigned long val, int bits) {
142 irsenddummy.sendSony(val, bits);
143 verify(val, bits, SONY);
145 void testRC5(unsigned long val, int bits) {
147 irsenddummy.sendRC5(val, bits);
148 verify(val, bits, RC5);
150 void testRC6(unsigned long val, int bits) {
152 irsenddummy.sendRC6(val, bits);
153 verify(val, bits, RC6);
157 Serial.println("NEC tests");
158 testNEC(0x00000000, 32);
159 testNEC(0xffffffff, 32);
160 testNEC(0xaaaaaaaa, 32);
161 testNEC(0x55555555, 32);
162 testNEC(0x12345678, 32);
163 Serial.println("Sony tests");
169 Serial.println("RC5 tests");
175 Serial.println("RC6 tests");
176 testRC6(0xfffff, 20);
177 testRC6(0x00000, 20);
178 testRC6(0xaaaaa, 20);
179 testRC6(0x55555, 20);
180 testRC6(0x12345, 20);