Gitakichi Tokyo / Mbed 2 deprecated IR-remote

Dependencies:   mbed

main.cpp

Committer:
gitakichi
Date:
2020-02-11
Revision:
4:ab83633679f4
Parent:
3:31c005437fa5
Child:
5:5bf6cd9dffb7

File content as of revision 4:ab83633679f4:

//reference from
//https://www.hiramine.com/physicalcomputing/mbed/irreceiver.html
//http://elm-chan.org/docs/ir_format.html
#include "mbed.h"

//compatible
//http://akizukidenshi.com/catalog/g/gM-07245/

//maybe 0000100011110111 is customer code
#define OPT_PWR     "000010001111011100011011111001001"
#define OPT_A       "000010001111011100011111111000001"
#define OPT_B       "000010001111011100011110111000011"
#define OPT_C       "000010001111011100011010111001011"
#define OPT_CENTRE  "000010001111011100000100111110111"
#define OPT_TOP     "000010001111011100000101111110101"
#define OPT_UNDER   "000010001111011100000000111111111"
#define OPT_LEFT    "000010001111011100001000111101111"
#define OPT_RIGHT   "000010001111011100000001111111101"

DigitalIn g_dpinIrReceiver(D2);
Serial g_serial(USBTX, USBRX);
DigitalOut myled(LED1);

int main()
{
    int iState_prev = 1;
    int t_negedge,t_posedge,t_delta,tx_delay;
    int data_en = 0;
    char rx_data[64] = "";
    int busy = 0;

    g_serial.baud(115200);
    Timer timer;
    timer.start();

    while(1) {
        if(busy == 1) {
            for(int i=0;i<tx_delay;i++){
                wait_ms(1000);
            }
            //tx IR
            myled = 0;
            busy = 0;
        } else {
            int iState = g_dpinIrReceiver;
            //detect H signal
            if( iState != iState_prev ) {
                if(iState == 0) {
                    //timer.start();//reset
                    t_negedge = timer.read_us();//start(detect negedge

                    if(data_en == 1) {
                        t_delta = t_negedge - t_posedge;
                        data_en = 0;
                        if(t_delta > 1523)    strcat(rx_data, "1");//625*2.5
                        else                  strcat(rx_data, "0");

                        if(strlen(rx_data) == 33) {
                            busy = 1;
                            g_serial.printf("received=");
                            if      (strcmp(rx_data,OPT_PWR) == 0) {
                                g_serial.printf("PWR");
                                myled = 1;
                                tx_delay = 10;
                                //tx_data = CHANGHONG_23;
                            } else if (strcmp(rx_data,OPT_A) == 0)        g_serial.printf("A");
                            else if (strcmp(rx_data,OPT_B) == 0)        g_serial.printf("B");
                            else if (strcmp(rx_data,OPT_C) == 0)        g_serial.printf("C");
                            else if (strcmp(rx_data,OPT_CENTRE) == 0)   g_serial.printf("CENTRE");
                            else if (strcmp(rx_data,OPT_TOP) == 0)      g_serial.printf("TOP");
                            else if (strcmp(rx_data,OPT_UNDER) == 0)    g_serial.printf("UNDER");
                            else if (strcmp(rx_data,OPT_LEFT) == 0)     g_serial.printf("LEFT");
                            else if (strcmp(rx_data,OPT_RIGHT) == 0)    g_serial.printf("RIGHT");
                            g_serial.printf("\r\n");
                        }
                    }
                } else if(iState == 1) { //end (detect posedge
                    t_posedge = timer.read_us();
                    //compare and output
                    t_delta = t_posedge - t_negedge;

                    if(t_delta > 8430) {//625*15, flame
                        rx_data[0] = '\0';
                    } else  data_en = 1;
                }
            }
            iState_prev = iState;
        }
    }
}