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