Working with multiple threads. Control of the speed of LED flashing using keyboard.
Fork of rtos_basic by
Revision 12:2952e72a8be9, committed 2018-01-18
- Comitter:
- natgovor
- Date:
- Thu Jan 18 16:58:02 2018 +0000
- Parent:
- 11:012b1294c1c4
- Commit message:
- Implemented control of LED flashing speed by user keyboard.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 012b1294c1c4 -r 2952e72a8be9 main.cpp --- a/main.cpp Fri Jun 23 15:11:31 2017 -0500 +++ b/main.cpp Thu Jan 18 16:58:02 2018 +0000 @@ -1,21 +1,38 @@ #include "mbed.h" DigitalOut led1(LED1); -DigitalOut led2(LED2); + +Serial pc(USBTX, USBRX); // tx, rx + Thread thread; - -void led2_thread() { + +void led1_thread(int *speed) { while (true) { - led2 = !led2; - wait(1); + led1 = !led1; + Thread::wait(*speed); } } int main() { - thread.start(led2_thread); + int speed = 200; + thread.start(callback(led1_thread, &speed)); - while (true) { - led1 = !led1; - wait(0.5); + char userInput; + pc.printf("Enter a character>"); + while(1) { + userInput = pc.getc(); + switch(userInput) { + case 'f': + pc.printf("faster"); + speed = speed - 50; + break; + case 's': + pc.printf("slower"); + speed = speed + 50; + break; + } + + thread.start(callback(led1_thread, &speed)); + Thread::wait(500); } }