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.
Diff: main.cpp
- Revision:
- 3:637374dbfc6d
- Parent:
- 2:1303607f8777
- Child:
- 4:877b4e9cad76
--- a/main.cpp Fri Sep 15 17:14:40 2017 +0000 +++ b/main.cpp Sat Sep 16 16:41:19 2017 +0000 @@ -1,60 +1,166 @@ #include "mbed.h" +#include "rtos.h" + +Serial pc(USBTX, USBRX); // tx, rx +DigitalIn en_1(p15); +DigitalIn en_2(p16); +AnalogIn ea_1(p19); +AnalogIn ea_2(p20); + +Ticker sample; + +// variable globale pour lecture button 1 +int previousStateButton1 = 0; +int currentStateButton1; +int validationStateButton1; + +// variable globale pour lecture button 2 +int previousStateButton2 = 0; +int currentStateButton2; +int validationStateButton2; -Serial pc(USBTX, USBRX); +// variable globale pour lecture potentiometre +int tab_counter =0; +int Nouvelle_moyenne_p19; +int Ancienne_moyenne_p19 = 0; +int Nouvelle_moyenne_p20; +int Ancienne_moyenne_p20 = 0; +unsigned short Tableau_Moyenne_p19[5]={0,0,0,0,0}; +unsigned short Tableau_Moyenne_p20[5]={0,0,0,0,0}; + +int ISRcounter = 0; + +Thread *threadNumerique; // pointeur vers le thread du lecture numérique +Thread *threadAnalogique; // pointeur vers le thread du lecture analogique -AnalogIn ain(p20); -AnalogIn ain2(p19); +// Thread pour lecture analogique +void lecture_analog(void const *args) { + while (true) + { + Thread::signal_wait(0x2); // WAITING STATE (waiting for an event) + + 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]; + } -Ticker display; -int i =0; -int y =0; -int z =0; -int Nouvelle_moyenne; -int Ancienne_moyenne = 0; -int Nouvelle_moyenne2; -int Ancienne_moyenne2 = 0; -unsigned short Tableau_Moyenne[5]={0,0,0,0,0}; -unsigned short Tableau_Moyenne2[5]={0,0,0,0,0}; + // vérification de la difference de la moyenne de 12.5% pour la première potentiometre + if((Ancienne_moyenne_p19 - Nouvelle_moyenne_p19) > 40960 || (Nouvelle_moyenne_p19 - Ancienne_moyenne_p19) > 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("Evenement detectee \n\r"); + } + + // vérification de la difference de la moyenne de 12.5% pour le deuxieme potentiometre + 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"); + } + + // mise a jour de l'ancienne moyenne pour les 2 potentiometres + if(tab_counter % 5 == 0) + { + Ancienne_moyenne_p19 = Nouvelle_moyenne_p19; + Ancienne_moyenne_p20 = Nouvelle_moyenne_p20; + } +} +} -void lire_Info(){ +// Thread pour lecture numérique +void lecture_num(void const *args) +{ + while (true) + { + Thread::signal_wait(0x1); // WAITING STATE (waiting for an event) + currentStateButton1 = en_1.read(); // lecture courante numérique button 1 + currentStateButton2 = en_2.read(); // lecture courante numerique button 2 - if(y%2 == 0){ - Tableau_Moyenne[i] = ain.read_u16(); //= LIRE PATTE 8 ; - i = i%5 + 1; - if(i%5 == 0){ - Nouvelle_moyenne = (int)Tableau_Moyenne[0]+(int)Tableau_Moyenne[1]+(int)Tableau_Moyenne[2]+(int)Tableau_Moyenne[3]+(int)Tableau_Moyenne[4]; + if(previousStateButton1 != currentStateButton1 || previousStateButton2 != currentStateButton2) // détection de changement d'état pour les 2 buttons + { + Thread::wait(50); // période de stabilisation de 50ms + + validationStateButton1 = en_1.read(); // nouvelle lecture apres stabilisation button 1 + validationStateButton2 = en_2.read(); // nouvelle lecture apres stabilisation button 2 + + if(currentStateButton1 == validationStateButton1 || currentStateButton2 == validationStateButton2) // validation apres stabilisation pour les 2 buttons + { + pc.printf("Button peser evenement detecter!\n\r"); // declarer evenement + } } + previousStateButton1 = validationStateButton1; // mise a jour de l'état précédente button 1 + previousStateButton2 = validationStateButton2; // mise a jour de l'etat precedente button 2 + } +} + +// sampling timer +void interuptTimer() +{ + 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 } - if(y%2 == 1){ - Tableau_Moyenne2[z] = ain2.read_u16(); //= LIRE PATTE 8 ; - z = z%5 + 1; - if(z%5 == 0){ - Nouvelle_moyenne2 = (int)Tableau_Moyenne2[0]+(int)Tableau_Moyenne2[1]+(int)Tableau_Moyenne2[2]+(int)Tableau_Moyenne2[3]+(int)Tableau_Moyenne2[4]; - } + if (ISRcounter % 5 == 0) ////permet échantillonage chaque 250ms + { + threadAnalogique->signal_set(0x2); //event occurs to trigger in response to signal waiting state } - if((Ancienne_moyenne - Nouvelle_moyenne) > 40960 || (Nouvelle_moyenne - Ancienne_moyenne) > 40960 || (Ancienne_moyenne2 - Nouvelle_moyenne2) > 40960 || (Nouvelle_moyenne2 - Ancienne_moyenne2) > 40960){ - //Detection dun evenement - pc.printf("Evenement detectee \n\r"); - } - - if(y%2 == 0 && i%5==0){ - Ancienne_moyenne = Nouvelle_moyenne; - } - if(y%2 == 1 && z%5 == 0){ - Ancienne_moyenne2 = Nouvelle_moyenne2; - } - y = y%2 + 1; + ISRcounter++; } int main() { - pc.printf("Debut prog"); - display.attach(&lire_Info, 0.125); - while(1){ - - } + + Thread thread1; //Create thread 1, READY state + threadNumerique = &thread1; + + Thread thread2; //Create thread 2, READY state + threadAnalogique = &thread2; + + threadNumerique->start(callback(lecture_num, (void *)NULL)); // RUNNING state + + threadAnalogique->start(callback(lecture_analog, (void *)NULL)); // RUNNING state + + sample.attach(&interuptTimer, 0.05); // période d'échantillonage chaque 50ms + while(1) {} } + + + + + + + + + + + + + + + + + + + + + + + + +