Learn how to use embedded RTOS (Beta version RTOS)

Dependencies:   mbed-rtos mbed

Revision:
2:daaf6b6ad568
Parent:
1:4d067c712d03
diff -r 4d067c712d03 -r daaf6b6ad568 main.cpp
--- a/main.cpp	Thu Sep 03 22:11:03 2015 +0000
+++ b/main.cpp	Thu Sep 03 22:35:15 2015 +0000
@@ -23,20 +23,10 @@
 // Mutex shared by threads that want's to use the serial port
 Mutex stdio_mutex;
 
-void led_thread(void const *args)
+void toogleLed(void const *args)
 {
-    while (true) {
-        // Toogle led
-        led2 = !led2;
-
-        // To avoid using the serial port at the same time by two threads use the mutex
-        stdio_mutex.lock();
-        serialIO.printf("Toogle led2 from thread1 !\r\n");
-        stdio_mutex.unlock();
-
-        // Put thread in wait state for 500ms
-        Thread::wait(500);
-    }
+    // Toogle led
+    led2 = !led2;
 }
 
 void check_button(void const *args)
@@ -51,7 +41,7 @@
         stdio_mutex.unlock();
 
         // Put thread in wait state for 100ms
-        Thread::wait(100);
+        Thread::wait(300);
     }
 }
 
@@ -64,7 +54,7 @@
         message->voltage = (i * 0.1) * 33;
         message->counter = i;
         queue.put(message);
-        Thread::wait(100);
+        Thread::wait(2000);
     }
 }
 
@@ -75,7 +65,7 @@
         osEvent evt = queue.get();
         if (evt.status == osEventMessage) {
             message_t *message = (message_t*)evt.value.p;
-            
+
             // Grab mutex to print data
             stdio_mutex.lock();
             serialIO.printf("\nVoltage: %.2f V\n\r"   , message->voltage);
@@ -88,19 +78,20 @@
 }
 
 int main()
-{    
+{
     // Configure the serial speed.
     serialIO.baud(115200);
     serialIO.printf("Learning Microcontrollers with mbed !\r\n");
 
-    // Start a thread to togle the led
-    Thread thread_1(led_thread);
+    // Start a thread to togle the led    
     Thread thread_2(check_button);
     Thread thread_3(data_producer);
     Thread thread_4(data_consumer);
+    
+    // Periodically call a function
+    RtosTimer timerTogLed(toogleLed, osTimerPeriodic);
+    timerTogLed.start(1000);
 
     // Lock forever here...
-    while(1) {
-
-    }
+    Thread::wait(osWaitForever);
 }