IR

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 #include <mbed.h>
00002 
00003 #include "ReceiverIR.h"
00004 #include "TextLCD.h"
00005 
00006 ReceiverIR ir_rx(p17);
00007 TextLCD lcd(p24, p26, p27, p28, p29, p30);
00008 BusOut led(LED4, LED3, LED2, LED1);
00009 Ticker ledTicker;
00010 
00011 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) {
00012     int cnt = 0;
00013     while (ir_rx.getState() != ReceiverIR::Received) {
00014         cnt++;
00015         if (timeout < cnt) {
00016             return -1;
00017         }
00018     }
00019     return ir_rx.getData(format, buf, bufsiz * 8);
00020 }
00021 
00022 void display_status(char *status, int bitlength) {
00023     lcd.locate(8, 0);
00024     lcd.printf("%-5.5s:%02d", status, bitlength);
00025 }
00026 
00027 void display_format(RemoteIR::Format format) {
00028     lcd.locate(0, 0);
00029     switch (format) {
00030         case RemoteIR::UNKNOWN:
00031             lcd.printf("????????");
00032             break;
00033         case RemoteIR::NEC:
00034             lcd.printf("NEC     ");
00035             break;
00036         case RemoteIR::NEC_REPEAT:
00037             lcd.printf("NEC  (R)");
00038             break;
00039         case RemoteIR::AEHA:
00040             lcd.printf("AEHA    ");
00041             break;
00042         case RemoteIR::AEHA_REPEAT:
00043             lcd.printf("AEHA (R)");
00044             break;
00045         case RemoteIR::SONY:
00046             lcd.printf("SONY    ");
00047             break;
00048     }
00049 }
00050 
00051 void display_data(uint8_t *buf, int bitlength) {
00052     lcd.locate(0, 1);
00053     const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
00054     for (int i = 0; i < n; i++) {
00055         lcd.printf("%02X", buf[i]);
00056     }
00057     for (int i = 0; i < 8 - n; i++) {
00058         lcd.printf("--");
00059     }
00060 }
00061 
00062 void ledfunc(void) {
00063     led = led + 1;
00064 }
00065 
00066 int main(void) {
00067     ledTicker.attach(&ledfunc, 0.5);
00068 
00069     led = 0;
00070     lcd.cls();
00071     lcd.locate(0, 0);
00072     lcd.printf("Press a button  ");
00073     lcd.locate(0, 1);
00074     lcd.printf("on a controller.");
00075 
00076     while (1) {
00077         uint8_t buf1[32];
00078         uint8_t buf2[32];
00079         int bitlength1;
00080         int bitlength2;
00081         RemoteIR::Format format;
00082 
00083         memset(buf1, 0x00, sizeof(buf1));
00084         memset(buf2, 0x00, sizeof(buf2));
00085 
00086         {
00087             bitlength1 = receive(&format, buf1, sizeof(buf1));
00088             if (bitlength1 < 0) {
00089                 continue;
00090             }
00091             display_status("RECV", bitlength1);
00092             display_data(buf1, bitlength1);
00093             display_format(format);
00094         }
00095 
00096     }
00097 }