Communicate to bluetooth devices or wifi per at-commands

Dependents:   Nucleo_bt

at.cpp

Committer:
rainerraul
Date:
2018-08-16
Revision:
1:ce7fb335aa1b
Parent:
0:413f3c13a00a
Child:
2:b8d28a4f6b02

File content as of revision 1:ce7fb335aa1b:


#include "mbed.h"
#include "at.h"

#define TX PA_9
#define RX PA_10

#define TX1 PC_6
#define RX1 PC_7

Serial atserial0(TX, RX); //UART1
Serial atserial1(TX1, RX1); //UART6

atterm at0;
atterm at1;
Timer t;

void CallBack0()
{
    at0.sign = atserial0.getc();


    if(at0.sign != '\r')  {  //with no linefeed and carriage return, string is written in buffer
        at0.buffer[at0.a] = at0.sign;
        at0.a ++;
    } else {   //if user typed in cr and lf, the content of buffer is send to atcommand device

        if(at0.off == 0)  {
            atserial0.printf("Read: %s\r\n", at0.buffer); // via printf formatted as string for debug use
        }

    }
}

void CallBack1()
{
    at1.sign = atserial1.getc();


    if(at1.sign != '\r')  {  //with no linefeed and carriage return, string is written in buffer
        at1.buffer[at1.a] = at1.sign;
        at1.a ++;
    } else {   //if user typed in cr and lf, the content of buffer is send to atcommand device

        if(at1.off == 0)  {
            atserial1.printf("Read: %s\r\n", at1.buffer); // via printf formatted as string for debug use
        }

    }
}

void atterm::debug_off(bool l)
{
    if(l) atterm::off = 1;
    if(!l) atterm::off = 0;
}

void atterm::at_send(char *format, char *buf)
{
    atserial0.printf(format, buf);
}

void atterm::clear()
{
    memset(atterm::buffer, 0, sizeof(atterm::buffer)); //set buffer to 0, for next clean read
    atterm::a = 0; //index pointer to zero

}

void atterm::device_init(unsigned long baud, _IRQ interrupt)
{
    atterm::debug_off(1);
    if(interrupt == ON) atserial0.attach(&CallBack0);
    atserial0.baud(baud);
   
}

void atterm::at_send1(char *format, char *buf)
{
    atserial1.printf(format, buf);
}



void atterm::device_init1(unsigned long baud, _IRQ interrupt)
{
    atterm::debug_off(1);
    if(interrupt == ON) atserial1.attach(&CallBack1);
    atserial1.baud(baud);
    at1.at_send1("AT\r\n", "");

}

char *atterm::getAnswer(uint32_t timeout)
{
    int b = 0;
    int t_time = 0;
    char sign, sign1;

    memset(read_timed_buffer, 0, sizeof(read_timed_buffer));

    t.start();
    t_time = t.read();

    while(1)  {
        if(atserial0.readable())  {
            sign = atserial0.getc();
            read_timed_buffer[b] = sign;
            b ++;
        }

        else if(atserial1.readable())  {
            sign1 = atserial1.getc();
            read_timed_buffer[b] = sign1;
            b ++;
        }
        if((t.read() - t_time) > timeout)  {
            t.stop();
            t.reset();
            break;

        }

    }
    return read_timed_buffer;
}