Working with multiple threads. Control of the speed of LED flashing using keyboard.

Fork of rtos_basic by mbed_example

main.cpp

Committer:
natgovor
Date:
2018-01-18
Revision:
12:2952e72a8be9
Parent:
10:dc33cd3f4eb9

File content as of revision 12:2952e72a8be9:

#include "mbed.h"
 
DigitalOut led1(LED1);

Serial pc(USBTX, USBRX); // tx, rx

Thread thread;

void led1_thread(int *speed) {
    while (true) {
        led1 = !led1;
        Thread::wait(*speed);
    }
}
 
int main() {
    int speed = 200;
    thread.start(callback(led1_thread, &speed));
    
    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);
    }
}