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.
Fork of APP2 by
main.cpp@0:b4d43279883c, 2016-01-24 (annotated)
- Committer:
- manl2003
- Date:
- Sun Jan 24 17:00:42 2016 +0000
- Revision:
- 0:b4d43279883c
- Child:
- 1:7b379ec59b5d
assd
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 | // synchronisation sur la période d'échantillonnage |
| manl2003 | 0:b4d43279883c | 67 | //TODO |
| manl2003 | 0:b4d43279883c | 68 | // lecture de l'étampe temporelle |
| manl2003 | 0:b4d43279883c | 69 | //TODO |
| manl2003 | 0:b4d43279883c | 70 | // lecture des échantillons analogiques |
| manl2003 | 0:b4d43279883c | 71 | //TODO soit inclure ea_2, soit rendre la méthode réutilisable (plus nice) |
| manl2003 | 0:b4d43279883c | 72 | echantillonsAnal[compteur] = ea_1; |
| manl2003 | 0:b4d43279883c | 73 | // calcul de la nouvelle moyenne courante |
| manl2003 | 0:b4d43279883c | 74 | if(premiereLectureDispo == true) |
| manl2003 | 0:b4d43279883c | 75 | { |
| manl2003 | 0:b4d43279883c | 76 | moyenneCourante = 0; |
| manl2003 | 0:b4d43279883c | 77 | for(int i = 0; i < 5; i++) |
| manl2003 | 0:b4d43279883c | 78 | { |
| manl2003 | 0:b4d43279883c | 79 | moyenneCourante += echantillonsAnal[i]; |
| manl2003 | 0:b4d43279883c | 80 | } |
| manl2003 | 0:b4d43279883c | 81 | moyenneCourante = moyenneCourante / 5; |
| manl2003 | 0:b4d43279883c | 82 | } |
| manl2003 | 0:b4d43279883c | 83 | |
| manl2003 | 0:b4d43279883c | 84 | compteur++; |
| manl2003 | 0:b4d43279883c | 85 | if(compteur >= 5) |
| manl2003 | 0:b4d43279883c | 86 | { |
| manl2003 | 0:b4d43279883c | 87 | compteur = 0; |
| manl2003 | 0:b4d43279883c | 88 | premiereLectureDispo = true; |
| manl2003 | 0:b4d43279883c | 89 | } |
| manl2003 | 0:b4d43279883c | 90 | // génération éventuelle d'un événement |
| manl2003 | 0:b4d43279883c | 91 | if(moyenneCourante > SEUIL_ANAL * NIVEAU_DC_ANAL) |
| manl2003 | 0:b4d43279883c | 92 | { |
| manl2003 | 0:b4d43279883c | 93 | //Générer un évènement |
| manl2003 | 0:b4d43279883c | 94 | } |
| manl2003 | 0:b4d43279883c | 95 | } |
| manl2003 | 0:b4d43279883c | 96 | } |
| manl2003 | 0:b4d43279883c | 97 | |
| manl2003 | 0:b4d43279883c | 98 | |
| manl2003 | 0:b4d43279883c | 99 | void lecture_num(void const *args) { |
| manl2003 | 0:b4d43279883c | 100 | bool lecture, lecturePrecedente; |
| manl2003 | 0:b4d43279883c | 101 | while (true) |
| manl2003 | 0:b4d43279883c | 102 | { |
| manl2003 | 0:b4d43279883c | 103 | // synchronisation sur la période d'échantillonnage |
| manl2003 | 0:b4d43279883c | 104 | //TODO |
| manl2003 | 0:b4d43279883c | 105 | // lecture de l'étampe temporelle |
| manl2003 | 0:b4d43279883c | 106 | //TODO |
| manl2003 | 0:b4d43279883c | 107 | // lecture des échantillons numériques |
| manl2003 | 0:b4d43279883c | 108 | //TODO, généralisé la fonction |
| manl2003 | 0:b4d43279883c | 109 | bool lectureDebut = en_1.read(); |
| manl2003 | 0:b4d43279883c | 110 | wait_ms(FREQ_NUM_STAB_MS); |
| manl2003 | 0:b4d43279883c | 111 | if(lectureDebut == en_1.read()) |
| manl2003 | 0:b4d43279883c | 112 | { |
| manl2003 | 0:b4d43279883c | 113 | |
| manl2003 | 0:b4d43279883c | 114 | } |
| manl2003 | 0:b4d43279883c | 115 | |
| manl2003 | 0:b4d43279883c | 116 | // prise en charge du phénomène de rebond |
| manl2003 | 0:b4d43279883c | 117 | // génération éventuelle d'un événement |
| manl2003 | 0:b4d43279883c | 118 | } |
| manl2003 | 0:b4d43279883c | 119 | } |
| manl2003 | 0:b4d43279883c | 120 | |
| manl2003 | 0:b4d43279883c | 121 | void collection(void const *args) { |
| manl2003 | 0:b4d43279883c | 122 | while (true) |
| manl2003 | 0:b4d43279883c | 123 | { |
| manl2003 | 0:b4d43279883c | 124 | // attente et lecture d'un événement |
| manl2003 | 0:b4d43279883c | 125 | // écriture de l'événement en sortie (port série) |
| manl2003 | 0:b4d43279883c | 126 | } |
| manl2003 | 0:b4d43279883c | 127 | } |
| manl2003 | 0:b4d43279883c | 128 | |
| manl2003 | 0:b4d43279883c | 129 | |
| manl2003 | 0:b4d43279883c | 130 | |
| manl2003 | 0:b4d43279883c | 131 | void signaler() { |
| manl2003 | 0:b4d43279883c | 132 | Thread thread_Anal(lecture_analog); |
| manl2003 | 0:b4d43279883c | 133 | Thread thread_Num(lecture_num); |
| manl2003 | 0:b4d43279883c | 134 | |
| manl2003 | 0:b4d43279883c | 135 | //Transitionner vers RTOS_TImer |
| manl2003 | 0:b4d43279883c | 136 | while (true) |
| manl2003 | 0:b4d43279883c | 137 | { |
| manl2003 | 0:b4d43279883c | 138 | thread_Anal.signal_set(SIGNAL_ANAL); |
| manl2003 | 0:b4d43279883c | 139 | thread_Num.signal_set(SIGNAL_NUM); |
| manl2003 | 0:b4d43279883c | 140 | } |
| manl2003 | 0:b4d43279883c | 141 | } |
| manl2003 | 0:b4d43279883c | 142 | |
| manl2003 | 0:b4d43279883c | 143 | int main() { |
| manl2003 | 0:b4d43279883c | 144 | // initialisation du RTC |
| manl2003 | 0:b4d43279883c | 145 | // démarrage des tâches |
| manl2003 | 0:b4d43279883c | 146 | while(1) { |
| manl2003 | 0:b4d43279883c | 147 | } |
| manl2003 | 0:b4d43279883c | 148 | } |
