g

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
ShaolinPoutine
Date:
Tue Jan 31 21:56:35 2017 +0000
Parent:
1:8475f46f050b
Commit message:
g

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 8475f46f050b -r 2d84e5bf72bd main.cpp
--- a/main.cpp	Tue Jan 31 15:43:41 2017 +0000
+++ b/main.cpp	Tue Jan 31 21:56:35 2017 +0000
@@ -1,155 +1,120 @@
-#include "mbed.h"
 #include "rtos.h"
-Serial pc(USBTX, USBRX);
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-DigitalIn en_1(p15, PullUp);
-DigitalIn en_2(p16, PullUp);
-AnalogIn ea_1(p19);
-AnalogIn ea_2(p20);
+DigitalIn* en_1 = new DigitalIn(p15, PullUp);
+DigitalIn* en_2 = new DigitalIn(p16, PullUp);
+AnalogIn* ea_1 = new AnalogIn(p19);
+AnalogIn* ea_2 = new AnalogIn(p20);
 
-int signalnewdata = 0x1 << 6;
-int signal100ms = 0x1 << 5;
-int signal50ms = 0x1 << 4;
-int signal250ms = 0x1 << 3;
+short signal100ms = 0x1 << 5;
+short signal50ms = 0x1 << 4;
+short signal250ms = 0x1 << 3;
+bool new_numeric = true;
 
-int typeAnalog = 0;
-int typeNumeric = 1;
+short typeAnalog = 0;
+short typeNumeric = 1;
 time_t begin_time = 0;
 
-MemoryPool<float, 16> analogpool;
-// TODO
-MemoryPool<int, 16> numericpool;
+MemoryPool<short, 16> *analogpool = NULL;
+MemoryPool<short, 16> *numericpool = NULL;
 
-Ticker analogtimer;
-Ticker numerictimer;
-
-Thread* panalog_thread;
-Thread* pnumeric_thread;
+Thread* panalog_thread = NULL;
+Thread* pnumeric_thread = NULL;
 
 
 typedef struct
 {
-    int type;               // Which type of event
-    int id;                 // Which pins changed
-    int* position;          // Position in the memory pool
-    unsigned long seconds;  // Timestamp when the event occured
+    bool analog;               // Which type of event
+    short id;                 // Which pins changed
+    short* position;          // Position in the memory pool
+    time_t seconds;  // Timestamp when the event occured
 } reading;
 
-Queue<reading, 16> queue;
+Queue<reading, 16> *queue = NULL;
+
+void SendMessage(time_t seconds, bool analog, short id, short state)
+{
+    reading *message = NULL;
+    message = new reading();
+    // type analog == true
+    if(analog)
+        message->position = analogpool->alloc();
+    else
+        message->position = numericpool->alloc();
+    
+    *message->position = state;
+    message->analog = analog;
+    message->id = id;
+    message->seconds = seconds;
+    queue->put(message);
+}
 
 void lecture_analog(void const *args) {
-    pc.printf("Debut des lectures analogiques\r\n");
-    float state[2] = {0,0};
-    float newstate[2] = {0,0};
-    float mean1[5] = {0,0,0,0,0};
-    float mean2[5] = {0,0,0,0,0};
-    int i = 0;
-    time_t timestamp = time(NULL);
-    unsigned long seconds = 0;
-    reading *message;
+    int state[] = {0,0};
+    int newstate[2] = {0,0};
+    int mean1[5] = {0,0,0,0,0};
+    int mean2[5] = {0,0,0,0,0};
+    short i = 0;
     while (true) {
         // synchronisation sur la période d'échantillonnage
         Thread::signal_wait(signal250ms);
         // lecture de l'étampe temporelle
-        seconds = (unsigned long) difftime(begin_time, 0);
         // lecture des échantillons analogiques
-        mean1[i] = ea_1;
-        mean2[i] = ea_2;
+        mean1[i] = ea_1->read()*3300;
+        mean2[i] = ea_2->read()*3300;
         // calcul de la nouvelle moyenne courante
-        newstate[0] = (mean1[0] + mean1[1] + mean1[2] + mean1[3] + mean1[4]) / 5.0;
-        newstate[1] = (mean2[0] + mean2[1] + mean2[2] + mean2[3] + mean2[4]) / 5.0;
+        newstate[0] = (mean1[0] + mean1[1] + mean1[2] + mean1[3] + mean1[4]) / 5;
+        newstate[1] = (mean2[0] + mean2[1] + mean2[2] + mean2[3] + mean2[4]) / 5;
         // génération éventuelle d'un événement
-        led3 = !led3;
-        if (abs(newstate[0] - state[0]) > 1.0/8.0)
+        if (abs(newstate[0] - state[0]) > 413)
         {
-            state[0] = newstate[0];
-            float *state = analogpool.alloc();
-            *state = state[0];
-            message->type = typeAnalog;
-            message->id = 1;
-            message->position = (int *) state;
-            message->seconds = seconds;
-            queue.put(message);
+           state[0] = newstate[0];
+           SendMessage(begin_time,true,1,(short) state[0]);
         }
-        if (abs(newstate[1] - state[1]) > 1.0/8.0)
+        if (abs(newstate[1] - state[1]) > 413)
         {
-            state[1] = newstate[1];
-            float *state = analogpool.alloc();
-            *state = state[1];
-            message->type = typeAnalog;
-            message->id = 2;
-            message->position = (int *) state;
-            message->seconds = seconds;
-            queue.put(message);
+           state[1] = newstate[1];
+           SendMessage(begin_time,true,2,(short) state[1]);
         }
         i = (i + 1) % 5;
+        Thread::yield();
     }
 }
 
 void lecture_num(void const *args) {
-    pc.printf("Debut des lectures numeriques\r\n");
     bool state[2] = {false, false};
     bool newstate[2] = {false, false};
     bool changed[2] = {false, false};
-    unsigned long seconds = 0;
-    reading *message;
     while (true) {
         // synchronisation sur la période d'échantillonnage
         //numeric_thread.signal_clr(signal100ms);
         Thread::signal_wait(signal100ms);
         
         // lecture de l'étampe temporelle
-        seconds = (unsigned long) difftime(begin_time, 0);
         
         // lecture des échantillons numériques
-        newstate[0] = !en_1;
-        newstate[1] = !en_2;
+        newstate[0] = !(en_1->read());
+        newstate[1] = !(en_2->read());
         
         // prise en charge du phénomène de rebond
         changed[0] = newstate[0] != state[0];
         changed[1] = newstate[1] != state[1];
         
-        int *test = numericpool.alloc();
-        *test = 42;
-        printf("%d", test);
-        numericpool.free(test);
-        
         if (changed[0] || changed [1])
         {
             pnumeric_thread->signal_clr(signal50ms);
             //flag isp
             Thread::signal_wait(signal50ms);
             
-            newstate[0] = !en_1;
-            newstate[1] = !en_2;
+            newstate[0] = !(en_1->read());
+            newstate[1] = !(en_2->read());
             if(newstate[0] != state[0] && changed[0])
             {
-                led1 = newstate[0];
-                state[0] = newstate[0];
-                // génération éventuelle d'un événement
-                int *state = numericpool.alloc();
-                *state = state[0];
-                message->type = typeNumeric;
-                message->id = 1;
-                message->position = state;
-                message->seconds = seconds;
-                queue.put(message);
+               state[0] = newstate[0];
+               SendMessage(begin_time,false,1,state[0]);
             }
             if(newstate[1] != state[1] && changed[1])
             {
-                led2 = newstate[1];
-                state[1] = newstate[1];
-                // génération éventuelle d'un événement
-                int *state = numericpool.alloc();
-                *state = state[1];
-                message->type = typeNumeric;
-                message->id = 2;
-                message->position = state;
-                message->seconds = seconds;
-                queue.put(message);
+               state[1] = newstate[1];
+               SendMessage(begin_time,false,2,state[1]);
             }
         }
         else
@@ -165,50 +130,67 @@
     while (true) {
         
         // attente et lecture d'un événement
-        event = queue.get();
+        event = queue->get();
         message = (reading*) event.value.p;
-        if (message->type == typeNumeric)
+        short sec, min, heure, jour;
+        sec = (short) message->seconds % 60;
+        min = (short) (message->seconds / 60) % 60;
+        heure = (short) (message->seconds / 3600) % 24;
+        jour = (short) message->seconds / 86400;
+        if (!message->analog)
         {
             // écriture de l'événement en sortie (port série)
-            pc.printf("The state on numeric pin %d has changed to %d at %lus\r\n", message->id, (int *) *(message->position), message->seconds);
-            numericpool.free(message->position);
+            printf("N%d is %d at %02dj %02d:%02d:%02d\r\n", message->id, *message->position, jour, heure, min, sec);
+            numericpool->free(message->position);
         }
-        if (message->type == typeAnalog)
+        else
         {
             // écriture de l'événement en sortie (port série)
-            pc.printf("The state on analog pin %d has changed to %d at %lus\r\n", message->id, *(message->position), message->seconds);
-            analogpool.free((float *) message->position);
+            printf("A%d is %dmV at %02dj %02d:%02d:%02d\r\n", message->id, *message->position, jour, heure, min, sec);
+            analogpool->free(message->position);
         }
+        delete message;
     }
 }
 
 void signalnumeric()
 {
-    pnumeric_thread->signal_set(signal100ms | signal50ms);
+    if (new_numeric)
+    {
+        new_numeric = !new_numeric;
+        begin_time = time(NULL);
+        pnumeric_thread->signal_set(signal100ms);
+    }
+    else
+        pnumeric_thread->signal_set(signal100ms | signal50ms);
 }
 
 void signalanalog()
 {
+    begin_time = time(NULL);
     panalog_thread->signal_set(signal250ms);
 }
-Ticker flipper;
-void flip() {
-    led4 = !led4;
-}
+
 int main(){
-   // set_time(0);
-    begin_time = time(NULL);
-    pc.printf("Welcome\r\n");
-    flipper.attach(&flip, 0.5);
+    //initialisation des variables
+    MemoryPool<short, 16> apool;
+    analogpool = &apool;
+    MemoryPool<short, 16> npool;
+    numericpool = &npool;
+    Queue<reading, 16> onequeue;
+    queue = &onequeue;
+
     // initialisation du RTC
+    Ticker analogtimer;
+    Ticker numerictimer;
     analogtimer.attach(&signalanalog, 0.250);
-    numerictimer.attach(&signalnumeric, 0.5);
+    numerictimer.attach(&signalnumeric, 0.05);
     
     // démarrage des tâches
     Thread analog_thread(lecture_analog);
     panalog_thread = &analog_thread;
-    Thread numeric_thread(lecture_num);
-    pnumeric_thread = &numeric_thread;
+    Thread num_thread(lecture_num);
+    pnumeric_thread = &num_thread;
     Thread q(collection);
     while(1){
     }