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