Thread example

main.cpp

Committer:
noutram
Date:
2017-10-31
Revision:
2:47c9c24b49fc
Parent:
1:20f8dc1354d0
Child:
3:4090b465745c

File content as of revision 2:47c9c24b49fc:

#include "mbed.h"


//Function declarations
void Function1();
void Function2();

//I/O
DigitalOut onBoardLED(LED1);
DigitalOut redLED(PE_15);
DigitalOut yellowLED(PB_10);
DigitalOut greenLED(PB_11);

DigitalIn  onBoardSwitch(USER_BUTTON);
DigitalIn  SW1(PE_12);
DigitalIn  SW2(PE_14);

//Create thread objects
Thread t1, t2;

//Thread
void Function1()
{
    while (true) {
        redLED = !redLED;
        Thread::wait(2000);
    }
}

//Thread
void Function2()
{
    while (true) {
        yellowLED = !yellowLED;
        Thread::wait(1000);
    }
}

//Main thread
int main() {
    //Initial state
    redLED    = 0;
    yellowLED = 0;
    greenLED  = 0;
    
    //Create and run threads (C function pointers)
    t1.start(Function1);           
    t2.start(Function2);    
    
    //Main thread loop
    while(1) {
        //Thread::wait(osWaitForever);
        Thread::wait(5000);
        printf("Main is Awake\n");  //Should not happen when osWaitForever is used
    }
}