RTOS timer usage

Dependencies:   mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
icserny
Date:
Thu Mar 31 14:15:35 2016 +0000
Parent:
0:95dae97a932f
Commit message:
First version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 95dae97a932f -r 10da8ca3d1cd main.cpp
--- a/main.cpp	Tue Feb 23 13:34:02 2016 +0000
+++ b/main.cpp	Thu Mar 31 14:15:35 2016 +0000
@@ -1,56 +1,32 @@
-/** 10_rtos_queue
+/** 12_rtos_timer
+ * LED blinking by four RTOS timer - a Handbook demo
+ * Link: https://developer.mbed.org/users/mbed_official/code/rtos_timer/docs/9dd7d49be2c3/main_8cpp_source.html
  *
- * Simple example to use a Queue object to send integer data
- * from thread1 to thread2. Note that Queue's put() method is
- * intended to send a pointer, therefore typecasting
- * should be appliedin in order to avoid compiler error messages.
+ * Hardware requirements:
+ * - FRDM-KL25Z board
+ * - 4 LEDs (with current limiting resistors) connected to the Arduino compatible D4,D5,D6,D7 pins
  */
 
+
 #include "mbed.h"
 #include "rtos.h"
 
-AnalogIn potmeter(A0);
-PwmOut myled(LED_BLUE);
-
-typedef uint32_t message_t;
-Queue<message_t, 16> queue;
-
-void led_thread(void const *argument)
-{
-    myled.period_us(16384);                                       //Set period to 16.384 ms
-    myled.pulsewidth_us(8192);                                    //Initialize to 50% duty cycle
-    while (true) {
-        osEvent evt = queue.get();                                //Wait for a message
-        switch(evt.status) {
-            case osOK:
-                printf("osOK\n");                                 //no error, no message arrived
-                break;
-            case osEventMessage:
-                printf("osEventMessage = %#05x\n",evt.value.v);  //message arrived
-                //Note that evt.value is a union.
-                //Read as value.v to get a data, or value.p to get a pointer...
-                myled.pulsewidth_us((int)(evt.value.v>>2));      //roughly 0..16 ms
-                break;
-            case osEventTimeout:
-                printf("osEventTimeout\n");                      //timeout occurred
-                break;
-            case osErrorParameter:
-                printf("osErrorParameter\n");
-                break;                                           //invalid parameter or is out of range.
-            default:
-                printf("Unknown error flag: %#08x\n",(uint32_t)evt.status);
-                break;
-        };
-    }
+DigitalOut LEDs[4] = {DigitalOut(D4), DigitalOut(D5), DigitalOut(D6), DigitalOut(D7)};
+ 
+void blink(void const *n) {
+    LEDs[(int)n] = !LEDs[(int)n];
 }
-
-int main (void)
-{
-    Thread thread2(led_thread);
-
-    while (true) {
-        Thread::wait(1000);
-        uint16_t raw = potmeter.read_u16();                     // read raw 16-bit data
-        queue.put((message_t*)raw);
-    }
-}
+ 
+int main(void) {            
+    RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
+    RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
+    RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
+    RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
+    
+    led_1_timer.start(2000);
+    led_2_timer.start(1000);
+    led_3_timer.start(500);
+    led_4_timer.start(250);
+    
+    Thread::wait(osWaitForever);
+}
\ No newline at end of file