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:
- 26:5700cde2350b
- Parent:
- 22:cccb77300fd5
- Child:
- 27:0c0dfdf8d953
--- a/main.cpp Thu Mar 26 17:08:26 2015 +0000 +++ b/main.cpp Mon Apr 06 17:33:20 2015 +0000 @@ -18,7 +18,7 @@ #include "Xbee.h" #define GO 0x01 -#define ACTIVATE_ACCEL 0 +#define ACTIVATE_ACCEL 1 Serial m_pc(USBTX, USBRX); LocalFileSystem local("local"); // file system for config.txt @@ -27,22 +27,21 @@ void gunner(void const* args); void rps(void const* args); void airGuitar(void const* args); -void get_sensor_data(void const* args); void flex(void const* args); void analyze_sensor_data(void const* args); -void analyze_GUNNER(uint8_t axe_x); -void analyze_RPS(uint8_t axe_z); -void analyze_GUITAR(uint8_t axe_z); -void etat_de_jeu(void const *args); -void reception_coord(void const *args); +void configure_GUNNER(); +void configure_RPS(); +void configure_GUITAR(); void ReadConfig(); +void timer2_init(void); uint8_t play = 0; FlexSensor flexSensors(p15, p16, p17); // flex sensor 1. Accel accel; +flex_t flex_data; RtosTimer *sync; -GameMode_e mode = GUNNER; +GameMode_e mode = RPS; short PanId; Xbee* xbee = NULL; @@ -52,29 +51,33 @@ Thread* airguitar_thread_ptr = NULL; // dans des define. guillaume Thread* flex_thread_ptr = NULL; // Lecture des entrées analogiques -typedef struct { - accel_t accel_data; - flex_t flex_data; -} sensors_t; +Mail<flex_t, 32> mailbox_flex; + -Mail<sensors_t, 32> mailbox_sensors; +extern "C" void TIMER2_IRQHandler(void) +{ + /*Flag du decodage - s'execute a chaque fronts, descendants et montants*/ + if ((LPC_TIM2->IR & 0x20) == 0x20) { + LPC_TIM2->IR |= 0x20; // clear Timer2 interrupt register + threads[1]->signal_set(0x02); + } +} int main(void const* args) { m_pc.printf("==== PROGRAM START MOBILE ====\r\n"); // Initializing the accelerometer - + ReadConfig(); //read config file m_pc.printf("PANID %#04x\r\n", PanId); xbee = new Xbee(PanId, p13, p14); //set PAN ID - - #if ACTIVATE_ACCEL - accel = Accel(); - accel.init_MMA8452(); - RtosTimer timer(get_sensor_data, osTimerPeriodic, (void *)0); - sync = &timer; - #endif + +#if ACTIVATE_ACCEL + accel = Accel(); + accel.init_MMA8452(); + timer2_init(); +#endif switch(mode) { case GUNNER: @@ -83,7 +86,9 @@ m_pc.printf("gunner mode started\r\n"); break; case RPS: + flex_thread_ptr = new Thread(flex); rps_thread_ptr = new Thread(rps); + m_pc.printf("rps mode started\r\n"); break; case AirGuitar: airguitar_thread_ptr = new Thread(airGuitar); @@ -91,19 +96,32 @@ default: break; } - #if ACTIVATE_ACCEL - Thread thread0(reception_coord); - Thread thread1(etat_de_jeu); - Thread thread2(analyze_sensor_data); - threads[0] = &thread0; - threads[1] = &thread1; - threads[2] = &thread2; - #endif + threads[0] = gunner_thread_ptr; + threads[1] = rps_thread_ptr; + threads[2] = airguitar_thread_ptr; while(true) { } } +void timer2_init(void) +{ + LPC_PINCON->PINSEL0 |= 0xc00; // set P0.5 to CAP2.1 + LPC_SC->PCONP |= (1 << 22); // Timer2 power on + LPC_SC->PCLKSEL1 |= (1 << 12); // Divide CCLK by 1 for Timer2 + LPC_TIM2->CCR |= 0x30; // set cap2.1 rising-edge/falling-edge and interrupt + LPC_TIM2->TCR |= (1 << 0); // start Timer2 + LPC_TIM2->EMR = 0x20; // + LPC_TIM2->IR |= 0xFFFFFFFF; + NVIC_EnableIRQ(TIMER2_IRQn); +} + +void flex(void const* args) +{ + flexSensors.Run(); +} + + void gunner(void const* args) { // local variables @@ -117,7 +135,7 @@ int target = rand() % 3; cible->set(target); countDown.run(); - + flex_t data = flexSensors.get_flex_values(); m_pc.printf("index: %u, majeur: %u, annulaire: %u\n\r", data.index, data.majeur, data.annulaire); char data_test[3] = {data.index, data.majeur, data.annulaire}; @@ -125,21 +143,39 @@ } } -void flex(void const* args) +void configure_GUNNER() { - flexSensors.Run(); + accel.set_TRANSIENT_MODE(0x18, 0x05, 0x08); } void rps(void const* args) { // local variables - + m_pc.printf("RPS Start! \n\r"); + configure_RPS(); + uint8_t windup = 0; while(true) { - Thread::signal_wait(GO); - // code... + 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(); + flex_t *mail = mailbox_flex.alloc(); + mail = &flex_data; + mailbox_flex.put(mail); + // send data frame to the fixed mbed for analyze + } + accel.clear_TRANSIENT_INTERRUPT(); } } +void configure_RPS() +{ + accel.set_TRANSIENT_MODE(0x12, 0x05, 0x08); +} + void airGuitar(void const* args) { // local variables @@ -150,112 +186,30 @@ } } - -void get_sensor_data(void const* args) -{ - sensors_t *mail = mailbox_sensors.alloc(); - mail->accel_data = accel.get_axis_values(); - mail->flex_data = flexSensors.get_flex_values(); - m_pc.printf("I2C Communication success: Data received %d; %d; %d;\r\n", mail->accel_data.x, mail->accel_data.y, mail->accel_data.z); - mailbox_sensors.put(mail); -} - -void analyze_GUNNER(uint8_t axe_x) -{ -} - -void analyze_RPS(uint8_t axe_z) -{ - -} - -void analyze_GUITAR(uint8_t axe_z) +void configure_GUITAR() { } -void etat_de_jeu(void const *args) -{ - - while(true) { - Thread::signal_wait(0x1); - m_pc.printf("Etat \r\n"); - switch(play) { - case 0: - sync->stop(); - break; - case 1: - sync->start(250); - break; - default: - break; - } - } -} - -void reception_coord(void const *args) -{ - while(true) { - if(play == 0) { - play = 1; - threads[1]->signal_set(0x1); - Thread::wait(2000); - } else { - play = 0; - threads[1]->signal_set(0x1); - Thread::wait(2000); - } - } -} - -void analyze_sensor_data(void const* args) -{ - while (true) { - // attente et lecture d'un événement digital - osEvent evtD = mailbox_sensors.get(); - if (evtD.status == osEventMail) { - sensors_t *mail = (sensors_t*)evtD.value.p; - // écriture de l'événement en sortie (port série) - m_pc.printf("Valeur des trois axes de l'accelerometre: %d, %d, %d\r\n" , mail->accel_data.x, mail->accel_data.y, mail->accel_data.z); - switch(mode) { - case GUNNER: - analyze_GUNNER(mail->accel_data.x); - break; - case RPS: - analyze_RPS(mail->accel_data.z); - break; - case AirGuitar: - analyze_GUITAR(mail->accel_data.z); - break; - default: - break; - } - mailbox_sensors.free(mail); - } - } -} //read config file void ReadConfig() { FILE* file = fopen("/local/config.txt","r"); - if (file != NULL) - { + if (file != NULL) { char buffer[2]; //char time; //char selector; - + fscanf(file, "%x", &buffer); //panID = 2 char //fscanf(file, "%d", &time); //read period = 1 decimal //fscanf(file, "%d", &selector); //end device selector = 1 decimal - + PanId = buffer[1] << 8 | buffer[0]; //set PAN ID global variable //ReadPeriod = time; //set read period global variable //EndDeviceSelection = selector; //set end device selection global variable - + fclose(file); //close file - } - else //if file is not found - { + } else { //if file is not found m_pc.printf("ERROR AT CONFIG FILE \r\n"); } }