mini code pour xbee

Dependencies:   mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos

Fork of app4Coordo by APP Team

main.cpp

Committer:
passelin
Date:
2014-02-24
Revision:
3:85025db3fbd1
Parent:
2:5942af2ffe8b
Child:
4:7d7d6e4b6810

File content as of revision 3:85025db3fbd1:

#include "mbed.h"
#include <string>
#include "ConfigFile.h"

DigitalOut myled(LED1);
DigitalOut myled2(LED2);
DigitalOut reset(p8);
Serial pc(USBTX, USBRX);
Serial xbee(p13, p14);

string test;
bool trame_ready;

LocalFileSystem local("local");
ConfigFile cfg;

enum {STEP_START, STEP_LENGTH, STEP_TYPE, STEP_MAC, STEP_NET, STEP_OPT, STEP_DATA, STEP_CHECK};

void configInit()
{
    char *key1 = "PANID";
    char *key2 = "URL";
    char value[BUFSIZ];
    
    // Read a configuration file from a mbed.   
    if (!cfg.read("/local/initconf.cfg")) 
    {
        error("Failure to read a configuration file.\n");
    }
 
    /*
     * Get a configuration value.
     */
    if (cfg.getValue(key1, &value[0], sizeof(value))) 
    {
        printf("'%s'='%s'\n", key1, value);
    }
    
    if (cfg.getValue(key2, &value[0], sizeof(value))) 
    {
        printf("'%s'='%s'\n", key2, value);
    }
}

void xbee_init()
{
    reset = 0;
    wait_ms(400);
    reset = 1;
}

void xbee_receive()
{
    // TODO: analyze trame before sending to PC (or WebSocket)
    static int state = STEP_START;
    
    char data = xbee.getc();
    
    static int length_i;
    static int length;
    
    static int mac_i;
    
    static int net_i;
    
    static string msg;
    
    switch(state)
    {
        case STEP_START:    if(data == 0x7E)
                            {
                                state = STEP_LENGTH;
                                length_i = 0;
                                length = 0;
                                msg = "xx";
                            }       
                            break;
                            
        case STEP_LENGTH:   length += data; 
                            length_i++;
                            if(length_i == 2)
                            {
                                state = STEP_TYPE;   
                                length -= 12;
                            }
                            break;
                            
        case STEP_TYPE:     if(data == 0x90) //Receive packet
                            {
                                state = STEP_MAC;  
                                mac_i = 0;  
                            }
                            else
                            {
                                state = STEP_START;
                            }
                            break;
                            
        case STEP_MAC:      mac_i++;
                            if(mac_i == 8)
                            {
                                state = STEP_NET;
                                net_i = 0;
                            }
                            break;
                            
        case STEP_NET:      net_i++;
                            if(net_i == 2)
                            {
                                state = STEP_OPT;
                            }
                            break;
                            
        case STEP_OPT:      if(data == 0x01)
                            {
                                state = STEP_DATA;    
                            }
                            else
                            {
                                state = STEP_START;
                            }
                            break;                     
                            
        case STEP_DATA:     length--;
                            msg += data;
                            if(length == 0)
                            {
                                state = STEP_CHECK; 
                                //pc.printf(msg.c_str()); 
                                test = msg.substr(2);
                                trame_ready = true;
                            }
                            break;
                            
        case STEP_CHECK:    //check CS
                            state = STEP_START;
                            break;
                               
                               
    }
  
}

int main() 
{
    
    configInit();
    xbee_init();

    
    myled = 1;
    myled2 = 1;
    
    trame_ready = false;
    
    while(1) 
    {
        if(xbee.readable())
        {
            xbee_receive();
            
            //pc.printf("%x ", xbee.getc());
            //pc.putc(xbee.getc()); 
            myled2 = ! myled2;
        }
        
        if(pc.readable())
        {
            xbee.putc(pc.getc()); 
            myled = ! myled;     
        }
        
        if(trame_ready == true)
        {
            pc.printf(test.c_str());
            trame_ready = false;
        }
    }
}