tiens nic

Dependencies:   mbed-rtos mbed

Fork of rtos_basic by mbed official

Files at this revision

API Documentation at this revision

Comitter:
philfontaine
Date:
Sun Jan 29 23:06:46 2017 +0000
Parent:
10:dc33cd3f4eb9
Commit message:
ok

Changed in this revision

AnalogBuffer.h 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
mbed-os.lib Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r dc33cd3f4eb9 -r fe7d53172c00 AnalogBuffer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogBuffer.h	Sun Jan 29 23:06:46 2017 +0000
@@ -0,0 +1,27 @@
+
+class AnalogBuffer {
+public:
+    AnalogBuffer(){
+        index = 0;
+        for(int i = 0; i < BUFFER_SIZE; i++) {
+            buffer[i] = 0;
+        }   
+    }
+    void put(unsigned short value){
+        buffer[index++] = value;
+        if (index >= BUFFER_SIZE) {
+            index = 0;
+        }
+    }
+    unsigned short average(){
+        unsigned short sum = 0;
+        for(int i = 0; i < BUFFER_SIZE; i++) {
+            sum += buffer[i];
+        }
+        return sum/BUFFER_SIZE;
+    }
+private:
+    static const int BUFFER_SIZE = 5;
+    unsigned short buffer[BUFFER_SIZE];
+    int index;
+};
\ No newline at end of file
diff -r dc33cd3f4eb9 -r fe7d53172c00 main.cpp
--- a/main.cpp	Thu Jan 12 23:35:40 2017 +0000
+++ b/main.cpp	Sun Jan 29 23:06:46 2017 +0000
@@ -1,21 +1,122 @@
 #include "mbed.h"
- 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-Thread thread;
- 
-void led2_thread() {
+#include "rtos.h"
+#include "AnalogBuffer.h"
+
+const int SEUIL_ANALOG = 0x2000;
+DigitalIn en_1(p15);
+DigitalIn en_2(p16);
+AnalogIn ea_1(p19);
+AnalogIn ea_2(p20);
+bool digital_1;
+bool digital_2;
+AnalogBuffer ab_1;
+AnalogBuffer ab_2;
+
+typedef struct {
+    bool isAnalog;
+    bool digital;
+    unsigned short analog;
+    time_t rtc_time;
+} mail_t;
+
+Mail<mail_t, 16> mail_box;
+
+void lecture_analog(void const *args) {
+    Timer timer;
+    unsigned short old_average_1 = 0;
+    unsigned short old_average_2 = 0;
     while (true) {
-        led2 = !led2;
-        wait(1);
+        timer.start(); // synchronisation sur la période d'échantillonnage
+        time_t rtc_time = time(NULL); // lecture de l'étampe temporelle
+        ab_1.put(ea_1.read_u16()); // lecture des échantillons analogiques
+        ab_2.put(ea_2.read_u16());
+        unsigned short new_average_1 = ab_1.average(); // calcul de la nouvelle moyenne courante
+        unsigned short new_average_2 = ab_2.average();
+        int diff_1 = abs(new_average_1 - old_average_1);
+        int diff_2 = abs(new_average_2 - old_average_2);
+        if (diff_1 > SEUIL_ANALOG) {
+            mail_t* mail = mail_box.alloc();
+            mail->isAnalog = true;
+            mail->analog = new_average_1;
+            mail->rtc_time = rtc_time;
+            mail_box.put(mail);
+        }
+        if (diff_2 > SEUIL_ANALOG) {
+            mail_t* mail = mail_box.alloc();
+            mail->isAnalog = true;
+            mail->analog = new_average_1;
+            mail->rtc_time = rtc_time;
+            mail_box.put(mail);
+        }
+        old_average_1 = new_average_1;
+        old_average_2 = new_average_2;
+        Thread::wait(250 - timer.read_ms());
+        timer.stop();
+        timer.reset(); // Necessaire??
     }
 }
- 
-int main() {
-    thread.start(led2_thread);
-    
+void lecture_num(void const *args) {
+    Timer timer;
     while (true) {
-        led1 = !led1;
-        wait(0.5);
+        timer.start(); // synchronisation sur la période d'échantillonnage
+        time_t rtc_time = time(NULL); // lecture de l'étampe temporelle
+        bool new_digital_1 = en_1.read();  // lecture des échantillons numériques
+        bool new_digital_2 = en_2.read();  // lecture des échantillons numériques
+        
+        if ((new_digital_1 != digital_1) || (new_digital_2 != digital_2)) {
+            // prise en charge du phénomène de rebond
+            Thread::wait(50);
+            if (new_digital_1 != digital_1) {
+                new_digital_1 = en_1.read();
+                if (new_digital_1 != digital_1) {
+                    // génération éventuelle d'un événement
+                    mail_t* mail = mail_box.alloc();
+                    mail->isAnalog = false;
+                    mail->digital = new_digital_1;
+                    mail->rtc_time = rtc_time;
+                    mail_box.put(mail);
+                    digital_1 = new_digital_1;
+                }
+            }
+            if (new_digital_2 != digital_2) {
+                new_digital_2 = en_2.read();
+                if (new_digital_2 != digital_2) {
+                    // génération éventuelle d'un événement
+                    mail_t* mail = mail_box.alloc();
+                    mail->isAnalog = false;
+                    mail->digital = new_digital_2;
+                    mail->rtc_time = rtc_time;
+                    mail_box.put(mail);
+                    digital_2 = new_digital_2;
+                }
+            }
+        }
+        Thread::wait(100 - timer.read_ms());
+        timer.stop();
+        timer.reset(); // Necessaire??
     }
 }
+
+void collection(void const *args) {
+    while (true) {
+    // attente et lecture d'un événement
+    // écriture de l'événement en sortie (port série)
+        osEvent evt = mail_box.get();
+        if (evt.status == osEventMail) {
+            mail_t *mail = (mail_t*)evt.value.p;
+            if(mail->isAnalog) {
+                pc.printf("%s Analog: %X", ctime(&mail->rtc_time), mail->analog);
+            } else {
+                pc.printf("%s Digital: %d", ctime(&mail->rtc_time), mail->digital);
+            }
+            mail_box.free(mail);
+        }
+    }
+}
+
+int main() {
+    // initialisation du RTC
+    // démarrage des tâches
+    while(1) {
+    }
+}
\ No newline at end of file
diff -r dc33cd3f4eb9 -r fe7d53172c00 mbed-os.lib
--- a/mbed-os.lib	Thu Jan 12 23:35:40 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://github.com/ARMmbed/mbed-os/#2885c1b41e63158cb6faf5f107cd821ae06ef26c
diff -r dc33cd3f4eb9 -r fe7d53172c00 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sun Jan 29 23:06:46 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
diff -r dc33cd3f4eb9 -r fe7d53172c00 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Jan 29 23:06:46 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/ad3be0349dc5
\ No newline at end of file