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.
main.cpp
00001 /** 00002 * RemoteIR library - Test program. 00003 * 00004 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) 00005 * http://shinta.main.jp/ 00006 */ 00007 00008 #include <mbed.h> 00009 00010 #include "ReceiverIR.h" 00011 #include "TransmitterIR.h" 00012 #include "TextLCD.h" 00013 00014 #define TEST_LOOP_BACK 0 00015 00016 ReceiverIR ir_rx(p15); 00017 TransmitterIR ir_tx(p21); 00018 TextLCD lcd(p24, p26, p27, p28, p29, p30); 00019 BusOut led(LED4, LED3, LED2, LED1); 00020 Ticker ledTicker; 00021 00022 /** 00023 * Receive. 00024 * 00025 * @param format Pointer to a format. 00026 * @param buf Pointer to a buffer. 00027 * @param bufsiz Size of the buffer. 00028 * 00029 * @return Bit length of the received data. 00030 */ 00031 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) { 00032 int cnt = 0; 00033 while (ir_rx.getState() != ReceiverIR::Received) { 00034 cnt++; 00035 if (timeout < cnt) { 00036 return -1; 00037 } 00038 } 00039 return ir_rx.getData(format, buf, bufsiz * 8); 00040 } 00041 00042 /** 00043 * Transmit. 00044 * 00045 * @param format Format. 00046 * @param buf Pointer to a buffer. 00047 * @param bitlength Bit length of the data. 00048 * 00049 * @return Bit length of the received data. 00050 */ 00051 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) { 00052 int cnt = 0; 00053 while (ir_tx.getState() != TransmitterIR::Idle) { 00054 cnt++; 00055 if (timeout < cnt) { 00056 return -1; 00057 } 00058 } 00059 return ir_tx.setData(format, buf, bitlength); 00060 } 00061 00062 /** 00063 * Display a current status. 00064 */ 00065 void display_status(char *status, int bitlength) { 00066 lcd.locate(8, 0); 00067 lcd.printf("%-5.5s:%02d", status, bitlength); 00068 } 00069 00070 /** 00071 * Display a format of a data. 00072 */ 00073 void display_format(RemoteIR::Format format) { 00074 lcd.locate(0, 0); 00075 switch (format) { 00076 case RemoteIR::UNKNOWN: 00077 lcd.printf("????????"); 00078 break; 00079 case RemoteIR::NEC: 00080 lcd.printf("NEC "); 00081 break; 00082 case RemoteIR::NEC_REPEAT: 00083 lcd.printf("NEC (R)"); 00084 break; 00085 case RemoteIR::AEHA: 00086 lcd.printf("AEHA "); 00087 break; 00088 case RemoteIR::AEHA_REPEAT: 00089 lcd.printf("AEHA (R)"); 00090 break; 00091 case RemoteIR::SONY: 00092 lcd.printf("SONY "); 00093 break; 00094 } 00095 } 00096 00097 /** 00098 * Display a data. 00099 * 00100 * @param buf Pointer to a buffer. 00101 * @param bitlength Bit length of a data. 00102 */ 00103 void display_data(uint8_t *buf, int bitlength) { 00104 lcd.locate(0, 1); 00105 const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0); 00106 for (int i = 0; i < n; i++) { 00107 lcd.printf("%02X", buf[i]); 00108 } 00109 for (int i = 0; i < 8 - n; i++) { 00110 lcd.printf("--"); 00111 } 00112 } 00113 00114 void ledfunc(void) { 00115 led = led + 1; 00116 } 00117 00118 /** 00119 * Entry point. 00120 */ 00121 int main(void) { 00122 00123 ledTicker.attach(&ledfunc, 0.5); 00124 00125 /* 00126 * Splash. 00127 */ 00128 lcd.cls(); 00129 lcd.locate(0, 0); 00130 lcd.printf("RemoteIR "); 00131 lcd.locate(0, 1); 00132 lcd.printf("Program example."); 00133 wait(3); 00134 00135 /* 00136 * Initialize. 00137 */ 00138 led = 0; 00139 lcd.cls(); 00140 lcd.locate(0, 0); 00141 lcd.printf("Press a button "); 00142 lcd.locate(0, 1); 00143 lcd.printf("on a controller."); 00144 00145 /* 00146 * Execute. 00147 */ 00148 while (1) { 00149 uint8_t buf1[32]; 00150 uint8_t buf2[32]; 00151 int bitlength1; 00152 int bitlength2; 00153 RemoteIR::Format format; 00154 00155 memset(buf1, 0x00, sizeof(buf1)); 00156 memset(buf2, 0x00, sizeof(buf2)); 00157 00158 { 00159 bitlength1 = receive(&format, buf1, sizeof(buf1)); 00160 if (bitlength1 < 0) { 00161 continue; 00162 } 00163 display_status("RECV", bitlength1); 00164 display_data(buf1, bitlength1); 00165 display_format(format); 00166 } 00167 00168 #if TEST_LOOP_BACK 00169 wait_ms(100); 00170 00171 { 00172 bitlength1 = transmit(format, buf1, bitlength1); 00173 if (bitlength1 < 0) { 00174 continue; 00175 } 00176 display_status("TRAN", bitlength1); 00177 display_data(buf1, bitlength1); 00178 display_format(format); 00179 } 00180 00181 wait_ms(100); 00182 00183 { 00184 bitlength2 = receive(&format, buf2, sizeof(buf2)); 00185 if (bitlength2 < 0) { 00186 continue; 00187 } 00188 display_status("RECV", bitlength2); 00189 display_data(buf2, bitlength2); 00190 display_format(format); 00191 } 00192 00193 wait_ms(100); 00194 00195 { 00196 for (int i = 0; i < sizeof(buf1); i++) { 00197 if (buf1[i] != buf2[i]) { 00198 display_status("CPERR", bitlength2); 00199 wait(1); 00200 continue; 00201 } 00202 } 00203 } 00204 #endif 00205 } 00206 }
Generated on Thu Jul 14 2022 04:09:28 by
1.7.2
Infrared Communication