Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Revision:
58:4cdc85ed04a9
Parent:
57:ce80fbd67161
--- a/Communication/Xbee.cpp	Sun Apr 12 03:02:21 2015 +0000
+++ b/Communication/Xbee.cpp	Sun Apr 12 18:59:37 2015 +0000
@@ -1,7 +1,7 @@
 #include "Xbee.h"
 
 Serial x_pc(USBTX, USBRX);
-
+Serial XbeePin(p13, p14);
 inline void PrintChar(char c);
 inline void PrintEndline();
 
@@ -13,28 +13,28 @@
 
 Xbee::Xbee(short panId, PinName pinTx, PinName pinRx)
 {
-    this->XbeePin = new Serial(pinTx, pinRx);
     PanId = panId;
     SetPanId(PanId);
 }
 
 Xbee::~Xbee()
 {
-    delete XbeePin;
 }
 
 //send frames to XBee (to set PanID and do the WR)
 void Xbee::SendXBee(char array[], int size)//SendXbee
 {
-    for(int i = 0; i < size; i++) {
-        XbeePin->putc(array[i]);
+
+    for(int i = 0; i < size-1; i++) {
+        XbeePin.putc(array[i]);
+        x_pc.printf("%#X \n\r", array[i]);
     }
 }
 
 void Xbee::EnvoyerStructure(Mobile_Vers_Fixe mvf)
 {
     char data[2] = {0x00, 0x00};
-    //x_pc.printf(" \r\n Gants id %c", mvf->gants);
+    x_pc.printf(" \r\n Gants id %c \n\r", mvf.gants);
     data[0] = mvf.gants;
     mvf.flexSensor.index == 1 ? data[1] = 0x04 : data[1] = 0;
     mvf.flexSensor.majeur == 1 ? data[1] += 0x02 : data[1] += 0;
@@ -91,29 +91,32 @@
 {
     data* input;
     while(true) {
-        if(XbeePin->readable()) { //gets 1 full frame and store in mailbox
-            input = mailbox.alloc(); //alloc in mailbox
+        if(XbeePin.readable()) { //gets 1 full frame and store in mailbox
+            char c = XbeePin.getc(); //start delimiter
+            x_pc.printf("Char available on connection: %#X\r\n", c);
+            if(c == 0x7e) {
 
-            if(input != NULL) { //if alloc was successful
-                char c = XbeePin->getc(); //start delimiter
-                char c1 = XbeePin->getc(); //length byte 1
-                char c2 = XbeePin->getc(); //length byte 2
-                short length = c1 << 8 | c2; //calculate length
-                input->size = length + 4; //length = dataLength + start delimiter + length (2) + checksum
-                input->array[0] = c;
-                input->array[1] = c1;
-                input->array[2] = c2;
-                short pos = 3;
-                while(length+1 > 0) { //include checksum
-                    c = XbeePin->getc(); //get char
-                    input->array[pos] = c; //store data
-                    pos++; //current position
-                    length--; //length left until the end of frame
+                input = mailbox.alloc(); //alloc in mailbox
+                if(input != NULL) { //if alloc was successful
+                    char c1 = XbeePin.getc(); //length byte 1
+                    char c2 = XbeePin.getc(); //length byte 2
+                    short length = c1 << 8 | c2; //calculate length
+                    input->size = length + 4; //length = dataLength + start delimiter + length (2) + checksum
+                    input->array[0] = c;
+                    input->array[1] = c1;
+                    input->array[2] = c2;
+                    short pos = 3;
+                    while(length+1 > 0) { //include checksum
+                        c = XbeePin.getc(); //get char
+                        input->array[pos] = c; //store data
+                        pos++; //current position
+                        length--; //length left until the end of frame
+                    }
+
+                    mailbox.put(input); //put data in mailbox
+                } else { //if mailbox is full
+                    x_pc.printf("Mailbox is full!\r\n");
                 }
-
-                mailbox.put(input); //put data in mailbox
-            } else { //if mailbox is full
-                x_pc.printf("Mailbox is full!\r\n");
             }
         }
     }
@@ -163,12 +166,12 @@
 {
     int size = 18 + messageSize; //18 bytes + message
     int dataSize = 14 + messageSize; //14 bytes + message
-        
+
     char length1 = dataSize >> 8; //get length char 1
     char length2 = dataSize; //get length char 2
-    
+
     char sum = 0x00; //to calculate the checksum char
-    
+
     char command[size];
     command[0] = 0x7E; //start delimiter
     command[1] = length1; //length first char
@@ -177,64 +180,65 @@
     command[4] = 0x01; //frame ID
     sum += 0x10;
     sum += 0x01;
-    
-    for(int i = 5; i <= 12; i++)
-    {
+
+    for(int i = 5; i <= 12; i++) {
         command[i] = 0x00; //64 bit address
     }
-    
+
     command[13] = 0xFF; //16 bit address
     command[14] = 0xFE; //16 bit address
     sum += 0xFF;
     sum += 0xFE;
-    
+
     command[15] = 0x00; //broadcast radius
     command[16] = 0x00; //options
-    
-    for(int i = 17; i < size-1; i++) //data
-    {
+
+    for(int i = 17; i < size-1; i++) { //data
         command[i] = data[i-17]; //data
         sum += command[i]; //keep calculating for checksum
-        x_pc.printf("");
     }
 
     command[size-1] = 0xFF - sum; //checksum
 
     SendXBee(command, size); //send frame array to XBee
+    x_pc.printf("Sortie SendXbee\r\n");
 }
 
 //receive packet frame
 //this function analyses it and puts in wsMailbox to send to websocket
 void Xbee::ReceivePacket(data* trame)
 {
-    Fixe_Vers_Mobile* fvm = fvm_mailbox.alloc(); //alloc in mailbox
-
-    if(fvm != NULL) { //if alloc was successful
-        x_pc.printf("Receive Packet Info");
-
-        switch(trame->array[14]) { //print receive option
-            case 0x01:
-                x_pc.printf("\r\nPacket acknowledged");
-                break;
-            case 0x02:
-                x_pc.printf("\r\nPacket was a broadcast packet");
-                break;
-            case 0x20:
-                x_pc.printf("\r\nPacket encrypted with APS encryption");
-                break;
-            case 0x40:
-                x_pc.printf("\r\nPacket was sent from an end device");
-                break;
-            default:
-                x_pc.printf("\r\nUnknown option: %x", trame->array[14]);
-                break;
-        }
-        // Data aquisition
-        fvm->game = (GameMode_e)trame->array[15];
-
-        fvm_mailbox.put(fvm); //put data in mailbox
-    } else {
-        x_pc.printf("Mobile Vers Fixe mailbox is full!\r\n");
+    x_pc.printf("Receive Packet Info \n\r");
+    switch(trame->array[3]) {
+        case 0x01:
+            x_pc.printf("\r\nPacket acknowledged");
+            break;
+        case 0x02:
+            x_pc.printf("\r\nPacket was a broadcast packet");
+            break;
+        case 0x20:
+            x_pc.printf("\r\nPacket encrypted with APS encryption");
+            break;
+        case 0x40:
+            x_pc.printf("\r\nPacket was sent from an end device");
+            break;
+        case 0x88:
+            x_pc.printf("\r\n AT Command");
+            break;
+        case 0x90:
+            x_pc.printf("\r\n Received Transmit Data");
+            Fixe_Vers_Mobile* fvm = fvm_mailbox.alloc(); //alloc in mailbox
+            if(fvm != NULL) { //if alloc was successful
+                // Data aquisition
+                fvm->game = (GameMode_e)trame->array[17];
+                fvm_mailbox.put(fvm); //put data in mailbox
+            } else {
+                x_pc.printf("Fixe vers mobile mailbox is full!\r\n");
+            }
+            break;
+        default:
+            x_pc.printf("\r\n Unknown option: %x", trame->array[14]);
+            break;
     }
 }