![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
School Project
main.cpp@5:e878d87782ce, 2017-01-31 (annotated)
- Committer:
- ericbisson
- Date:
- Tue Jan 31 19:28:54 2017 +0000
- Revision:
- 5:e878d87782ce
- Parent:
- 4:87e9b434bb4d
- Child:
- 6:193013e37136
sans sleep
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericbisson | 3:3ecbcc05bc85 | 1 | //#include "mbed.h" |
ericbisson | 0:c637467eeb8f | 2 | #include "rtos.h" |
ericbisson | 0:c637467eeb8f | 3 | |
ericbisson | 2:c6465d4e82d2 | 4 | #define UN_HUITIEME_SHORT 0x1FFF |
ericbisson | 2:c6465d4e82d2 | 5 | #define MOVING_AVG_SIZE 5 |
ericbisson | 1:b3ae0d9f02ad | 6 | |
ericbisson | 1:b3ae0d9f02ad | 7 | struct message_t |
ericbisson | 1:b3ae0d9f02ad | 8 | { |
ericbisson | 1:b3ae0d9f02ad | 9 | time_t timestamp; |
ericbisson | 1:b3ae0d9f02ad | 10 | char pin_id; |
ericbisson | 1:b3ae0d9f02ad | 11 | unsigned short value; |
ericbisson | 1:b3ae0d9f02ad | 12 | }; |
ericbisson | 1:b3ae0d9f02ad | 13 | |
ericbisson | 1:b3ae0d9f02ad | 14 | struct MovingAverage_t |
ericbisson | 1:b3ae0d9f02ad | 15 | { |
ericbisson | 1:b3ae0d9f02ad | 16 | unsigned short buffer[MOVING_AVG_SIZE]; |
ericbisson | 1:b3ae0d9f02ad | 17 | char cursor; |
ericbisson | 1:b3ae0d9f02ad | 18 | bool bFilled; |
ericbisson | 1:b3ae0d9f02ad | 19 | }; |
ericbisson | 2:c6465d4e82d2 | 20 | |
ericbisson | 2:c6465d4e82d2 | 21 | Thread* t1; |
ericbisson | 2:c6465d4e82d2 | 22 | Thread* t2; |
ericbisson | 4:87e9b434bb4d | 23 | Thread* t3; |
ericbisson | 2:c6465d4e82d2 | 24 | |
ericbisson | 2:c6465d4e82d2 | 25 | DigitalIn* en_1; |
ericbisson | 2:c6465d4e82d2 | 26 | DigitalIn* en_2; |
ericbisson | 2:c6465d4e82d2 | 27 | AnalogIn* ea_1; |
ericbisson | 2:c6465d4e82d2 | 28 | AnalogIn* ea_2; |
ericbisson | 2:c6465d4e82d2 | 29 | Mutex* mutex; |
ericbisson | 2:c6465d4e82d2 | 30 | Mail<message_t, 16>* Mailbox; |
ericbisson | 2:c6465d4e82d2 | 31 | |
ericbisson | 2:c6465d4e82d2 | 32 | MovingAverage_t* MovingAverageP19; |
ericbisson | 2:c6465d4e82d2 | 33 | MovingAverage_t* MovingAverageP20; |
ericbisson | 1:b3ae0d9f02ad | 34 | |
ericbisson | 1:b3ae0d9f02ad | 35 | bool bLast_p15 = false; |
ericbisson | 1:b3ae0d9f02ad | 36 | bool bLast_p16 = false; |
ericbisson | 1:b3ae0d9f02ad | 37 | unsigned short u16Last_p19 = 0; |
ericbisson | 1:b3ae0d9f02ad | 38 | unsigned short u16Last_p20 = 0; |
ericbisson | 0:c637467eeb8f | 39 | |
ericbisson | 5:e878d87782ce | 40 | unsigned char currentTimer = 0; |
ericbisson | 0:c637467eeb8f | 41 | void signal_digital() |
ericbisson | 0:c637467eeb8f | 42 | { |
ericbisson | 5:e878d87782ce | 43 | currentTimer++; |
ericbisson | 5:e878d87782ce | 44 | |
ericbisson | 5:e878d87782ce | 45 | switch (currentTimer) |
ericbisson | 5:e878d87782ce | 46 | { |
ericbisson | 5:e878d87782ce | 47 | case 2: |
ericbisson | 5:e878d87782ce | 48 | case 4: |
ericbisson | 5:e878d87782ce | 49 | case 6: |
ericbisson | 5:e878d87782ce | 50 | case 8: |
ericbisson | 5:e878d87782ce | 51 | { |
ericbisson | 5:e878d87782ce | 52 | t2->signal_set(1); |
ericbisson | 5:e878d87782ce | 53 | break; |
ericbisson | 5:e878d87782ce | 54 | } |
ericbisson | 5:e878d87782ce | 55 | case 5: |
ericbisson | 5:e878d87782ce | 56 | { |
ericbisson | 5:e878d87782ce | 57 | t1->signal_set(1); |
ericbisson | 5:e878d87782ce | 58 | break; |
ericbisson | 5:e878d87782ce | 59 | } |
ericbisson | 5:e878d87782ce | 60 | case 10: |
ericbisson | 5:e878d87782ce | 61 | t2->signal_set(1); |
ericbisson | 5:e878d87782ce | 62 | t1->signal_set(1); |
ericbisson | 5:e878d87782ce | 63 | currentTimer = 0; |
ericbisson | 5:e878d87782ce | 64 | break; |
ericbisson | 5:e878d87782ce | 65 | } |
ericbisson | 5:e878d87782ce | 66 | t2->signal_set(2); |
ericbisson | 0:c637467eeb8f | 67 | } |
ericbisson | 0:c637467eeb8f | 68 | |
ericbisson | 1:b3ae0d9f02ad | 69 | unsigned short moyenne_mobile(MovingAverage_t* MA, unsigned short newData) |
ericbisson | 1:b3ae0d9f02ad | 70 | { |
ericbisson | 1:b3ae0d9f02ad | 71 | int sum = 0; |
ericbisson | 1:b3ae0d9f02ad | 72 | MA->buffer[MA->cursor] = newData; |
ericbisson | 1:b3ae0d9f02ad | 73 | MA->cursor++; |
ericbisson | 1:b3ae0d9f02ad | 74 | if (MA->cursor >= MOVING_AVG_SIZE) |
ericbisson | 1:b3ae0d9f02ad | 75 | { |
ericbisson | 1:b3ae0d9f02ad | 76 | MA->cursor = 0; |
ericbisson | 1:b3ae0d9f02ad | 77 | MA->bFilled = true; |
ericbisson | 1:b3ae0d9f02ad | 78 | } |
ericbisson | 1:b3ae0d9f02ad | 79 | |
ericbisson | 1:b3ae0d9f02ad | 80 | if (MA->bFilled) |
ericbisson | 1:b3ae0d9f02ad | 81 | { |
ericbisson | 1:b3ae0d9f02ad | 82 | for (char i = 0; i < MOVING_AVG_SIZE; i++) |
ericbisson | 1:b3ae0d9f02ad | 83 | { |
ericbisson | 1:b3ae0d9f02ad | 84 | sum += MA->buffer[i]; |
ericbisson | 1:b3ae0d9f02ad | 85 | } |
ericbisson | 1:b3ae0d9f02ad | 86 | sum = sum / MOVING_AVG_SIZE; |
ericbisson | 1:b3ae0d9f02ad | 87 | } |
ericbisson | 1:b3ae0d9f02ad | 88 | else |
ericbisson | 1:b3ae0d9f02ad | 89 | { |
ericbisson | 1:b3ae0d9f02ad | 90 | for (char i = 0; i < MA->cursor; i++) |
ericbisson | 1:b3ae0d9f02ad | 91 | { |
ericbisson | 1:b3ae0d9f02ad | 92 | sum += MA->buffer[i]; |
ericbisson | 1:b3ae0d9f02ad | 93 | } |
ericbisson | 1:b3ae0d9f02ad | 94 | sum = sum / MA->cursor; |
ericbisson | 1:b3ae0d9f02ad | 95 | } |
ericbisson | 1:b3ae0d9f02ad | 96 | |
ericbisson | 1:b3ae0d9f02ad | 97 | return sum; |
ericbisson | 1:b3ae0d9f02ad | 98 | } |
ericbisson | 1:b3ae0d9f02ad | 99 | |
ericbisson | 2:c6465d4e82d2 | 100 | void MailBox_put(time_t timestamp, char pin_id, unsigned short value) |
ericbisson | 1:b3ae0d9f02ad | 101 | { |
ericbisson | 2:c6465d4e82d2 | 102 | // Prepare the message to send |
ericbisson | 2:c6465d4e82d2 | 103 | message_t* msg = Mailbox->calloc(); |
ericbisson | 2:c6465d4e82d2 | 104 | msg->timestamp = timestamp; |
ericbisson | 2:c6465d4e82d2 | 105 | msg->pin_id = pin_id; |
ericbisson | 2:c6465d4e82d2 | 106 | msg->value = value; |
ericbisson | 2:c6465d4e82d2 | 107 | |
ericbisson | 2:c6465d4e82d2 | 108 | // Add the message to the mailbox |
ericbisson | 2:c6465d4e82d2 | 109 | mutex->lock(); |
ericbisson | 2:c6465d4e82d2 | 110 | Mailbox->put(msg); |
ericbisson | 2:c6465d4e82d2 | 111 | mutex->unlock(); |
ericbisson | 1:b3ae0d9f02ad | 112 | } |
ericbisson | 1:b3ae0d9f02ad | 113 | |
ericbisson | 2:c6465d4e82d2 | 114 | void lecture_analog() { |
ericbisson | 2:c6465d4e82d2 | 115 | while (true) { |
ericbisson | 2:c6465d4e82d2 | 116 | // synchronisation sur la période d'échantillonnage |
ericbisson | 3:3ecbcc05bc85 | 117 | t1->signal_clr(1); |
ericbisson | 2:c6465d4e82d2 | 118 | t1->signal_wait(1); |
ericbisson | 2:c6465d4e82d2 | 119 | |
ericbisson | 2:c6465d4e82d2 | 120 | // lecture de l'étampe temporelle |
ericbisson | 2:c6465d4e82d2 | 121 | time_t curTime = time(NULL); |
ericbisson | 2:c6465d4e82d2 | 122 | |
ericbisson | 2:c6465d4e82d2 | 123 | // lecture des échantillons analogiques |
ericbisson | 2:c6465d4e82d2 | 124 | // calcul de la nouvelle moyenne courante |
ericbisson | 2:c6465d4e82d2 | 125 | unsigned short res_p19 = moyenne_mobile(MovingAverageP19, ea_1->read_u16()); |
ericbisson | 2:c6465d4e82d2 | 126 | unsigned short res_p20 = moyenne_mobile(MovingAverageP20, ea_2->read_u16()); |
ericbisson | 2:c6465d4e82d2 | 127 | |
ericbisson | 2:c6465d4e82d2 | 128 | // génération éventuelle d'un événement |
ericbisson | 2:c6465d4e82d2 | 129 | if ((res_p19 + UN_HUITIEME_SHORT) < u16Last_p19 || (res_p19 - UN_HUITIEME_SHORT) > u16Last_p19 ) |
ericbisson | 2:c6465d4e82d2 | 130 | { |
ericbisson | 2:c6465d4e82d2 | 131 | MailBox_put(curTime, 19, res_p19); |
ericbisson | 2:c6465d4e82d2 | 132 | } |
ericbisson | 2:c6465d4e82d2 | 133 | if ((res_p20 + UN_HUITIEME_SHORT < u16Last_p20) || (res_p20 - UN_HUITIEME_SHORT) > u16Last_p20 ) |
ericbisson | 2:c6465d4e82d2 | 134 | { |
ericbisson | 2:c6465d4e82d2 | 135 | MailBox_put(curTime, 20, res_p20); |
ericbisson | 2:c6465d4e82d2 | 136 | } |
ericbisson | 2:c6465d4e82d2 | 137 | |
ericbisson | 2:c6465d4e82d2 | 138 | u16Last_p19 = res_p19; |
ericbisson | 2:c6465d4e82d2 | 139 | u16Last_p20 = res_p20; |
ericbisson | 1:b3ae0d9f02ad | 140 | } |
ericbisson | 0:c637467eeb8f | 141 | } |
ericbisson | 2:c6465d4e82d2 | 142 | void lecture_num() { |
ericbisson | 2:c6465d4e82d2 | 143 | while (true) { |
ericbisson | 2:c6465d4e82d2 | 144 | // synchronisation sur la période d'échantillonnage |
ericbisson | 3:3ecbcc05bc85 | 145 | t2->signal_clr(1); |
ericbisson | 2:c6465d4e82d2 | 146 | t2->signal_wait(1); |
ericbisson | 2:c6465d4e82d2 | 147 | |
ericbisson | 2:c6465d4e82d2 | 148 | // lecture de l'étampe temporelle |
ericbisson | 2:c6465d4e82d2 | 149 | time_t curTime = time(NULL); |
ericbisson | 2:c6465d4e82d2 | 150 | |
ericbisson | 2:c6465d4e82d2 | 151 | // lecture des échantillons numériques |
ericbisson | 2:c6465d4e82d2 | 152 | bool bP15 = en_1->read(); |
ericbisson | 2:c6465d4e82d2 | 153 | bool bP16 = en_2->read(); |
ericbisson | 2:c6465d4e82d2 | 154 | |
ericbisson | 2:c6465d4e82d2 | 155 | // prise en charge du phénomène de rebond |
ericbisson | 5:e878d87782ce | 156 | t2->signal_clr(2); |
ericbisson | 5:e878d87782ce | 157 | t2->signal_wait(2); // :') |
ericbisson | 2:c6465d4e82d2 | 158 | |
ericbisson | 2:c6465d4e82d2 | 159 | // génération éventuelle d'un événement |
ericbisson | 2:c6465d4e82d2 | 160 | if (en_1->read() == bP15 && bP15 != bLast_p15) |
ericbisson | 2:c6465d4e82d2 | 161 | { |
ericbisson | 2:c6465d4e82d2 | 162 | MailBox_put(curTime, 15, bP15); |
ericbisson | 3:3ecbcc05bc85 | 163 | bLast_p15 = bP15; |
ericbisson | 2:c6465d4e82d2 | 164 | } |
ericbisson | 2:c6465d4e82d2 | 165 | if (en_2->read() == bP16 && bP16 != bLast_p16) |
ericbisson | 2:c6465d4e82d2 | 166 | { |
ericbisson | 2:c6465d4e82d2 | 167 | MailBox_put(curTime, 16, bP16); |
ericbisson | 3:3ecbcc05bc85 | 168 | bLast_p16 = bP16; |
ericbisson | 2:c6465d4e82d2 | 169 | } |
ericbisson | 1:b3ae0d9f02ad | 170 | } |
ericbisson | 0:c637467eeb8f | 171 | } |
ericbisson | 3:3ecbcc05bc85 | 172 | |
ericbisson | 4:87e9b434bb4d | 173 | void collection() |
ericbisson | 4:87e9b434bb4d | 174 | { |
ericbisson | 4:87e9b434bb4d | 175 | while(true) |
ericbisson | 4:87e9b434bb4d | 176 | { |
ericbisson | 4:87e9b434bb4d | 177 | // attente et lecture d'un événement |
ericbisson | 4:87e9b434bb4d | 178 | osEvent Mail = Mailbox->get(); |
ericbisson | 4:87e9b434bb4d | 179 | if (Mail.status == osEventMail) |
ericbisson | 4:87e9b434bb4d | 180 | { |
ericbisson | 4:87e9b434bb4d | 181 | message_t* msg = (message_t*)Mail.value.p; |
ericbisson | 4:87e9b434bb4d | 182 | |
ericbisson | 4:87e9b434bb4d | 183 | // écriture de l'événement en sortie (port série) |
ericbisson | 4:87e9b434bb4d | 184 | printf("%s - Evenement de la pin %d avec valeur %d\n\n", ctime(&(msg->timestamp)), msg->pin_id, msg->value); |
ericbisson | 4:87e9b434bb4d | 185 | |
ericbisson | 4:87e9b434bb4d | 186 | Mailbox->free(msg); |
ericbisson | 4:87e9b434bb4d | 187 | } |
ericbisson | 4:87e9b434bb4d | 188 | } |
ericbisson | 4:87e9b434bb4d | 189 | } |
ericbisson | 4:87e9b434bb4d | 190 | |
ericbisson | 2:c6465d4e82d2 | 191 | int main() { |
ericbisson | 2:c6465d4e82d2 | 192 | // Initialization de tous les globals |
ericbisson | 2:c6465d4e82d2 | 193 | Mailbox = new Mail<message_t, 16>(); |
ericbisson | 2:c6465d4e82d2 | 194 | MovingAverageP19 = new MovingAverage_t(); |
ericbisson | 2:c6465d4e82d2 | 195 | MovingAverageP20 = new MovingAverage_t(); |
ericbisson | 2:c6465d4e82d2 | 196 | en_1 = new DigitalIn(p15); |
ericbisson | 2:c6465d4e82d2 | 197 | en_2 = new DigitalIn(p16); |
ericbisson | 2:c6465d4e82d2 | 198 | ea_1 = new AnalogIn(p19); |
ericbisson | 2:c6465d4e82d2 | 199 | ea_2 = new AnalogIn(p20); |
ericbisson | 2:c6465d4e82d2 | 200 | mutex = new Mutex(); |
ericbisson | 2:c6465d4e82d2 | 201 | |
ericbisson | 0:c637467eeb8f | 202 | // set the time |
ericbisson | 3:3ecbcc05bc85 | 203 | set_time(1485887400 - 18000); // -18000 car GMT -5, hardcodé pour 31 janvier 13h30 |
ericbisson | 0:c637467eeb8f | 204 | |
ericbisson | 0:c637467eeb8f | 205 | // démarrage des tâches |
ericbisson | 0:c637467eeb8f | 206 | t1 = new Thread(); |
ericbisson | 0:c637467eeb8f | 207 | t2 = new Thread(); |
ericbisson | 4:87e9b434bb4d | 208 | t3 = new Thread(); |
ericbisson | 0:c637467eeb8f | 209 | |
ericbisson | 0:c637467eeb8f | 210 | t1->start(lecture_analog); |
ericbisson | 0:c637467eeb8f | 211 | t2->start(lecture_num); |
ericbisson | 4:87e9b434bb4d | 212 | t3->start(collection); |
ericbisson | 0:c637467eeb8f | 213 | |
ericbisson | 5:e878d87782ce | 214 | Ticker tick; |
ericbisson | 2:c6465d4e82d2 | 215 | |
ericbisson | 5:e878d87782ce | 216 | tick.attach(&signal_digital, 0.05); // tick aux 50ms |
ericbisson | 0:c637467eeb8f | 217 | |
ericbisson | 3:3ecbcc05bc85 | 218 | while(true) { |
ericbisson | 0:c637467eeb8f | 219 | } |
ericbisson | 0:c637467eeb8f | 220 | } |