![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test for Receiving IR
Dependencies: mbed TextLCD RemoteIR
main.cpp@5:40750d5779ca, 2010-10-13 (annotated)
- Committer:
- shintamainjp
- Date:
- Wed Oct 13 10:51:41 2010 +0000
- Revision:
- 5:40750d5779ca
- Parent:
- 4:a96406427829
- Child:
- 6:365e9629ef25
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 2:9e3081533f70 | 1 | /** |
shintamainjp | 5:40750d5779ca | 2 | * RemoteIR library - Test program. |
shintamainjp | 2:9e3081533f70 | 3 | * |
shintamainjp | 2:9e3081533f70 | 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 2:9e3081533f70 | 5 | * http://shinta.main.jp/ |
shintamainjp | 2:9e3081533f70 | 6 | */ |
shintamainjp | 2:9e3081533f70 | 7 | |
shintamainjp | 2:9e3081533f70 | 8 | #include <mbed.h> |
shintamainjp | 2:9e3081533f70 | 9 | |
shintamainjp | 2:9e3081533f70 | 10 | #include "ReceiverIR.h" |
shintamainjp | 2:9e3081533f70 | 11 | #include "TransmitterIR.h" |
shintamainjp | 2:9e3081533f70 | 12 | #include "TextLCD.h" |
shintamainjp | 2:9e3081533f70 | 13 | |
shintamainjp | 4:a96406427829 | 14 | #define TEST_LOOP_BACK 0 |
shintamainjp | 2:9e3081533f70 | 15 | |
shintamainjp | 3:10d1579b495c | 16 | ReceiverIR ir_rx(p15); |
shintamainjp | 2:9e3081533f70 | 17 | TransmitterIR ir_tx(p21); |
shintamainjp | 2:9e3081533f70 | 18 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
shintamainjp | 2:9e3081533f70 | 19 | BusOut led(LED4, LED3, LED2, LED1); |
shintamainjp | 2:9e3081533f70 | 20 | Ticker ledTicker; |
shintamainjp | 2:9e3081533f70 | 21 | |
shintamainjp | 2:9e3081533f70 | 22 | /** |
shintamainjp | 2:9e3081533f70 | 23 | * Receive. |
shintamainjp | 2:9e3081533f70 | 24 | * |
shintamainjp | 2:9e3081533f70 | 25 | * @param format Pointer to a format. |
shintamainjp | 2:9e3081533f70 | 26 | * @param buf Pointer to a buffer. |
shintamainjp | 2:9e3081533f70 | 27 | * @param bufsiz Size of the buffer. |
shintamainjp | 2:9e3081533f70 | 28 | * |
shintamainjp | 2:9e3081533f70 | 29 | * @return Bit length of the received data. |
shintamainjp | 2:9e3081533f70 | 30 | */ |
shintamainjp | 2:9e3081533f70 | 31 | int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) { |
shintamainjp | 2:9e3081533f70 | 32 | int cnt = 0; |
shintamainjp | 2:9e3081533f70 | 33 | while (ir_rx.getState() != ReceiverIR::Received) { |
shintamainjp | 2:9e3081533f70 | 34 | cnt++; |
shintamainjp | 2:9e3081533f70 | 35 | if (timeout < cnt) { |
shintamainjp | 2:9e3081533f70 | 36 | return -1; |
shintamainjp | 2:9e3081533f70 | 37 | } |
shintamainjp | 2:9e3081533f70 | 38 | } |
shintamainjp | 2:9e3081533f70 | 39 | return ir_rx.getData(format, buf, bufsiz * 8); |
shintamainjp | 2:9e3081533f70 | 40 | } |
shintamainjp | 2:9e3081533f70 | 41 | |
shintamainjp | 2:9e3081533f70 | 42 | /** |
shintamainjp | 2:9e3081533f70 | 43 | * Transmit. |
shintamainjp | 2:9e3081533f70 | 44 | * |
shintamainjp | 2:9e3081533f70 | 45 | * @param format Format. |
shintamainjp | 2:9e3081533f70 | 46 | * @param buf Pointer to a buffer. |
shintamainjp | 2:9e3081533f70 | 47 | * @param bitlength Bit length of the data. |
shintamainjp | 2:9e3081533f70 | 48 | * |
shintamainjp | 2:9e3081533f70 | 49 | * @return Bit length of the received data. |
shintamainjp | 2:9e3081533f70 | 50 | */ |
shintamainjp | 2:9e3081533f70 | 51 | int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) { |
shintamainjp | 2:9e3081533f70 | 52 | int cnt = 0; |
shintamainjp | 2:9e3081533f70 | 53 | while (ir_tx.getState() != TransmitterIR::Idle) { |
shintamainjp | 2:9e3081533f70 | 54 | cnt++; |
shintamainjp | 2:9e3081533f70 | 55 | if (timeout < cnt) { |
shintamainjp | 2:9e3081533f70 | 56 | return -1; |
shintamainjp | 2:9e3081533f70 | 57 | } |
shintamainjp | 2:9e3081533f70 | 58 | } |
shintamainjp | 2:9e3081533f70 | 59 | return ir_tx.setData(format, buf, bitlength); |
shintamainjp | 2:9e3081533f70 | 60 | } |
shintamainjp | 2:9e3081533f70 | 61 | |
shintamainjp | 2:9e3081533f70 | 62 | /** |
shintamainjp | 2:9e3081533f70 | 63 | * Display a current status. |
shintamainjp | 2:9e3081533f70 | 64 | */ |
shintamainjp | 2:9e3081533f70 | 65 | void display_status(char *status, int bitlength) { |
shintamainjp | 2:9e3081533f70 | 66 | lcd.locate(8, 0); |
shintamainjp | 2:9e3081533f70 | 67 | lcd.printf("%-5.5s:%02d", status, bitlength); |
shintamainjp | 2:9e3081533f70 | 68 | } |
shintamainjp | 2:9e3081533f70 | 69 | |
shintamainjp | 2:9e3081533f70 | 70 | /** |
shintamainjp | 2:9e3081533f70 | 71 | * Display a format of a data. |
shintamainjp | 2:9e3081533f70 | 72 | */ |
shintamainjp | 2:9e3081533f70 | 73 | void display_format(RemoteIR::Format format) { |
shintamainjp | 2:9e3081533f70 | 74 | lcd.locate(0, 0); |
shintamainjp | 2:9e3081533f70 | 75 | switch (format) { |
shintamainjp | 2:9e3081533f70 | 76 | case RemoteIR::UNKNOWN: |
shintamainjp | 2:9e3081533f70 | 77 | lcd.printf("????????"); |
shintamainjp | 2:9e3081533f70 | 78 | break; |
shintamainjp | 2:9e3081533f70 | 79 | case RemoteIR::NEC: |
shintamainjp | 2:9e3081533f70 | 80 | lcd.printf("NEC "); |
shintamainjp | 2:9e3081533f70 | 81 | break; |
shintamainjp | 2:9e3081533f70 | 82 | case RemoteIR::NEC_REPEAT: |
shintamainjp | 2:9e3081533f70 | 83 | lcd.printf("NEC (R)"); |
shintamainjp | 2:9e3081533f70 | 84 | break; |
shintamainjp | 2:9e3081533f70 | 85 | case RemoteIR::AEHA: |
shintamainjp | 2:9e3081533f70 | 86 | lcd.printf("AEHA "); |
shintamainjp | 2:9e3081533f70 | 87 | break; |
shintamainjp | 2:9e3081533f70 | 88 | case RemoteIR::AEHA_REPEAT: |
shintamainjp | 2:9e3081533f70 | 89 | lcd.printf("AEHA (R)"); |
shintamainjp | 2:9e3081533f70 | 90 | break; |
shintamainjp | 2:9e3081533f70 | 91 | case RemoteIR::SONY: |
shintamainjp | 2:9e3081533f70 | 92 | lcd.printf("SONY "); |
shintamainjp | 2:9e3081533f70 | 93 | break; |
shintamainjp | 2:9e3081533f70 | 94 | } |
shintamainjp | 2:9e3081533f70 | 95 | } |
shintamainjp | 2:9e3081533f70 | 96 | |
shintamainjp | 2:9e3081533f70 | 97 | /** |
shintamainjp | 2:9e3081533f70 | 98 | * Display a data. |
shintamainjp | 2:9e3081533f70 | 99 | * |
shintamainjp | 2:9e3081533f70 | 100 | * @param buf Pointer to a buffer. |
shintamainjp | 2:9e3081533f70 | 101 | * @param bitlength Bit length of a data. |
shintamainjp | 2:9e3081533f70 | 102 | */ |
shintamainjp | 2:9e3081533f70 | 103 | void display_data(uint8_t *buf, int bitlength) { |
shintamainjp | 2:9e3081533f70 | 104 | lcd.locate(0, 1); |
shintamainjp | 2:9e3081533f70 | 105 | const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0); |
shintamainjp | 2:9e3081533f70 | 106 | for (int i = 0; i < n; i++) { |
shintamainjp | 2:9e3081533f70 | 107 | lcd.printf("%02X", buf[i]); |
shintamainjp | 2:9e3081533f70 | 108 | } |
shintamainjp | 2:9e3081533f70 | 109 | for (int i = 0; i < 8 - n; i++) { |
shintamainjp | 2:9e3081533f70 | 110 | lcd.printf("--"); |
shintamainjp | 2:9e3081533f70 | 111 | } |
shintamainjp | 2:9e3081533f70 | 112 | } |
shintamainjp | 2:9e3081533f70 | 113 | |
shintamainjp | 2:9e3081533f70 | 114 | void ledfunc(void) { |
shintamainjp | 2:9e3081533f70 | 115 | led = led + 1; |
shintamainjp | 2:9e3081533f70 | 116 | } |
shintamainjp | 2:9e3081533f70 | 117 | |
shintamainjp | 2:9e3081533f70 | 118 | /** |
shintamainjp | 2:9e3081533f70 | 119 | * Entry point. |
shintamainjp | 2:9e3081533f70 | 120 | */ |
shintamainjp | 2:9e3081533f70 | 121 | int main(void) { |
shintamainjp | 2:9e3081533f70 | 122 | |
shintamainjp | 2:9e3081533f70 | 123 | ledTicker.attach(&ledfunc, 0.5); |
shintamainjp | 2:9e3081533f70 | 124 | |
shintamainjp | 2:9e3081533f70 | 125 | /* |
shintamainjp | 2:9e3081533f70 | 126 | * Splash. |
shintamainjp | 2:9e3081533f70 | 127 | */ |
shintamainjp | 2:9e3081533f70 | 128 | lcd.cls(); |
shintamainjp | 2:9e3081533f70 | 129 | lcd.locate(0, 0); |
shintamainjp | 2:9e3081533f70 | 130 | lcd.printf("RemoteIR "); |
shintamainjp | 2:9e3081533f70 | 131 | lcd.locate(0, 1); |
shintamainjp | 2:9e3081533f70 | 132 | lcd.printf("Program example."); |
shintamainjp | 2:9e3081533f70 | 133 | wait(3); |
shintamainjp | 2:9e3081533f70 | 134 | |
shintamainjp | 2:9e3081533f70 | 135 | /* |
shintamainjp | 2:9e3081533f70 | 136 | * Initialize. |
shintamainjp | 2:9e3081533f70 | 137 | */ |
shintamainjp | 2:9e3081533f70 | 138 | led = 0; |
shintamainjp | 2:9e3081533f70 | 139 | lcd.cls(); |
shintamainjp | 2:9e3081533f70 | 140 | lcd.locate(0, 0); |
shintamainjp | 2:9e3081533f70 | 141 | lcd.printf("Press a button "); |
shintamainjp | 2:9e3081533f70 | 142 | lcd.locate(0, 1); |
shintamainjp | 2:9e3081533f70 | 143 | lcd.printf("on a controller."); |
shintamainjp | 2:9e3081533f70 | 144 | |
shintamainjp | 2:9e3081533f70 | 145 | /* |
shintamainjp | 2:9e3081533f70 | 146 | * Execute. |
shintamainjp | 2:9e3081533f70 | 147 | */ |
shintamainjp | 2:9e3081533f70 | 148 | while (1) { |
shintamainjp | 2:9e3081533f70 | 149 | uint8_t buf1[32]; |
shintamainjp | 2:9e3081533f70 | 150 | uint8_t buf2[32]; |
shintamainjp | 2:9e3081533f70 | 151 | int bitlength1; |
shintamainjp | 2:9e3081533f70 | 152 | int bitlength2; |
shintamainjp | 2:9e3081533f70 | 153 | RemoteIR::Format format; |
shintamainjp | 2:9e3081533f70 | 154 | |
shintamainjp | 2:9e3081533f70 | 155 | memset(buf1, 0x00, sizeof(buf1)); |
shintamainjp | 2:9e3081533f70 | 156 | memset(buf2, 0x00, sizeof(buf2)); |
shintamainjp | 2:9e3081533f70 | 157 | |
shintamainjp | 2:9e3081533f70 | 158 | { |
shintamainjp | 2:9e3081533f70 | 159 | bitlength1 = receive(&format, buf1, sizeof(buf1)); |
shintamainjp | 2:9e3081533f70 | 160 | if (bitlength1 < 0) { |
shintamainjp | 2:9e3081533f70 | 161 | continue; |
shintamainjp | 2:9e3081533f70 | 162 | } |
shintamainjp | 2:9e3081533f70 | 163 | display_status("RECV", bitlength1); |
shintamainjp | 2:9e3081533f70 | 164 | display_data(buf1, bitlength1); |
shintamainjp | 2:9e3081533f70 | 165 | display_format(format); |
shintamainjp | 2:9e3081533f70 | 166 | } |
shintamainjp | 2:9e3081533f70 | 167 | |
shintamainjp | 2:9e3081533f70 | 168 | #if TEST_LOOP_BACK |
shintamainjp | 2:9e3081533f70 | 169 | wait_ms(100); |
shintamainjp | 2:9e3081533f70 | 170 | |
shintamainjp | 2:9e3081533f70 | 171 | { |
shintamainjp | 2:9e3081533f70 | 172 | bitlength1 = transmit(format, buf1, bitlength1); |
shintamainjp | 2:9e3081533f70 | 173 | if (bitlength1 < 0) { |
shintamainjp | 2:9e3081533f70 | 174 | continue; |
shintamainjp | 2:9e3081533f70 | 175 | } |
shintamainjp | 2:9e3081533f70 | 176 | display_status("TRAN", bitlength1); |
shintamainjp | 2:9e3081533f70 | 177 | display_data(buf1, bitlength1); |
shintamainjp | 2:9e3081533f70 | 178 | display_format(format); |
shintamainjp | 2:9e3081533f70 | 179 | } |
shintamainjp | 2:9e3081533f70 | 180 | |
shintamainjp | 2:9e3081533f70 | 181 | wait_ms(100); |
shintamainjp | 2:9e3081533f70 | 182 | |
shintamainjp | 2:9e3081533f70 | 183 | { |
shintamainjp | 2:9e3081533f70 | 184 | bitlength2 = receive(&format, buf2, sizeof(buf2)); |
shintamainjp | 2:9e3081533f70 | 185 | if (bitlength2 < 0) { |
shintamainjp | 2:9e3081533f70 | 186 | continue; |
shintamainjp | 2:9e3081533f70 | 187 | } |
shintamainjp | 2:9e3081533f70 | 188 | display_status("RECV", bitlength2); |
shintamainjp | 2:9e3081533f70 | 189 | display_data(buf2, bitlength2); |
shintamainjp | 2:9e3081533f70 | 190 | display_format(format); |
shintamainjp | 2:9e3081533f70 | 191 | } |
shintamainjp | 2:9e3081533f70 | 192 | |
shintamainjp | 2:9e3081533f70 | 193 | wait_ms(100); |
shintamainjp | 2:9e3081533f70 | 194 | |
shintamainjp | 2:9e3081533f70 | 195 | { |
shintamainjp | 2:9e3081533f70 | 196 | for (int i = 0; i < sizeof(buf1); i++) { |
shintamainjp | 2:9e3081533f70 | 197 | if (buf1[i] != buf2[i]) { |
shintamainjp | 2:9e3081533f70 | 198 | display_status("CPERR", bitlength2); |
shintamainjp | 2:9e3081533f70 | 199 | wait(1); |
shintamainjp | 2:9e3081533f70 | 200 | continue; |
shintamainjp | 2:9e3081533f70 | 201 | } |
shintamainjp | 2:9e3081533f70 | 202 | } |
shintamainjp | 2:9e3081533f70 | 203 | } |
shintamainjp | 2:9e3081533f70 | 204 | #endif |
shintamainjp | 2:9e3081533f70 | 205 | } |
shintamainjp | 0:d081f6c767da | 206 | } |