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
main.cpp
- Committer:
- llarose
- Date:
- 2015-03-22
- Revision:
- 22:cccb77300fd5
- Parent:
- 17:1a634bb615f3
- Child:
- 26:5700cde2350b
- Child:
- 30:389d09853cd1
File content as of revision 22:cccb77300fd5:
/* S5 Projet - Conception d'un systeme embarque reseaute * main.cpp * * @author Equipe de projet 2 * */ // System libraries #include "mbed.h" #include "rtos.h" // Proprietary libraries #include "Cible.h" #include "CountDown.h" #include "FlexSensor.h" #include "MMA8452Q.h" #include "Structure.h" #include "Xbee.h" #define GO 0x01 #define ACTIVATE_ACCEL 0 Serial m_pc(USBTX, USBRX); LocalFileSystem local("local"); // file system for config.txt //PROTOTYPES DE FONCTION 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 ReadConfig(); uint8_t play = 0; FlexSensor flexSensors(p15, p16, p17); // flex sensor 1. Accel accel; RtosTimer *sync; GameMode_e mode = GUNNER; short PanId; Xbee* xbee = NULL; Thread *threads[3]; Thread* gunner_thread_ptr = NULL; // Possiblement mettre dans un tableau Thread* rps_thread_ptr = NULL; // avec des position codees 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<sensors_t, 32> mailbox_sensors; 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 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: rps_thread_ptr = new Thread(rps); break; case AirGuitar: airguitar_thread_ptr = new Thread(airGuitar); break; 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 while(true) { } } void gunner(void const* args) { // local variables Cible* cible = new Cible(); CountDown countDown(500); countDown.run(); while(true) { // Thread::signal_wait(GO); cible->reset(); 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}; xbee->EnvoyerDonnees(data_test,3); } } void flex(void const* args) { flexSensors.Run(); } void rps(void const* args) { // local variables while(true) { Thread::signal_wait(GO); // code... } } void airGuitar(void const* args) { // local variables while(true) { Thread::signal_wait(GO); // code... } } 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 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) { 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 { m_pc.printf("ERROR AT CONFIG FILE \r\n"); } }