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:
groygirard
Date:
2015-04-12
Revision:
55:adde1b6081b8
Parent:
52:10fb8ca4ed03
Child:
56:6af8f2c9ddbe

File content as of revision 55:adde1b6081b8:

#include "Xbee.h"
#define VALEURFLEX 0

Serial x_pc(USBTX, USBRX);

Xbee::Xbee()
{
    //PanId = 0x1337;
    //SetPanId(PanId);
}

Xbee::Xbee(short panId, PinName pinTx, PinName pinRx)
{
    this->XbeePin = new Serial(pinTx, pinRx);
    PanId = panId;
    SetPanId(PanId);
    frameID=0;
}

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]);
        x_pc.printf("Send array: %X \r\n", array[i]);
        
    }
    x_pc.printf("Send Complete! \r\n");
}

//function that generates the Transmit Request frame
//and the data[] parameter is only the data we want to send
void Xbee::EnvoyerDonnees(char data[], int messageSize)
{
    //Si possible le messageSize sera une constante en fonction de la struc qu'on envoie a chaque fois (18 + data)
    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
    command[2] = length2; //length second char
    command[3] = 0x10; //frame type - Send Request
    command[4] = 0x01; //frame ID
    sum += 0x10;
    sum += 0x01;

    //Blank
    for(int i = 5; i <= 12; i++) {
        command[i] = 0x00; //64 bit address
    }

    //Broadcast 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
        command[i] = data[i-17]; //data
         x_pc.printf("Send : %d \r\n", data[i-17]);
        sum += command[i]; //keep calculating for checksum
    }

    command[size-1] = 0xFF - sum; //checksum
    Envoyer(command, size);       //send frame array to XBee
    frameID++;
}

void Xbee::EnvoyerStructure(Mobile_Vers_Fixe mvf)
{
    char data[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    data[0] = mvf.gants;
    data[1] = mvf.accelData.x >> 8;
    data[2] = mvf.accelData.x & 0x00FF;
    data[3] = mvf.accelData.y >> 8;
    data[4] = mvf.accelData.y & 0x00FF;
    data[5] = mvf.accelData.z >> 8;
    data[6] = mvf.accelData.z & 0x00FF;
    x_pc.printf("Class Accel x: %d \r\n", mvf.accelData.x);
    mvf.flexSensor.index == 1 ? data[7] = 0x04 : data[7] = 0;
    mvf.flexSensor.majeur == 1 ? data[7] += 0x02 : data[7] += 0;
    mvf.flexSensor.annulaire == 1 ? data[7] += 0x01 : data[7] += 0;
    
    EnvoyerDonnees(data, 8);
}

void Xbee::EnvoyerStructure(Mobile_Vers_Fixe* mvf)
{
    char data[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    //x_pc.printf(" \r\n Gants id %c", mvf->gants);
    data[0] = mvf->gants;
    data[1] = mvf->accelData.x >> 8;
    data[2] = mvf->accelData.x & 0x00FF;
    data[3] = mvf->accelData.y >> 8;
    data[4] = mvf->accelData.y & 0x00FF;
    data[5] = mvf->accelData.z >> 8;
    data[6] = mvf->accelData.z & 0x00FF;
    x_pc.printf("\r\nPointer Accel x: %d", mvf->accelData.x);

    mvf->flexSensor.index == 1 ? data[7] = 0x04 : data[7] = 0;
    mvf->flexSensor.majeur == 1 ? data[7] += 0x02 : data[7] += 0;
    mvf->flexSensor.annulaire == 1 ? data[7] += 0x01 : data[7] += 0;

    EnvoyerDonnees(data, 8);
}

//Le noeud mobile va recevoir certaines informations comme le type de jeux et les signaux de depart/fin de parties
//Structure de reception sera Fixe_Vers_Mobile
void Xbee::Recevoir()
{
    char buffer[3];
    int index = 0;
    x_pc.printf("\r\n Start recevoir thread x");
    while(true) {
        if (XbeePin->readable()) {
            //x_pc.printf("Data recu: %x",XbeePin->getc());
            //Start byte
            if (XbeePin->getc() == 0x7E) {
                index = 0;
                //x_pc.printf("\r\n Start Byte x");
                //Get length and frame type
                while (index < 3) {
                    buffer[index] = XbeePin->getc();
                    index++;
                }
                //x_pc.printf("\n\r Type de trame: %x", buffer[2]);
                index = 0;
                //If frame is a receive packet
                if (buffer[2] == 0x90) {
                    //x_pc.printf("\r\n Bonne trame");
                    //Ici va falloir compter le nombre de bytes de niaiseries
                    //qui se passent avant les donnees, 11 je crois
                    do {
                        char c = XbeePin->getc();
                        index++;
                    } while (index < 11);
                    index = 0;

                    //GameID

                    int gameMode = XbeePin->getc();
                    index++;
                    x_pc.printf("\r\n gameMode %x", gameMode);
                    //ChkSum
                    buffer[0] = XbeePin->getc();

                    Fixe_Vers_Mobile *emile = mailbox_TypeDeJeu.alloc();

                    // Verifier si mail pointe pas vers 0 [boite pleine]
                    while (emile == 0) {
                        wait_ms(25);
                        emile = mailbox_TypeDeJeu.alloc();
                    }
                    emile->game = GameMode_e(gameMode);
                    mailbox_TypeDeJeu.put(emile);
                    index = 0;
                }
            }
        }
        index = 0;
    }
}

//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};

    x_pc.printf("SetPanID \r\n");
    Envoyer(array, sizeof(array)); //send ID AT Command frame
    x_pc.printf("ID AT cmd \r\n");
    Envoyer(wr, sizeof(wr)); //send WR AT Command frame
    x_pc.printf("WR AR cmd \r\n");
}