team app1 / Mbed 2 deprecated app2

Dependencies:   mbed mbed-rtos

Revision:
5:85cbea9f3eb7
Parent:
4:877b4e9cad76
Child:
6:a16c0a024fd5
--- a/main.cpp	Sun Sep 17 15:21:54 2017 +0000
+++ b/main.cpp	Tue Sep 19 16:59:01 2017 +0000
@@ -1,12 +1,20 @@
 #include "mbed.h"
 #include "rtos.h"
 
-Serial pc(USBTX, USBRX); // tx, rx
+//Serial pc(USBTX, USBRX); // tx, rx
 DigitalIn en_1(p15);
 DigitalIn en_2(p16);
 AnalogIn ea_1(p19);
 AnalogIn ea_2(p20);
 
+// pout vérification d'échantionnage avec oscilloscope
+DigitalOut sn_test(p18);
+
+#define POTENTIOMETRE 1
+#define BOUTON 0
+#define SIGNAL_ANALOG 0x2
+#define SIGNAL_NUM 0x1
+
 Ticker sample;
 
 // variable globale pour lecture button 1
@@ -41,7 +49,8 @@
 typedef struct {
     time_t seconds;
     int pinNumero;
-    //int state;
+    int info;
+    bool type; // 
 } data;
 
 // Create queue
@@ -55,91 +64,84 @@
 
 // Thread pour lecture analogique 
 void lecture_analog(void const *args) {
+    //int counterRTC = 0;
     while (true) 
     {
-        Thread::signal_wait(0x2);   // WAITING STATE (waiting for an event)
-    
+        Thread::signal_wait(SIGNAL_ANALOG);   // WAITING STATE (waiting for an event)
+    /*
+        if(counterRTC == 500)
+        {
+            time_t seconds = time(NULL);
+            printf("\n\r");
+            printf("valider les frequences dechantillonnage pour analogique \n\r");
+            printf("Nombre de secondes passer pour 500 iterations = %d\n\r", seconds);
+            printf("Secondes/iterations = %f\n\r", ((float)seconds/500));
+        }
+        counterRTC++;
+    */    
         // create data structure
         data *analogData;
         
         Tableau_Moyenne_p19[tab_counter] = ea_1.read_u16(); //= LIRE PATTE 8 ;
         Tableau_Moyenne_p20[tab_counter] = ea_2.read_u16(); //= LIRE PATTE 8 ;
         
-        tab_counter = tab_counter % 5 + 1; // incrémentation de i
-        
         if(tab_counter % 5 == 0)    // validation duree de 1.25 seconde sont terminée
         {
         Nouvelle_moyenne_p19 = (int)Tableau_Moyenne_p19[0]+(int)Tableau_Moyenne_p19[1]+(int)Tableau_Moyenne_p19[2]+(int)Tableau_Moyenne_p19[3]+(int)Tableau_Moyenne_p19[4];
         Nouvelle_moyenne_p20 = (int)Tableau_Moyenne_p20[0]+(int)Tableau_Moyenne_p20[1]+(int)Tableau_Moyenne_p20[2]+(int)Tableau_Moyenne_p20[3]+(int)Tableau_Moyenne_p20[4];
         }
 
-        // vérification de la difference de la moyenne de 12.5% pour la première potentiometre 
+        // vérification de la difference de la moyenne de 12.5% pour les 2 potentiometres 
         if((Ancienne_moyenne_p19 - Nouvelle_moyenne_p19) > 40960 || (Nouvelle_moyenne_p19 - Ancienne_moyenne_p19) > 40960 || (Ancienne_moyenne_p20 - Nouvelle_moyenne_p20) > 40960 || (Nouvelle_moyenne_p20 - Ancienne_moyenne_p20) > 40960)
         {
-        //Detection dun evenement
-        
-        pc.printf("ancienne moyenne pin 19 1 = %d ", Ancienne_moyenne_p19);
-        pc.printf("nouvelle moyenne pin 19= %d ", Nouvelle_moyenne_p19);
-        pc.printf("difference pin 19 = %d ", (Ancienne_moyenne_p19 - Nouvelle_moyenne_p19)/5);
-        pc.printf("ancienne moyenne pin 20= %d ", Ancienne_moyenne_p20);
-        pc.printf("nouvelle moyenne pin 20= %d ", Nouvelle_moyenne_p20);
-        pc.printf("difference pin 20= %d ", (Ancienne_moyenne_p20 - Nouvelle_moyenne_p20)/5);
-        
-        
         // associte data structure to memory pool
         analogData = mpool.alloc();
         
-        
-        
+        if((Ancienne_moyenne_p19 - Nouvelle_moyenne_p19) > 40960 || (Nouvelle_moyenne_p19 - Ancienne_moyenne_p19) > 40960)
+        {
+            analogData->pinNumero = 19;  
+            analogData->info = (Nouvelle_moyenne_p19/5);
+        }
         if((Ancienne_moyenne_p20 - Nouvelle_moyenne_p20) > 40960 || (Nouvelle_moyenne_p20 - Ancienne_moyenne_p20) > 40960)
         {
-            analogData->pinNumero = 20;  
+            analogData->info = (Nouvelle_moyenne_p20/5);
+            analogData->pinNumero = 20;      
         }
-        else 
-        {
-            analogData->pinNumero = 19;      
-        }
-             
+        
+        analogData->type = POTENTIOMETRE; 
         analogData->seconds = time(NULL); 
         queue.put(analogData);
         }
-    
-        // vérification de la difference de la moyenne de 12.5% pour le deuxieme potentiometre 
-        /*
-        else if((Ancienne_moyenne_p20 - Nouvelle_moyenne_p20) > 40960 || (Nouvelle_moyenne_p20 - Ancienne_moyenne_p20) > 40960)
-        {
-        //Detection dun evenement
-        
-        pc.printf("ancienne moyenne pin 20= %d ", Ancienne_moyenne_p20);
-        pc.printf("nouvelle moyenne pin 20= %d ", Nouvelle_moyenne_p20);
-        pc.printf("difference pin 20= %d ", (Ancienne_moyenne_p20 - Nouvelle_moyenne_p20)/5);
-        pc.printf("Evenement detectee \n\r");
-        
-        
-        // associte data structure to memory pool
-        analogData = mpool.alloc();
-        analogData->pinNumero = 20;        
-        analogData->seconds = time(NULL); 
-        queue.put(analogData);        
-        }
-        */
-    
+      
         // mise a jour de l'ancienne moyenne pour les 2 potentiometres
         if(tab_counter % 5 == 0)
         {      
             Ancienne_moyenne_p20 = Nouvelle_moyenne_p20;
             Ancienne_moyenne_p19 = Nouvelle_moyenne_p19;
         }
+        
+        tab_counter = tab_counter % 5 + 1; // incrémentation de i
 }
 }
 
 // Thread pour lecture numérique 
 void lecture_num(void const *args) 
 {
+    //int counterRTC = 0;
     while (true) 
     {    
-        Thread::signal_wait(0x1);   // WAITING STATE (waiting for an event)
-        
+        Thread::signal_wait(SIGNAL_NUM);   // WAITING STATE (waiting for an event)
+        /*
+        if(counterRTC == 1000)
+        {
+            time_t seconds = time(NULL);
+            printf("\n\r");
+            printf("valider les frequences dechantillonnage pour numerique\n\r");
+            printf("Nombre de secondes passer pour 1000 iterations = %d\n\r", seconds);
+            printf("Secondes/iterations = %f\n\r", ((float)seconds/1000));
+        }
+        counterRTC++;
+        */
         // create data structure
         data *numData;
         
@@ -161,13 +163,16 @@
                 if(previousStateButton1 != currentStateButton1)
                 {
                     numData->pinNumero = 15;
+                    numData->info = currentStateButton1;
                 }
                     
                 if(previousStateButton2 != currentStateButton2)
                 {
                     numData->pinNumero = 16;
+                    numData->info = currentStateButton2;
                 }
     
+                numData->type = BOUTON;
                 numData->seconds = time(NULL); 
                 queue.put(numData);
             } 
@@ -189,10 +194,20 @@
             // retrive information from queue to data structure
             data *dataDisplay = (data*)evt.value.p;
             
+            printf("Pin numero = %d\n\r", dataDisplay->pinNumero);
             // conversion des secondes en format AA:MM:JJ:HH:MM:SS
             strftime(buffer, 32, "%y:%m:%d:%H:%M:%S \n\r", localtime(&dataDisplay->seconds));
             printf("Time as a custom formatted string = %s\n\r", buffer);
-            printf("Pin numero = %d\n\r", dataDisplay->pinNumero);
+            
+            if(dataDisplay->type == POTENTIOMETRE)
+            {
+                printf("Moyenne courante = %d\n\r", dataDisplay->info);
+            }
+            
+            if(dataDisplay->type == BOUTON)
+            {
+                printf("etat = %d\n\r", dataDisplay->info);
+            }
             
             mpool.free(dataDisplay);    // free memory pool
      }
@@ -205,18 +220,17 @@
 {
     if (ISRcounter % 2 == 0) //permet échantillonage chaque 100ms car detecter 1 fois sur 2 
     {
-    threadNumerique->signal_set(0x1); //event occurs to trigger in response to signal waiting state
+    threadNumerique->signal_set(SIGNAL_NUM); //event occurs to trigger in response to signal waiting state
     }
-    
+ 
     if (ISRcounter % 5 == 0) ////permet échantillonage chaque 250ms
     {
-    threadAnalogique->signal_set(0x2); //event occurs to trigger in response to signal waiting state
+    threadAnalogique->signal_set(SIGNAL_ANALOG); //event occurs to trigger in response to signal waiting state
     }
     
     ISRcounter++;
 }
 
-
 int main() {
     set_time(1505568984);  // Set RTC time to today
     
@@ -235,6 +249,7 @@
     
     threadCollection->start(callback(collection, (void *)NULL)); // RUNNING state
     
+    //set_time(0);  // Set RTC time to debut de la terre
     sample.attach(&interuptTimer, 0.05); // période d'échantillonage chaque 50ms
     while(1) {}
 }