mini code pour xbee

Dependencies:   mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos

Fork of app4Coordo by APP Team

Files at this revision

API Documentation at this revision

Comitter:
passelin
Date:
Sat Feb 22 23:45:52 2014 +0000
Parent:
1:568707763458
Child:
3:85025db3fbd1
Commit message:
xbee fonctionnel

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Feb 22 19:26:05 2014 +0000
+++ b/main.cpp	Sat Feb 22 23:45:52 2014 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include <string>
 
 DigitalOut myled(LED1);
 DigitalOut myled2(LED2);
@@ -6,26 +7,141 @@
 Serial pc(USBTX, USBRX);
 Serial xbee(p13, p14);
 
-int main() 
+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()
 {
-    myled = 1;
-    myled2 = 1;
     reset = 0;
     wait_ms(400);
     reset = 1;
-    myled = 0;
-    myled2 = 0;
+}
+
+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())
         {
-            pc.putc(xbee.getc()); 
+            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;
+        }
     }
 }