Laurent Huot
/
APP2
RTC et Timers
Fork of APP2 by
main.cpp@1:7b379ec59b5d, 2016-01-24 (annotated)
- Committer:
- larryspaghetti
- Date:
- Sun Jan 24 20:36:08 2016 +0000
- Revision:
- 1:7b379ec59b5d
- Parent:
- 0:b4d43279883c
- Child:
- 2:28fd10eccd74
Allo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manl2003 | 0:b4d43279883c | 1 | #include "mbed.h" |
manl2003 | 0:b4d43279883c | 2 | #include "rtos.h" |
manl2003 | 0:b4d43279883c | 3 | |
manl2003 | 0:b4d43279883c | 4 | /* |
manl2003 | 0:b4d43279883c | 5 | DigitalOut led1(LED1); |
manl2003 | 0:b4d43279883c | 6 | DigitalOut led2(LED2); |
manl2003 | 0:b4d43279883c | 7 | DigitalIn sw1(p15); |
manl2003 | 0:b4d43279883c | 8 | DigitalIn sw2(p16); |
manl2003 | 0:b4d43279883c | 9 | |
manl2003 | 0:b4d43279883c | 10 | Mutex mute; |
manl2003 | 0:b4d43279883c | 11 | Queue<string, 5> mb; |
manl2003 | 0:b4d43279883c | 12 | |
manl2003 | 0:b4d43279883c | 13 | |
manl2003 | 0:b4d43279883c | 14 | void producer_Thread(void const *args) |
manl2003 | 0:b4d43279883c | 15 | { |
manl2003 | 0:b4d43279883c | 16 | while (true) { |
manl2003 | 0:b4d43279883c | 17 | mute.lock(); |
manl2003 | 0:b4d43279883c | 18 | mb.alloc |
manl2003 | 0:b4d43279883c | 19 | mute.unlock(); |
manl2003 | 0:b4d43279883c | 20 | } |
manl2003 | 0:b4d43279883c | 21 | } |
manl2003 | 0:b4d43279883c | 22 | void consumer_Thread(void const *args) |
manl2003 | 0:b4d43279883c | 23 | { |
manl2003 | 0:b4d43279883c | 24 | while (true) { |
manl2003 | 0:b4d43279883c | 25 | mute.lock(); |
manl2003 | 0:b4d43279883c | 26 | led1 = 0; |
manl2003 | 0:b4d43279883c | 27 | led2 = 0; |
manl2003 | 0:b4d43279883c | 28 | wait(0.5); |
manl2003 | 0:b4d43279883c | 29 | mute.unlock(); |
manl2003 | 0:b4d43279883c | 30 | } |
manl2003 | 0:b4d43279883c | 31 | } |
manl2003 | 0:b4d43279883c | 32 | |
manl2003 | 0:b4d43279883c | 33 | int main() |
manl2003 | 0:b4d43279883c | 34 | { |
manl2003 | 0:b4d43279883c | 35 | Thread thread1(producer_Thread); |
manl2003 | 0:b4d43279883c | 36 | Thread thread2(consumer_Thread); |
manl2003 | 0:b4d43279883c | 37 | while (true) { |
manl2003 | 0:b4d43279883c | 38 | } |
manl2003 | 0:b4d43279883c | 39 | } */ |
manl2003 | 0:b4d43279883c | 40 | |
manl2003 | 0:b4d43279883c | 41 | //Pins |
manl2003 | 0:b4d43279883c | 42 | DigitalIn en_1(p15); |
manl2003 | 0:b4d43279883c | 43 | DigitalIn en_2(p16); |
manl2003 | 0:b4d43279883c | 44 | AnalogIn ea_1(p19); |
manl2003 | 0:b4d43279883c | 45 | AnalogIn ea_2(p20); |
manl2003 | 0:b4d43279883c | 46 | |
manl2003 | 0:b4d43279883c | 47 | //Constantes |
manl2003 | 0:b4d43279883c | 48 | #define FREQ_NUM_MS 100 |
manl2003 | 0:b4d43279883c | 49 | #define FREQ_ANAL_MS 250 |
manl2003 | 0:b4d43279883c | 50 | #define FREQ_NUM_STAB_MS 50 |
manl2003 | 0:b4d43279883c | 51 | |
manl2003 | 0:b4d43279883c | 52 | #define SIGNAL_NUM 0x1 |
manl2003 | 0:b4d43279883c | 53 | #define SIGNAL_ANAL 0x2 |
manl2003 | 0:b4d43279883c | 54 | |
manl2003 | 0:b4d43279883c | 55 | #define NIVEAU_DC_ANAL 3.3 |
manl2003 | 0:b4d43279883c | 56 | #define SEUIL_ANAL 1.125 |
manl2003 | 0:b4d43279883c | 57 | |
manl2003 | 0:b4d43279883c | 58 | void lecture_analog(void const *args) { |
manl2003 | 0:b4d43279883c | 59 | bool premiereLectureDispo = false; |
manl2003 | 0:b4d43279883c | 60 | int echantillonsAnal[5] = {0, 0, 0, 0, 0}; |
manl2003 | 0:b4d43279883c | 61 | int compteur = 0; |
manl2003 | 0:b4d43279883c | 62 | float moyenneCourante = 0; |
manl2003 | 0:b4d43279883c | 63 | |
manl2003 | 0:b4d43279883c | 64 | while (true) |
manl2003 | 0:b4d43279883c | 65 | { |
manl2003 | 0:b4d43279883c | 66 | // lecture des échantillons analogiques |
manl2003 | 0:b4d43279883c | 67 | //TODO soit inclure ea_2, soit rendre la méthode réutilisable (plus nice) |
manl2003 | 0:b4d43279883c | 68 | echantillonsAnal[compteur] = ea_1; |
manl2003 | 0:b4d43279883c | 69 | // calcul de la nouvelle moyenne courante |
manl2003 | 0:b4d43279883c | 70 | if(premiereLectureDispo == true) |
manl2003 | 0:b4d43279883c | 71 | { |
manl2003 | 0:b4d43279883c | 72 | moyenneCourante = 0; |
manl2003 | 0:b4d43279883c | 73 | for(int i = 0; i < 5; i++) |
manl2003 | 0:b4d43279883c | 74 | { |
manl2003 | 0:b4d43279883c | 75 | moyenneCourante += echantillonsAnal[i]; |
manl2003 | 0:b4d43279883c | 76 | } |
manl2003 | 0:b4d43279883c | 77 | moyenneCourante = moyenneCourante / 5; |
manl2003 | 0:b4d43279883c | 78 | } |
manl2003 | 0:b4d43279883c | 79 | |
manl2003 | 0:b4d43279883c | 80 | compteur++; |
manl2003 | 0:b4d43279883c | 81 | if(compteur >= 5) |
manl2003 | 0:b4d43279883c | 82 | { |
manl2003 | 0:b4d43279883c | 83 | compteur = 0; |
manl2003 | 0:b4d43279883c | 84 | premiereLectureDispo = true; |
manl2003 | 0:b4d43279883c | 85 | } |
manl2003 | 0:b4d43279883c | 86 | // génération éventuelle d'un événement |
manl2003 | 0:b4d43279883c | 87 | if(moyenneCourante > SEUIL_ANAL * NIVEAU_DC_ANAL) |
manl2003 | 0:b4d43279883c | 88 | { |
manl2003 | 0:b4d43279883c | 89 | //Générer un évènement |
manl2003 | 0:b4d43279883c | 90 | } |
manl2003 | 0:b4d43279883c | 91 | } |
manl2003 | 0:b4d43279883c | 92 | } |
manl2003 | 0:b4d43279883c | 93 | |
manl2003 | 0:b4d43279883c | 94 | |
manl2003 | 0:b4d43279883c | 95 | void lecture_num(void const *args) { |
manl2003 | 0:b4d43279883c | 96 | bool lecture, lecturePrecedente; |
manl2003 | 0:b4d43279883c | 97 | while (true) |
larryspaghetti | 1:7b379ec59b5d | 98 | { |
manl2003 | 0:b4d43279883c | 99 | // lecture des échantillons numériques |
larryspaghetti | 1:7b379ec59b5d | 100 | //TODO, généralisé la fonction |
manl2003 | 0:b4d43279883c | 101 | bool lectureDebut = en_1.read(); |
manl2003 | 0:b4d43279883c | 102 | wait_ms(FREQ_NUM_STAB_MS); |
manl2003 | 0:b4d43279883c | 103 | if(lectureDebut == en_1.read()) |
manl2003 | 0:b4d43279883c | 104 | { |
larryspaghetti | 1:7b379ec59b5d | 105 | |
manl2003 | 0:b4d43279883c | 106 | } |
manl2003 | 0:b4d43279883c | 107 | |
larryspaghetti | 1:7b379ec59b5d | 108 | // prise en charge du phénomène de rebond |
larryspaghetti | 1:7b379ec59b5d | 109 | // génération éventuelle d'un événement |
larryspaghetti | 1:7b379ec59b5d | 110 | } |
manl2003 | 0:b4d43279883c | 111 | } |
manl2003 | 0:b4d43279883c | 112 | |
manl2003 | 0:b4d43279883c | 113 | void collection(void const *args) { |
manl2003 | 0:b4d43279883c | 114 | while (true) |
manl2003 | 0:b4d43279883c | 115 | { |
larryspaghetti | 1:7b379ec59b5d | 116 | // attente et lecture d'un événement |
larryspaghetti | 1:7b379ec59b5d | 117 | // écriture de l'événement en sortie (port série) |
manl2003 | 0:b4d43279883c | 118 | } |
manl2003 | 0:b4d43279883c | 119 | } |
manl2003 | 0:b4d43279883c | 120 | |
manl2003 | 0:b4d43279883c | 121 | |
manl2003 | 0:b4d43279883c | 122 | |
manl2003 | 0:b4d43279883c | 123 | void signaler() { |
larryspaghetti | 1:7b379ec59b5d | 124 | RtosTimer anal_timer(lecture_num, osTimerPeriodic, NULL); |
larryspaghetti | 1:7b379ec59b5d | 125 | RtosTimer num_timer(lecture_analog, osTimerPeriodic, NULL); |
manl2003 | 0:b4d43279883c | 126 | |
larryspaghetti | 1:7b379ec59b5d | 127 | anal_timer.start(FREQ_ANAL_MS); |
larryspaghetti | 1:7b379ec59b5d | 128 | num_timer.start(FREQ_NUM_MS); |
larryspaghetti | 1:7b379ec59b5d | 129 | |
larryspaghetti | 1:7b379ec59b5d | 130 | //Thread thread_Anal(lecture_analog); |
larryspaghetti | 1:7b379ec59b5d | 131 | //Thread thread_Num(lecture_num); |
manl2003 | 0:b4d43279883c | 132 | //Transitionner vers RTOS_TImer |
larryspaghetti | 1:7b379ec59b5d | 133 | //while (true) |
larryspaghetti | 1:7b379ec59b5d | 134 | //{ |
larryspaghetti | 1:7b379ec59b5d | 135 | // thread_Anal.signal_set(SIGNAL_ANAL); |
larryspaghetti | 1:7b379ec59b5d | 136 | // thread_Num.signal_set(SIGNAL_NUM); |
larryspaghetti | 1:7b379ec59b5d | 137 | //} |
manl2003 | 0:b4d43279883c | 138 | } |
manl2003 | 0:b4d43279883c | 139 | |
manl2003 | 0:b4d43279883c | 140 | int main() { |
larryspaghetti | 1:7b379ec59b5d | 141 | // initialisation du RTC |
larryspaghetti | 1:7b379ec59b5d | 142 | set_time(1453667014); |
larryspaghetti | 1:7b379ec59b5d | 143 | // démarrage des tâches |
larryspaghetti | 1:7b379ec59b5d | 144 | while(1) { |
larryspaghetti | 1:7b379ec59b5d | 145 | } |
manl2003 | 0:b4d43279883c | 146 | } |