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@5:85cbea9f3eb7, 2017-09-19 (annotated)
- Committer:
- kkalsi
- Date:
- Tue Sep 19 16:59:01 2017 +0000
- Revision:
- 5:85cbea9f3eb7
- Parent:
- 4:877b4e9cad76
- Child:
- 6:a16c0a024fd5
last version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vincentlabbe | 0:0fb169e1b9b4 | 1 | #include "mbed.h" |
kkalsi | 3:637374dbfc6d | 2 | #include "rtos.h" |
kkalsi | 3:637374dbfc6d | 3 | |
kkalsi | 5:85cbea9f3eb7 | 4 | //Serial pc(USBTX, USBRX); // tx, rx |
kkalsi | 3:637374dbfc6d | 5 | DigitalIn en_1(p15); |
kkalsi | 3:637374dbfc6d | 6 | DigitalIn en_2(p16); |
kkalsi | 3:637374dbfc6d | 7 | AnalogIn ea_1(p19); |
kkalsi | 3:637374dbfc6d | 8 | AnalogIn ea_2(p20); |
kkalsi | 3:637374dbfc6d | 9 | |
kkalsi | 5:85cbea9f3eb7 | 10 | // pout vérification d'échantionnage avec oscilloscope |
kkalsi | 5:85cbea9f3eb7 | 11 | DigitalOut sn_test(p18); |
kkalsi | 5:85cbea9f3eb7 | 12 | |
kkalsi | 5:85cbea9f3eb7 | 13 | #define POTENTIOMETRE 1 |
kkalsi | 5:85cbea9f3eb7 | 14 | #define BOUTON 0 |
kkalsi | 5:85cbea9f3eb7 | 15 | #define SIGNAL_ANALOG 0x2 |
kkalsi | 5:85cbea9f3eb7 | 16 | #define SIGNAL_NUM 0x1 |
kkalsi | 5:85cbea9f3eb7 | 17 | |
kkalsi | 3:637374dbfc6d | 18 | Ticker sample; |
kkalsi | 3:637374dbfc6d | 19 | |
kkalsi | 3:637374dbfc6d | 20 | // variable globale pour lecture button 1 |
kkalsi | 3:637374dbfc6d | 21 | int previousStateButton1 = 0; |
kkalsi | 3:637374dbfc6d | 22 | int currentStateButton1; |
kkalsi | 3:637374dbfc6d | 23 | int validationStateButton1; |
kkalsi | 3:637374dbfc6d | 24 | |
kkalsi | 3:637374dbfc6d | 25 | // variable globale pour lecture button 2 |
kkalsi | 3:637374dbfc6d | 26 | int previousStateButton2 = 0; |
kkalsi | 3:637374dbfc6d | 27 | int currentStateButton2; |
kkalsi | 3:637374dbfc6d | 28 | int validationStateButton2; |
vincentlabbe | 0:0fb169e1b9b4 | 29 | |
kkalsi | 3:637374dbfc6d | 30 | // variable globale pour lecture potentiometre |
kkalsi | 3:637374dbfc6d | 31 | int tab_counter =0; |
kkalsi | 3:637374dbfc6d | 32 | int Nouvelle_moyenne_p19; |
kkalsi | 3:637374dbfc6d | 33 | int Ancienne_moyenne_p19 = 0; |
kkalsi | 3:637374dbfc6d | 34 | int Nouvelle_moyenne_p20; |
kkalsi | 3:637374dbfc6d | 35 | int Ancienne_moyenne_p20 = 0; |
kkalsi | 3:637374dbfc6d | 36 | unsigned short Tableau_Moyenne_p19[5]={0,0,0,0,0}; |
kkalsi | 3:637374dbfc6d | 37 | unsigned short Tableau_Moyenne_p20[5]={0,0,0,0,0}; |
kkalsi | 3:637374dbfc6d | 38 | |
kkalsi | 3:637374dbfc6d | 39 | int ISRcounter = 0; |
kkalsi | 3:637374dbfc6d | 40 | |
kkalsi | 3:637374dbfc6d | 41 | Thread *threadNumerique; // pointeur vers le thread du lecture numérique |
kkalsi | 3:637374dbfc6d | 42 | Thread *threadAnalogique; // pointeur vers le thread du lecture analogique |
kkalsi | 4:877b4e9cad76 | 43 | Thread *threadCollection; // pointeur vers le thread d'affichage d'evenement |
kkalsi | 4:877b4e9cad76 | 44 | |
kkalsi | 4:877b4e9cad76 | 45 | // Pour le time stamp |
kkalsi | 4:877b4e9cad76 | 46 | char buffer[32]; |
kkalsi | 4:877b4e9cad76 | 47 | |
kkalsi | 4:877b4e9cad76 | 48 | // structure de data |
kkalsi | 4:877b4e9cad76 | 49 | typedef struct { |
kkalsi | 4:877b4e9cad76 | 50 | time_t seconds; |
kkalsi | 4:877b4e9cad76 | 51 | int pinNumero; |
kkalsi | 5:85cbea9f3eb7 | 52 | int info; |
kkalsi | 5:85cbea9f3eb7 | 53 | bool type; // |
kkalsi | 4:877b4e9cad76 | 54 | } data; |
kkalsi | 4:877b4e9cad76 | 55 | |
kkalsi | 4:877b4e9cad76 | 56 | // Create queue |
kkalsi | 4:877b4e9cad76 | 57 | Queue<data, 32> queue; |
kkalsi | 4:877b4e9cad76 | 58 | |
kkalsi | 4:877b4e9cad76 | 59 | // Create memory pool |
kkalsi | 4:877b4e9cad76 | 60 | MemoryPool<data, 32> mpool; |
kkalsi | 4:877b4e9cad76 | 61 | |
kkalsi | 4:877b4e9cad76 | 62 | // osEvent to get from queue |
kkalsi | 4:877b4e9cad76 | 63 | osEvent evt; |
vincentlabbe | 1:a18a07e03e7d | 64 | |
kkalsi | 3:637374dbfc6d | 65 | // Thread pour lecture analogique |
kkalsi | 3:637374dbfc6d | 66 | void lecture_analog(void const *args) { |
kkalsi | 5:85cbea9f3eb7 | 67 | //int counterRTC = 0; |
kkalsi | 3:637374dbfc6d | 68 | while (true) |
kkalsi | 3:637374dbfc6d | 69 | { |
kkalsi | 5:85cbea9f3eb7 | 70 | Thread::signal_wait(SIGNAL_ANALOG); // WAITING STATE (waiting for an event) |
kkalsi | 5:85cbea9f3eb7 | 71 | /* |
kkalsi | 5:85cbea9f3eb7 | 72 | if(counterRTC == 500) |
kkalsi | 5:85cbea9f3eb7 | 73 | { |
kkalsi | 5:85cbea9f3eb7 | 74 | time_t seconds = time(NULL); |
kkalsi | 5:85cbea9f3eb7 | 75 | printf("\n\r"); |
kkalsi | 5:85cbea9f3eb7 | 76 | printf("valider les frequences dechantillonnage pour analogique \n\r"); |
kkalsi | 5:85cbea9f3eb7 | 77 | printf("Nombre de secondes passer pour 500 iterations = %d\n\r", seconds); |
kkalsi | 5:85cbea9f3eb7 | 78 | printf("Secondes/iterations = %f\n\r", ((float)seconds/500)); |
kkalsi | 5:85cbea9f3eb7 | 79 | } |
kkalsi | 5:85cbea9f3eb7 | 80 | counterRTC++; |
kkalsi | 5:85cbea9f3eb7 | 81 | */ |
kkalsi | 4:877b4e9cad76 | 82 | // create data structure |
kkalsi | 4:877b4e9cad76 | 83 | data *analogData; |
kkalsi | 4:877b4e9cad76 | 84 | |
kkalsi | 3:637374dbfc6d | 85 | Tableau_Moyenne_p19[tab_counter] = ea_1.read_u16(); //= LIRE PATTE 8 ; |
kkalsi | 3:637374dbfc6d | 86 | Tableau_Moyenne_p20[tab_counter] = ea_2.read_u16(); //= LIRE PATTE 8 ; |
kkalsi | 3:637374dbfc6d | 87 | |
kkalsi | 3:637374dbfc6d | 88 | if(tab_counter % 5 == 0) // validation duree de 1.25 seconde sont terminée |
kkalsi | 3:637374dbfc6d | 89 | { |
kkalsi | 3:637374dbfc6d | 90 | Nouvelle_moyenne_p19 = (int)Tableau_Moyenne_p19[0]+(int)Tableau_Moyenne_p19[1]+(int)Tableau_Moyenne_p19[2]+(int)Tableau_Moyenne_p19[3]+(int)Tableau_Moyenne_p19[4]; |
kkalsi | 3:637374dbfc6d | 91 | Nouvelle_moyenne_p20 = (int)Tableau_Moyenne_p20[0]+(int)Tableau_Moyenne_p20[1]+(int)Tableau_Moyenne_p20[2]+(int)Tableau_Moyenne_p20[3]+(int)Tableau_Moyenne_p20[4]; |
kkalsi | 3:637374dbfc6d | 92 | } |
vincentlabbe | 1:a18a07e03e7d | 93 | |
kkalsi | 5:85cbea9f3eb7 | 94 | // vérification de la difference de la moyenne de 12.5% pour les 2 potentiometres |
kkalsi | 4:877b4e9cad76 | 95 | if((Ancienne_moyenne_p19 - Nouvelle_moyenne_p19) > 40960 || (Nouvelle_moyenne_p19 - Ancienne_moyenne_p19) > 40960 || (Ancienne_moyenne_p20 - Nouvelle_moyenne_p20) > 40960 || (Nouvelle_moyenne_p20 - Ancienne_moyenne_p20) > 40960) |
kkalsi | 3:637374dbfc6d | 96 | { |
kkalsi | 4:877b4e9cad76 | 97 | // associte data structure to memory pool |
kkalsi | 4:877b4e9cad76 | 98 | analogData = mpool.alloc(); |
kkalsi | 4:877b4e9cad76 | 99 | |
kkalsi | 5:85cbea9f3eb7 | 100 | if((Ancienne_moyenne_p19 - Nouvelle_moyenne_p19) > 40960 || (Nouvelle_moyenne_p19 - Ancienne_moyenne_p19) > 40960) |
kkalsi | 5:85cbea9f3eb7 | 101 | { |
kkalsi | 5:85cbea9f3eb7 | 102 | analogData->pinNumero = 19; |
kkalsi | 5:85cbea9f3eb7 | 103 | analogData->info = (Nouvelle_moyenne_p19/5); |
kkalsi | 5:85cbea9f3eb7 | 104 | } |
kkalsi | 4:877b4e9cad76 | 105 | if((Ancienne_moyenne_p20 - Nouvelle_moyenne_p20) > 40960 || (Nouvelle_moyenne_p20 - Ancienne_moyenne_p20) > 40960) |
kkalsi | 4:877b4e9cad76 | 106 | { |
kkalsi | 5:85cbea9f3eb7 | 107 | analogData->info = (Nouvelle_moyenne_p20/5); |
kkalsi | 5:85cbea9f3eb7 | 108 | analogData->pinNumero = 20; |
kkalsi | 4:877b4e9cad76 | 109 | } |
kkalsi | 5:85cbea9f3eb7 | 110 | |
kkalsi | 5:85cbea9f3eb7 | 111 | analogData->type = POTENTIOMETRE; |
kkalsi | 4:877b4e9cad76 | 112 | analogData->seconds = time(NULL); |
kkalsi | 4:877b4e9cad76 | 113 | queue.put(analogData); |
kkalsi | 3:637374dbfc6d | 114 | } |
kkalsi | 5:85cbea9f3eb7 | 115 | |
kkalsi | 3:637374dbfc6d | 116 | // mise a jour de l'ancienne moyenne pour les 2 potentiometres |
kkalsi | 3:637374dbfc6d | 117 | if(tab_counter % 5 == 0) |
kkalsi | 4:877b4e9cad76 | 118 | { |
kkalsi | 4:877b4e9cad76 | 119 | Ancienne_moyenne_p20 = Nouvelle_moyenne_p20; |
kkalsi | 3:637374dbfc6d | 120 | Ancienne_moyenne_p19 = Nouvelle_moyenne_p19; |
kkalsi | 3:637374dbfc6d | 121 | } |
kkalsi | 5:85cbea9f3eb7 | 122 | |
kkalsi | 5:85cbea9f3eb7 | 123 | tab_counter = tab_counter % 5 + 1; // incrémentation de i |
kkalsi | 3:637374dbfc6d | 124 | } |
kkalsi | 3:637374dbfc6d | 125 | } |
vincentlabbe | 1:a18a07e03e7d | 126 | |
kkalsi | 3:637374dbfc6d | 127 | // Thread pour lecture numérique |
kkalsi | 3:637374dbfc6d | 128 | void lecture_num(void const *args) |
kkalsi | 3:637374dbfc6d | 129 | { |
kkalsi | 5:85cbea9f3eb7 | 130 | //int counterRTC = 0; |
kkalsi | 3:637374dbfc6d | 131 | while (true) |
kkalsi | 3:637374dbfc6d | 132 | { |
kkalsi | 5:85cbea9f3eb7 | 133 | Thread::signal_wait(SIGNAL_NUM); // WAITING STATE (waiting for an event) |
kkalsi | 5:85cbea9f3eb7 | 134 | /* |
kkalsi | 5:85cbea9f3eb7 | 135 | if(counterRTC == 1000) |
kkalsi | 5:85cbea9f3eb7 | 136 | { |
kkalsi | 5:85cbea9f3eb7 | 137 | time_t seconds = time(NULL); |
kkalsi | 5:85cbea9f3eb7 | 138 | printf("\n\r"); |
kkalsi | 5:85cbea9f3eb7 | 139 | printf("valider les frequences dechantillonnage pour numerique\n\r"); |
kkalsi | 5:85cbea9f3eb7 | 140 | printf("Nombre de secondes passer pour 1000 iterations = %d\n\r", seconds); |
kkalsi | 5:85cbea9f3eb7 | 141 | printf("Secondes/iterations = %f\n\r", ((float)seconds/1000)); |
kkalsi | 5:85cbea9f3eb7 | 142 | } |
kkalsi | 5:85cbea9f3eb7 | 143 | counterRTC++; |
kkalsi | 5:85cbea9f3eb7 | 144 | */ |
kkalsi | 4:877b4e9cad76 | 145 | // create data structure |
kkalsi | 4:877b4e9cad76 | 146 | data *numData; |
kkalsi | 4:877b4e9cad76 | 147 | |
kkalsi | 3:637374dbfc6d | 148 | currentStateButton1 = en_1.read(); // lecture courante numérique button 1 |
kkalsi | 3:637374dbfc6d | 149 | currentStateButton2 = en_2.read(); // lecture courante numerique button 2 |
vincentlabbe | 2:1303607f8777 | 150 | |
kkalsi | 3:637374dbfc6d | 151 | if(previousStateButton1 != currentStateButton1 || previousStateButton2 != currentStateButton2) // détection de changement d'état pour les 2 buttons |
kkalsi | 3:637374dbfc6d | 152 | { |
kkalsi | 3:637374dbfc6d | 153 | Thread::wait(50); // période de stabilisation de 50ms |
kkalsi | 3:637374dbfc6d | 154 | |
kkalsi | 3:637374dbfc6d | 155 | validationStateButton1 = en_1.read(); // nouvelle lecture apres stabilisation button 1 |
kkalsi | 3:637374dbfc6d | 156 | validationStateButton2 = en_2.read(); // nouvelle lecture apres stabilisation button 2 |
kkalsi | 3:637374dbfc6d | 157 | |
kkalsi | 4:877b4e9cad76 | 158 | if(currentStateButton1 == validationStateButton1 || currentStateButton2 == validationStateButton2) |
kkalsi | 4:877b4e9cad76 | 159 | { |
kkalsi | 4:877b4e9cad76 | 160 | // associte data structure to memory pool |
kkalsi | 4:877b4e9cad76 | 161 | numData = mpool.alloc(); |
kkalsi | 4:877b4e9cad76 | 162 | |
kkalsi | 4:877b4e9cad76 | 163 | if(previousStateButton1 != currentStateButton1) |
kkalsi | 4:877b4e9cad76 | 164 | { |
kkalsi | 4:877b4e9cad76 | 165 | numData->pinNumero = 15; |
kkalsi | 5:85cbea9f3eb7 | 166 | numData->info = currentStateButton1; |
kkalsi | 4:877b4e9cad76 | 167 | } |
kkalsi | 4:877b4e9cad76 | 168 | |
kkalsi | 4:877b4e9cad76 | 169 | if(previousStateButton2 != currentStateButton2) |
kkalsi | 4:877b4e9cad76 | 170 | { |
kkalsi | 4:877b4e9cad76 | 171 | numData->pinNumero = 16; |
kkalsi | 5:85cbea9f3eb7 | 172 | numData->info = currentStateButton2; |
kkalsi | 4:877b4e9cad76 | 173 | } |
kkalsi | 4:877b4e9cad76 | 174 | |
kkalsi | 5:85cbea9f3eb7 | 175 | numData->type = BOUTON; |
kkalsi | 4:877b4e9cad76 | 176 | numData->seconds = time(NULL); |
kkalsi | 4:877b4e9cad76 | 177 | queue.put(numData); |
kkalsi | 4:877b4e9cad76 | 178 | } |
kkalsi | 4:877b4e9cad76 | 179 | |
vincentlabbe | 2:1303607f8777 | 180 | } |
kkalsi | 3:637374dbfc6d | 181 | previousStateButton1 = validationStateButton1; // mise a jour de l'état précédente button 1 |
kkalsi | 3:637374dbfc6d | 182 | previousStateButton2 = validationStateButton2; // mise a jour de l'etat precedente button 2 |
kkalsi | 3:637374dbfc6d | 183 | } |
kkalsi | 3:637374dbfc6d | 184 | } |
kkalsi | 3:637374dbfc6d | 185 | |
kkalsi | 4:877b4e9cad76 | 186 | // Thread pour afficher les evenements |
kkalsi | 4:877b4e9cad76 | 187 | void collection(void const *args) { |
kkalsi | 4:877b4e9cad76 | 188 | while (true) { |
kkalsi | 4:877b4e9cad76 | 189 | |
kkalsi | 4:877b4e9cad76 | 190 | // get data from queue and store in evt |
kkalsi | 4:877b4e9cad76 | 191 | evt = queue.get(); |
kkalsi | 4:877b4e9cad76 | 192 | // error check |
kkalsi | 4:877b4e9cad76 | 193 | if (evt.status == osEventMessage) { |
kkalsi | 4:877b4e9cad76 | 194 | // retrive information from queue to data structure |
kkalsi | 4:877b4e9cad76 | 195 | data *dataDisplay = (data*)evt.value.p; |
kkalsi | 4:877b4e9cad76 | 196 | |
kkalsi | 5:85cbea9f3eb7 | 197 | printf("Pin numero = %d\n\r", dataDisplay->pinNumero); |
kkalsi | 4:877b4e9cad76 | 198 | // conversion des secondes en format AA:MM:JJ:HH:MM:SS |
kkalsi | 4:877b4e9cad76 | 199 | strftime(buffer, 32, "%y:%m:%d:%H:%M:%S \n\r", localtime(&dataDisplay->seconds)); |
kkalsi | 4:877b4e9cad76 | 200 | printf("Time as a custom formatted string = %s\n\r", buffer); |
kkalsi | 5:85cbea9f3eb7 | 201 | |
kkalsi | 5:85cbea9f3eb7 | 202 | if(dataDisplay->type == POTENTIOMETRE) |
kkalsi | 5:85cbea9f3eb7 | 203 | { |
kkalsi | 5:85cbea9f3eb7 | 204 | printf("Moyenne courante = %d\n\r", dataDisplay->info); |
kkalsi | 5:85cbea9f3eb7 | 205 | } |
kkalsi | 5:85cbea9f3eb7 | 206 | |
kkalsi | 5:85cbea9f3eb7 | 207 | if(dataDisplay->type == BOUTON) |
kkalsi | 5:85cbea9f3eb7 | 208 | { |
kkalsi | 5:85cbea9f3eb7 | 209 | printf("etat = %d\n\r", dataDisplay->info); |
kkalsi | 5:85cbea9f3eb7 | 210 | } |
kkalsi | 4:877b4e9cad76 | 211 | |
kkalsi | 4:877b4e9cad76 | 212 | mpool.free(dataDisplay); // free memory pool |
kkalsi | 4:877b4e9cad76 | 213 | } |
kkalsi | 4:877b4e9cad76 | 214 | } |
kkalsi | 4:877b4e9cad76 | 215 | } |
kkalsi | 4:877b4e9cad76 | 216 | |
kkalsi | 4:877b4e9cad76 | 217 | |
kkalsi | 3:637374dbfc6d | 218 | // sampling timer |
kkalsi | 3:637374dbfc6d | 219 | void interuptTimer() |
kkalsi | 3:637374dbfc6d | 220 | { |
kkalsi | 3:637374dbfc6d | 221 | if (ISRcounter % 2 == 0) //permet échantillonage chaque 100ms car detecter 1 fois sur 2 |
kkalsi | 3:637374dbfc6d | 222 | { |
kkalsi | 5:85cbea9f3eb7 | 223 | threadNumerique->signal_set(SIGNAL_NUM); //event occurs to trigger in response to signal waiting state |
vincentlabbe | 2:1303607f8777 | 224 | } |
kkalsi | 5:85cbea9f3eb7 | 225 | |
kkalsi | 3:637374dbfc6d | 226 | if (ISRcounter % 5 == 0) ////permet échantillonage chaque 250ms |
kkalsi | 3:637374dbfc6d | 227 | { |
kkalsi | 5:85cbea9f3eb7 | 228 | threadAnalogique->signal_set(SIGNAL_ANALOG); //event occurs to trigger in response to signal waiting state |
vincentlabbe | 2:1303607f8777 | 229 | } |
vincentlabbe | 2:1303607f8777 | 230 | |
kkalsi | 3:637374dbfc6d | 231 | ISRcounter++; |
vincentlabbe | 1:a18a07e03e7d | 232 | } |
vincentlabbe | 0:0fb169e1b9b4 | 233 | |
vincentlabbe | 0:0fb169e1b9b4 | 234 | int main() { |
kkalsi | 4:877b4e9cad76 | 235 | set_time(1505568984); // Set RTC time to today |
kkalsi | 3:637374dbfc6d | 236 | |
kkalsi | 3:637374dbfc6d | 237 | Thread thread1; //Create thread 1, READY state |
kkalsi | 3:637374dbfc6d | 238 | threadNumerique = &thread1; |
kkalsi | 3:637374dbfc6d | 239 | |
kkalsi | 3:637374dbfc6d | 240 | Thread thread2; //Create thread 2, READY state |
kkalsi | 4:877b4e9cad76 | 241 | threadAnalogique = &thread2; |
kkalsi | 4:877b4e9cad76 | 242 | |
kkalsi | 4:877b4e9cad76 | 243 | Thread thread3; //Create thread 3, READY state |
kkalsi | 4:877b4e9cad76 | 244 | threadCollection = &thread3; |
kkalsi | 3:637374dbfc6d | 245 | |
kkalsi | 3:637374dbfc6d | 246 | threadNumerique->start(callback(lecture_num, (void *)NULL)); // RUNNING state |
kkalsi | 3:637374dbfc6d | 247 | |
kkalsi | 3:637374dbfc6d | 248 | threadAnalogique->start(callback(lecture_analog, (void *)NULL)); // RUNNING state |
kkalsi | 3:637374dbfc6d | 249 | |
kkalsi | 4:877b4e9cad76 | 250 | threadCollection->start(callback(collection, (void *)NULL)); // RUNNING state |
kkalsi | 4:877b4e9cad76 | 251 | |
kkalsi | 5:85cbea9f3eb7 | 252 | //set_time(0); // Set RTC time to debut de la terre |
kkalsi | 3:637374dbfc6d | 253 | sample.attach(&interuptTimer, 0.05); // période d'échantillonage chaque 50ms |
kkalsi | 3:637374dbfc6d | 254 | while(1) {} |
vincentlabbe | 0:0fb169e1b9b4 | 255 | } |
vincentlabbe | 1:a18a07e03e7d | 256 |