Working with multiple threads. Control of the speed of LED flashing using keyboard.
Fork of rtos_basic by
main.cpp@12:2952e72a8be9, 2018-01-18 (annotated)
- Committer:
- natgovor
- Date:
- Thu Jan 18 16:58:02 2018 +0000
- Revision:
- 12:2952e72a8be9
- Parent:
- 10:dc33cd3f4eb9
Implemented control of LED flashing speed by user keyboard.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
emilmont | 1:491820ee784d | 2 | |
emilmont | 1:491820ee784d | 3 | DigitalOut led1(LED1); |
natgovor | 12:2952e72a8be9 | 4 | |
natgovor | 12:2952e72a8be9 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
natgovor | 12:2952e72a8be9 | 6 | |
geky | 7:8d9919175929 | 7 | Thread thread; |
natgovor | 12:2952e72a8be9 | 8 | |
natgovor | 12:2952e72a8be9 | 9 | void led1_thread(int *speed) { |
emilmont | 1:491820ee784d | 10 | while (true) { |
natgovor | 12:2952e72a8be9 | 11 | led1 = !led1; |
natgovor | 12:2952e72a8be9 | 12 | Thread::wait(*speed); |
emilmont | 1:491820ee784d | 13 | } |
emilmont | 1:491820ee784d | 14 | } |
emilmont | 1:491820ee784d | 15 | |
emilmont | 1:491820ee784d | 16 | int main() { |
natgovor | 12:2952e72a8be9 | 17 | int speed = 200; |
natgovor | 12:2952e72a8be9 | 18 | thread.start(callback(led1_thread, &speed)); |
emilmont | 1:491820ee784d | 19 | |
natgovor | 12:2952e72a8be9 | 20 | char userInput; |
natgovor | 12:2952e72a8be9 | 21 | pc.printf("Enter a character>"); |
natgovor | 12:2952e72a8be9 | 22 | while(1) { |
natgovor | 12:2952e72a8be9 | 23 | userInput = pc.getc(); |
natgovor | 12:2952e72a8be9 | 24 | switch(userInput) { |
natgovor | 12:2952e72a8be9 | 25 | case 'f': |
natgovor | 12:2952e72a8be9 | 26 | pc.printf("faster"); |
natgovor | 12:2952e72a8be9 | 27 | speed = speed - 50; |
natgovor | 12:2952e72a8be9 | 28 | break; |
natgovor | 12:2952e72a8be9 | 29 | case 's': |
natgovor | 12:2952e72a8be9 | 30 | pc.printf("slower"); |
natgovor | 12:2952e72a8be9 | 31 | speed = speed + 50; |
natgovor | 12:2952e72a8be9 | 32 | break; |
natgovor | 12:2952e72a8be9 | 33 | } |
natgovor | 12:2952e72a8be9 | 34 | |
natgovor | 12:2952e72a8be9 | 35 | thread.start(callback(led1_thread, &speed)); |
natgovor | 12:2952e72a8be9 | 36 | Thread::wait(500); |
emilmont | 1:491820ee784d | 37 | } |
emilmont | 1:491820ee784d | 38 | } |