Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Revision:
11:a9fbf205233a
Parent:
8:51f6c8f59449
--- a/main.cpp	Thu Mar 05 19:36:48 2015 +0000
+++ b/main.cpp	Thu Mar 05 22:01:28 2015 +0000
@@ -17,35 +17,81 @@
 
 #define GO 0x01
 
+Serial m_pc(USBTX, USBRX);
+
 enum GameMode { GUNNER, RPS, AirGuitar};
 
 //PROTOTYPES DE FONCTION
 void gunner(void const* args);
 void rps(void const* args);
 void airGuitar(void const* args);
-void accelTesting(void const* args);
+void get_sensor_data(void const* args);
+
+uint8_t play = 0;
 
 FlexSensor index(DIGITAL, p15);       // flex sensor 1.
 FlexSensor majeur(DIGITAL, p16);      // flex sensor 2.
 FlexSensor annulaire(DIGITAL, p17);   // flex sensor 3.
 CountDown countDown;
 Accel accel;
+FlexSensor flex;
+RtosTimer *sync;
+
+Thread *threads[2];
 
 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
 
+typedef struct {
+    accel_t accel_data;
+    flex_t flex_data;
+} sensors_t;
+
+Mail<sensors_t, 32> mailbox_sensors;
+
+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);
+        }
+    }
+}
+
 int main(void const* args)
 {
     // Initializing the accelerometer
     accel = Accel();
     accel.init_MMA8452();
-
-    RtosTimer sync(accelTesting, osTimerPeriodic, (void *)0);
-    sync.start(250);
-
+    RtosTimer timer(get_sensor_data, osTimerPeriodic, (void *)0);
+    sync = &timer;
     GameMode mode = GUNNER;
-    countDown.run();
     switch(mode) {
         case GUNNER:
             gunner_thread_ptr =  new Thread(gunner);
@@ -59,22 +105,27 @@
         default:
             break;
     }
-    while(1) {
+    
+    Thread thread(reception_coord);
+    Thread thread2(etat_de_jeu);
+    threads[0] = &thread;
+    threads[1] = &thread2;
+    
+    while(true) {
     }
 }
 
 void gunner(void const* args)
-{ 
-    // local variables 
+{
+    // local variables
     Cible* cible = new Cible();
     countDown.run();
-    
-    while(true)
-    {
+
+    while(true) {
         // Thread::signal_wait(GO);
         cible->reset();
         int target = rand() % 3;
-            
+
         cible->set(target);
         countDown.run();
     }
@@ -100,8 +151,13 @@
     }
 }
 
-void accelTesting(void const* args)
+
+void get_sensor_data(void const* args)
 {
-    accel.get_axis_values();
+    sensors_t *mail = mailbox_sensors.alloc();
+    mail->accel_data = accel.get_axis_values();
+    mail->flex_data = flex.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);
 }