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 main.cpp Source File

main.cpp

00001 /* S5 Projet - Conception d'un systeme embarque reseaute
00002  * main.cpp
00003  *
00004  * @author Equipe de projet 2
00005  *
00006  */
00007 
00008 // System libraries
00009 #include "mbed.h"
00010 #include "rtos.h"
00011 
00012 // Proprietary libraries
00013 #include "Cible.h"
00014 #include "CountDown.h"
00015 #include "FlexSensor.h"
00016 #include "MMA8452Q.h"
00017 #include "Structure.h"
00018 #include "Xbee.h"
00019 
00020 #define GO 0x01
00021 #define ACTIVATE_FLEX 1
00022 #define ACTIVATE_ACCEL 1
00023 #define XBEE_TRANSMIT 3
00024 
00025 Serial m_pc(USBTX, USBRX);
00026 LocalFileSystem local("local"); // file system for config.txt
00027 
00028 //PROTOTYPES DE FONCTION
00029 void gunner(void const* args);
00030 void rps(void const* args);
00031 void airGuitar(void const* args);
00032 void flex(void const* args);
00033 void transmit_xbee_data(void const* args);
00034 void analyze_sensor_data(void const* args);
00035 void configure_GUNNER();
00036 void configure_RPS();
00037 void configure_GUITAR();
00038 void ReadConfig();
00039 void timer2_init(void);
00040 void GetGameMode(void const* args);
00041 
00042 uint8_t play = 0;
00043 uint8_t windup = 0;
00044 
00045 FlexSensor flexSensors(p18, p19, p20, p17);       // flex sensor 1.
00046 Accel accel;
00047 flex_t flex_data;
00048 Mobile_Vers_Fixe mail_mvf;
00049 //RtosTimer *sync;
00050 GameMode_e mode = RPS;
00051 AirGuitar_opMode_e opMode;
00052 short PanId;
00053 char GantID;
00054 Xbee xbee;
00055 
00056 Thread *threads[4];
00057 Thread* gunner_thread_ptr = NULL;       // Possiblement mettre dans un tableau
00058 Thread* rps_thread_ptr = NULL;          // avec des position codees
00059 Thread* airguitar_thread_ptr = NULL;    // dans des define. guillaume
00060 Thread* flex_thread_ptr = NULL;         // Lecture des entrées analogiques
00061 Thread* Thread_Send_Data_To_Fixe = NULL;// Thread pour envoyer les données des sensors au fixe
00062 Thread* transmit_xbee_data_thread_ptr = NULL;
00063 
00064 Thread* xbeeAnalyzer = NULL;
00065 Thread* xbeeReceive = NULL;
00066 Thread* threadGetGameMode = NULL;
00067 
00068 
00069 extern "C" void TIMER2_IRQHandler(void)
00070 {
00071     /*Flag du decodage - s'execute a chaque fronts, descendants et montants*/
00072     if ((LPC_TIM2->IR & 0x20) == 0x20) {
00073         LPC_TIM2->IR |= 0x20;     // clear Timer2 interrupt register
00074         threads[mode]->signal_set(0x02);
00075     }
00076 }
00077 
00078 //Fonction qui part le thread
00079 void startXbeeReceive(void const* args)
00080 {
00081     xbee.GetData();
00082 }
00083 
00084 void startXbeeAnalyzer(void const* args)
00085 {
00086     xbee.OutputData();
00087 }
00088 
00089 
00090 int main(void const* args)
00091 {
00092     m_pc.printf("\r\n==== PROGRAM START MOBILE ====\r\n");
00093     // Initializing the accelerometer
00094 
00095     ReadConfig(); //read config file
00096     opMode = PICK; // temporaire
00097     m_pc.printf("PANID %x\r\n", PanId);
00098     m_pc.printf("opMode %u\r\n", opMode);
00099     xbee = Xbee(PanId, p13, p14); //set PAN ID
00100     xbeeAnalyzer = new Thread(startXbeeAnalyzer);
00101     xbeeReceive = new Thread(startXbeeReceive);
00102     threadGetGameMode = new Thread(GetGameMode);
00103 
00104 #if ACTIVATE_ACCEL
00105     accel = Accel();
00106     accel.init_MMA8452();
00107     timer2_init();
00108 #endif
00109     transmit_xbee_data_thread_ptr =  new Thread(transmit_xbee_data);
00110     gunner_thread_ptr =  new Thread(gunner);
00111     flex_thread_ptr = new Thread(flex);
00112     rps_thread_ptr =  new Thread(rps);
00113 
00114     airguitar_thread_ptr = new Thread(airGuitar);
00115 
00116     threads[GUNNER] = gunner_thread_ptr;
00117     threads[RPS] = rps_thread_ptr;
00118     threads[AirGuitar] = airguitar_thread_ptr;
00119     threads[XBEE_TRANSMIT] = transmit_xbee_data_thread_ptr;
00120     configure_RPS();
00121     while(true) {
00122     }
00123 }
00124 
00125 void timer2_init(void)
00126 {
00127     LPC_PINCON->PINSEL0  |= 0xc00;      // set P0.5 to CAP2.1
00128     LPC_PINCON->PINMODE0 |= 0xc00;       // pull-down enable
00129     LPC_SC->PCONP    |= (1 << 22);      // Timer2 power on
00130     LPC_SC->PCLKSEL1 |= (1 << 12);      // Divide CCLK by 1 for Timer2
00131     LPC_TIM2->CCR    |= 0x30;           // set cap2.1 rising-edge/falling-edge and interrupt
00132     LPC_TIM2->TCR    |= (1 << 0);       // start Timer2
00133     LPC_TIM2->EMR     = 0x20;           //
00134     LPC_TIM2->IR     |= 0xFFFFFFFF;
00135     NVIC_EnableIRQ(TIMER2_IRQn);
00136 }
00137 
00138 void flex(void const* args)
00139 {
00140     flexSensors.Run();
00141 }
00142 
00143 void GetGameMode(void const* args)
00144 {
00145     while (true) {
00146         // attente et lecture d'un événement digital
00147         osEvent evtD = xbee.fvm_mailbox.get();
00148         if (evtD.status == osEventMail) {
00149             Fixe_Vers_Mobile *mail = (Fixe_Vers_Mobile*)evtD.value.p;
00150             mode = mail->game;
00151             // Envoyer la structure
00152             xbee.fvm_mailbox.free(mail);
00153             switch(mode) {
00154                 case 0:
00155                     m_pc.printf("Led Gunner Start! FVM \n\r");
00156                     configure_GUNNER();
00157                     break;
00158                 case 1:
00159                     m_pc.printf("RPS Start! FVM\n\r");
00160                     configure_RPS();
00161                     break;
00162                 case 2:
00163                     m_pc.printf("Air Guitar Start! FVM\n\r");
00164                     configure_GUITAR();
00165                     break;
00166                 default:
00167                     break;
00168             }
00169             m_pc.printf("\n\r Mode recu: %x \n\r", mode);
00170         }
00171     }
00172 }
00173 
00174 
00175 void gunner(void const* args)
00176 {
00177     // local variables
00178     while(true) {
00179         Thread::signal_wait(0x02);
00180         flex_data = flexSensors.get_flex_values();
00181         while(flex_data.index != 0) {
00182             wait(10);
00183             flex_data = flexSensors.get_flex_values();
00184         }
00185         mail_mvf.flexSensor = flex_data;
00186         mail_mvf.gants = 'D';
00187         threads[XBEE_TRANSMIT]->signal_set(0x01);
00188     }
00189 }
00190 
00191 void configure_GUNNER()
00192 {
00193     accel.set_TRANSIENT_MODE(0x18, 0x05, 0x08); // z plan transient motion detection
00194 }
00195 
00196 void transmit_xbee_data(void const* args)
00197 {
00198     while(1) {
00199         Thread::signal_wait(0x01);
00200         xbee.EnvoyerStructure(mail_mvf);
00201         wait(0.1);
00202     }
00203 }
00204 
00205 void rps(void const* args)
00206 {
00207     Mobile_Vers_Fixe mailGauche;
00208     mailGauche.flexSensor.index = true;
00209     mailGauche.flexSensor.annulaire = true;
00210     mailGauche.flexSensor.majeur = true;
00211     mailGauche.gants = 'G'; // Gauche ou Droit
00212 
00213     // local variables
00214 
00215     while(true) {
00216         Thread::signal_wait(0x02);
00217         windup++;
00218         m_pc.printf("Decide in : %d \n\r", windup);
00219         if(windup >= 3) {
00220             windup = 0;
00221             Thread::wait(500);
00222             //flex_data = flexSensors.get_flex_values();
00223             mail_mvf.flexSensor.index = false;
00224             mail_mvf.flexSensor.annulaire = false;
00225             mail_mvf.flexSensor.majeur = false;
00226             mail_mvf.gants = 'D'; // Gauche ou Droit
00227             m_pc.printf("Envoie de mail");
00228             threads[XBEE_TRANSMIT]->signal_set(0x01);
00229             /* mail_mvf = mailGauche;
00230              threads[XBEE_TRANSMIT]->signal_set(0x01);
00231             */
00232             // send data frame to the fixed mbed for analyze
00233         }
00234         accel.clear_TRANSIENT_INTERRUPT();
00235     }
00236 }
00237 
00238 
00239 void configure_RPS()
00240 {
00241     accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08);
00242 }
00243 
00244 void airGuitar(void const* args)
00245 {
00246     mail_mvf.flexSensor.index = true;
00247     mail_mvf.flexSensor.annulaire = true;
00248     mail_mvf.flexSensor.majeur = true;
00249     if(opMode == PICK) {
00250         mail_mvf.gants = 'P'; // Droit
00251     } else if(opMode == FRET) {
00252         mail_mvf.gants = 'F'; // Gauche
00253     }
00254 
00255     flex_t flex_data;
00256     while(true) {
00257         switch(opMode) {
00258             case PICK:
00259                 Thread::signal_wait(0x02);
00260                 threads[XBEE_TRANSMIT]->signal_set(0x01);
00261                 accel.clear_TRANSIENT_INTERRUPT();
00262                 break;
00263             case FRET:
00264 
00265                 flex_data = flexSensors.get_flex_values();
00266                 if(flex_data != mail_mvf.flexSensor) {
00267                     mail_mvf.flexSensor = flex_data;
00268                     threads[XBEE_TRANSMIT]->signal_set(0x01);
00269                 }
00270                 Thread::wait(50);
00271 
00272                 break;
00273             default:
00274                 break;
00275         }
00276 
00277     }
00278 }
00279 
00280 void configure_GUITAR()
00281 {
00282     accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08); // z plan transient motion detection
00283 }
00284 
00285 //read config file
00286 void ReadConfig()
00287 {
00288     FILE* file = fopen("/local/config.txt","r");
00289     if (file != NULL) {
00290         char buffer[2];
00291 
00292         fscanf(file, "%x", &buffer); //panID = 2 char
00293         PanId = buffer[1] << 8 | buffer[0]; //set PAN ID global variable
00294 
00295         fscanf(file, "%x", &buffer);
00296         GantID = buffer[0];
00297 
00298         char mode[4];
00299         fscanf(file, "%x", mode);
00300         if(mode == "FRET")
00301             opMode = FRET;
00302         if(mode == "PICK")
00303             opMode = PICK;
00304 
00305         fclose(file); //close file
00306     } else { //if file is not found
00307         m_pc.printf("ERROR AT CONFIG FILE \r\n");
00308     }
00309 }