Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Revision:
16:982409595a7a
diff -r 93c6acf1cf40 -r 982409595a7a Communication/Xbee.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Communication/Xbee.cpp	Sat Mar 21 17:19:51 2015 +0000
@@ -0,0 +1,95 @@
+#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;
+    char accelBuffer[6], flexoBuffer;
+    int index = 0;
+    while(true) {
+        if (XbeePin->readable()) {
+            //Start byte
+            if (XbeePin->getc() == 0x7E) {
+                //Ici va falloir compter le nombre de bytes de niaiseries qui se passent avant les datas
+
+                //GantsID
+                receivedData.gants = XbeePin->getc();
+                //Accelero (6 bytes)
+                while (index < 6) {
+                    accelBuffer[index] = XbeePin->getc();
+                    index++;
+                }
+                index = 0;
+                receivedData.accelData.x = (accelBuffer[0] << 8) + accelBuffer[1];
+                receivedData.accelData.y = (accelBuffer[2] << 8) + accelBuffer[3];
+                receivedData.accelData.z = (accelBuffer[4] << 8) + accelBuffer[5];
+                //Should be the byte containing the states of the flexo in the first 3 bits
+                flexoBuffer = XbeePin->getc();
+
+                if (flexoBuffer >= 127) {
+                    flexoBuffer -= 128;
+                    receivedData.majeur = true;
+                }
+                if (flexoBuffer >= 63) {
+                    flexoBuffer -= 64;
+                    receivedData.index = true;
+                }
+                if (flexoBuffer > 31) {
+                    receivedData.annulaire = true;
+                }
+                //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