...

Dependencies:   RemoteIR mbed

Fork of SmartRemote by Sam Kirsch

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IR.cpp Source File

IR.cpp

00001 #include "mbed.h"
00002 #include "IR.h"
00003 #include "ReceiverIR.h"
00004 #include "TransmitterIR.h"
00005 
00006 ReceiverIR ir_rx(p15);
00007 TransmitterIR ir_tx(p21);
00008 
00009 /**
00010  * Receive.
00011  *
00012  * @param format Pointer to a format.
00013  * @param buf Pointer to a buffer.
00014  * @param bufsiz Size of the buffer.
00015  *
00016  * @return Bit length of the received data.
00017  */
00018 int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout) {
00019     int cnt = 0;
00020     while (ir_rx.getState() != ReceiverIR::Received) {
00021         cnt++;
00022         if (timeout < cnt) {
00023             return -1;
00024         }
00025     }
00026     return ir_rx.getData(format, buf, bufsiz * 8);
00027 }
00028 
00029 /**
00030  * Transmit.
00031  *
00032  * @param format Format.
00033  * @param buf Pointer to a buffer.
00034  * @param bitlength Bit length of the data.
00035  *
00036  * @return Bit length of the received data.
00037  */
00038 int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout) {
00039     int cnt = 0;
00040     while (ir_tx.getState() != TransmitterIR::Idle) {
00041         cnt++;
00042         if (timeout < cnt) {
00043             return -1;
00044         }
00045     }
00046     return ir_tx.setData(format, buf, bitlength);
00047 }
00048 
00049 /**
00050  * Display a current status.
00051  */
00052 void display_status(char *status, int bitlength) {
00053 
00054     printf("%-5.5s:%02d \n \r", status, bitlength);
00055 }
00056 
00057 /**
00058  * Display a format of a data.
00059  */
00060 void display_format(RemoteIR::Format format) {
00061 
00062     switch (format) {
00063         case RemoteIR::UNKNOWN:
00064             printf("????????");
00065             break;
00066         case RemoteIR::NEC:
00067             printf("NEC     ");
00068             break;
00069         case RemoteIR::NEC_REPEAT:
00070             printf("NEC  (R)");
00071             break;
00072         case RemoteIR::AEHA:
00073             printf("AEHA    ");
00074             break;
00075         case RemoteIR::AEHA_REPEAT:
00076             printf("AEHA (R)");
00077             break;
00078         case RemoteIR::SONY:
00079             printf("SONY    ");
00080             break;
00081     }
00082 }
00083 
00084 /**
00085  * Display a data.
00086  *
00087  * @param buf Pointer to a buffer.
00088  * @param bitlength Bit length of a data.
00089  */
00090 void display_data(uint8_t *buf, int bitlength) {
00091     //lcd.locate(0, 1);
00092     const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
00093     for (int i = 0; i < n; i++) {
00094         printf("%02X", buf[i]);
00095     }
00096     for (int i = 0; i < 8 - n; i++) {
00097         printf("--\n \r");
00098     }
00099     
00100 }