![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
app4
Dependencies: mbed-rtos mbed CRC16
Fork of S5info_APP2 by
Diff: main.cpp
- Revision:
- 2:c6465d4e82d2
- Parent:
- 1:b3ae0d9f02ad
- Child:
- 3:3ecbcc05bc85
--- a/main.cpp Mon Jan 30 22:40:48 2017 +0000 +++ b/main.cpp Tue Jan 31 00:51:06 2017 +0000 @@ -1,18 +1,8 @@ #include "mbed.h" #include "rtos.h" -Ticker tick1; -Ticker tick2; -Thread* t1; -Thread* t2; -Thread* t3; - -DigitalIn en_1(p15); -DigitalIn en_2(p16); -AnalogIn ea_1(p19); -AnalogIn ea_2(p20); -Serial pc(USBTX, USBRX); -Mutex mutex; +#define UN_HUITIEME_SHORT 0x1FFF +#define MOVING_AVG_SIZE 5 struct message_t { @@ -21,27 +11,27 @@ 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 - }; + +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; @@ -55,7 +45,7 @@ void signal_digital() { - t2->signal_set(2); + t2->signal_set(1); } unsigned short moyenne_mobile(MovingAverage_t* MA, unsigned short newData) @@ -89,111 +79,117 @@ return sum; } -inline void MailBox_put(message_t* data) +void MailBox_put(time_t timestamp, char pin_id, unsigned short value) { - 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); + // 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(void/* const *args*/) { - while (true) { -// synchronisation sur la période d'échantillonnage - t1->signal_wait(1); - -// 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); +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(); } - 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(); - } } -void lecture_num(void/* const *args*/) { - while (true) { -// synchronisation sur la période d'échantillonnage - t2->signal_wait(2); - -// 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); +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(); } - 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) { -// attente et lecture d'un événement - 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(); - } +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() { +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); @@ -206,10 +202,12 @@ t2->start(lecture_num); t3->start(collection); - tick1.attach(&signal_digital, 0.1); // fréquence de 100ms - tick2.attach(&signal_analog, 0.25); // fréquence de 250ms + Ticker tick1; + Ticker tick2; + + tick1.attach(&signal_digital, 0.1); + tick2.attach(&signal_analog, 0.25); while(1) { - wait_ms(1000); // wait forever } } \ No newline at end of file