Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- benjaminroy
- Date:
- 2017-01-30
- Revision:
- 17:24e8d26ad707
- Parent:
- 14:3b72daf433fb
- Child:
- 19:abddc058f6d1
File content as of revision 17:24e8d26ad707:
#include "AnalogIn.h"
#include "DigitalIn.h"
#include "Ticker.h"
#include "Serial.h"
#include "rtos.h"
Serial pc(USBTX, USBRX); // tx, rx
Ticker ticker;
DigitalIn en_1(p15);
DigitalIn en_2(p16);
AnalogIn ea_1(p19);
AnalogIn ea_2(p20);
Thread *t1;
Thread *t2;
Thread *t3;
Mutex mutex;
uint8_t compteur = 0;
bool verifierSiStable = false;
/* Mail */
typedef struct {
char* temps; // Étampe indiquant l’heure et la date de l'occurrence
uint8_t broche; // Le numéro de la broche
} mail_t;
Mail<mail_t, 16> mail_box;
// -------------------------------------------------------
// -------------------------------------------------------
void envoyer_mail(uint8_t broche) {
mutex.lock();
time_t secondes = time(NULL);
mutex.unlock();
mail_t *mail = mail_box.alloc();
mail->temps = ctime(&secondes);
mail->broche = broche;
mail_box.put(mail);
}
void lecture_analog(void const *args) {
uint8_t i = 0;
float moyenne_passee1 = -1;
float moyenne_passee2 = -1;
float echantillons1[5];
float echantillons2[5];
float seuil = 0.125;
while (true) {
/*** Synchronisation sur la période d'échantillonnage ***/
Thread::signal_wait(0x1);
/*** Lecture des échantillons analogiques ***/
float entreeanal_1 = ea_1.read();
float entreeanal_2 = ea_2.read();
echantillons1[i] = entreeanal_1;
echantillons2[i] = entreeanal_2;
i++;
if (i == 5) {
/*** Calcul de la moyenne courante du signal (calculée sur 5 échantillons successifs) ***/
float moyenne_courante1 = (echantillons1[0] + echantillons1[1] + echantillons1[2] + echantillons1[3] + echantillons1[4]) / 5;
float moyenne_courante2 = (echantillons2[0] + echantillons2[1] + echantillons2[2] + echantillons2[3] + echantillons2[4]) / 5;
if (moyenne_passee1 != -1) {
if ((std::abs(1 - (moyenne_courante1 / moyenne_passee1))) > seuil) {
envoyer_mail(19); /*** Génération éventuelle d'un événement ***/
}
}
if (moyenne_passee2 != -1) {
if ((std::abs(1 - (moyenne_courante2 / moyenne_passee2))) > seuil) {
envoyer_mail(20); /*** Génération éventuelle d'un événement ***/
}
}
moyenne_passee1 = moyenne_courante1;
moyenne_passee2 = moyenne_courante2;
i = 0;
}
}
}
void lecture_num(void const *args) {
bool valeurs[2] = {0};
while (true) {
/*** Synchronisation sur la période d'échantillonnage ***/
Thread::signal_wait(0x1);
/*** Lecture des échantillons numériques ***/
uint8_t entreenum_1 = en_1.read();
uint8_t entreenum_2 = en_2.read();
/*** Prise en charge du phénomène de rebond ***/
if (entreenum_1 != valeurs[0]) {
verifierSiStable = true;
Thread::signal_wait(0x2);
valeurs[0] = entreenum_1;
if (entreenum_1 == en_1.read()) {
envoyer_mail(15); /*** Génération d'un évènement... ***/
}
}
if (entreenum_2 != valeurs[1]) {
verifierSiStable = true;
Thread::signal_wait(0x2);
valeurs[1] = entreenum_2;
if (entreenum_2 == en_2.read()) {
envoyer_mail(16); /*** Génération d'un évènement... ***/
}
}
Thread::yield();
}
}
void collection(void const *args) {
while (true) {
/*** Attente et lecture d'un événement ***/
osEvent evt = mail_box.get();
if (evt.status == osEventMail) {
mail_t *mail = (mail_t*)evt.value.p;
/*** Écriture de l'événement en sortie (port série) ***/
pc.printf("Entree %d\nDate: %s\n", mail->broche, mail->temps);
mail_box.free(mail);
}
}
}
void alarm() {
compteur++;
if (verifierSiStable) {
t1->signal_set(0x2);
verifierSiStable = false;
}
if (compteur % 2 == 0) { // Entrées numériques échantillionnées à tous les 100 ms
t1->signal_set(0x1);
} else if (compteur % 5 == 0) { // Entrées analogiques échantillonnées à tous les 250 ms
t2->signal_set(0x1);
}
if (compteur % 10 == 0) {
compteur = 0;
}
}
int main() {
/*** Démarrage des tâches ***/
Thread t_lectureNum(lecture_num);
Thread t_lectureAnalog(lecture_analog);
Thread t_collection(collection);
t1 = &t_lectureNum;
t2 = &t_lectureAnalog;
t3 = &t_collection;
/*** Initialisation du RTC ***/
set_time(1485514920); // Set RTC time to...
ticker.attach(&alarm, 0.05);
while(1);
}