![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RTC et Timers
Fork of APP2 by
main.cpp@2:28fd10eccd74, 2016-01-25 (annotated)
- Committer:
- larryspaghetti
- Date:
- Mon Jan 25 18:01:51 2016 +0000
- Revision:
- 2:28fd10eccd74
- Parent:
- 1:7b379ec59b5d
dddd
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 | { |
larryspaghetti | 2:28fd10eccd74 | 89 | //Générer un évènement |
larryspaghetti | 2:28fd10eccd74 | 90 | time_t seconds = time(NULL); |
larryspaghetti | 2:28fd10eccd74 | 91 | char buffer[32]; |
larryspaghetti | 2:28fd10eccd74 | 92 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
manl2003 | 0:b4d43279883c | 93 | } |
manl2003 | 0:b4d43279883c | 94 | } |
manl2003 | 0:b4d43279883c | 95 | } |
manl2003 | 0:b4d43279883c | 96 | |
manl2003 | 0:b4d43279883c | 97 | |
manl2003 | 0:b4d43279883c | 98 | void lecture_num(void const *args) { |
larryspaghetti | 2:28fd10eccd74 | 99 | bool lecture, lecturePrecedente, lectureDebut; |
manl2003 | 0:b4d43279883c | 100 | while (true) |
larryspaghetti | 1:7b379ec59b5d | 101 | { |
manl2003 | 0:b4d43279883c | 102 | // lecture des échantillons numériques |
larryspaghetti | 1:7b379ec59b5d | 103 | //TODO, généralisé la fonction |
larryspaghetti | 2:28fd10eccd74 | 104 | lectureDebut = en_1.read(); |
manl2003 | 0:b4d43279883c | 105 | wait_ms(FREQ_NUM_STAB_MS); |
larryspaghetti | 2:28fd10eccd74 | 106 | if(lectureDebut != en_1.read()) |
manl2003 | 0:b4d43279883c | 107 | { |
larryspaghetti | 2:28fd10eccd74 | 108 | time_t seconds = time(NULL); |
larryspaghetti | 2:28fd10eccd74 | 109 | char buffer[32]; |
larryspaghetti | 2:28fd10eccd74 | 110 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
manl2003 | 0:b4d43279883c | 111 | } |
manl2003 | 0:b4d43279883c | 112 | |
larryspaghetti | 1:7b379ec59b5d | 113 | // prise en charge du phénomène de rebond |
larryspaghetti | 1:7b379ec59b5d | 114 | // génération éventuelle d'un événement |
larryspaghetti | 1:7b379ec59b5d | 115 | } |
manl2003 | 0:b4d43279883c | 116 | } |
manl2003 | 0:b4d43279883c | 117 | |
manl2003 | 0:b4d43279883c | 118 | void collection(void const *args) { |
manl2003 | 0:b4d43279883c | 119 | while (true) |
manl2003 | 0:b4d43279883c | 120 | { |
larryspaghetti | 1:7b379ec59b5d | 121 | // attente et lecture d'un événement |
larryspaghetti | 1:7b379ec59b5d | 122 | // écriture de l'événement en sortie (port série) |
manl2003 | 0:b4d43279883c | 123 | } |
manl2003 | 0:b4d43279883c | 124 | } |
manl2003 | 0:b4d43279883c | 125 | |
manl2003 | 0:b4d43279883c | 126 | |
manl2003 | 0:b4d43279883c | 127 | |
manl2003 | 0:b4d43279883c | 128 | void signaler() { |
larryspaghetti | 1:7b379ec59b5d | 129 | RtosTimer anal_timer(lecture_num, osTimerPeriodic, NULL); |
larryspaghetti | 1:7b379ec59b5d | 130 | RtosTimer num_timer(lecture_analog, osTimerPeriodic, NULL); |
manl2003 | 0:b4d43279883c | 131 | |
larryspaghetti | 1:7b379ec59b5d | 132 | anal_timer.start(FREQ_ANAL_MS); |
larryspaghetti | 1:7b379ec59b5d | 133 | num_timer.start(FREQ_NUM_MS); |
larryspaghetti | 1:7b379ec59b5d | 134 | |
larryspaghetti | 1:7b379ec59b5d | 135 | //Thread thread_Anal(lecture_analog); |
larryspaghetti | 1:7b379ec59b5d | 136 | //Thread thread_Num(lecture_num); |
manl2003 | 0:b4d43279883c | 137 | //Transitionner vers RTOS_TImer |
larryspaghetti | 1:7b379ec59b5d | 138 | //while (true) |
larryspaghetti | 1:7b379ec59b5d | 139 | //{ |
larryspaghetti | 1:7b379ec59b5d | 140 | // thread_Anal.signal_set(SIGNAL_ANAL); |
larryspaghetti | 1:7b379ec59b5d | 141 | // thread_Num.signal_set(SIGNAL_NUM); |
larryspaghetti | 1:7b379ec59b5d | 142 | //} |
manl2003 | 0:b4d43279883c | 143 | } |
manl2003 | 0:b4d43279883c | 144 | |
manl2003 | 0:b4d43279883c | 145 | int main() { |
larryspaghetti | 1:7b379ec59b5d | 146 | // initialisation du RTC |
larryspaghetti | 1:7b379ec59b5d | 147 | set_time(1453667014); |
larryspaghetti | 2:28fd10eccd74 | 148 | |
larryspaghetti | 1:7b379ec59b5d | 149 | // démarrage des tâches |
larryspaghetti | 2:28fd10eccd74 | 150 | RtosTimer anal_timer(lecture_num, osTimerPeriodic, NULL); |
larryspaghetti | 2:28fd10eccd74 | 151 | RtosTimer num_timer(lecture_analog, osTimerPeriodic, NULL); |
larryspaghetti | 2:28fd10eccd74 | 152 | |
larryspaghetti | 2:28fd10eccd74 | 153 | anal_timer.start(FREQ_ANAL_MS); |
larryspaghetti | 2:28fd10eccd74 | 154 | num_timer.start(FREQ_NUM_MS); |
larryspaghetti | 2:28fd10eccd74 | 155 | |
larryspaghetti | 2:28fd10eccd74 | 156 | while(1); |
manl2003 | 0:b4d43279883c | 157 | } |