Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Repo_Noeud_Mobile by
Diff: Communication/Xbee.cpp
- Revision:
- 23:5bb76b7c35da
- Parent:
- 22:cccb77300fd5
- Child:
- 24:1a16e7eabb0c
diff -r cccb77300fd5 -r 5bb76b7c35da Communication/Xbee.cpp --- a/Communication/Xbee.cpp Sun Mar 22 19:57:40 2015 +0000 +++ b/Communication/Xbee.cpp Thu Mar 26 15:40:56 2015 +0000 @@ -1,4 +1,5 @@ #include "Xbee.h" +#define VALEURFLEX 0 Serial x_pc(USBTX, USBRX); @@ -31,15 +32,15 @@ //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. + //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 @@ -48,24 +49,22 @@ command[4] = 0x01; //frame ID sum += 0x10; sum += 0x01; - + //Blank - for(int i = 5; i <= 12; i++) - { + 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 - { + + for(int i = 17; i < size-1; i++) { //data command[i] = data[i-17]; //data sum += command[i]; //keep calculating for checksum } @@ -75,42 +74,81 @@ Envoyer(command, size); //send frame array to XBee } +void Xbee::EnvoyerStructure(Mobile_Vers_Fixe mvf) +{ + char data[8]; + + 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; + + //Pas tres elegant, j'ai pas encore eu mon 2e cafe.. + 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 void Xbee::Recevoir() { - Mobile_Vers_Fixe receivedData; + Mobile_Vers_Fixe receivedData; + int index = 0; + char buffer[8]; 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(); - - //AccelData - receivedData.accelData.x = XbeePin->getc(); - receivedData.accelData.y = XbeePin->getc(); - receivedData.accelData.z = XbeePin->getc(); - - //Should be the byte containing the states of the flexo - receivedData.flexSensor.index = XbeePin->getc(); - receivedData.flexSensor.majeur = XbeePin->getc(); - receivedData.flexSensor.annulaire = XbeePin->getc(); - receivedData.flexSensor.indexU32 = XbeePin->getc(); - receivedData.flexSensor.majeurU32 = XbeePin->getc(); - receivedData.flexSensor.annulaireU32 = XbeePin->getc(); - - //Validate end byte - if (XbeePin->getc() == 0x7E) { - message *emile = Mailbox.alloc(); + //Get length and frame type + while (index != 3) { + buffer[index] = XbeePin->getc(); + index++; + } + index = 0; + //If frame is a receive packet + if (buffer[2] == 0x90) { + //Ici va falloir compter le nombre de bytes de niaiseries + //qui se passent avant les datas, 13 je crois + do { + char c = XbeePin->getc(); + index++; + } while (index != 13); + + //GantsID + receivedData.gants = XbeePin->getc(); + + //AccelData + receivedData.accelData.x = XbeePin->getc(); + receivedData.accelData.y = XbeePin->getc(); + receivedData.accelData.z = XbeePin->getc(); - // Verifier si mail pointe pas vers 0 [boite pleine] - while (emile == 0) { - wait_ms(25); - emile = Mailbox.alloc(); + //Should be the byte containing the states of the flexo + receivedData.flexSensor.index = XbeePin->getc(); + receivedData.flexSensor.majeur = XbeePin->getc(); + receivedData.flexSensor.annulaire = XbeePin->getc(); +#if VALEURFLEX + receivedData.flexSensor.indexU32 = XbeePin->getc(); + receivedData.flexSensor.majeurU32 = XbeePin->getc(); + receivedData.flexSensor.annulaireU32 = XbeePin->getc(); +#endif + + //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); } - emile->donnees = receivedData; - Mailbox.put(emile); } } }