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 rtos_basic by
main.cpp@12:9573a2e293e2, 2017-01-30 (annotated)
- Committer:
- saudraisn
- Date:
- Mon Jan 30 21:20:31 2017 +0000
- Revision:
- 12:9573a2e293e2
- Parent:
- 11:fe7d53172c00
- Child:
- 13:bd6c24a90b4a
b
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
| philfontaine | 11:fe7d53172c00 | 2 | #include "rtos.h" |
| philfontaine | 11:fe7d53172c00 | 3 | #include "AnalogBuffer.h" |
| philfontaine | 11:fe7d53172c00 | 4 | |
| philfontaine | 11:fe7d53172c00 | 5 | const int SEUIL_ANALOG = 0x2000; |
| philfontaine | 11:fe7d53172c00 | 6 | DigitalIn en_1(p15); |
| philfontaine | 11:fe7d53172c00 | 7 | DigitalIn en_2(p16); |
| philfontaine | 11:fe7d53172c00 | 8 | AnalogIn ea_1(p19); |
| philfontaine | 11:fe7d53172c00 | 9 | AnalogIn ea_2(p20); |
| philfontaine | 11:fe7d53172c00 | 10 | bool digital_1; |
| philfontaine | 11:fe7d53172c00 | 11 | bool digital_2; |
| philfontaine | 11:fe7d53172c00 | 12 | AnalogBuffer ab_1; |
| philfontaine | 11:fe7d53172c00 | 13 | AnalogBuffer ab_2; |
| philfontaine | 11:fe7d53172c00 | 14 | |
| saudraisn | 12:9573a2e293e2 | 15 | Serial pc(USBTX, USBRX); // tx, rx |
| saudraisn | 12:9573a2e293e2 | 16 | |
| philfontaine | 11:fe7d53172c00 | 17 | typedef struct { |
| philfontaine | 11:fe7d53172c00 | 18 | bool isAnalog; |
| philfontaine | 11:fe7d53172c00 | 19 | bool digital; |
| philfontaine | 11:fe7d53172c00 | 20 | unsigned short analog; |
| saudraisn | 12:9573a2e293e2 | 21 | unsigned short id; |
| philfontaine | 11:fe7d53172c00 | 22 | time_t rtc_time; |
| philfontaine | 11:fe7d53172c00 | 23 | } mail_t; |
| philfontaine | 11:fe7d53172c00 | 24 | |
| philfontaine | 11:fe7d53172c00 | 25 | Mail<mail_t, 16> mail_box; |
| philfontaine | 11:fe7d53172c00 | 26 | |
| saudraisn | 12:9573a2e293e2 | 27 | void lecture_analog(void const *args) |
| saudraisn | 12:9573a2e293e2 | 28 | { |
| saudraisn | 12:9573a2e293e2 | 29 | // Timer timer; |
| philfontaine | 11:fe7d53172c00 | 30 | unsigned short old_average_1 = 0; |
| philfontaine | 11:fe7d53172c00 | 31 | unsigned short old_average_2 = 0; |
| emilmont | 1:491820ee784d | 32 | while (true) { |
| saudraisn | 12:9573a2e293e2 | 33 | // pc.printf("Thread anal attend"); |
| saudraisn | 12:9573a2e293e2 | 34 | // Thread::signal_wait(0x1); |
| saudraisn | 12:9573a2e293e2 | 35 | // timer.start(); // synchronisation sur la période d'échantillonnage |
| philfontaine | 11:fe7d53172c00 | 36 | time_t rtc_time = time(NULL); // lecture de l'étampe temporelle |
| philfontaine | 11:fe7d53172c00 | 37 | ab_1.put(ea_1.read_u16()); // lecture des échantillons analogiques |
| philfontaine | 11:fe7d53172c00 | 38 | ab_2.put(ea_2.read_u16()); |
| philfontaine | 11:fe7d53172c00 | 39 | unsigned short new_average_1 = ab_1.average(); // calcul de la nouvelle moyenne courante |
| philfontaine | 11:fe7d53172c00 | 40 | unsigned short new_average_2 = ab_2.average(); |
| philfontaine | 11:fe7d53172c00 | 41 | int diff_1 = abs(new_average_1 - old_average_1); |
| philfontaine | 11:fe7d53172c00 | 42 | int diff_2 = abs(new_average_2 - old_average_2); |
| saudraisn | 12:9573a2e293e2 | 43 | |
| philfontaine | 11:fe7d53172c00 | 44 | if (diff_1 > SEUIL_ANALOG) { |
| philfontaine | 11:fe7d53172c00 | 45 | mail_t* mail = mail_box.alloc(); |
| philfontaine | 11:fe7d53172c00 | 46 | mail->isAnalog = true; |
| philfontaine | 11:fe7d53172c00 | 47 | mail->analog = new_average_1; |
| philfontaine | 11:fe7d53172c00 | 48 | mail->rtc_time = rtc_time; |
| saudraisn | 12:9573a2e293e2 | 49 | mail->id = 1; |
| philfontaine | 11:fe7d53172c00 | 50 | mail_box.put(mail); |
| saudraisn | 12:9573a2e293e2 | 51 | old_average_1 = new_average_1; |
| philfontaine | 11:fe7d53172c00 | 52 | } |
| philfontaine | 11:fe7d53172c00 | 53 | if (diff_2 > SEUIL_ANALOG) { |
| philfontaine | 11:fe7d53172c00 | 54 | mail_t* mail = mail_box.alloc(); |
| philfontaine | 11:fe7d53172c00 | 55 | mail->isAnalog = true; |
| saudraisn | 12:9573a2e293e2 | 56 | mail->analog = new_average_2; |
| philfontaine | 11:fe7d53172c00 | 57 | mail->rtc_time = rtc_time; |
| saudraisn | 12:9573a2e293e2 | 58 | mail->id = 2; |
| philfontaine | 11:fe7d53172c00 | 59 | mail_box.put(mail); |
| saudraisn | 12:9573a2e293e2 | 60 | old_average_2 = new_average_2; |
| philfontaine | 11:fe7d53172c00 | 61 | } |
| saudraisn | 12:9573a2e293e2 | 62 | //Thread::wait(250 - timer.read_ms()); |
| saudraisn | 12:9573a2e293e2 | 63 | // timer.stop(); |
| saudraisn | 12:9573a2e293e2 | 64 | // timer.reset(); |
| emilmont | 1:491820ee784d | 65 | } |
| emilmont | 1:491820ee784d | 66 | } |
| saudraisn | 12:9573a2e293e2 | 67 | void lecture_num(void const *args) |
| saudraisn | 12:9573a2e293e2 | 68 | { |
| saudraisn | 12:9573a2e293e2 | 69 | // Timer timer; |
| saudraisn | 12:9573a2e293e2 | 70 | // timer1.start(); |
| saudraisn | 12:9573a2e293e2 | 71 | // unsigned int old; |
| saudraisn | 12:9573a2e293e2 | 72 | // unsigned int newT; |
| saudraisn | 12:9573a2e293e2 | 73 | |
| emilmont | 1:491820ee784d | 74 | while (true) { |
| saudraisn | 12:9573a2e293e2 | 75 | // pc.printf("Thread num attend"); |
| saudraisn | 12:9573a2e293e2 | 76 | // Thread::signal_wait(0x1); |
| saudraisn | 12:9573a2e293e2 | 77 | // timer.start(); // synchronisation sur la période d'échantillonnage |
| saudraisn | 12:9573a2e293e2 | 78 | //old = newT; |
| saudraisn | 12:9573a2e293e2 | 79 | // newT = timer1.read_ms(); |
| saudraisn | 12:9573a2e293e2 | 80 | |
| philfontaine | 11:fe7d53172c00 | 81 | time_t rtc_time = time(NULL); // lecture de l'étampe temporelle |
| philfontaine | 11:fe7d53172c00 | 82 | bool new_digital_1 = en_1.read(); // lecture des échantillons numériques |
| philfontaine | 11:fe7d53172c00 | 83 | bool new_digital_2 = en_2.read(); // lecture des échantillons numériques |
| saudraisn | 12:9573a2e293e2 | 84 | |
| philfontaine | 11:fe7d53172c00 | 85 | if ((new_digital_1 != digital_1) || (new_digital_2 != digital_2)) { |
| philfontaine | 11:fe7d53172c00 | 86 | // prise en charge du phénomène de rebond |
| philfontaine | 11:fe7d53172c00 | 87 | Thread::wait(50); |
| philfontaine | 11:fe7d53172c00 | 88 | if (new_digital_1 != digital_1) { |
| philfontaine | 11:fe7d53172c00 | 89 | new_digital_1 = en_1.read(); |
| philfontaine | 11:fe7d53172c00 | 90 | if (new_digital_1 != digital_1) { |
| philfontaine | 11:fe7d53172c00 | 91 | // génération éventuelle d'un événement |
| philfontaine | 11:fe7d53172c00 | 92 | mail_t* mail = mail_box.alloc(); |
| philfontaine | 11:fe7d53172c00 | 93 | mail->isAnalog = false; |
| philfontaine | 11:fe7d53172c00 | 94 | mail->digital = new_digital_1; |
| philfontaine | 11:fe7d53172c00 | 95 | mail->rtc_time = rtc_time; |
| saudraisn | 12:9573a2e293e2 | 96 | mail->id = 1; |
| philfontaine | 11:fe7d53172c00 | 97 | mail_box.put(mail); |
| philfontaine | 11:fe7d53172c00 | 98 | digital_1 = new_digital_1; |
| philfontaine | 11:fe7d53172c00 | 99 | } |
| philfontaine | 11:fe7d53172c00 | 100 | } |
| philfontaine | 11:fe7d53172c00 | 101 | if (new_digital_2 != digital_2) { |
| philfontaine | 11:fe7d53172c00 | 102 | new_digital_2 = en_2.read(); |
| philfontaine | 11:fe7d53172c00 | 103 | if (new_digital_2 != digital_2) { |
| philfontaine | 11:fe7d53172c00 | 104 | // génération éventuelle d'un événement |
| philfontaine | 11:fe7d53172c00 | 105 | mail_t* mail = mail_box.alloc(); |
| philfontaine | 11:fe7d53172c00 | 106 | mail->isAnalog = false; |
| philfontaine | 11:fe7d53172c00 | 107 | mail->digital = new_digital_2; |
| philfontaine | 11:fe7d53172c00 | 108 | mail->rtc_time = rtc_time; |
| saudraisn | 12:9573a2e293e2 | 109 | mail->id = 2; |
| philfontaine | 11:fe7d53172c00 | 110 | mail_box.put(mail); |
| philfontaine | 11:fe7d53172c00 | 111 | digital_2 = new_digital_2; |
| philfontaine | 11:fe7d53172c00 | 112 | } |
| philfontaine | 11:fe7d53172c00 | 113 | } |
| philfontaine | 11:fe7d53172c00 | 114 | } |
| saudraisn | 12:9573a2e293e2 | 115 | // pc.printf("time : %d\n", timer.read_ms()); |
| saudraisn | 12:9573a2e293e2 | 116 | //// Thread::wait(100 - timer.read_ms()); |
| saudraisn | 12:9573a2e293e2 | 117 | // Thread::wait(1000); |
| saudraisn | 12:9573a2e293e2 | 118 | // pc.printf("time2 : %d\n", timer.read_ms()); |
| saudraisn | 12:9573a2e293e2 | 119 | // timer.stop(); |
| saudraisn | 12:9573a2e293e2 | 120 | // timer.reset(); |
| emilmont | 1:491820ee784d | 121 | } |
| emilmont | 1:491820ee784d | 122 | } |
| philfontaine | 11:fe7d53172c00 | 123 | |
| saudraisn | 12:9573a2e293e2 | 124 | void collection(void const *args) |
| saudraisn | 12:9573a2e293e2 | 125 | { |
| philfontaine | 11:fe7d53172c00 | 126 | while (true) { |
| saudraisn | 12:9573a2e293e2 | 127 | // attente et lecture d'un événement |
| saudraisn | 12:9573a2e293e2 | 128 | // écriture de l'événement en sortie (port série) |
| saudraisn | 12:9573a2e293e2 | 129 | pc.printf("je fonctionne"); |
| philfontaine | 11:fe7d53172c00 | 130 | osEvent evt = mail_box.get(); |
| philfontaine | 11:fe7d53172c00 | 131 | if (evt.status == osEventMail) { |
| philfontaine | 11:fe7d53172c00 | 132 | mail_t *mail = (mail_t*)evt.value.p; |
| philfontaine | 11:fe7d53172c00 | 133 | if(mail->isAnalog) { |
| saudraisn | 12:9573a2e293e2 | 134 | pc.printf("Analog : (%d,%X) %s", mail->id, mail->analog, ctime(&mail->rtc_time)); |
| philfontaine | 11:fe7d53172c00 | 135 | } else { |
| saudraisn | 12:9573a2e293e2 | 136 | pc.printf("Digital : (%d,%X) %s", mail->id, mail->digital, ctime(&mail->rtc_time)); |
| philfontaine | 11:fe7d53172c00 | 137 | } |
| philfontaine | 11:fe7d53172c00 | 138 | mail_box.free(mail); |
| philfontaine | 11:fe7d53172c00 | 139 | } |
| philfontaine | 11:fe7d53172c00 | 140 | } |
| philfontaine | 11:fe7d53172c00 | 141 | } |
| saudraisn | 12:9573a2e293e2 | 142 | //Ticker ticker1, ticker2; |
| philfontaine | 11:fe7d53172c00 | 143 | |
| saudraisn | 12:9573a2e293e2 | 144 | //Thread *t1, *t2; |
| saudraisn | 12:9573a2e293e2 | 145 | |
| saudraisn | 12:9573a2e293e2 | 146 | //void wakeupThread1(void const *args) |
| saudraisn | 12:9573a2e293e2 | 147 | //{ |
| saudraisn | 12:9573a2e293e2 | 148 | // pc.printf("12345"); |
| saudraisn | 12:9573a2e293e2 | 149 | // t1->signal_set(0x1); |
| saudraisn | 12:9573a2e293e2 | 150 | //} |
| saudraisn | 12:9573a2e293e2 | 151 | // |
| saudraisn | 12:9573a2e293e2 | 152 | //void wakeupThread2(void const *args) |
| saudraisn | 12:9573a2e293e2 | 153 | //{ |
| saudraisn | 12:9573a2e293e2 | 154 | // pc.printf("67890"); |
| saudraisn | 12:9573a2e293e2 | 155 | // t2->signal_set(0x1); |
| saudraisn | 12:9573a2e293e2 | 156 | //} |
| saudraisn | 12:9573a2e293e2 | 157 | |
| saudraisn | 12:9573a2e293e2 | 158 | int main() |
| saudraisn | 12:9573a2e293e2 | 159 | { |
| saudraisn | 12:9573a2e293e2 | 160 | // pc.printf("gogogo"); |
| philfontaine | 11:fe7d53172c00 | 161 | // initialisation du RTC |
| saudraisn | 12:9573a2e293e2 | 162 | set_time(1485732233); |
| saudraisn | 12:9573a2e293e2 | 163 | |
| philfontaine | 11:fe7d53172c00 | 164 | // démarrage des tâches |
| saudraisn | 12:9573a2e293e2 | 165 | // Thread analogThread(lecture_analog); |
| saudraisn | 12:9573a2e293e2 | 166 | // Thread digitalThread(lecture_num); |
| saudraisn | 12:9573a2e293e2 | 167 | Thread collectionThread(collection); |
| saudraisn | 12:9573a2e293e2 | 168 | |
| saudraisn | 12:9573a2e293e2 | 169 | // pc.printf("pogogogogo"); |
| saudraisn | 12:9573a2e293e2 | 170 | // *t1 = analogThread; |
| saudraisn | 12:9573a2e293e2 | 171 | // *t2 = digitalThread; |
| saudraisn | 12:9573a2e293e2 | 172 | |
| saudraisn | 12:9573a2e293e2 | 173 | //ticker1.attach(&wakeupThread1, 0.250); |
| saudraisn | 12:9573a2e293e2 | 174 | // ticker2.attach(&wakeupThread2, 0.100); |
| saudraisn | 12:9573a2e293e2 | 175 | |
| saudraisn | 12:9573a2e293e2 | 176 | RtosTimer led_1_timer(lecture_analog, osTimerPeriodic); |
| saudraisn | 12:9573a2e293e2 | 177 | RtosTimer led_2_timer(lecture_num, osTimerPeriodic); |
| saudraisn | 12:9573a2e293e2 | 178 | |
| saudraisn | 12:9573a2e293e2 | 179 | led_1_timer.start(250); |
| saudraisn | 12:9573a2e293e2 | 180 | |
| saudraisn | 12:9573a2e293e2 | 181 | Thread::wait(10); |
| saudraisn | 12:9573a2e293e2 | 182 | led_2_timer.start(100); |
| saudraisn | 12:9573a2e293e2 | 183 | |
| saudraisn | 12:9573a2e293e2 | 184 | // pc.printf("fuck"); |
| saudraisn | 12:9573a2e293e2 | 185 | while(1); |
| saudraisn | 12:9573a2e293e2 | 186 | } |
