Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Revision:
9:b8503f5ad3bd
Child:
18:7b187bef18d8
Child:
19:19adf49351b0
diff -r 51f6c8f59449 -r b8503f5ad3bd Communication/Xbee.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Communication/Xbee.cpp	Thu Mar 05 21:20:23 2015 +0000
@@ -0,0 +1,82 @@
+#include "Xbee.h"
+
+Xbee::Xbee()
+{
+    PanId = 0x1337;
+    SetPanId(PanId);
+}
+
+Xbee::Xbee(short panId, PinName pinTx, PinName pinRx)
+{
+    PanId = panId;
+    SetPanId(PanId);
+
+    this->XbeePin = new Serial(pinTx, pinRx);
+}
+
+Xbee::~Xbee()
+{
+}
+
+//send frames to XBee (to set PanID and do the WR)
+void Xbee::Envoyer(char array[], int size)
+{
+    for(int i = 0; i < size; i++) {
+        XbeePin->putc(array[i]);
+    }
+}
+
+void Xbee::Recevoir()
+{
+    Mobile_Vers_Fixe receivedData;    
+    int index = 0;
+    while(true) {
+        if (XbeePin->readable()) {
+            //Start byte
+            if (XbeePin->getc() == 0x7E) {
+                //Ici va falloir compter le nombre de byte de niaiseries qui se passent avant les datas
+                
+                //GantsID
+                receivedData.gants = XbeePin->getc();
+                //Accelero
+                while (index < 6) {
+                    receivedData.xyz[index] = XbeePin->getc();
+                    index++;
+                }
+                index = 0;
+                //Should be the byte containing the states of the flexo
+                receivedData.majeur = XbeePin->getc();
+                receivedData.index = XbeePin->getc();
+                receivedData.annulaire = XbeePin->getc();
+
+                //Validate end byte
+                if (XbeePin->getc() == 0x7E) {
+                    message *emile = Mailbox.alloc();
+
+                    // Verifier si mail pointe pas vers 0 [boite pleine]
+                    while (emile == 0) {
+                        wait_ms(25);
+                        emile = Mailbox.alloc();
+                    }
+                    emile->donnees = receivedData;
+                    Mailbox.put(emile);
+                }
+            }
+        }
+    }
+}
+
+//function to set the PAN ID
+void Xbee::SetPanId(short panId)
+{
+    char c1 = panId >> 8; //PAN ID char 1
+    char c2 = panId; //PAN ID char 2
+    char checksum = 0xFF - (0x08 + 0x01 + 0x49 + 0x44 + c1 + c2); //calculate checksum
+
+    //ID and WR AT Commands
+    char array[] = {0x7E, 0x00, 0x06, 0x08, 0x01, 0x49, 0x44, c1, c2, checksum};
+    char wr[] = {0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D};
+
+    Envoyer(array, sizeof(array)); //send ID AT Command frame
+    Envoyer(wr, sizeof(wr)); //send WR AT Command frame
+}
\ No newline at end of file