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: main.cpp
- Revision:
- 51:299408ceee3a
- Parent:
- 48:bc531288c220
- Parent:
- 50:48e8da5fc1ae
- Child:
- 52:10fb8ca4ed03
- Child:
- 53:7b15fe4c9edf
--- a/main.cpp Sat Apr 11 00:05:01 2015 +0000 +++ b/main.cpp Sat Apr 11 19:30:24 2015 +0000 @@ -18,8 +18,8 @@ #include "Xbee.h" #define GO 0x01 -#define ACTIVATE_ACCEL 0 #define ACTIVATE_FLEX 1 +#define ACTIVATE_ACCEL 1 Serial m_pc(USBTX, USBRX); LocalFileSystem local("local"); // file system for config.txt @@ -36,21 +36,16 @@ void configure_GUITAR(); void ReadConfig(); void timer2_init(void); -void Send_Data_To_Fixe(void const* args); +void GetGameMode(void const* args); uint8_t play = 0; +uint8_t windup = 0; FlexSensor flexSensors(p18, p19, p20, p17); // flex sensor 1. Accel accel; flex_t flex_data; -RtosTimer *sync; - -#if ACTIVATE_ACCEL -GameMode_e mode = RPS; -#else -GameMode_e mode = RPS; -#endif - +//RtosTimer *sync; +GameMode_e mode = RPS; short PanId; char GantID; Xbee xbee; @@ -63,7 +58,9 @@ Thread* Thread_Send_Data_To_Fixe = NULL;// Thread pour envoyer les données des sensors au fixe Thread* test_thread_ptr = NULL; -Mail<Mobile_Vers_Fixe, 32> mailbox_Mobile_Vers_Fixe; +Thread* xbeeReceive = NULL; +Thread* threadGetGameMode = NULL; + extern "C" void TIMER2_IRQHandler(void) { @@ -74,8 +71,13 @@ } } +//Fonction qui part le thread +void startXbeeReceive(void const* args) +{ + xbee.Recevoir(); +} -int main(void const* args) +int main(void const* args) { m_pc.printf("\r\n==== PROGRAM START MOBILE ====\r\n"); // Initializing the accelerometer @@ -83,6 +85,8 @@ ReadConfig(); //read config file m_pc.printf("PANID %x\r\n", PanId); xbee = Xbee(PanId, p13, p14); //set PAN ID + xbeeReceive = new Thread(startXbeeReceive); + threadGetGameMode = new Thread(GetGameMode); #if ACTIVATE_ACCEL accel = Accel(); @@ -90,35 +94,16 @@ timer2_init(); #endif - switch(mode) { - case GUNNER: - gunner_thread_ptr = new Thread(gunner); - flex_thread_ptr = new Thread(flex); - m_pc.printf("gunner mode started\r\n"); - break; - case RPS: - #if ACTIVATE_FLEX - flex_thread_ptr = new Thread(flex); - #endif - //rps_thread_ptr = new Thread(rps); - m_pc.printf("rps mode started\r\n"); - break; - case AirGuitar: - airguitar_thread_ptr = new Thread(airGuitar); - break; - case TEST: - test_thread_ptr = new Thread(test); - default: - break; - } + gunner_thread_ptr = new Thread(gunner); + flex_thread_ptr = new Thread(flex); + rps_thread_ptr = new Thread(rps); + airguitar_thread_ptr = new Thread(airGuitar); + threads[GUNNER] = gunner_thread_ptr; threads[RPS] = rps_thread_ptr; threads[AirGuitar] = airguitar_thread_ptr; - Thread_Send_Data_To_Fixe = new Thread(Send_Data_To_Fixe); - while(true) { - } } @@ -144,12 +129,30 @@ { while (true) { // attente et lecture d'un événement digital - osEvent evtD = xbee.mailbox_TypeDeJeu.get(); + osEvent evtD = xbee.mailbox_TypeDeJeu.get(25); if (evtD.status == osEventMail) { Fixe_Vers_Mobile *mail = (Fixe_Vers_Mobile*)evtD.value.p; mode = mail->game; + m_pc.printf("Mode \r\n %c", mode); // Envoyer la structure xbee.mailbox_TypeDeJeu.free(mail); + switch(mode) { + case 0: + m_pc.printf("Led Gunner Start! \n\r"); + configure_GUNNER(); + break; + case 1: + m_pc.printf("RPS Start! \n\r"); + configure_RPS(); + break; + case 2: + m_pc.printf("Air Guitar Start! \n\r"); + configure_GUITAR(); + break; + default: + break; + } + m_pc.printf("\n\r Mode recu: %x", mode); } } } @@ -158,20 +161,21 @@ void gunner(void const* args) { // local variables - while(true) { + while(mode == GUNNER) { + m_pc.printf("Gunner thread"); Thread::signal_wait(0x02); flex_data = flexSensors.get_flex_values(); while(flex_data.index != 0) { wait(10); flex_data = flexSensors.get_flex_values(); } - Mobile_Vers_Fixe *mail = mailbox_Mobile_Vers_Fixe.alloc(); + Mobile_Vers_Fixe *mail = new Mobile_Vers_Fixe; mail->flexSensor = flex_data; mail->gants = GantID; mail->accelData.x = 0x00; mail->accelData.y = 0x00; mail->accelData.z = 0x01; - mailbox_Mobile_Vers_Fixe.put(mail); + xbee.EnvoyerStructure(mail); } } @@ -182,25 +186,44 @@ void rps(void const* args) { + Mobile_Vers_Fixe *mailGauche = new Mobile_Vers_Fixe; + mailGauche->flexSensor.index = true; + mailGauche->flexSensor.annulaire = true; + mailGauche->flexSensor.majeur = true; + mailGauche->gants = 'G'; // Gauche ou Droit + mailGauche->accelData.x = 0x01; + mailGauche->accelData.y = 0x00; + mailGauche->accelData.z = 0x01; + + /* + while (1) { + m_pc.printf("Sending test \r\n"); + wait(1); + xbee.EnvoyerStructure(mailGauche); + //mailbox_Mobile_Vers_Fixe.put(mail); + } + */ // local variables - m_pc.printf("RPS Start! \n\r"); - configure_RPS(); - uint8_t windup = 0; - while(true) { + + while(mode == RPS) { Thread::signal_wait(0x02); windup++; m_pc.printf("Decide in : %d \n\r", windup); if(windup >= 3) { windup = 0; Thread::wait(500); - flex_data = flexSensors.get_flex_values(); - Mobile_Vers_Fixe *mail = mailbox_Mobile_Vers_Fixe.alloc(); - mail->flexSensor = flex_data; - mail->gants = GantID; // Gauche ou Droit + //flex_data = flexSensors.get_flex_values(); + /*Mobile_Vers_Fixe *mail = new Mobile_Vers_Fixe; + mail->flexSensor.index = false; + mail->flexSensor.annulaire = false; + mail->flexSensor.majeur = false; + mail->gants = 'D'; // Gauche ou Droit mail->accelData.x = 0x01; mail->accelData.y = 0x00; - mail->accelData.z = 0x00; - mailbox_Mobile_Vers_Fixe.put(mail); + mail->accelData.z = 0x00;*/ + m_pc.printf("Envoie de mail"); + + xbee.EnvoyerStructure(mailGauche); // send data frame to the fixed mbed for analyze } accel.clear_TRANSIENT_INTERRUPT(); @@ -212,7 +235,7 @@ m_pc.printf("TEST Thread\r\n"); flex_t flex_data = {0,1,0}; - Mobile_Vers_Fixe *mail = mailbox_Mobile_Vers_Fixe.alloc(); + Mobile_Vers_Fixe *mail = new Mobile_Vers_Fixe; mail->flexSensor = flex_data; mail->gants = GantID; mail->accelData.x = 0x01; @@ -221,28 +244,11 @@ while(1) { m_pc.printf("send\r\n"); - mailbox_Mobile_Vers_Fixe.put(mail); + xbee.EnvoyerStructure(mail); wait(1); } } -void Send_Data_To_Fixe(void const* args) -{ - while (true) { - // attente et lecture d'un événement digital - osEvent evtD = mailbox_Mobile_Vers_Fixe.get(); - if (evtD.status == osEventMail) { - Mobile_Vers_Fixe *mail = (Mobile_Vers_Fixe*)evtD.value.p; - // écriture de l'événement en sortie (port série) - m_pc.printf("mail trouve"); - - // Envoyer la structure - xbee.EnvoyerStructure(mail); - mailbox_Mobile_Vers_Fixe.free(mail); - } - } -} - void configure_RPS() { accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08); @@ -260,6 +266,7 @@ void configure_GUITAR() { + accel.set_TRANSIENT_MODE(0x18, 0x05, 0x08); // z plan transient motion detection } //read config file @@ -271,10 +278,10 @@ fscanf(file, "%x", &buffer); //panID = 2 char PanId = buffer[1] << 8 | buffer[0]; //set PAN ID global variable - - fscanf(file, "%x", &buffer); + + fscanf(file, "%x", &buffer); GantID = buffer[0]; - + fclose(file); //close file } else { //if file is not found m_pc.printf("ERROR AT CONFIG FILE \r\n");