Taguchi Yuuki / IRremote

Dependents:   Lilnija_29012017 NucleoF042K6_IRReceiver

Committer:
yuhki50
Date:
Sat Jan 23 06:16:48 2016 +0000
Revision:
0:70c8e56bac45
import https://github.com/z3t0/Arduino-IRremote e3ec11d

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuhki50 0:70c8e56bac45 1 /*
yuhki50 0:70c8e56bac45 2 * Test send/receive functions of IRremote, using a pair of Arduinos.
yuhki50 0:70c8e56bac45 3 *
yuhki50 0:70c8e56bac45 4 * Arduino #1 should have an IR LED connected to the send pin (3).
yuhki50 0:70c8e56bac45 5 * Arduino #2 should have an IR detector/demodulator connected to the
yuhki50 0:70c8e56bac45 6 * receive pin (11) and a visible LED connected to pin 3.
yuhki50 0:70c8e56bac45 7 *
yuhki50 0:70c8e56bac45 8 * The cycle:
yuhki50 0:70c8e56bac45 9 * Arduino #1 will wait 2 seconds, then run through the tests.
yuhki50 0:70c8e56bac45 10 * It repeats this forever.
yuhki50 0:70c8e56bac45 11 * Arduino #2 will wait for at least one second of no signal
yuhki50 0:70c8e56bac45 12 * (to synchronize with #1). It will then wait for the same test
yuhki50 0:70c8e56bac45 13 * signals. It will log all the status to the serial port. It will
yuhki50 0:70c8e56bac45 14 * also indicate status through the LED, which will flash each time a test
yuhki50 0:70c8e56bac45 15 * is completed. If there is an error, it will light up for 5 seconds.
yuhki50 0:70c8e56bac45 16 *
yuhki50 0:70c8e56bac45 17 * The test passes if the LED flashes 19 times, pauses, and then repeats.
yuhki50 0:70c8e56bac45 18 * The test fails if the LED lights for 5 seconds.
yuhki50 0:70c8e56bac45 19 *
yuhki50 0:70c8e56bac45 20 * The test software automatically decides which board is the sender and which is
yuhki50 0:70c8e56bac45 21 * the receiver by looking for an input on the send pin, which will indicate
yuhki50 0:70c8e56bac45 22 * the sender. You should hook the serial port to the receiver for debugging.
yuhki50 0:70c8e56bac45 23 *
yuhki50 0:70c8e56bac45 24 * Copyright 2010 Ken Shirriff
yuhki50 0:70c8e56bac45 25 * http://arcfn.com
yuhki50 0:70c8e56bac45 26 */
yuhki50 0:70c8e56bac45 27
yuhki50 0:70c8e56bac45 28 #include <IRremote.h>
yuhki50 0:70c8e56bac45 29
yuhki50 0:70c8e56bac45 30 int RECV_PIN = 11;
yuhki50 0:70c8e56bac45 31 int LED_PIN = 3;
yuhki50 0:70c8e56bac45 32
yuhki50 0:70c8e56bac45 33 IRrecv irrecv(RECV_PIN);
yuhki50 0:70c8e56bac45 34 IRsend irsend;
yuhki50 0:70c8e56bac45 35
yuhki50 0:70c8e56bac45 36 decode_results results;
yuhki50 0:70c8e56bac45 37
yuhki50 0:70c8e56bac45 38 #define RECEIVER 1
yuhki50 0:70c8e56bac45 39 #define SENDER 2
yuhki50 0:70c8e56bac45 40 #define ERROR 3
yuhki50 0:70c8e56bac45 41
yuhki50 0:70c8e56bac45 42 int mode;
yuhki50 0:70c8e56bac45 43
yuhki50 0:70c8e56bac45 44 void setup()
yuhki50 0:70c8e56bac45 45 {
yuhki50 0:70c8e56bac45 46 Serial.begin(9600);
yuhki50 0:70c8e56bac45 47 // Check RECV_PIN to decide if we're RECEIVER or SENDER
yuhki50 0:70c8e56bac45 48 if (digitalRead(RECV_PIN) == HIGH) {
yuhki50 0:70c8e56bac45 49 mode = RECEIVER;
yuhki50 0:70c8e56bac45 50 irrecv.enableIRIn();
yuhki50 0:70c8e56bac45 51 pinMode(LED_PIN, OUTPUT);
yuhki50 0:70c8e56bac45 52 digitalWrite(LED_PIN, LOW);
yuhki50 0:70c8e56bac45 53 Serial.println("Receiver mode");
yuhki50 0:70c8e56bac45 54 }
yuhki50 0:70c8e56bac45 55 else {
yuhki50 0:70c8e56bac45 56 mode = SENDER;
yuhki50 0:70c8e56bac45 57 Serial.println("Sender mode");
yuhki50 0:70c8e56bac45 58 }
yuhki50 0:70c8e56bac45 59 }
yuhki50 0:70c8e56bac45 60
yuhki50 0:70c8e56bac45 61 // Wait for the gap between tests, to synchronize with
yuhki50 0:70c8e56bac45 62 // the sender.
yuhki50 0:70c8e56bac45 63 // Specifically, wait for a signal followed by a gap of at last gap ms.
yuhki50 0:70c8e56bac45 64 void waitForGap(int gap) {
yuhki50 0:70c8e56bac45 65 Serial.println("Waiting for gap");
yuhki50 0:70c8e56bac45 66 while (1) {
yuhki50 0:70c8e56bac45 67 while (digitalRead(RECV_PIN) == LOW) {
yuhki50 0:70c8e56bac45 68 }
yuhki50 0:70c8e56bac45 69 unsigned long time = millis();
yuhki50 0:70c8e56bac45 70 while (digitalRead(RECV_PIN) == HIGH) {
yuhki50 0:70c8e56bac45 71 if (millis() - time > gap) {
yuhki50 0:70c8e56bac45 72 return;
yuhki50 0:70c8e56bac45 73 }
yuhki50 0:70c8e56bac45 74 }
yuhki50 0:70c8e56bac45 75 }
yuhki50 0:70c8e56bac45 76 }
yuhki50 0:70c8e56bac45 77
yuhki50 0:70c8e56bac45 78 // Dumps out the decode_results structure.
yuhki50 0:70c8e56bac45 79 // Call this after IRrecv::decode()
yuhki50 0:70c8e56bac45 80 void dump(decode_results *results) {
yuhki50 0:70c8e56bac45 81 int count = results->rawlen;
yuhki50 0:70c8e56bac45 82 if (results->decode_type == UNKNOWN) {
yuhki50 0:70c8e56bac45 83 Serial.println("Could not decode message");
yuhki50 0:70c8e56bac45 84 }
yuhki50 0:70c8e56bac45 85 else {
yuhki50 0:70c8e56bac45 86 if (results->decode_type == NEC) {
yuhki50 0:70c8e56bac45 87 Serial.print("Decoded NEC: ");
yuhki50 0:70c8e56bac45 88 }
yuhki50 0:70c8e56bac45 89 else if (results->decode_type == SONY) {
yuhki50 0:70c8e56bac45 90 Serial.print("Decoded SONY: ");
yuhki50 0:70c8e56bac45 91 }
yuhki50 0:70c8e56bac45 92 else if (results->decode_type == RC5) {
yuhki50 0:70c8e56bac45 93 Serial.print("Decoded RC5: ");
yuhki50 0:70c8e56bac45 94 }
yuhki50 0:70c8e56bac45 95 else if (results->decode_type == RC6) {
yuhki50 0:70c8e56bac45 96 Serial.print("Decoded RC6: ");
yuhki50 0:70c8e56bac45 97 }
yuhki50 0:70c8e56bac45 98 Serial.print(results->value, HEX);
yuhki50 0:70c8e56bac45 99 Serial.print(" (");
yuhki50 0:70c8e56bac45 100 Serial.print(results->bits, DEC);
yuhki50 0:70c8e56bac45 101 Serial.println(" bits)");
yuhki50 0:70c8e56bac45 102 }
yuhki50 0:70c8e56bac45 103 Serial.print("Raw (");
yuhki50 0:70c8e56bac45 104 Serial.print(count, DEC);
yuhki50 0:70c8e56bac45 105 Serial.print("): ");
yuhki50 0:70c8e56bac45 106
yuhki50 0:70c8e56bac45 107 for (int i = 0; i < count; i++) {
yuhki50 0:70c8e56bac45 108 if ((i % 2) == 1) {
yuhki50 0:70c8e56bac45 109 Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
yuhki50 0:70c8e56bac45 110 }
yuhki50 0:70c8e56bac45 111 else {
yuhki50 0:70c8e56bac45 112 Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
yuhki50 0:70c8e56bac45 113 }
yuhki50 0:70c8e56bac45 114 Serial.print(" ");
yuhki50 0:70c8e56bac45 115 }
yuhki50 0:70c8e56bac45 116 Serial.println("");
yuhki50 0:70c8e56bac45 117 }
yuhki50 0:70c8e56bac45 118
yuhki50 0:70c8e56bac45 119
yuhki50 0:70c8e56bac45 120 // Test send or receive.
yuhki50 0:70c8e56bac45 121 // If mode is SENDER, send a code of the specified type, value, and bits
yuhki50 0:70c8e56bac45 122 // If mode is RECEIVER, receive a code and verify that it is of the
yuhki50 0:70c8e56bac45 123 // specified type, value, and bits. For success, the LED is flashed;
yuhki50 0:70c8e56bac45 124 // for failure, the mode is set to ERROR.
yuhki50 0:70c8e56bac45 125 // The motivation behind this method is that the sender and the receiver
yuhki50 0:70c8e56bac45 126 // can do the same test calls, and the mode variable indicates whether
yuhki50 0:70c8e56bac45 127 // to send or receive.
yuhki50 0:70c8e56bac45 128 void test(char *label, int type, unsigned long value, int bits) {
yuhki50 0:70c8e56bac45 129 if (mode == SENDER) {
yuhki50 0:70c8e56bac45 130 Serial.println(label);
yuhki50 0:70c8e56bac45 131 if (type == NEC) {
yuhki50 0:70c8e56bac45 132 irsend.sendNEC(value, bits);
yuhki50 0:70c8e56bac45 133 }
yuhki50 0:70c8e56bac45 134 else if (type == SONY) {
yuhki50 0:70c8e56bac45 135 irsend.sendSony(value, bits);
yuhki50 0:70c8e56bac45 136 }
yuhki50 0:70c8e56bac45 137 else if (type == RC5) {
yuhki50 0:70c8e56bac45 138 irsend.sendRC5(value, bits);
yuhki50 0:70c8e56bac45 139 }
yuhki50 0:70c8e56bac45 140 else if (type == RC6) {
yuhki50 0:70c8e56bac45 141 irsend.sendRC6(value, bits);
yuhki50 0:70c8e56bac45 142 }
yuhki50 0:70c8e56bac45 143 else {
yuhki50 0:70c8e56bac45 144 Serial.print(label);
yuhki50 0:70c8e56bac45 145 Serial.println("Bad type!");
yuhki50 0:70c8e56bac45 146 }
yuhki50 0:70c8e56bac45 147 delay(200);
yuhki50 0:70c8e56bac45 148 }
yuhki50 0:70c8e56bac45 149 else if (mode == RECEIVER) {
yuhki50 0:70c8e56bac45 150 irrecv.resume(); // Receive the next value
yuhki50 0:70c8e56bac45 151 unsigned long max_time = millis() + 30000;
yuhki50 0:70c8e56bac45 152 Serial.print(label);
yuhki50 0:70c8e56bac45 153
yuhki50 0:70c8e56bac45 154 // Wait for decode or timeout
yuhki50 0:70c8e56bac45 155 while (!irrecv.decode(&results)) {
yuhki50 0:70c8e56bac45 156 if (millis() > max_time) {
yuhki50 0:70c8e56bac45 157 Serial.println("Timeout receiving data");
yuhki50 0:70c8e56bac45 158 mode = ERROR;
yuhki50 0:70c8e56bac45 159 return;
yuhki50 0:70c8e56bac45 160 }
yuhki50 0:70c8e56bac45 161 }
yuhki50 0:70c8e56bac45 162 if (type == results.decode_type && value == results.value && bits == results.bits) {
yuhki50 0:70c8e56bac45 163 Serial.println (": OK");
yuhki50 0:70c8e56bac45 164 digitalWrite(LED_PIN, HIGH);
yuhki50 0:70c8e56bac45 165 delay(20);
yuhki50 0:70c8e56bac45 166 digitalWrite(LED_PIN, LOW);
yuhki50 0:70c8e56bac45 167 }
yuhki50 0:70c8e56bac45 168 else {
yuhki50 0:70c8e56bac45 169 Serial.println(": BAD");
yuhki50 0:70c8e56bac45 170 dump(&results);
yuhki50 0:70c8e56bac45 171 mode = ERROR;
yuhki50 0:70c8e56bac45 172 }
yuhki50 0:70c8e56bac45 173 }
yuhki50 0:70c8e56bac45 174 }
yuhki50 0:70c8e56bac45 175
yuhki50 0:70c8e56bac45 176 // Test raw send or receive. This is similar to the test method,
yuhki50 0:70c8e56bac45 177 // except it send/receives raw data.
yuhki50 0:70c8e56bac45 178 void testRaw(char *label, unsigned int *rawbuf, int rawlen) {
yuhki50 0:70c8e56bac45 179 if (mode == SENDER) {
yuhki50 0:70c8e56bac45 180 Serial.println(label);
yuhki50 0:70c8e56bac45 181 irsend.sendRaw(rawbuf, rawlen, 38 /* kHz */);
yuhki50 0:70c8e56bac45 182 delay(200);
yuhki50 0:70c8e56bac45 183 }
yuhki50 0:70c8e56bac45 184 else if (mode == RECEIVER ) {
yuhki50 0:70c8e56bac45 185 irrecv.resume(); // Receive the next value
yuhki50 0:70c8e56bac45 186 unsigned long max_time = millis() + 30000;
yuhki50 0:70c8e56bac45 187 Serial.print(label);
yuhki50 0:70c8e56bac45 188
yuhki50 0:70c8e56bac45 189 // Wait for decode or timeout
yuhki50 0:70c8e56bac45 190 while (!irrecv.decode(&results)) {
yuhki50 0:70c8e56bac45 191 if (millis() > max_time) {
yuhki50 0:70c8e56bac45 192 Serial.println("Timeout receiving data");
yuhki50 0:70c8e56bac45 193 mode = ERROR;
yuhki50 0:70c8e56bac45 194 return;
yuhki50 0:70c8e56bac45 195 }
yuhki50 0:70c8e56bac45 196 }
yuhki50 0:70c8e56bac45 197
yuhki50 0:70c8e56bac45 198 // Received length has extra first element for gap
yuhki50 0:70c8e56bac45 199 if (rawlen != results.rawlen - 1) {
yuhki50 0:70c8e56bac45 200 Serial.print("Bad raw length ");
yuhki50 0:70c8e56bac45 201 Serial.println(results.rawlen, DEC);
yuhki50 0:70c8e56bac45 202 mode = ERROR;
yuhki50 0:70c8e56bac45 203 return;
yuhki50 0:70c8e56bac45 204 }
yuhki50 0:70c8e56bac45 205 for (int i = 0; i < rawlen; i++) {
yuhki50 0:70c8e56bac45 206 long got = results.rawbuf[i+1] * USECPERTICK;
yuhki50 0:70c8e56bac45 207 // Adjust for extra duration of marks
yuhki50 0:70c8e56bac45 208 if (i % 2 == 0) {
yuhki50 0:70c8e56bac45 209 got -= MARK_EXCESS;
yuhki50 0:70c8e56bac45 210 }
yuhki50 0:70c8e56bac45 211 else {
yuhki50 0:70c8e56bac45 212 got += MARK_EXCESS;
yuhki50 0:70c8e56bac45 213 }
yuhki50 0:70c8e56bac45 214 // See if close enough, within 25%
yuhki50 0:70c8e56bac45 215 if (rawbuf[i] * 1.25 < got || got * 1.25 < rawbuf[i]) {
yuhki50 0:70c8e56bac45 216 Serial.println(": BAD");
yuhki50 0:70c8e56bac45 217 dump(&results);
yuhki50 0:70c8e56bac45 218 mode = ERROR;
yuhki50 0:70c8e56bac45 219 return;
yuhki50 0:70c8e56bac45 220 }
yuhki50 0:70c8e56bac45 221
yuhki50 0:70c8e56bac45 222 }
yuhki50 0:70c8e56bac45 223 Serial.println (": OK");
yuhki50 0:70c8e56bac45 224 digitalWrite(LED_PIN, HIGH);
yuhki50 0:70c8e56bac45 225 delay(20);
yuhki50 0:70c8e56bac45 226 digitalWrite(LED_PIN, LOW);
yuhki50 0:70c8e56bac45 227 }
yuhki50 0:70c8e56bac45 228 }
yuhki50 0:70c8e56bac45 229
yuhki50 0:70c8e56bac45 230 // This is the raw data corresponding to NEC 0x12345678
yuhki50 0:70c8e56bac45 231 unsigned int sendbuf[] = { /* NEC format */
yuhki50 0:70c8e56bac45 232 9000, 4500,
yuhki50 0:70c8e56bac45 233 560, 560, 560, 560, 560, 560, 560, 1690, /* 1 */
yuhki50 0:70c8e56bac45 234 560, 560, 560, 560, 560, 1690, 560, 560, /* 2 */
yuhki50 0:70c8e56bac45 235 560, 560, 560, 560, 560, 1690, 560, 1690, /* 3 */
yuhki50 0:70c8e56bac45 236 560, 560, 560, 1690, 560, 560, 560, 560, /* 4 */
yuhki50 0:70c8e56bac45 237 560, 560, 560, 1690, 560, 560, 560, 1690, /* 5 */
yuhki50 0:70c8e56bac45 238 560, 560, 560, 1690, 560, 1690, 560, 560, /* 6 */
yuhki50 0:70c8e56bac45 239 560, 560, 560, 1690, 560, 1690, 560, 1690, /* 7 */
yuhki50 0:70c8e56bac45 240 560, 1690, 560, 560, 560, 560, 560, 560, /* 8 */
yuhki50 0:70c8e56bac45 241 560};
yuhki50 0:70c8e56bac45 242
yuhki50 0:70c8e56bac45 243 void loop() {
yuhki50 0:70c8e56bac45 244 if (mode == SENDER) {
yuhki50 0:70c8e56bac45 245 delay(2000); // Delay for more than gap to give receiver a better chance to sync.
yuhki50 0:70c8e56bac45 246 }
yuhki50 0:70c8e56bac45 247 else if (mode == RECEIVER) {
yuhki50 0:70c8e56bac45 248 waitForGap(1000);
yuhki50 0:70c8e56bac45 249 }
yuhki50 0:70c8e56bac45 250 else if (mode == ERROR) {
yuhki50 0:70c8e56bac45 251 // Light up for 5 seconds for error
yuhki50 0:70c8e56bac45 252 digitalWrite(LED_PIN, HIGH);
yuhki50 0:70c8e56bac45 253 delay(5000);
yuhki50 0:70c8e56bac45 254 digitalWrite(LED_PIN, LOW);
yuhki50 0:70c8e56bac45 255 mode = RECEIVER; // Try again
yuhki50 0:70c8e56bac45 256 return;
yuhki50 0:70c8e56bac45 257 }
yuhki50 0:70c8e56bac45 258
yuhki50 0:70c8e56bac45 259 // The test suite.
yuhki50 0:70c8e56bac45 260 test("SONY1", SONY, 0x123, 12);
yuhki50 0:70c8e56bac45 261 test("SONY2", SONY, 0x000, 12);
yuhki50 0:70c8e56bac45 262 test("SONY3", SONY, 0xfff, 12);
yuhki50 0:70c8e56bac45 263 test("SONY4", SONY, 0x12345, 20);
yuhki50 0:70c8e56bac45 264 test("SONY5", SONY, 0x00000, 20);
yuhki50 0:70c8e56bac45 265 test("SONY6", SONY, 0xfffff, 20);
yuhki50 0:70c8e56bac45 266 test("NEC1", NEC, 0x12345678, 32);
yuhki50 0:70c8e56bac45 267 test("NEC2", NEC, 0x00000000, 32);
yuhki50 0:70c8e56bac45 268 test("NEC3", NEC, 0xffffffff, 32);
yuhki50 0:70c8e56bac45 269 test("NEC4", NEC, REPEAT, 32);
yuhki50 0:70c8e56bac45 270 test("RC51", RC5, 0x12345678, 32);
yuhki50 0:70c8e56bac45 271 test("RC52", RC5, 0x0, 32);
yuhki50 0:70c8e56bac45 272 test("RC53", RC5, 0xffffffff, 32);
yuhki50 0:70c8e56bac45 273 test("RC61", RC6, 0x12345678, 32);
yuhki50 0:70c8e56bac45 274 test("RC62", RC6, 0x0, 32);
yuhki50 0:70c8e56bac45 275 test("RC63", RC6, 0xffffffff, 32);
yuhki50 0:70c8e56bac45 276
yuhki50 0:70c8e56bac45 277 // Tests of raw sending and receiving.
yuhki50 0:70c8e56bac45 278 // First test sending raw and receiving raw.
yuhki50 0:70c8e56bac45 279 // Then test sending raw and receiving decoded NEC
yuhki50 0:70c8e56bac45 280 // Then test sending NEC and receiving raw
yuhki50 0:70c8e56bac45 281 testRaw("RAW1", sendbuf, 67);
yuhki50 0:70c8e56bac45 282 if (mode == SENDER) {
yuhki50 0:70c8e56bac45 283 testRaw("RAW2", sendbuf, 67);
yuhki50 0:70c8e56bac45 284 test("RAW3", NEC, 0x12345678, 32);
yuhki50 0:70c8e56bac45 285 }
yuhki50 0:70c8e56bac45 286 else {
yuhki50 0:70c8e56bac45 287 test("RAW2", NEC, 0x12345678, 32);
yuhki50 0:70c8e56bac45 288 testRaw("RAW3", sendbuf, 67);
yuhki50 0:70c8e56bac45 289 }
yuhki50 0:70c8e56bac45 290 }