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
Communication/Xbee.cpp@24:1a16e7eabb0c, 2015-03-26 (annotated)
- Committer:
- Thierry19
- Date:
- Thu Mar 26 17:08:26 2015 +0000
- Revision:
- 24:1a16e7eabb0c
- Parent:
- 23:5bb76b7c35da
- Child:
- 30:389d09853cd1
- Child:
- 31:658b90e226b9
Update Struct -> Mobile_Vers_Fixe et Fixe_Vers_Mobile
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pete1801 | 9:b8503f5ad3bd | 1 | #include "Xbee.h" |
Thierry19 | 23:5bb76b7c35da | 2 | #define VALEURFLEX 0 |
pete1801 | 9:b8503f5ad3bd | 3 | |
llarose | 22:cccb77300fd5 | 4 | Serial x_pc(USBTX, USBRX); |
llarose | 22:cccb77300fd5 | 5 | |
pete1801 | 9:b8503f5ad3bd | 6 | Xbee::Xbee() |
pete1801 | 9:b8503f5ad3bd | 7 | { |
pete1801 | 9:b8503f5ad3bd | 8 | PanId = 0x1337; |
pete1801 | 9:b8503f5ad3bd | 9 | SetPanId(PanId); |
pete1801 | 9:b8503f5ad3bd | 10 | } |
pete1801 | 9:b8503f5ad3bd | 11 | |
pete1801 | 9:b8503f5ad3bd | 12 | Xbee::Xbee(short panId, PinName pinTx, PinName pinRx) |
pete1801 | 9:b8503f5ad3bd | 13 | { |
llarose | 22:cccb77300fd5 | 14 | this->XbeePin = new Serial(pinTx, pinRx); |
pete1801 | 9:b8503f5ad3bd | 15 | PanId = panId; |
pete1801 | 9:b8503f5ad3bd | 16 | SetPanId(PanId); |
pete1801 | 9:b8503f5ad3bd | 17 | } |
pete1801 | 9:b8503f5ad3bd | 18 | |
pete1801 | 9:b8503f5ad3bd | 19 | Xbee::~Xbee() |
pete1801 | 9:b8503f5ad3bd | 20 | { |
pete1801 | 9:b8503f5ad3bd | 21 | } |
pete1801 | 9:b8503f5ad3bd | 22 | |
pete1801 | 9:b8503f5ad3bd | 23 | //send frames to XBee (to set PanID and do the WR) |
pete1801 | 9:b8503f5ad3bd | 24 | void Xbee::Envoyer(char array[], int size) |
pete1801 | 9:b8503f5ad3bd | 25 | { |
pete1801 | 9:b8503f5ad3bd | 26 | for(int i = 0; i < size; i++) { |
pete1801 | 9:b8503f5ad3bd | 27 | XbeePin->putc(array[i]); |
pete1801 | 9:b8503f5ad3bd | 28 | } |
pete1801 | 9:b8503f5ad3bd | 29 | } |
pete1801 | 9:b8503f5ad3bd | 30 | |
Thierry19 | 19:19adf49351b0 | 31 | //function that generates the Transmit Request frame |
Thierry19 | 19:19adf49351b0 | 32 | //and the data[] parameter is only the data we want to send |
Thierry19 | 19:19adf49351b0 | 33 | void Xbee::EnvoyerDonnees(char data[], int messageSize) |
Thierry19 | 19:19adf49351b0 | 34 | { |
Thierry19 | 23:5bb76b7c35da | 35 | //Si possible le messageSize sera une constante en fonction de la struc qu'on envoie a chaque fois (18 + data) |
Thierry19 | 19:19adf49351b0 | 36 | int size = 18 + messageSize; //18 bytes + message |
Thierry19 | 19:19adf49351b0 | 37 | int dataSize = 14 + messageSize; //14 bytes + message |
Thierry19 | 23:5bb76b7c35da | 38 | |
Thierry19 | 19:19adf49351b0 | 39 | char length1 = dataSize >> 8; //get length char 1 |
Thierry19 | 19:19adf49351b0 | 40 | char length2 = dataSize; //get length char 2 |
Thierry19 | 23:5bb76b7c35da | 41 | |
Thierry19 | 19:19adf49351b0 | 42 | char sum = 0x00; //to calculate the checksum char |
Thierry19 | 23:5bb76b7c35da | 43 | |
Thierry19 | 19:19adf49351b0 | 44 | char command[size]; |
Thierry19 | 19:19adf49351b0 | 45 | command[0] = 0x7E; //start delimiter |
Thierry19 | 19:19adf49351b0 | 46 | command[1] = length1; //length first char |
Thierry19 | 19:19adf49351b0 | 47 | command[2] = length2; //length second char |
Thierry19 | 19:19adf49351b0 | 48 | command[3] = 0x10; //frame type - Send Request |
Thierry19 | 19:19adf49351b0 | 49 | command[4] = 0x01; //frame ID |
Thierry19 | 19:19adf49351b0 | 50 | sum += 0x10; |
Thierry19 | 19:19adf49351b0 | 51 | sum += 0x01; |
Thierry19 | 23:5bb76b7c35da | 52 | |
Thierry19 | 19:19adf49351b0 | 53 | //Blank |
Thierry19 | 23:5bb76b7c35da | 54 | for(int i = 5; i <= 12; i++) { |
Thierry19 | 19:19adf49351b0 | 55 | command[i] = 0x00; //64 bit address |
Thierry19 | 19:19adf49351b0 | 56 | } |
Thierry19 | 23:5bb76b7c35da | 57 | |
Thierry19 | 19:19adf49351b0 | 58 | //Broadcast address |
Thierry19 | 19:19adf49351b0 | 59 | command[13] = 0xFF; //16 bit address |
Thierry19 | 19:19adf49351b0 | 60 | command[14] = 0xFE; //16 bit address |
Thierry19 | 19:19adf49351b0 | 61 | sum += 0xFF; |
Thierry19 | 19:19adf49351b0 | 62 | sum += 0xFE; |
Thierry19 | 23:5bb76b7c35da | 63 | |
Thierry19 | 19:19adf49351b0 | 64 | command[15] = 0x00; //broadcast radius |
Thierry19 | 19:19adf49351b0 | 65 | command[16] = 0x00; //options |
Thierry19 | 23:5bb76b7c35da | 66 | |
Thierry19 | 23:5bb76b7c35da | 67 | for(int i = 17; i < size-1; i++) { //data |
Thierry19 | 19:19adf49351b0 | 68 | command[i] = data[i-17]; //data |
Thierry19 | 19:19adf49351b0 | 69 | sum += command[i]; //keep calculating for checksum |
Thierry19 | 19:19adf49351b0 | 70 | } |
Thierry19 | 19:19adf49351b0 | 71 | |
Thierry19 | 19:19adf49351b0 | 72 | command[size-1] = 0xFF - sum; //checksum |
Thierry19 | 19:19adf49351b0 | 73 | |
Thierry19 | 19:19adf49351b0 | 74 | Envoyer(command, size); //send frame array to XBee |
Thierry19 | 19:19adf49351b0 | 75 | } |
Thierry19 | 19:19adf49351b0 | 76 | |
Thierry19 | 23:5bb76b7c35da | 77 | void Xbee::EnvoyerStructure(Mobile_Vers_Fixe mvf) |
Thierry19 | 23:5bb76b7c35da | 78 | { |
Thierry19 | 23:5bb76b7c35da | 79 | char data[8]; |
Thierry19 | 23:5bb76b7c35da | 80 | |
Thierry19 | 23:5bb76b7c35da | 81 | data[0] = mvf.gants; |
Thierry19 | 23:5bb76b7c35da | 82 | data[1] = mvf.accelData.x >> 8; |
Thierry19 | 23:5bb76b7c35da | 83 | data[2] = mvf.accelData.x & 0x00FF; |
Thierry19 | 23:5bb76b7c35da | 84 | data[3] = mvf.accelData.y >> 8; |
Thierry19 | 23:5bb76b7c35da | 85 | data[4] = mvf.accelData.y & 0x00FF; |
Thierry19 | 23:5bb76b7c35da | 86 | data[5] = mvf.accelData.z >> 8; |
Thierry19 | 23:5bb76b7c35da | 87 | data[6] = mvf.accelData.z & 0x00FF; |
Thierry19 | 23:5bb76b7c35da | 88 | |
Thierry19 | 23:5bb76b7c35da | 89 | //Pas tres elegant, j'ai pas encore eu mon 2e cafe.. |
Thierry19 | 23:5bb76b7c35da | 90 | mvf.flexSensor.index = 1 ? data[7] = 0x04 : data[7] = 0; |
Thierry19 | 23:5bb76b7c35da | 91 | mvf.flexSensor.majeur = 1 ? data[7] += 0x02 : data[7] += 0; |
Thierry19 | 23:5bb76b7c35da | 92 | mvf.flexSensor.annulaire = 1 ? data[7] += 0x01 : data[7] += 0; |
Thierry19 | 23:5bb76b7c35da | 93 | |
Thierry19 | 23:5bb76b7c35da | 94 | EnvoyerDonnees(data, 8); |
Thierry19 | 23:5bb76b7c35da | 95 | } |
Thierry19 | 23:5bb76b7c35da | 96 | |
Thierry19 | 23:5bb76b7c35da | 97 | //Le noeud mobile va recevoir certaines informations comme le type de jeux et les signaux de depart/fin de parties |
Thierry19 | 24:1a16e7eabb0c | 98 | //Structure de reception sera Fixe_Vers_Mobile |
pete1801 | 9:b8503f5ad3bd | 99 | void Xbee::Recevoir() |
pete1801 | 9:b8503f5ad3bd | 100 | { |
Thierry19 | 24:1a16e7eabb0c | 101 | Fixe_Vers_Mobile receivedData; |
Thierry19 | 23:5bb76b7c35da | 102 | int index = 0; |
Thierry19 | 23:5bb76b7c35da | 103 | char buffer[8]; |
pete1801 | 9:b8503f5ad3bd | 104 | while(true) { |
pete1801 | 9:b8503f5ad3bd | 105 | if (XbeePin->readable()) { |
pete1801 | 9:b8503f5ad3bd | 106 | //Start byte |
pete1801 | 9:b8503f5ad3bd | 107 | if (XbeePin->getc() == 0x7E) { |
Thierry19 | 23:5bb76b7c35da | 108 | //Get length and frame type |
Thierry19 | 23:5bb76b7c35da | 109 | while (index != 3) { |
Thierry19 | 23:5bb76b7c35da | 110 | buffer[index] = XbeePin->getc(); |
Thierry19 | 23:5bb76b7c35da | 111 | index++; |
Thierry19 | 23:5bb76b7c35da | 112 | } |
Thierry19 | 23:5bb76b7c35da | 113 | index = 0; |
Thierry19 | 23:5bb76b7c35da | 114 | //If frame is a receive packet |
Thierry19 | 23:5bb76b7c35da | 115 | if (buffer[2] == 0x90) { |
Thierry19 | 23:5bb76b7c35da | 116 | //Ici va falloir compter le nombre de bytes de niaiseries |
Thierry19 | 23:5bb76b7c35da | 117 | //qui se passent avant les datas, 13 je crois |
Thierry19 | 23:5bb76b7c35da | 118 | do { |
Thierry19 | 23:5bb76b7c35da | 119 | char c = XbeePin->getc(); |
Thierry19 | 23:5bb76b7c35da | 120 | index++; |
Thierry19 | 23:5bb76b7c35da | 121 | } while (index != 13); |
Thierry19 | 23:5bb76b7c35da | 122 | |
Thierry19 | 24:1a16e7eabb0c | 123 | //Game |
Thierry19 | 24:1a16e7eabb0c | 124 | receivedData.game = XbeePin->getc(); |
pete1801 | 9:b8503f5ad3bd | 125 | |
Thierry19 | 24:1a16e7eabb0c | 126 | //Etat |
Thierry19 | 24:1a16e7eabb0c | 127 | buffer[0] = XbeePin->getc(); |
Thierry19 | 24:1a16e7eabb0c | 128 | receivedData.etat = buffer[0] == 0 ? false : true; |
Thierry19 | 24:1a16e7eabb0c | 129 | |
Thierry19 | 24:1a16e7eabb0c | 130 | //Est-ce qu'il y a un minimum de byte dans les data? |
Thierry19 | 23:5bb76b7c35da | 131 | |
Thierry19 | 23:5bb76b7c35da | 132 | //Validate end byte |
Thierry19 | 23:5bb76b7c35da | 133 | if (XbeePin->getc() == 0x7E) { |
Thierry19 | 24:1a16e7eabb0c | 134 | message_vers_mobile *emile = Mailbox.alloc(); |
Thierry19 | 23:5bb76b7c35da | 135 | |
Thierry19 | 23:5bb76b7c35da | 136 | // Verifier si mail pointe pas vers 0 [boite pleine] |
Thierry19 | 23:5bb76b7c35da | 137 | while (emile == 0) { |
Thierry19 | 23:5bb76b7c35da | 138 | wait_ms(25); |
Thierry19 | 23:5bb76b7c35da | 139 | emile = Mailbox.alloc(); |
Thierry19 | 23:5bb76b7c35da | 140 | } |
Thierry19 | 23:5bb76b7c35da | 141 | emile->donnees = receivedData; |
Thierry19 | 23:5bb76b7c35da | 142 | Mailbox.put(emile); |
pete1801 | 9:b8503f5ad3bd | 143 | } |
pete1801 | 9:b8503f5ad3bd | 144 | } |
pete1801 | 9:b8503f5ad3bd | 145 | } |
pete1801 | 9:b8503f5ad3bd | 146 | } |
pete1801 | 9:b8503f5ad3bd | 147 | } |
pete1801 | 9:b8503f5ad3bd | 148 | } |
pete1801 | 9:b8503f5ad3bd | 149 | |
pete1801 | 9:b8503f5ad3bd | 150 | //function to set the PAN ID |
pete1801 | 9:b8503f5ad3bd | 151 | void Xbee::SetPanId(short panId) |
pete1801 | 9:b8503f5ad3bd | 152 | { |
pete1801 | 9:b8503f5ad3bd | 153 | char c1 = panId >> 8; //PAN ID char 1 |
pete1801 | 9:b8503f5ad3bd | 154 | char c2 = panId; //PAN ID char 2 |
pete1801 | 9:b8503f5ad3bd | 155 | char checksum = 0xFF - (0x08 + 0x01 + 0x49 + 0x44 + c1 + c2); //calculate checksum |
pete1801 | 9:b8503f5ad3bd | 156 | |
pete1801 | 9:b8503f5ad3bd | 157 | //ID and WR AT Commands |
pete1801 | 9:b8503f5ad3bd | 158 | char array[] = {0x7E, 0x00, 0x06, 0x08, 0x01, 0x49, 0x44, c1, c2, checksum}; |
pete1801 | 9:b8503f5ad3bd | 159 | char wr[] = {0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D}; |
pete1801 | 9:b8503f5ad3bd | 160 | |
llarose | 22:cccb77300fd5 | 161 | x_pc.printf("SetPanID \r\n"); |
pete1801 | 9:b8503f5ad3bd | 162 | Envoyer(array, sizeof(array)); //send ID AT Command frame |
llarose | 22:cccb77300fd5 | 163 | x_pc.printf("ID AT cmd \r\n"); |
pete1801 | 9:b8503f5ad3bd | 164 | Envoyer(wr, sizeof(wr)); //send WR AT Command frame |
llarose | 22:cccb77300fd5 | 165 | x_pc.printf("WR AR cmd \r\n"); |
pete1801 | 9:b8503f5ad3bd | 166 | } |