APP3 / Zigbee_communication

Dependents:   Coordinator_node Router_node

Revision:
7:78985e92c1c5
Parent:
6:3b97770f30e6
Parent:
5:9b4d93bd6725
Child:
8:ba349a2eeb37
--- a/xbee.cpp	Tue Feb 14 04:03:55 2017 +0000
+++ b/xbee.cpp	Tue Feb 14 04:05:53 2017 +0000
@@ -1,5 +1,16 @@
 #include "xbee.h"
 
+XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m) : 
+    rst(reset), comm(transfer, receive)
+{
+    // Constructor
+    mail = m;
+    rst = 0;
+    wait(0.4);
+    rst = 1;
+    wait(3);    // waiting for initiation
+}
+
 void XBee::pcPrint(char* c)
 {
     int i = 0;
@@ -10,6 +21,44 @@
     }
 }
 
+void XBee::printInt(int i)
+{
+    bool signe = i > 0;
+    char *c = mail->alloc();
+    if (signe)
+    {
+        *c = '-';
+        mail->put(c);
+    }
+    
+    int diviseur = 1;
+    int modulo = 10;
+    int j = 9;
+    char chiffre[10];
+    
+    while (i / diviseur > 0 && j >= 0)
+    {
+        chiffre[j] = (i % modulo)/diviseur;
+        modulo *= 10;
+        diviseur *= 10;
+        j--;
+    }
+    
+    j = 0;
+    bool numberHasStarted = false;
+    while (j < 10)
+    {
+        if (chiffre[j] != 0 || numberHasStarted || j == 9)
+        {
+            numberHasStarted = true;
+            c = mail->alloc();
+            *c = chiffre[j];
+            mail->put(c);
+        }
+        j++;
+    }
+}
+
 void XBee::printHexa(char c)
 {
     char *msb = mail->alloc();
@@ -35,17 +84,6 @@
     pcPrint(str);
 }
 
-XBee::XBee(PinName reset, PinName transfer, PinName receive, Mail<char, 250>* m) : 
-    rst(reset), comm(transfer, receive)
-{
-    // Constructor
-    mail = m;
-    rst = 0;
-    wait(0.4);
-    rst = 1;
-    wait(3);    // waiting for initiation
-}
-
 void XBee::SendATCommand(char firstChar, char secondChar, char *optionalParam, int paramLen)
 {
     // Frame Type 0x08
@@ -127,10 +165,13 @@
                 break;
             case 0x90: ZigBeeReceivePacket(len);
                 break;
-            default: pcPrint("Please implement response of type\0");
-                //printsHexa(type);
+            default: pcPrint("Please implement response of type ");
+                printHexa(type);
                 pcPrint("\r\n\0");
-                for (int i = -1; i <len; i++) comm.getc();
+                for (int i = 0; i <len; i++) 
+                {      
+                    comm.getc();
+                }
         }
     }
     return response;
@@ -226,9 +267,11 @@
     char retry =  comm.getc();
     char status = comm.getc();
     char discovery = comm.getc();
+    char checksum;
     
-    //pcPrint("Response to transmit id %d is : ", id);
-    pcPrint("Response to transmit id  is : ");
+    pcPrint("Response to transmit # ");
+    printInt((int)id);
+    pcPrint(" is : ");
     
     if (status == 0)
     {
@@ -255,6 +298,9 @@
             default : pcPrint("Unknow error ...\r\n"); break;
         }
     }
+    
+    checksum = comm.getc();
+    // Validate checksum TODO
 }
 
 void XBee::ZigBeeReceivePacket(int len)
@@ -263,6 +309,7 @@
     char adresse64bit[8];
     char adresse16bit[2];
     char receiveOptions;
+    char checksum;
         
     while(i < 8)
     {
@@ -283,7 +330,9 @@
         printHexa(comm.getc());
         i++;   
     }
+    pcPrint("\r\n");
     
+    checksum = comm.getc();
     // Validate checksum TODO
 }