![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
School Project
main.cpp
- Committer:
- ericbisson
- Date:
- 2017-01-31
- Revision:
- 2:c6465d4e82d2
- Parent:
- 1:b3ae0d9f02ad
- Child:
- 3:3ecbcc05bc85
File content as of revision 2:c6465d4e82d2:
#include "mbed.h" #include "rtos.h" #define UN_HUITIEME_SHORT 0x1FFF #define MOVING_AVG_SIZE 5 struct message_t { time_t timestamp; char pin_id; unsigned short value; }; struct MovingAverage_t { unsigned short buffer[MOVING_AVG_SIZE]; char cursor; bool bFilled; }; Thread* t1; Thread* t2; Thread* t3; DigitalIn* en_1; DigitalIn* en_2; AnalogIn* ea_1; AnalogIn* ea_2; Serial* pc; Mutex* mutex; Mail<message_t, 16>* Mailbox; MovingAverage_t* MovingAverageP19; MovingAverage_t* MovingAverageP20; bool bLast_p15 = false; bool bLast_p16 = false; unsigned short u16Last_p19 = 0; unsigned short u16Last_p20 = 0; void signal_analog() { t1->signal_set(1); } void signal_digital() { t2->signal_set(1); } 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; } void MailBox_put(time_t timestamp, char pin_id, unsigned short value) { // Prepare the message to send message_t* msg = Mailbox->calloc(); msg->timestamp = timestamp; msg->pin_id = pin_id; msg->value = value; // Add the message to the mailbox mutex->lock(); Mailbox->put(msg); mutex->unlock(); // Signal the new message t3->signal_set(1); t1->yield(); t2->yield(); } void lecture_analog() { while (true) { // synchronisation sur la période d'échantillonnage t1->signal_wait(1); // lecture de l'étampe temporelle time_t curTime = 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 ) { MailBox_put(curTime, 19, res_p19); } if ((res_p20 + UN_HUITIEME_SHORT < u16Last_p20) || (res_p20 - UN_HUITIEME_SHORT) > u16Last_p20 ) { MailBox_put(curTime, 20, res_p20); } u16Last_p19 = res_p19; u16Last_p20 = res_p20; t1->signal_clr(1); t1->yield(); } } void lecture_num() { while (true) { // synchronisation sur la période d'échantillonnage t2->signal_wait(1); // lecture de l'étampe temporelle time_t curTime = 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) { MailBox_put(curTime, 15, bP15); } if (en_2->read() == bP16 && bP16 != bLast_p16) { MailBox_put(curTime, 16, bP16); } bLast_p15 = bP15; bLast_p16 = bP16; t2->signal_clr(1); t2->yield(); } } void collection() { while (true) { // attente et lecture d'un événement t3->signal_wait(1); mutex->lock(); while (Mailbox->get().status == osEventMail) { message_t* msg = (message_t*)Mailbox->get().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); } mutex->unlock(); t3->signal_clr(1); t3->yield(); } } int main() { // Initialization de tous les globals Mailbox = new Mail<message_t, 16>(); MovingAverageP19 = new MovingAverage_t(); MovingAverageP20 = new MovingAverage_t(); en_1 = new DigitalIn(p15); en_2 = new DigitalIn(p16); ea_1 = new AnalogIn(p19); ea_2 = new AnalogIn(p20); mutex = new Mutex(); pc = new Serial(USBTX, USBRX); // set the time set_time(1485887400); // démarrage des tâches t1 = new Thread(); t2 = new Thread(); t3 = new Thread(); t1->start(lecture_analog); t2->start(lecture_num); t3->start(collection); Ticker tick1; Ticker tick2; tick1.attach(&signal_digital, 0.1); tick2.attach(&signal_analog, 0.25); while(1) { } }