Thread example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 //Function declarations
00005 void Function1();
00006 void Function2();
00007 
00008 //I/O
00009 DigitalOut onBoardLED(LED1);
00010 DigitalOut redLED(PE_15);
00011 DigitalOut yellowLED(PB_10);
00012 DigitalOut greenLED(PB_11);
00013 
00014 DigitalIn  onBoardSwitch(USER_BUTTON);
00015 DigitalIn  SW1(PE_12);
00016 DigitalIn  SW2(PE_14);
00017 
00018 //Create thread objects
00019 Thread t1, t2;
00020 
00021 //Thread
00022 void Function1()
00023 {
00024     while (true) {
00025         redLED = !redLED;
00026         ThisThread::sleep_for(2000);
00027     }
00028 }
00029 
00030 //Thread
00031 void Function2()
00032 {
00033     while (true) {
00034         yellowLED = !yellowLED;
00035         ThisThread::sleep_for(1000);
00036     }
00037 }
00038 
00039 //Main thread
00040 int main() {
00041     //Initial state
00042     redLED    = 0;
00043     yellowLED = 0;
00044     greenLED  = 0;
00045     
00046     //Create and run threads (C function pointers)
00047     t1.start(Function1);           
00048     t2.start(Function2);    
00049     
00050     //Main thread loop
00051     while(1) {
00052         //ThisThread::sleep_for(osWaitForever); //There is a task to uncomment this
00053         ThisThread::sleep_for(5000);
00054         printf("Main is Awake\n");  //Should not happen when osWaitForever is used
00055     }
00056 }