Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Lilnija_29012017 NucleoF042K6_IRReceiver
examples/IRtest/IRtest.ino@0:70c8e56bac45, 2016-01-23 (annotated)
- 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?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yuhki50 | 0:70c8e56bac45 | 1 | /* |
| yuhki50 | 0:70c8e56bac45 | 2 | * IRremote: IRtest unittest |
| yuhki50 | 0:70c8e56bac45 | 3 | * Version 0.1 July, 2009 |
| yuhki50 | 0:70c8e56bac45 | 4 | * Copyright 2009 Ken Shirriff |
| yuhki50 | 0:70c8e56bac45 | 5 | * http://arcfn.com |
| yuhki50 | 0:70c8e56bac45 | 6 | * |
| yuhki50 | 0:70c8e56bac45 | 7 | * Note: to run these tests, edit IRremote/IRremote.h to add "#define TEST" |
| yuhki50 | 0:70c8e56bac45 | 8 | * You must then recompile the library by removing IRremote.o and restarting |
| yuhki50 | 0:70c8e56bac45 | 9 | * the arduino IDE. |
| yuhki50 | 0:70c8e56bac45 | 10 | */ |
| yuhki50 | 0:70c8e56bac45 | 11 | |
| yuhki50 | 0:70c8e56bac45 | 12 | #include <IRremote.h> |
| yuhki50 | 0:70c8e56bac45 | 13 | #include <IRremoteInt.h> |
| yuhki50 | 0:70c8e56bac45 | 14 | |
| yuhki50 | 0:70c8e56bac45 | 15 | // Dumps out the decode_results structure. |
| yuhki50 | 0:70c8e56bac45 | 16 | // Call this after IRrecv::decode() |
| yuhki50 | 0:70c8e56bac45 | 17 | // void * to work around compiler issue |
| yuhki50 | 0:70c8e56bac45 | 18 | //void dump(void *v) { |
| yuhki50 | 0:70c8e56bac45 | 19 | // decode_results *results = (decode_results *)v |
| yuhki50 | 0:70c8e56bac45 | 20 | void dump(decode_results *results) { |
| yuhki50 | 0:70c8e56bac45 | 21 | int count = results->rawlen; |
| yuhki50 | 0:70c8e56bac45 | 22 | if (results->decode_type == UNKNOWN) { |
| yuhki50 | 0:70c8e56bac45 | 23 | Serial.println("Could not decode message"); |
| yuhki50 | 0:70c8e56bac45 | 24 | } |
| yuhki50 | 0:70c8e56bac45 | 25 | else { |
| yuhki50 | 0:70c8e56bac45 | 26 | if (results->decode_type == NEC) { |
| yuhki50 | 0:70c8e56bac45 | 27 | Serial.print("Decoded NEC: "); |
| yuhki50 | 0:70c8e56bac45 | 28 | } |
| yuhki50 | 0:70c8e56bac45 | 29 | else if (results->decode_type == SONY) { |
| yuhki50 | 0:70c8e56bac45 | 30 | Serial.print("Decoded SONY: "); |
| yuhki50 | 0:70c8e56bac45 | 31 | } |
| yuhki50 | 0:70c8e56bac45 | 32 | else if (results->decode_type == RC5) { |
| yuhki50 | 0:70c8e56bac45 | 33 | Serial.print("Decoded RC5: "); |
| yuhki50 | 0:70c8e56bac45 | 34 | } |
| yuhki50 | 0:70c8e56bac45 | 35 | else if (results->decode_type == RC6) { |
| yuhki50 | 0:70c8e56bac45 | 36 | Serial.print("Decoded RC6: "); |
| yuhki50 | 0:70c8e56bac45 | 37 | } |
| yuhki50 | 0:70c8e56bac45 | 38 | Serial.print(results->value, HEX); |
| yuhki50 | 0:70c8e56bac45 | 39 | Serial.print(" ("); |
| yuhki50 | 0:70c8e56bac45 | 40 | Serial.print(results->bits, DEC); |
| yuhki50 | 0:70c8e56bac45 | 41 | Serial.println(" bits)"); |
| yuhki50 | 0:70c8e56bac45 | 42 | } |
| yuhki50 | 0:70c8e56bac45 | 43 | Serial.print("Raw ("); |
| yuhki50 | 0:70c8e56bac45 | 44 | Serial.print(count, DEC); |
| yuhki50 | 0:70c8e56bac45 | 45 | Serial.print("): "); |
| yuhki50 | 0:70c8e56bac45 | 46 | |
| yuhki50 | 0:70c8e56bac45 | 47 | for (int i = 0; i < count; i++) { |
| yuhki50 | 0:70c8e56bac45 | 48 | if ((i % 2) == 1) { |
| yuhki50 | 0:70c8e56bac45 | 49 | Serial.print(results->rawbuf[i]*USECPERTICK, DEC); |
| yuhki50 | 0:70c8e56bac45 | 50 | } |
| yuhki50 | 0:70c8e56bac45 | 51 | else { |
| yuhki50 | 0:70c8e56bac45 | 52 | Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); |
| yuhki50 | 0:70c8e56bac45 | 53 | } |
| yuhki50 | 0:70c8e56bac45 | 54 | Serial.print(" "); |
| yuhki50 | 0:70c8e56bac45 | 55 | } |
| yuhki50 | 0:70c8e56bac45 | 56 | Serial.println(""); |
| yuhki50 | 0:70c8e56bac45 | 57 | } |
| yuhki50 | 0:70c8e56bac45 | 58 | |
| yuhki50 | 0:70c8e56bac45 | 59 | IRrecv irrecv(0); |
| yuhki50 | 0:70c8e56bac45 | 60 | decode_results results; |
| yuhki50 | 0:70c8e56bac45 | 61 | |
| yuhki50 | 0:70c8e56bac45 | 62 | class IRsendDummy : |
| yuhki50 | 0:70c8e56bac45 | 63 | public IRsend |
| yuhki50 | 0:70c8e56bac45 | 64 | { |
| yuhki50 | 0:70c8e56bac45 | 65 | public: |
| yuhki50 | 0:70c8e56bac45 | 66 | // For testing, just log the marks/spaces |
| yuhki50 | 0:70c8e56bac45 | 67 | #define SENDLOG_LEN 128 |
| yuhki50 | 0:70c8e56bac45 | 68 | int sendlog[SENDLOG_LEN]; |
| yuhki50 | 0:70c8e56bac45 | 69 | int sendlogcnt; |
| yuhki50 | 0:70c8e56bac45 | 70 | IRsendDummy() : |
| yuhki50 | 0:70c8e56bac45 | 71 | IRsend() { |
| yuhki50 | 0:70c8e56bac45 | 72 | } |
| yuhki50 | 0:70c8e56bac45 | 73 | void reset() { |
| yuhki50 | 0:70c8e56bac45 | 74 | sendlogcnt = 0; |
| yuhki50 | 0:70c8e56bac45 | 75 | } |
| yuhki50 | 0:70c8e56bac45 | 76 | void mark(int time) { |
| yuhki50 | 0:70c8e56bac45 | 77 | sendlog[sendlogcnt] = time; |
| yuhki50 | 0:70c8e56bac45 | 78 | if (sendlogcnt < SENDLOG_LEN) sendlogcnt++; |
| yuhki50 | 0:70c8e56bac45 | 79 | } |
| yuhki50 | 0:70c8e56bac45 | 80 | void space(int time) { |
| yuhki50 | 0:70c8e56bac45 | 81 | sendlog[sendlogcnt] = -time; |
| yuhki50 | 0:70c8e56bac45 | 82 | if (sendlogcnt < SENDLOG_LEN) sendlogcnt++; |
| yuhki50 | 0:70c8e56bac45 | 83 | } |
| yuhki50 | 0:70c8e56bac45 | 84 | // Copies the dummy buf into the interrupt buf |
| yuhki50 | 0:70c8e56bac45 | 85 | void useDummyBuf() { |
| yuhki50 | 0:70c8e56bac45 | 86 | int last = SPACE; |
| yuhki50 | 0:70c8e56bac45 | 87 | irparams.rcvstate = STATE_STOP; |
| yuhki50 | 0:70c8e56bac45 | 88 | irparams.rawlen = 1; // Skip the gap |
| yuhki50 | 0:70c8e56bac45 | 89 | for (int i = 0 ; i < sendlogcnt; i++) { |
| yuhki50 | 0:70c8e56bac45 | 90 | if (sendlog[i] < 0) { |
| yuhki50 | 0:70c8e56bac45 | 91 | if (last == MARK) { |
| yuhki50 | 0:70c8e56bac45 | 92 | // New space |
| yuhki50 | 0:70c8e56bac45 | 93 | irparams.rawbuf[irparams.rawlen++] = (-sendlog[i] - MARK_EXCESS) / USECPERTICK; |
| yuhki50 | 0:70c8e56bac45 | 94 | last = SPACE; |
| yuhki50 | 0:70c8e56bac45 | 95 | } |
| yuhki50 | 0:70c8e56bac45 | 96 | else { |
| yuhki50 | 0:70c8e56bac45 | 97 | // More space |
| yuhki50 | 0:70c8e56bac45 | 98 | irparams.rawbuf[irparams.rawlen - 1] += -sendlog[i] / USECPERTICK; |
| yuhki50 | 0:70c8e56bac45 | 99 | } |
| yuhki50 | 0:70c8e56bac45 | 100 | } |
| yuhki50 | 0:70c8e56bac45 | 101 | else if (sendlog[i] > 0) { |
| yuhki50 | 0:70c8e56bac45 | 102 | if (last == SPACE) { |
| yuhki50 | 0:70c8e56bac45 | 103 | // New mark |
| yuhki50 | 0:70c8e56bac45 | 104 | irparams.rawbuf[irparams.rawlen++] = (sendlog[i] + MARK_EXCESS) / USECPERTICK; |
| yuhki50 | 0:70c8e56bac45 | 105 | last = MARK; |
| yuhki50 | 0:70c8e56bac45 | 106 | } |
| yuhki50 | 0:70c8e56bac45 | 107 | else { |
| yuhki50 | 0:70c8e56bac45 | 108 | // More mark |
| yuhki50 | 0:70c8e56bac45 | 109 | irparams.rawbuf[irparams.rawlen - 1] += sendlog[i] / USECPERTICK; |
| yuhki50 | 0:70c8e56bac45 | 110 | } |
| yuhki50 | 0:70c8e56bac45 | 111 | } |
| yuhki50 | 0:70c8e56bac45 | 112 | } |
| yuhki50 | 0:70c8e56bac45 | 113 | if (irparams.rawlen % 2) { |
| yuhki50 | 0:70c8e56bac45 | 114 | irparams.rawlen--; // Remove trailing space |
| yuhki50 | 0:70c8e56bac45 | 115 | } |
| yuhki50 | 0:70c8e56bac45 | 116 | } |
| yuhki50 | 0:70c8e56bac45 | 117 | }; |
| yuhki50 | 0:70c8e56bac45 | 118 | |
| yuhki50 | 0:70c8e56bac45 | 119 | IRsendDummy irsenddummy; |
| yuhki50 | 0:70c8e56bac45 | 120 | |
| yuhki50 | 0:70c8e56bac45 | 121 | void verify(unsigned long val, int bits, int type) { |
| yuhki50 | 0:70c8e56bac45 | 122 | irsenddummy.useDummyBuf(); |
| yuhki50 | 0:70c8e56bac45 | 123 | irrecv.decode(&results); |
| yuhki50 | 0:70c8e56bac45 | 124 | Serial.print("Testing "); |
| yuhki50 | 0:70c8e56bac45 | 125 | Serial.print(val, HEX); |
| yuhki50 | 0:70c8e56bac45 | 126 | if (results.value == val && results.bits == bits && results.decode_type == type) { |
| yuhki50 | 0:70c8e56bac45 | 127 | Serial.println(": OK"); |
| yuhki50 | 0:70c8e56bac45 | 128 | } |
| yuhki50 | 0:70c8e56bac45 | 129 | else { |
| yuhki50 | 0:70c8e56bac45 | 130 | Serial.println(": Error"); |
| yuhki50 | 0:70c8e56bac45 | 131 | dump(&results); |
| yuhki50 | 0:70c8e56bac45 | 132 | } |
| yuhki50 | 0:70c8e56bac45 | 133 | } |
| yuhki50 | 0:70c8e56bac45 | 134 | |
| yuhki50 | 0:70c8e56bac45 | 135 | void testNEC(unsigned long val, int bits) { |
| yuhki50 | 0:70c8e56bac45 | 136 | irsenddummy.reset(); |
| yuhki50 | 0:70c8e56bac45 | 137 | irsenddummy.sendNEC(val, bits); |
| yuhki50 | 0:70c8e56bac45 | 138 | verify(val, bits, NEC); |
| yuhki50 | 0:70c8e56bac45 | 139 | } |
| yuhki50 | 0:70c8e56bac45 | 140 | void testSony(unsigned long val, int bits) { |
| yuhki50 | 0:70c8e56bac45 | 141 | irsenddummy.reset(); |
| yuhki50 | 0:70c8e56bac45 | 142 | irsenddummy.sendSony(val, bits); |
| yuhki50 | 0:70c8e56bac45 | 143 | verify(val, bits, SONY); |
| yuhki50 | 0:70c8e56bac45 | 144 | } |
| yuhki50 | 0:70c8e56bac45 | 145 | void testRC5(unsigned long val, int bits) { |
| yuhki50 | 0:70c8e56bac45 | 146 | irsenddummy.reset(); |
| yuhki50 | 0:70c8e56bac45 | 147 | irsenddummy.sendRC5(val, bits); |
| yuhki50 | 0:70c8e56bac45 | 148 | verify(val, bits, RC5); |
| yuhki50 | 0:70c8e56bac45 | 149 | } |
| yuhki50 | 0:70c8e56bac45 | 150 | void testRC6(unsigned long val, int bits) { |
| yuhki50 | 0:70c8e56bac45 | 151 | irsenddummy.reset(); |
| yuhki50 | 0:70c8e56bac45 | 152 | irsenddummy.sendRC6(val, bits); |
| yuhki50 | 0:70c8e56bac45 | 153 | verify(val, bits, RC6); |
| yuhki50 | 0:70c8e56bac45 | 154 | } |
| yuhki50 | 0:70c8e56bac45 | 155 | |
| yuhki50 | 0:70c8e56bac45 | 156 | void test() { |
| yuhki50 | 0:70c8e56bac45 | 157 | Serial.println("NEC tests"); |
| yuhki50 | 0:70c8e56bac45 | 158 | testNEC(0x00000000, 32); |
| yuhki50 | 0:70c8e56bac45 | 159 | testNEC(0xffffffff, 32); |
| yuhki50 | 0:70c8e56bac45 | 160 | testNEC(0xaaaaaaaa, 32); |
| yuhki50 | 0:70c8e56bac45 | 161 | testNEC(0x55555555, 32); |
| yuhki50 | 0:70c8e56bac45 | 162 | testNEC(0x12345678, 32); |
| yuhki50 | 0:70c8e56bac45 | 163 | Serial.println("Sony tests"); |
| yuhki50 | 0:70c8e56bac45 | 164 | testSony(0xfff, 12); |
| yuhki50 | 0:70c8e56bac45 | 165 | testSony(0x000, 12); |
| yuhki50 | 0:70c8e56bac45 | 166 | testSony(0xaaa, 12); |
| yuhki50 | 0:70c8e56bac45 | 167 | testSony(0x555, 12); |
| yuhki50 | 0:70c8e56bac45 | 168 | testSony(0x123, 12); |
| yuhki50 | 0:70c8e56bac45 | 169 | Serial.println("RC5 tests"); |
| yuhki50 | 0:70c8e56bac45 | 170 | testRC5(0xfff, 12); |
| yuhki50 | 0:70c8e56bac45 | 171 | testRC5(0x000, 12); |
| yuhki50 | 0:70c8e56bac45 | 172 | testRC5(0xaaa, 12); |
| yuhki50 | 0:70c8e56bac45 | 173 | testRC5(0x555, 12); |
| yuhki50 | 0:70c8e56bac45 | 174 | testRC5(0x123, 12); |
| yuhki50 | 0:70c8e56bac45 | 175 | Serial.println("RC6 tests"); |
| yuhki50 | 0:70c8e56bac45 | 176 | testRC6(0xfffff, 20); |
| yuhki50 | 0:70c8e56bac45 | 177 | testRC6(0x00000, 20); |
| yuhki50 | 0:70c8e56bac45 | 178 | testRC6(0xaaaaa, 20); |
| yuhki50 | 0:70c8e56bac45 | 179 | testRC6(0x55555, 20); |
| yuhki50 | 0:70c8e56bac45 | 180 | testRC6(0x12345, 20); |
| yuhki50 | 0:70c8e56bac45 | 181 | } |
| yuhki50 | 0:70c8e56bac45 | 182 | |
| yuhki50 | 0:70c8e56bac45 | 183 | void setup() |
| yuhki50 | 0:70c8e56bac45 | 184 | { |
| yuhki50 | 0:70c8e56bac45 | 185 | Serial.begin(9600); |
| yuhki50 | 0:70c8e56bac45 | 186 | test(); |
| yuhki50 | 0:70c8e56bac45 | 187 | } |
| yuhki50 | 0:70c8e56bac45 | 188 | |
| yuhki50 | 0:70c8e56bac45 | 189 | void loop() { |
| yuhki50 | 0:70c8e56bac45 | 190 | } |