Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Communication/Xbee.cpp

Committer:
pete1801
Date:
2015-03-05
Revision:
9:b8503f5ad3bd
Child:
18:7b187bef18d8
Child:
19:19adf49351b0

File content as of revision 9:b8503f5ad3bd:

#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
}