refactor xbee complet

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Xbee.cpp Source File

Xbee.cpp

00001 #include "Xbee.h"
00002 
00003 Serial x_pc(USBTX, USBRX);
00004 Serial XbeePin(p13, p14);
00005 inline void PrintChar(char c);
00006 inline void PrintEndline();
00007 
00008 Xbee::Xbee()
00009 {
00010     //PanId = 0x1337;
00011     //SetPanId(PanId);
00012 }
00013 
00014 Xbee::Xbee(short panId, PinName pinTx, PinName pinRx)
00015 {
00016     PanId = panId;
00017     SetPanId(PanId);
00018 }
00019 
00020 Xbee::~Xbee()
00021 {
00022 }
00023 
00024 //send frames to XBee (to set PanID and do the WR)
00025 void Xbee::SendXBee(char array[], int size)//SendXbee
00026 {
00027 
00028     for(int i = 0; i < size-1; i++) {
00029         XbeePin.putc(array[i]);
00030         x_pc.printf("%#X \n\r", array[i]);
00031     }
00032 }
00033 
00034 void Xbee::EnvoyerStructure(Mobile_Vers_Fixe mvf)
00035 {
00036     char data[2] = {0x00, 0x00};
00037     x_pc.printf(" \r\n Gants id %c \n\r", mvf.gants);
00038     data[0] = mvf.gants;
00039     mvf.flexSensor.index == 1 ? data[1] = 0x04 : data[1] = 0;
00040     mvf.flexSensor.majeur == 1 ? data[1] += 0x02 : data[1] += 0;
00041     mvf.flexSensor.annulaire == 1 ? data[1] += 0x01 : data[1] += 0;
00042 
00043     SendData(data, 2);
00044 }
00045 
00046 //function to set the PAN ID
00047 void Xbee::SetPanId(short panId)
00048 {
00049     char c1 = panId >> 8; //PAN ID char 1
00050     char c2 = panId; //PAN ID char 2
00051     char checksum = 0xFF - (0x08 + 0x01 + 0x49 + 0x44 + c1 + c2); //calculate checksum
00052 
00053     //ID and WR AT Commands
00054     char array[] = {0x7E, 0x00, 0x06, 0x08, 0x01, 0x49, 0x44, c1, c2, checksum};
00055     char wr[] = {0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D};
00056 
00057     SendXBee(array, sizeof(array)); //send ID AT Command frame
00058     SendXBee(wr, sizeof(wr)); //send WR AT Command frame
00059 }
00060 
00061 //Patate
00062 
00063 //function to set the PAN ID
00064 
00065 
00066 
00067 
00068 //initialise HexToAscii table for correct output on Websocket Server
00069 void Xbee::BuildHexToAsciiTable()
00070 {
00071     HexToAscii[0x0] = '0';
00072     HexToAscii[0x1] = '1';
00073     HexToAscii[0x2] = '2';
00074     HexToAscii[0x3] = '3';
00075     HexToAscii[0x4] = '4';
00076     HexToAscii[0x5] = '5';
00077     HexToAscii[0x6] = '6';
00078     HexToAscii[0x7] = '7';
00079     HexToAscii[0x8] = '8';
00080     HexToAscii[0x9] = '9';
00081     HexToAscii[0xa] = 'A';
00082     HexToAscii[0xb] = 'B';
00083     HexToAscii[0xc] = 'C';
00084     HexToAscii[0xd] = 'D';
00085     HexToAscii[0xe] = 'E';
00086     HexToAscii[0xf] = 'F';
00087 }
00088 
00089 //get data from xbee thread
00090 void Xbee::GetData()
00091 {
00092     data* input;
00093     while(true) {
00094         if(XbeePin.readable()) { //gets 1 full frame and store in mailbox
00095             char c = XbeePin.getc(); //start delimiter
00096             x_pc.printf("Char available on connection: %#X\r\n", c);
00097             if(c == 0x7e) {
00098 
00099                 input = mailbox.alloc(); //alloc in mailbox
00100                 if(input != NULL) { //if alloc was successful
00101                     char c1 = XbeePin.getc(); //length byte 1
00102                     char c2 = XbeePin.getc(); //length byte 2
00103                     short length = c1 << 8 | c2; //calculate length
00104                     input->size = length + 4; //length = dataLength + start delimiter + length (2) + checksum
00105                     input->array[0] = c;
00106                     input->array[1] = c1;
00107                     input->array[2] = c2;
00108                     short pos = 3;
00109                     while(length+1 > 0) { //include checksum
00110                         c = XbeePin.getc(); //get char
00111                         input->array[pos] = c; //store data
00112                         pos++; //current position
00113                         length--; //length left until the end of frame
00114                     }
00115 
00116                     mailbox.put(input); //put data in mailbox
00117                 } else { //if mailbox is full
00118                     x_pc.printf("Mailbox is full!\r\n");
00119                 }
00120             }
00121         }
00122     }
00123 }
00124 
00125 //output data on PC terminal thread
00126 void Xbee::OutputData()
00127 {
00128     osEvent event;
00129     data* mail;
00130     while(true) {
00131         event = mailbox.get(); //get data from mailbox  blocking function
00132         mail = (data*)event.value.p; //this mail is of type data*, our own struct defined at the top
00133         if (event.status == osEventMail) { //if Event
00134             if(mail->array[0] != 0x7e) { //verify header is correct
00135                 x_pc.printf("Header error! ");
00136             }
00137 
00138             char sum = 0x00; //to calculate checksum
00139             PrintChar(mail->array[0]); //print start delimiter
00140             PrintChar(mail->array[1]); //print length byte 1
00141             PrintChar(mail->array[2]); //print length byte 2
00142             for(int i = 3; i < mail->size-1; i++) { //data
00143                 PrintChar(mail->array[i]); //print data
00144                 sum+= mail->array[i]; //calculate checksum
00145             }
00146             PrintChar(mail->array[(mail->size)-1]); //print checksum
00147             char expected = 0xFF - sum; //expected checksum
00148             if(mail->array[mail->size-1] != expected) { //verify checksum is OK
00149                 x_pc.printf(" Checksum Error! expected: %x, received: %x", expected, mail->array[mail->size-1]);
00150             }
00151         } else { //not an event mail
00152             x_pc.printf("Status != osEventMail\r\n");
00153         }
00154 
00155         //POINTER based on the FRAME TYPE (4th byte of the trame)
00156         ReceivePacket(mail);
00157 
00158         mailbox.free(mail); //free the space in the mailbox
00159         PrintEndline();
00160     }
00161 }
00162 
00163 //function that generates the Transmit Request frame
00164 //and the data[] parameter is only the data we want to send
00165 void Xbee::SendData(char data[], int messageSize)
00166 {
00167     int size = 18 + messageSize; //18 bytes + message
00168     int dataSize = 14 + messageSize; //14 bytes + message
00169 
00170     char length1 = dataSize >> 8; //get length char 1
00171     char length2 = dataSize; //get length char 2
00172 
00173     char sum = 0x00; //to calculate the checksum char
00174 
00175     char command[size];
00176     command[0] = 0x7E; //start delimiter
00177     command[1] = length1; //length first char
00178     command[2] = length2; //length second char
00179     command[3] = 0x10; //frame type
00180     command[4] = 0x01; //frame ID
00181     sum += 0x10;
00182     sum += 0x01;
00183 
00184     for(int i = 5; i <= 12; i++) {
00185         command[i] = 0x00; //64 bit address
00186     }
00187 
00188     command[13] = 0xFF; //16 bit address
00189     command[14] = 0xFE; //16 bit address
00190     sum += 0xFF;
00191     sum += 0xFE;
00192 
00193     command[15] = 0x00; //broadcast radius
00194     command[16] = 0x00; //options
00195 
00196     for(int i = 17; i < size-1; i++) { //data
00197         command[i] = data[i-17]; //data
00198         sum += command[i]; //keep calculating for checksum
00199     }
00200 
00201     command[size-1] = 0xFF - sum; //checksum
00202 
00203     SendXBee(command, size); //send frame array to XBee
00204     x_pc.printf("Sortie SendXbee\r\n");
00205 }
00206 
00207 //receive packet frame
00208 //this function analyses it and puts in wsMailbox to send to websocket
00209 void Xbee::ReceivePacket(data* trame)
00210 {
00211     x_pc.printf("Receive Packet Info \n\r");
00212     switch(trame->array[3]) {
00213         case 0x01:
00214             x_pc.printf("\r\nPacket acknowledged");
00215             break;
00216         case 0x02:
00217             x_pc.printf("\r\nPacket was a broadcast packet");
00218             break;
00219         case 0x20:
00220             x_pc.printf("\r\nPacket encrypted with APS encryption");
00221             break;
00222         case 0x40:
00223             x_pc.printf("\r\nPacket was sent from an end device");
00224             break;
00225         case 0x88:
00226             x_pc.printf("\r\n AT Command");
00227             break;
00228         case 0x90:
00229             x_pc.printf("\r\n Received Transmit Data");
00230             Fixe_Vers_Mobile* fvm = fvm_mailbox.alloc(); //alloc in mailbox
00231             if(fvm != NULL) { //if alloc was successful
00232                 // Data aquisition
00233                 fvm->game = (GameMode_e)trame->array[17];
00234                 fvm_mailbox.put(fvm); //put data in mailbox
00235             } else {
00236                 x_pc.printf("Fixe vers mobile mailbox is full!\r\n");
00237             }
00238             break;
00239         default:
00240             x_pc.printf("\r\n Unknown option: %x", trame->array[14]);
00241             break;
00242     }
00243 }
00244 
00245 //utility function for proper output on PC Terminal
00246 inline void PrintChar(char c)
00247 {
00248     if(c < 0x10) { //if char is between 0 and F, put a 0 before
00249         x_pc.printf("0%x ", c);
00250     } else {
00251         x_pc.printf("%x ", c);
00252     }
00253 }
00254 
00255 //utility function to print an endline
00256 inline void PrintEndline()
00257 {
00258     x_pc.printf("\r\n");
00259 }