mini code pour xbee
Dependencies: mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos
Fork of app4Coordo by
main.cpp
- Committer:
- passelin
- Date:
- 2014-02-22
- Revision:
- 2:5942af2ffe8b
- Parent:
- 1:568707763458
- Child:
- 3:85025db3fbd1
File content as of revision 2:5942af2ffe8b:
#include "mbed.h" #include <string> DigitalOut myled(LED1); DigitalOut myled2(LED2); DigitalOut reset(p8); Serial pc(USBTX, USBRX); Serial xbee(p13, p14); string test; bool trame_ready; enum {STEP_START, STEP_LENGTH, STEP_TYPE, STEP_MAC, STEP_NET, STEP_OPT, STEP_DATA, STEP_CHECK}; 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() { 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; } } }