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
Diff: main.cpp
- Revision:
- 12:9573a2e293e2
- Parent:
- 11:fe7d53172c00
- Child:
- 13:bd6c24a90b4a
diff -r fe7d53172c00 -r 9573a2e293e2 main.cpp
--- a/main.cpp Sun Jan 29 23:06:46 2017 +0000
+++ b/main.cpp Mon Jan 30 21:20:31 2017 +0000
@@ -12,21 +12,27 @@
AnalogBuffer ab_1;
AnalogBuffer ab_2;
+Serial pc(USBTX, USBRX); // tx, rx
+
typedef struct {
bool isAnalog;
bool digital;
unsigned short analog;
+ unsigned short id;
time_t rtc_time;
} mail_t;
Mail<mail_t, 16> mail_box;
-void lecture_analog(void const *args) {
- Timer timer;
+void lecture_analog(void const *args)
+{
+// Timer timer;
unsigned short old_average_1 = 0;
unsigned short old_average_2 = 0;
while (true) {
- timer.start(); // synchronisation sur la période d'échantillonnage
+// pc.printf("Thread anal attend");
+// Thread::signal_wait(0x1);
+ // timer.start(); // synchronisation sur la période d'échantillonnage
time_t rtc_time = time(NULL); // lecture de l'étampe temporelle
ab_1.put(ea_1.read_u16()); // lecture des échantillons analogiques
ab_2.put(ea_2.read_u16());
@@ -34,35 +40,48 @@
unsigned short new_average_2 = ab_2.average();
int diff_1 = abs(new_average_1 - old_average_1);
int diff_2 = abs(new_average_2 - old_average_2);
+
if (diff_1 > SEUIL_ANALOG) {
mail_t* mail = mail_box.alloc();
mail->isAnalog = true;
mail->analog = new_average_1;
mail->rtc_time = rtc_time;
+ mail->id = 1;
mail_box.put(mail);
+ old_average_1 = new_average_1;
}
if (diff_2 > SEUIL_ANALOG) {
mail_t* mail = mail_box.alloc();
mail->isAnalog = true;
- mail->analog = new_average_1;
+ mail->analog = new_average_2;
mail->rtc_time = rtc_time;
+ mail->id = 2;
mail_box.put(mail);
+ old_average_2 = new_average_2;
}
- old_average_1 = new_average_1;
- old_average_2 = new_average_2;
- Thread::wait(250 - timer.read_ms());
- timer.stop();
- timer.reset(); // Necessaire??
+ //Thread::wait(250 - timer.read_ms());
+// timer.stop();
+// timer.reset();
}
}
-void lecture_num(void const *args) {
- Timer timer;
+void lecture_num(void const *args)
+{
+// Timer timer;
+ // timer1.start();
+// unsigned int old;
+// unsigned int newT;
+
while (true) {
- timer.start(); // synchronisation sur la période d'échantillonnage
+// pc.printf("Thread num attend");
+// Thread::signal_wait(0x1);
+// timer.start(); // synchronisation sur la période d'échantillonnage
+ //old = newT;
+// newT = timer1.read_ms();
+
time_t rtc_time = time(NULL); // lecture de l'étampe temporelle
bool new_digital_1 = en_1.read(); // lecture des échantillons numériques
bool new_digital_2 = en_2.read(); // lecture des échantillons numériques
-
+
if ((new_digital_1 != digital_1) || (new_digital_2 != digital_2)) {
// prise en charge du phénomène de rebond
Thread::wait(50);
@@ -74,6 +93,7 @@
mail->isAnalog = false;
mail->digital = new_digital_1;
mail->rtc_time = rtc_time;
+ mail->id = 1;
mail_box.put(mail);
digital_1 = new_digital_1;
}
@@ -86,37 +106,81 @@
mail->isAnalog = false;
mail->digital = new_digital_2;
mail->rtc_time = rtc_time;
+ mail->id = 2;
mail_box.put(mail);
digital_2 = new_digital_2;
}
}
}
- Thread::wait(100 - timer.read_ms());
- timer.stop();
- timer.reset(); // Necessaire??
+ // pc.printf("time : %d\n", timer.read_ms());
+//// Thread::wait(100 - timer.read_ms());
+// Thread::wait(1000);
+// pc.printf("time2 : %d\n", timer.read_ms());
+// timer.stop();
+// timer.reset();
}
}
-void collection(void const *args) {
+void collection(void const *args)
+{
while (true) {
- // attente et lecture d'un événement
- // écriture de l'événement en sortie (port série)
+ // attente et lecture d'un événement
+ // écriture de l'événement en sortie (port série)
+ pc.printf("je fonctionne");
osEvent evt = mail_box.get();
if (evt.status == osEventMail) {
mail_t *mail = (mail_t*)evt.value.p;
if(mail->isAnalog) {
- pc.printf("%s Analog: %X", ctime(&mail->rtc_time), mail->analog);
+ pc.printf("Analog : (%d,%X) %s", mail->id, mail->analog, ctime(&mail->rtc_time));
} else {
- pc.printf("%s Digital: %d", ctime(&mail->rtc_time), mail->digital);
+ pc.printf("Digital : (%d,%X) %s", mail->id, mail->digital, ctime(&mail->rtc_time));
}
mail_box.free(mail);
}
}
}
+//Ticker ticker1, ticker2;
-int main() {
+//Thread *t1, *t2;
+
+//void wakeupThread1(void const *args)
+//{
+// pc.printf("12345");
+// t1->signal_set(0x1);
+//}
+//
+//void wakeupThread2(void const *args)
+//{
+// pc.printf("67890");
+// t2->signal_set(0x1);
+//}
+
+int main()
+{
+// pc.printf("gogogo");
// initialisation du RTC
+ set_time(1485732233);
+
// démarrage des tâches
- while(1) {
- }
-}
\ No newline at end of file
+ // Thread analogThread(lecture_analog);
+// Thread digitalThread(lecture_num);
+ Thread collectionThread(collection);
+
+// pc.printf("pogogogogo");
+ // *t1 = analogThread;
+// *t2 = digitalThread;
+
+ //ticker1.attach(&wakeupThread1, 0.250);
+// ticker2.attach(&wakeupThread2, 0.100);
+
+ RtosTimer led_1_timer(lecture_analog, osTimerPeriodic);
+ RtosTimer led_2_timer(lecture_num, osTimerPeriodic);
+
+ led_1_timer.start(250);
+
+ Thread::wait(10);
+ led_2_timer.start(100);
+
+// pc.printf("fuck");
+ while(1);
+}
