Shinichiro Nakamura / Mbed 2 deprecated StarBoardOrangeExample2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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