APP3 / Mbed 2 deprecated com_xbee

Dependencies:   mbed mbed-rtos

Revision:
2:c6d623a30254
Parent:
1:318ad4f36a90
Child:
4:04351e4aad49
--- a/main.cpp	Mon Feb 13 17:42:31 2017 +0000
+++ b/main.cpp	Mon Feb 13 19:41:52 2017 +0000
@@ -64,7 +64,75 @@
     pc.putc(' ');
 }
 
-void GetATResponse()
+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
@@ -73,25 +141,27 @@
     
     int len = ((int) len_msb << 4) + (int) len_lsb;
     char frame_data[len];
-    int i = 0;
-    pc.printf("%d : ", len);
-    while (i < len)
-    {
-        if (xbee.readable())
-        {   
-            frame_data[i] = xbee.getc();
-            printhexa(frame_data[i]);
-            i++;
-        }
+    
+    // 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();
     }
-    
-    char checksum = xbee.getc();
-    // Verify checksum
-    pc.printf(" checksum = ");
-    printhexa(checksum);
-    pc.printf("\r\n");
+    return response;
 }
 
+
 void ReadSerial()
 {
     // 00 13 A2 00 
@@ -105,7 +175,7 @@
     cmdtosend[3] = 0x08;
     cmdtosend[4] = 0x52;
     cmdtosend[5] = 'S'; // 0x53
-    cmdtosend[6] = 'L'; // 0x48
+    cmdtosend[6] = 'H'; // 0x48
     cmdtosend[7] = 0xFF - (cmdtosend[3]+cmdtosend[4]+cmdtosend[5]+cmdtosend[6]);
     
     while (i < 8)
@@ -122,7 +192,7 @@
     cmdtosend[3] = 0x08;
     cmdtosend[4] = 0x52;
     cmdtosend[5] = 'S'; // 0x53
-    cmdtosend[6] = 'H'; // 0x48
+    cmdtosend[6] = 'L'; // 0x48
     cmdtosend[7] = 0xFF - (cmdtosend[3]+cmdtosend[4]+cmdtosend[5]+cmdtosend[6]);
     
     while (i < 8)