Junichi Katsu / Mbed 2 deprecated MIB_RemoteIR_TestProgram

Dependencies:   mbed TextLCD RemoteIR

Fork of RemoteIR_TestProgram by Shinichiro Nakamura

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 
00013 #define TEST_LOOP_BACK  0
00014 
00015 ReceiverIR ir_rx(PA_2);
00016 TransmitterIR ir_tx(PF_0);
00017 Serial pc(USBTX,USBRX);
00018 
00019 /**
00020  * Receive.
00021  *
00022  * @param format Pointer to a format.
00023  * @param buf Pointer to a buffer.
00024  * @param bufsiz Size of the buffer.
00025  *
00026  * @return Bit length of the received data.
00027  */
00028 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) {
00029     int cnt = 0;
00030     while (ir_rx.getState() != ReceiverIR::Received) {
00031         cnt++;
00032         if (timeout < cnt) {
00033             return -1;
00034         }
00035     }
00036     return ir_rx.getData(format, buf, bufsiz * 8);
00037 }
00038 
00039 /**
00040  * Transmit.
00041  *
00042  * @param format Format.
00043  * @param buf Pointer to a buffer.
00044  * @param bitlength Bit length of the data.
00045  *
00046  * @return Bit length of the received data.
00047  */
00048 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) {
00049     int cnt = 0;
00050     while (ir_tx.getState() != TransmitterIR::Idle) {
00051         cnt++;
00052         if (timeout < cnt) {
00053             return -1;
00054         }
00055     }
00056     return ir_tx.setData(format, buf, bitlength);
00057 }
00058 
00059 /**
00060  * Display a current status.
00061  */
00062 void display_status(char *status, int bitlength) {
00063     pc.printf("%-5.5s:%02d\r\n", status, bitlength);
00064 }
00065 
00066 /**
00067  * Display a format of a data.
00068  */
00069 void display_format(RemoteIR::Format format) {
00070 
00071     switch (format) {
00072         case RemoteIR::UNKNOWN:
00073             pc.printf("????????\r\n");
00074             break;
00075         case RemoteIR::NEC:
00076             pc.printf("NEC     \r\n");
00077             break;
00078         case RemoteIR::NEC_REPEAT:
00079             pc.printf("NEC  (R)\r\n");
00080             break;
00081         case RemoteIR::AEHA:
00082             pc.printf("AEHA    \r\n");
00083             break;
00084         case RemoteIR::AEHA_REPEAT:
00085             pc.printf("AEHA (R)\r\n");
00086             break;
00087         case RemoteIR::SONY:
00088             pc.printf("SONY    \r\n");
00089             break;
00090     }
00091 }
00092 
00093 /**
00094  * Display a data.
00095  *
00096  * @param buf Pointer to a buffer.
00097  * @param bitlength Bit length of a data.
00098  */
00099 void display_data(uint8_t *buf, int bitlength) {
00100     const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
00101     for (int i = 0; i < n; i++) {
00102         pc.printf("%02X", buf[i]);
00103     }
00104     for (int i = 0; i < 8 - n; i++) {
00105         pc.printf("--");
00106     }
00107 }
00108 
00109 /**
00110  * Entry point.
00111  */
00112 int main(void) {
00113 
00114 
00115     /*
00116      * Splash.
00117      */
00118     pc.printf("RemoteIR        \r\n");
00119     pc.printf("Program example.\r\n");
00120     wait(3);
00121 
00122     /*
00123      * Initialize.
00124      */
00125 
00126     pc.printf("Press a button  \r\n");
00127     pc.printf("on a controller.\r\n");
00128 
00129     /*
00130      * Execute.
00131      */
00132     while (1) {
00133         uint8_t buf1[32];
00134         uint8_t buf2[32];
00135         int bitlength1;
00136         int bitlength2;
00137         RemoteIR::Format format;
00138 
00139         memset(buf1, 0x00, sizeof(buf1));
00140         memset(buf2, 0x00, sizeof(buf2));
00141 
00142         {
00143             bitlength1 = receive(&format, buf1, sizeof(buf1));
00144             if (bitlength1 < 0) {
00145                 continue;
00146             }
00147             display_status("RECV", bitlength1);
00148             display_data(buf1, bitlength1);
00149             display_format(format);
00150         }
00151 
00152 #if TEST_LOOP_BACK
00153         wait_ms(100);
00154 
00155         {
00156             bitlength1 = transmit(format, buf1, bitlength1);
00157             if (bitlength1 < 0) {
00158                 continue;
00159             }
00160             display_status("TRAN", bitlength1);
00161             display_data(buf1, bitlength1);
00162             display_format(format);
00163         }
00164 
00165         wait_ms(100);
00166 
00167         {
00168             bitlength2 = receive(&format, buf2, sizeof(buf2));
00169             if (bitlength2 < 0) {
00170                 continue;
00171             }
00172             display_status("RECV", bitlength2);
00173             display_data(buf2, bitlength2);
00174             display_format(format);
00175         }
00176 
00177         wait_ms(100);
00178 
00179         {
00180             for (int i = 0; i < sizeof(buf1); i++) {
00181                 if (buf1[i] != buf2[i]) {
00182                     display_status("CPERR", bitlength2);
00183                     wait(1);
00184                     continue;
00185                 }
00186             }
00187         }
00188 #endif
00189     }
00190 }