app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
Diff: main.cpp
- Revision:
- 1:b3ae0d9f02ad
- Parent:
- 0:c637467eeb8f
- Child:
- 2:c6465d4e82d2
diff -r c637467eeb8f -r b3ae0d9f02ad main.cpp --- a/main.cpp Mon Jan 30 18:52:54 2017 +0000 +++ b/main.cpp Mon Jan 30 22:40:48 2017 +0000 @@ -1,7 +1,8 @@ #include "mbed.h" #include "rtos.h" -Ticker tick; +Ticker tick1; +Ticker tick2; Thread* t1; Thread* t2; Thread* t3; @@ -11,6 +12,41 @@ AnalogIn ea_1(p19); AnalogIn ea_2(p20); Serial pc(USBTX, USBRX); +Mutex mutex; + +struct message_t +{ + time_t timestamp; + char pin_id; + unsigned short value; +}; + +Mail<message_t, 16> Mailbox; + +#define UN_HUITIEME_SHORT 0x1FFF +#define MOVING_AVG_SIZE 5 +struct MovingAverage_t +{ + unsigned short buffer[MOVING_AVG_SIZE]; + char cursor; + bool bFilled; +}; +MovingAverage_t MovingAverageP19 = { + {}, + 0, + false + }; + +MovingAverage_t MovingAverageP20 = { + {}, + 0, + false + }; + +bool bLast_p15 = false; +bool bLast_p16 = false; +unsigned short u16Last_p19 = 0; +unsigned short u16Last_p20 = 0; void signal_analog() { @@ -22,14 +58,81 @@ t2->signal_set(2); } +unsigned short moyenne_mobile(MovingAverage_t* MA, unsigned short newData) +{ + int sum = 0; + MA->buffer[MA->cursor] = newData; + MA->cursor++; + if (MA->cursor >= MOVING_AVG_SIZE) + { + MA->cursor = 0; + MA->bFilled = true; + } + + if (MA->bFilled) + { + for (char i = 0; i < MOVING_AVG_SIZE; i++) + { + sum += MA->buffer[i]; + } + sum = sum / MOVING_AVG_SIZE; + } + else + { + for (char i = 0; i < MA->cursor; i++) + { + sum += MA->buffer[i]; + } + sum = sum / MA->cursor; + } + + return sum; +} + +inline void MailBox_put(message_t* data) +{ + mutex.lock(); + message_t* msg = Mailbox.calloc(); + msg->timestamp = data->timestamp; + msg->pin_id = data->pin_id; + msg->value = data->value; + Mailbox.put(msg); + mutex.unlock(); + t3->signal_set(4); + t1->yield(); + t2->yield(); +} + void lecture_analog(void/* const *args*/) { while (true) { +// synchronisation sur la période d'échantillonnage t1->signal_wait(1); -// synchronisation sur la période d'échantillonnage + // lecture de l'étampe temporelle + message_t msg; + msg.timestamp = time(NULL); + // lecture des échantillons analogiques // calcul de la nouvelle moyenne courante + unsigned short res_p19 = moyenne_mobile(&MovingAverageP19, ea_1.read_u16()); + unsigned short res_p20 = moyenne_mobile(&MovingAverageP20, ea_2.read_u16()); + // génération éventuelle d'un événement + if ((res_p19 + UN_HUITIEME_SHORT) < u16Last_p19 || (res_p19 - UN_HUITIEME_SHORT) > u16Last_p19 ) + { + msg.pin_id = 19; + msg.value = res_p19; + MailBox_put(&msg); + } + if ((res_p20 + UN_HUITIEME_SHORT < u16Last_p20) || (res_p20 - UN_HUITIEME_SHORT) > u16Last_p20 ) + { + msg.pin_id = 20; + msg.value = res_p20; + MailBox_put(&msg); + } + + u16Last_p19 = res_p19; + u16Last_p20 = res_p20; t1->signal_clr(1); t1->yield(); @@ -37,39 +140,62 @@ } void lecture_num(void/* const *args*/) { while (true) { +// synchronisation sur la période d'échantillonnage t2->signal_wait(2); -// synchronisation sur la période d'échantillonnage + // lecture de l'étampe temporelle + message_t msg; + msg.timestamp = time(NULL); + // lecture des échantillons numériques + bool bP15 = en_1.read(); + bool bP16 = en_2.read(); + // prise en charge du phénomène de rebond + wait_ms(50); // :') + // génération éventuelle d'un événement + if (en_1.read() == bP15 && bP15 != bLast_p15) + { + msg.pin_id = 15; + msg.value = bP15; + MailBox_put(&msg); + } + if (en_2.read() == bP16 && bP16 != bLast_p16) + { + msg.pin_id = 16; + msg.value = bP16; + MailBox_put(&msg); + } + bLast_p15 = bP15; + bLast_p16 = bP16; t2->signal_clr(2); t2->yield(); } } void collection(void/* const *args*/) { - while (true) { + while (true) { // attente et lecture d'un événement -// écriture de l'événement en sortie (port série) + t3->signal_wait(4); + mutex.lock(); + osEvent evt = Mailbox.get(); + if (evt.status == osEventMail) { + message_t *msg = (message_t*)evt.value.p; + + // écriture de l'événement en sortie (port série) + pc.printf("[%s] Evenement de la pin %d avec valeur %d\n", ctime(&(msg->timestamp)), msg->pin_id, msg->value); + + Mailbox.free(msg); + } + t3->signal_clr(4); + mutex.unlock(); + t3->yield(); } } -int main() { - // initialisation du RTC - - // get the current time from the terminal - struct tm t; - pc.printf("Enter current date and time:\n"); - pc.printf("YYYY MM DD HH MM SS[enter]\n"); - pc.scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday - , &t.tm_hour, &t.tm_min, &t.tm_sec); - - // adjust for tm structure required values - t.tm_year = t.tm_year - 1900; - t.tm_mon = t.tm_mon - 1; - +int main() { // set the time - set_time(mktime(&t)); + set_time(1485887400); // démarrage des tâches t1 = new Thread(); @@ -80,9 +206,10 @@ t2->start(lecture_num); t3->start(collection); - tick.attach(&signal_analog, 0.25); // fréquence de 250ms - tick.attach(&signal_digital, 0.1); // fréquence de 100ms + tick1.attach(&signal_digital, 0.1); // fréquence de 100ms + tick2.attach(&signal_analog, 0.25); // fréquence de 250ms while(1) { + wait_ms(1000); // wait forever } } \ No newline at end of file