]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/IRremote/examples/IRtest/IRtest.ino
first commit
[arduino] / books / pdummies / Libraries / IRremote / examples / IRtest / IRtest.ino
1 /*
2  * IRremote: IRtest unittest
3  * Version 0.1 July, 2009
4  * Copyright 2009 Ken Shirriff
5  * http://arcfn.com
6  *
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
9  * the arduino IDE.
10  */
11
12 #include <IRremote.h>
13 #include <IRremoteInt.h>
14
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");
24   } 
25   else {
26     if (results->decode_type == NEC) {
27       Serial.print("Decoded NEC: ");
28     } 
29     else if (results->decode_type == SONY) {
30       Serial.print("Decoded SONY: ");
31     } 
32     else if (results->decode_type == RC5) {
33       Serial.print("Decoded RC5: ");
34     } 
35     else if (results->decode_type == RC6) {
36       Serial.print("Decoded RC6: ");
37     }
38     Serial.print(results->value, HEX);
39     Serial.print(" (");
40     Serial.print(results->bits, DEC);
41     Serial.println(" bits)");
42   }
43   Serial.print("Raw (");
44   Serial.print(count, DEC);
45   Serial.print("): ");
46
47   for (int i = 0; i < count; i++) {
48     if ((i % 2) == 1) {
49       Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
50     } 
51     else {
52       Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
53     }
54     Serial.print(" ");
55   }
56   Serial.println("");
57 }
58
59 IRrecv irrecv(0);
60 decode_results results;
61
62 class IRsendDummy : 
63 public IRsend
64 {
65 public:
66   // For testing, just log the marks/spaces
67 #define SENDLOG_LEN 128
68   int sendlog[SENDLOG_LEN];
69   int sendlogcnt;
70   IRsendDummy() : 
71   IRsend() {
72   }
73   void reset() {
74     sendlogcnt = 0;
75   }
76   void mark(int time) {
77     sendlog[sendlogcnt] = time;
78     if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
79   }
80   void space(int time) {
81     sendlog[sendlogcnt] = -time;
82     if (sendlogcnt < SENDLOG_LEN) sendlogcnt++;
83   }
84   // Copies the dummy buf into the interrupt buf
85   void useDummyBuf() {
86     int last = SPACE;
87     irparams.rcvstate = STATE_STOP;
88     irparams.rawlen = 1; // Skip the gap
89     for (int i = 0 ; i < sendlogcnt; i++) {
90       if (sendlog[i] < 0) {
91         if (last == MARK) {
92           // New space
93           irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK;
94           last = SPACE;
95         } 
96         else {
97           // More space
98           irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK;
99         }
100       } 
101       else if (sendlog[i] > 0) {
102         if (last == SPACE) {
103           // New mark
104           irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK;
105           last = MARK;
106         } 
107         else {
108           // More mark
109           irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK;
110         }
111       }
112     }
113     if (irparams.rawlen % 2) {
114       irparams.rawlen--; // Remove trailing space
115     }
116   }
117 };
118
119 IRsendDummy irsenddummy;
120
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");
128   } 
129   else {
130     Serial.println(": Error");
131     dump(&results);
132   }
133 }  
134
135 void testNEC(unsigned long val, int bits) {
136   irsenddummy.reset();
137   irsenddummy.sendNEC(val, bits);
138   verify(val, bits, NEC);
139 }
140 void testSony(unsigned long val, int bits) {
141   irsenddummy.reset();
142   irsenddummy.sendSony(val, bits);
143   verify(val, bits, SONY);
144 }
145 void testRC5(unsigned long val, int bits) {
146   irsenddummy.reset();
147   irsenddummy.sendRC5(val, bits);
148   verify(val, bits, RC5);
149 }
150 void testRC6(unsigned long val, int bits) {
151   irsenddummy.reset();
152   irsenddummy.sendRC6(val, bits);
153   verify(val, bits, RC6);
154 }
155
156 void test() {
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");
164   testSony(0xfff, 12);
165   testSony(0x000, 12);
166   testSony(0xaaa, 12);
167   testSony(0x555, 12);
168   testSony(0x123, 12);
169   Serial.println("RC5 tests");
170   testRC5(0xfff, 12);
171   testRC5(0x000, 12);
172   testRC5(0xaaa, 12);
173   testRC5(0x555, 12);
174   testRC5(0x123, 12);
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);
181 }
182
183 void setup()
184 {
185   Serial.begin(9600);
186   test();
187 }
188
189 void loop() {
190 }