asd

Dependencies:   mbed-rtos mbed RTC

Files at this revision

API Documentation at this revision

Comitter:
manl2003
Date:
Fri Jan 29 15:26:00 2016 +0000
Parent:
2:ebbca46b415f
Commit message:
asd

Changed in this revision

RTC.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ebbca46b415f -r 1c077a1de3e5 RTC.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC.lib	Fri Jan 29 15:26:00 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Sissors/code/RTC/#be9d058ee5c7
diff -r ebbca46b415f -r 1c077a1de3e5 main.cpp
--- a/main.cpp	Mon Jan 25 18:03:29 2016 +0000
+++ b/main.cpp	Fri Jan 29 15:26:00 2016 +0000
@@ -1,87 +1,130 @@
+//Laurent et Laurent
+//manl2003 huol2102
+
 #include "mbed.h"
 #include "rtos.h"
+#include "RTC.h"
 
-/*
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalIn sw1(p15);
-DigitalIn sw2(p16);
+//Pins
+DigitalIn en_1(p27);
+DigitalIn en_2(p28);
+AnalogIn ea_1(p19);
+AnalogIn ea_2(p20);
+RawSerial pc(USBTX,USBRX);
+
+//Mutex
+Mutex mute;
 
-Mutex mute;
-Queue<string, 5> mb;
+//Compteurs
+int tic_count = 0;
+int tic_count_num1 = 0;
+int FREQ_NUM_MS = 100;
+int FREQ_ANAL_MS = 250;
 
+//Constantes
+#define FREQ_NUM_STAB_MS 50
+        
+#define SIGNAL_NUM 0x1
+#define SIGNAL_ANALOG 0x2
+#define SEUIL_ANAL 0xB999
 
-void producer_Thread(void const *args)
+//Timers
+RtosTimer *ticker_num1 = NULL;
+RtosTimer *ticker_num2 = NULL;
+RtosTimer *ticker_analog1 = NULL;
+RtosTimer *ticker_analog2 = NULL;
+
+struct MailData
 {
-    while (true) {
-            mute.lock();
-            mb.alloc
-            mute.unlock();
+    //Structure permettant d'encapsuler toutes les données nécessaires à l'affichage
+    void FillData(int _eventType, time_t _seconds)
+    {
+        eventType = _eventType;
+        seconds = _seconds;    
+    }
+    int eventType;
+    time_t seconds;
+    void print()
+    {
+        char buffer[32];
+        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
+        switch(eventType)
+        {
+            case 0: pc.printf("Evenement detecte sur le premier canal numerique a %s.\r\n", buffer); break;
+            case 1: pc.printf("Evenement detecte sur le deuxieme canal numerique a %s.\r\n", buffer); break;
+            case 2: pc.printf("Evenement detecte sur le premier canal analogique a %s.\r\n", buffer); break;
+            case 3: pc.printf("Evenement detecte sur le deuxieme canal analogique a %s.\r\n", buffer); break;
+            
+        }            
     }
-}
-void consumer_Thread(void const *args)
+};
+
+//Mailbox
+Mail<MailData, 16> mailBox;
+
+struct num_args
 {
-    while (true) {
+    //Structure permettant d'encapsuler toutes les arguments pour un événement numérique
+    num_args(DigitalIn *in_arg, int type_arg)
+    {
+        in = in_arg;
+        type = type_arg;
+    }
+    int type;
+    DigitalIn *in;    
+};
+
+struct analog_args
+{
+    //Structure permettant d'encapsuler toutes les arguments pour un événement analogique
+    analog_args(AnalogIn *in_arg, int type_arg){
+        in = in_arg;
+        type = type_arg;
+    }
+    int type;
+    AnalogIn* in;   
+};
+
+void collection(void const *args) {
+    //Thread collecteur
+    while (1) 
+    {
+        // attente et lecture d'un événement
+        osEvent evt = mailBox.get();
+        if (evt.status == osEventMail) 
+        {
             mute.lock();
-            led1 = 0;
-            led2 = 0;
-            wait(0.5);
+            // écriture de l'événement en sortie (port série)
+            MailData *mail = (MailData*)evt.value.p;
+            mail->print();
+            mailBox.free(mail);
             mute.unlock();
+        }
     }
 }
 
-int main()
-{
-    Thread thread1(producer_Thread);
-    Thread thread2(consumer_Thread);
-    while (true) {
-    }
-} */
-
-//Pins
-DigitalIn en_1(p15);
-DigitalIn en_2(p16);
-AnalogIn ea_1(p19);
-AnalogIn ea_2(p20);
-
-//Constantes
-#define FREQ_NUM_MS  100
-#define FREQ_ANAL_MS 250
-#define FREQ_NUM_STAB_MS 50
-        
-#define SIGNAL_NUM 0x1
-#define SIGNAL_ANAL 0x2
+void lecture_analog(void const *args) {
+    //Lecture des événements analogues
+    analog_args *analArgs = (analog_args*)args;
+    bool premiereLectureDispo = false, deuxMoyennesPretes = false;
+    uint16_t echantillonsAnal[5] = {0, 0, 0, 0, 0};
+    int compteur = 0;
+    int moyenneCourante = 0, moyennePrecedente = 0;
+    while(1)
+    {
+        Thread::signal_wait(SIGNAL_ANALOG);
+        // lecture des échantillons analogiques
+        echantillonsAnal[compteur] = analArgs->in->read_u16();
 
-#define NIVEAU_DC_ANAL 3.3
-#define SEUIL_ANAL 1.125
-
-struct AnalArgs
-{
-    AnalogIn* In;
-    //Mailbox    
-};
-
-void lecture_analog(void const *args) {
-    AnalArgs *analArgs = (AnalArgs*)args;
-    
-    bool premiereLectureDispo = false;
-    int echantillonsAnal[5] = {0, 0, 0, 0, 0};
-    int compteur = 0;
-    float moyenneCourante = 0;
-    
-    while (true) 
-    {        
-        // lecture des échantillons analogiques
-        echantillonsAnal[compteur] = *analArgs->In;
         // calcul de la nouvelle moyenne courante
         if(premiereLectureDispo == true)
         {
+            moyennePrecedente = moyenneCourante;
             moyenneCourante = 0;
             for(int i = 0; i < 5; i++)
             {
                 moyenneCourante += echantillonsAnal[i];
             }
-            moyenneCourante = moyenneCourante / 5;
         }
         
         compteur++;
@@ -91,70 +134,142 @@
             premiereLectureDispo = true;
         }
         // génération éventuelle d'un événement
-        if(moyenneCourante > SEUIL_ANAL * NIVEAU_DC_ANAL)
+        if(!deuxMoyennesPretes)
+        {
+            deuxMoyennesPretes = true;
+        }
+        else if(abs(moyenneCourante - moyennePrecedente) > SEUIL_ANAL)
         {
-            //Générer un évènement    
+            //On écrit dans la mailbox lorsqu'une variation excède le seuil
+            mute.lock();
+            MailData * mailData = mailBox.alloc();
+            mailData->FillData(analArgs->type, time(NULL));
+            mailBox.put(mailData);     
+            mute.unlock();
         }
     }
 }
 
 
-struct NumArgs
-{
-    DigitalIn *In;    
-};
-
 void lecture_num(void const *args) {
-    NumArgs *numArgs = (NumArgs*)args;
-    
-    bool lecture, lecturePrecedente;
-    while (true) 
+    // lecture des échantillons numériques
+    num_args *numArgs = (num_args*)args;
+    bool lectureDebut;
+    while (1)
     {
-        // lecture des échantillons numériques
-        //TODO, généralisé la fonction
-        bool lectureDebut = numArgs->In->read();
+        Thread::signal_wait(SIGNAL_NUM);
+        lectureDebut = numArgs->in->read();
         wait_ms(FREQ_NUM_STAB_MS);
-        if(lectureDebut == numArgs->In->read())
+        if(lectureDebut != numArgs->in->read())
         {
-            
+            //On écrit dans la mailbox lorsqu'un événement numérique est détecté
+            mute.lock();
+            MailData * mailData = mailBox.alloc();
+            mailData->FillData(numArgs->type, time(NULL));
+            mailBox.put(mailData);
+            mute.unlock();
         }
-        
-    // prise en charge du phénomène de rebond
-    // génération éventuelle d'un événement
+        if (numArgs->type == 0)
+        {
+            tic_count_num1++;
+        }
     }
 }
 
-void collection(void const *args) {
-while (true) 
+void signaleur_num(void const *args){
+    //Fonction qui envoie periodiquement un signal aux threads numériques
+    Thread* signaled_thread = (Thread*)args;
+    signaled_thread->signal_set(SIGNAL_NUM);
+}
+
+void signaleur_analog(void const *args){
+    //Fonction qui envoie periodiquement un signal aux threads analogiques
+    Thread* signaled_thread = (Thread*)args;
+    signaled_thread->signal_set(SIGNAL_ANALOG);
+}
+
+    
+    
+void starttickers()
+{
+    ticker_num1->start(FREQ_NUM_MS);
+    ticker_num2->start(FREQ_NUM_MS);
+    ticker_analog1->start(FREQ_ANAL_MS);
+    ticker_analog2->start(FREQ_ANAL_MS);
+    pc.printf("Tickers started \n\r");
+}
+    
+void restartTickers()
+{
+    ticker_num1->stop();
+    ticker_num2->stop();
+    ticker_analog1->stop();
+    ticker_analog2->stop();
+    ticker_num1->start(FREQ_NUM_MS);
+    ticker_num2->start(FREQ_NUM_MS);
+    ticker_analog1->start(FREQ_ANAL_MS);
+    ticker_analog2->start(FREQ_ANAL_MS);
+}
+
+void tictoc()
+{
+    //Fonction servant a vérifier et corriger l'écart entre la RTC et la CPU clock
+    int tic_diff;
+    tic_count++;
+    if (tic_count == 10)
     {
-    // attente et lecture d'un événement
-    // écriture de l'événement en sortie (port série)
+        if (!(99 < tic_count_num1 < 101))
+        {
+            tic_diff = 100-tic_count_num1;
+            FREQ_NUM_MS += tic_diff;
+            FREQ_ANAL_MS += tic_diff;
+            restartTickers();
+             pc.printf("Modif: %i %i", FREQ_NUM_MS, FREQ_ANAL_MS);
+        }
+        pc.printf("RTC Tic count: %i, Real Tic count: %i\r\n", tic_count, tic_count_num1);
+        tic_count = 0;
+        tic_count_num1 = 0;
     }
 }
 
-
-
-void signaler() {
-    RtosTimer anal_timer(lecture_num, osTimerPeriodic, NULL);
-    RtosTimer num_timer(lecture_analog, osTimerPeriodic, NULL);
+int main() {
+    en_1.mode(PullUp);
+    en_2.mode(PullUp);
+    
+    pc.printf("Depart\r\n");
     
-    anal_timer.start(FREQ_ANAL_MS);
-    num_timer.start(FREQ_NUM_MS);
+    
+    //Création des specs des threads
+    num_args argsNum1(&en_1, 0);
+    num_args argsNum2(&en_2, 1);
+    analog_args argsAnalog1(&ea_1, 2);
+    analog_args argsAnalog2(&ea_2, 3);
     
-    //Thread thread_Anal(lecture_analog);
-    //Thread thread_Num(lecture_num);
-    //Transitionner vers RTOS_TImer
-    //while (true)
-    //{           
-    //    thread_Anal.signal_set(SIGNAL_ANAL);
-    //    thread_Num.signal_set(SIGNAL_NUM);        
-    //}
-}
-
-int main() {
+    //Créations des threads
+    Thread num1(lecture_num, &argsNum1);
+    Thread num2(lecture_num, &argsNum2);
+    Thread analog1(lecture_analog, &argsAnalog1);
+    Thread analog2(lecture_analog, &argsAnalog2);
+    Thread collecteur(collection);
+    
+    //Initialisation des tickers
+    RtosTimer temp_ticker_num1(signaleur_num, osTimerPeriodic, &num1);
+    ticker_num1 = &temp_ticker_num1;
+    RtosTimer temp_ticker_num2(signaleur_num, osTimerPeriodic, &num2);
+    ticker_num2 = &temp_ticker_num2;
+    RtosTimer temp_ticker_analog1(signaleur_analog, osTimerPeriodic, &analog1);
+    ticker_analog1 = &temp_ticker_analog1;
+    RtosTimer temp_ticker_analog2(signaleur_analog, osTimerPeriodic, &analog2);
+    ticker_analog2 = &temp_ticker_analog2;
+    
     // initialisation du RTC
     set_time(1453667014);
-    // démarrage des tâches
-    while(1) {
-    }
+    
+    //Lancement des tickers
+    starttickers();
+    
+    RTC::attach(&tictoc, RTC::Second);
+
+    
+    while(1);
 }
\ No newline at end of file