A modified version of the hello world program to run using RTOS threads. Also, it cycles through seven colors on the RGB LED while it's doing its thing.
Fork of frdm_helloworld by
Revision 4:f52f79d2f703, committed 2016-08-23
- Comitter:
- bjepson
- Date:
- Tue Aug 23 01:31:09 2016 +0000
- Parent:
- 3:fd99fd0c7e60
- Commit message:
- All three functions (led cycle, two buttons) are in threads now.
Changed in this revision
diff -r fd99fd0c7e60 -r f52f79d2f703 main.cpp --- a/main.cpp Tue Aug 23 01:00:23 2016 +0000 +++ b/main.cpp Tue Aug 23 01:31:09 2016 +0000 @@ -4,56 +4,79 @@ DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); + DigitalIn sw2(SW2); DigitalIn sw3(SW3); + Serial pc(USBTX, USBRX); -void check_sw2_thread(void const *args) +void check_sw2_thread() { while(true) { if (sw2 == 0) { pc.printf("SW2 button pressed. \n"); + while(sw2 == 0) { // debounce + Thread::wait(.3); + } + } + Thread::wait(.3); + } +} + +void check_sw3_thread() +{ + while(true) { + if (sw3 == 0) { + pc.printf("SW3 button pressed. \n"); + pc.printf("5 characters will be echoed. Start typing. \n"); + for (uint32_t i = 0; i < 5; i++) { + pc.putc(pc.getc()); + } + pc.putc(13); /* CR */ + pc.putc(10); /* LF */ } Thread::wait(.3); } } -void check_sw3(void) +// Cycle through seven colors +void strobe_led_thread() { - if (sw3 == 0) { - pc.printf("SW3 button pressed. \n"); - //led_green = 0; - //led_red = 1; - pc.printf("5 characters will be echoed. Start typing. \n"); - for (uint32_t i = 0; i < 5; i++) { - pc.putc(pc.getc()); + while(true) { + for (int i = 0; i < 8; i++) { + led_red = !(i & 0x01); + led_green = !(i & 0x02); + led_blue = !(i & 0x04); + wait(0.5); } - pc.putc(13); /* CR */ - pc.putc(10); /* LF */ } } int main() { - Thread thread(check_sw2_thread); - pc.baud(115200); pc.printf("Hello World from FRDM-K64F board.\n"); - int i = 1; - while (true) { - check_sw3(); + Thread sw2thread; + osStatus err = sw2thread.start(&check_sw2_thread); + if (err) { + pc.printf("%d", err); + } + + Thread sw3thread; + err = sw3thread.start(&check_sw3_thread); + if (err) { + pc.printf("%d", err); + } - i += 1; - if (i > 7) { - i = 1; - } - led_red = !(i & 0x01); - led_green = !(i & 0x02); - led_blue = !(i & 0x04); - //pc.printf("i = %d: R = %d; G = %d; B = %d\n", i, led_red.read(), led_green.read(), led_blue.read()); + Thread strobethread; + err = strobethread.start(&strobe_led_thread); + if (err) { + pc.printf("%d", err); + } - wait(0.1); - - } + // wait for threads to terminate + sw2thread.join(); + sw3thread.join(); + strobethread.join(); }
diff -r fd99fd0c7e60 -r f52f79d2f703 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Aug 23 01:31:09 2016 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#d2202045ac8f2fc63836a5b7131c9b56ac58f06a
diff -r fd99fd0c7e60 -r f52f79d2f703 mbed.bld --- a/mbed.bld Tue Aug 23 01:00:23 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6473597d706e \ No newline at end of file