Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "test_env.h"
00003 #include "rtos.h"
00004 
00005 #if defined(MBED_RTOS_SINGLE_THREAD)
00006   #error [NOT_SUPPORTED] test not supported
00007 #endif
00008 
00009 DigitalOut LEDs[4] = {
00010     DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
00011 };
00012 
00013 void print_char(char c = '*')
00014 {
00015     printf("%c", c);
00016     fflush(stdout);
00017 }
00018 
00019 void blink(void const *n) {
00020     static int counter = 0;
00021     const int led_id = int(n);
00022     LEDs[led_id] = !LEDs[led_id];
00023     if (++counter == 75) {
00024         print_char();
00025         counter = 0;
00026     }
00027 }
00028 
00029 int main(void) {
00030     MBED_HOSTTEST_TIMEOUT(15);
00031     MBED_HOSTTEST_SELECT(wait_us_auto);
00032     MBED_HOSTTEST_DESCRIPTION(Timer);
00033     MBED_HOSTTEST_START("RTOS_7");
00034 
00035     RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
00036     RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
00037     RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
00038     RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
00039 
00040     led_1_timer.start(200);
00041     led_2_timer.start(100);
00042     led_3_timer.start(50);
00043     led_4_timer.start(25);
00044 
00045     Thread::wait(osWaitForever);
00046 }