APP3 / Mbed 2 deprecated com_xbee

Dependencies:   mbed mbed-rtos

main.cpp

Committer:
ShaolinPoutine
Date:
2017-02-13
Revision:
2:c6d623a30254
Parent:
1:318ad4f36a90
Child:
4:04351e4aad49

File content as of revision 2:c6d623a30254:

#include "mbed.h"
#define FRAMEDELIMITER 0x7E
#include "rtos.h"

// 4269
DigitalOut resetswitch(p8);
Serial xbee(p13, p14);
Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);

int StartAPIMode ()
{
    char reponse[4] = {42,42,42,'\0'};
    
    wait(1);
    xbee.putc('+');
    xbee.putc('+');
    xbee.putc('+');
    wait(0.5);
    
    int i = 0;
    
    while (i < 4)
    {
        reponse[i] = xbee.getc();
        pc.putc(reponse[i]);
        i++;
    }
    
    return reponse[1];
}

int CalculateChecksum()
{
    // Does not include frame delimiter and lenght
    return 1;
}

int CalculateLenght()
{
    // Calculate the number of bytes in the frame data
    return 1;
}

void printhexa(char c)
{
    char msb = c >> 4;
    char lsb = c & 0xF;
    
    if (msb < 10)
        msb += 0x30;
    else
        msb += 0x37;
        
    if (lsb < 10)
        lsb += 0x30;
    else
        lsb += 0x37;
    
    pc.putc('0');
    pc.putc('x');
    pc.putc(msb);
    pc.putc(lsb);
    pc.putc(' ');
}

void ATCommandResponse(int len, char* ans)
{
    char total = 0x88;
    char id = xbee.getc();
    total += id;
    char command[2];
    command[0] = xbee.getc();
    total += command[0];
    command[1] = xbee.getc();
    total += command[1];
    char status = xbee.getc();
    total += status;
    int i = 0;
    len-= 4;
    char data[len];
    
    pc.printf("response to command %c%c", command[0], command[1]);
    pc.printf(" is ");
    
    while (i < len)
    {
        if (xbee.readable())
        {
            data[i] = xbee.getc();
            total += data[i];
            printhexa(data[i]);
            i++;
        }
    }
    
    char checksum = xbee.getc();
    total += checksum;
    // Verify checksum
    if (total != 0xFF)
    {
        pc.printf("Checksum is wrong");
    }
    pc.printf("\r\n");
    ans = data;
}
    
void ModemStatus(int len, char* ans)
{
    char status = xbee.getc();
    
    switch (status){
        case 0 : pc.printf("Hardware reset\r\n"); break;
        case 1 : pc.printf("Watchdog timer reset\r\n"); break;
        case 2 : pc.printf("Joined network (routers and end devices)\r\n"); break;
        case 3 : pc.printf("Disassociated\r\n"); break;
        case 6 : pc.printf("Coordinator started\r\n"); break;
        case 7 : pc.printf("Network security key was updated\r\n"); break;
        case 0x0D : pc.printf("Voltage supply limit exceeded\r\n"); break;
        case 0x11 : pc.printf("Modem configuration changed while join in progress\r\n"); break;
        default : pc.printf("stack error\r\n"); break;
    }
    
    char checksum = xbee.getc();
    
    checksum += 0x8A + status;
    
    if (checksum != 0xFF)
    {
        pc.printf("Checksum is wrong\r\n");
    }
    *ans = status;
}
    
char* GetATResponse()
{
    char start = xbee.getc(); // = FRAMEDELIMITER
    //assert
    char len_msb = xbee.getc();
    char len_lsb = xbee.getc();
    
    int len = ((int) len_msb << 4) + (int) len_lsb;
    char frame_data[len];
    
    // Resolving frame type
    char type = xbee.getc();
    char *response;
    len--;
    
    switch (type){
        case 0x88: ATCommandResponse(len, response);
            break;
        case 0x8A: ModemStatus(len, response);
            pc.printf(response);
            break;
        default: pc.printf("Please implement response of type");
            printhexa(type);
            pc.printf("\r\n");
            for (int i = -1; i <len; i++) xbee.getc();
    }
    return response;
}


void ReadSerial()
{
    // 00 13 A2 00 
    // 40 3E 09 63
    char cmdtosend[8];
    int i = 0;
    
    cmdtosend[0] = FRAMEDELIMITER;
    cmdtosend[1] = 0x00;
    cmdtosend[2] = 0x04;
    cmdtosend[3] = 0x08;
    cmdtosend[4] = 0x52;
    cmdtosend[5] = 'S'; // 0x53
    cmdtosend[6] = 'H'; // 0x48
    cmdtosend[7] = 0xFF - (cmdtosend[3]+cmdtosend[4]+cmdtosend[5]+cmdtosend[6]);
    
    while (i < 8)
    {
        xbee.putc(cmdtosend[i]);
        i++;
    }
    
    wait(0.5);
    i = 0;
    cmdtosend[0] = FRAMEDELIMITER;
    cmdtosend[1] = 0x00;
    cmdtosend[2] = 0x04;
    cmdtosend[3] = 0x08;
    cmdtosend[4] = 0x52;
    cmdtosend[5] = 'S'; // 0x53
    cmdtosend[6] = 'L'; // 0x48
    cmdtosend[7] = 0xFF - (cmdtosend[3]+cmdtosend[4]+cmdtosend[5]+cmdtosend[6]);
    
    while (i < 8)
    {
        xbee.putc(cmdtosend[i]);
        i++;
    }
}


void xbee_reader()
{
    while(1)
    {
        if (xbee.readable())
        {
            GetATResponse();
        }
        wait(0.001);   
    }
}

void tick()
{
    myled = !myled;   
}

int main() {
    Thread thread(xbee_reader);
    Ticker ticker;
    ticker.attach(&tick, 1);
    pc.printf("\r\n");
    resetswitch = 0;
    wait(0.4);
    resetswitch = 1;
    
    wait(3);

    ReadSerial();
    
    while(1) {
    }
}