Stage-1 Students SoCEM / Mbed 2 deprecated Task611Solution

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 //Function declarations
00005 void Function1(void const *args);
00006 void Function2(void const *args);
00007 void Function3(void const *args);
00008 
00009 //I/O
00010 DigitalOut onBoardLED(LED1);
00011 DigitalOut redLED(D7);
00012 DigitalOut yellowLED(D6);
00013 DigitalOut greenLED(D5);
00014 
00015 DigitalIn  onBoardSwitch(USER_BUTTON);
00016 DigitalIn  SW1(D4);
00017 DigitalIn  SW2(D3);
00018 
00019 Thread* t1;
00020 Thread* t2;
00021 Thread* t3;
00022 
00023 void Function1(void const *args)
00024 {
00025     while (true) {
00026         redLED = !redLED;
00027         Thread::wait(2000);
00028     }
00029 }
00030 
00031 void Function2(void const *args)
00032 {
00033     while (true) {
00034         yellowLED = !yellowLED;
00035         Thread::wait(1000);
00036     }
00037 }
00038 
00039 //Green Flashing
00040 void Function3(void const *args)
00041 {
00042     while (true) {
00043         greenLED = !greenLED;
00044         Thread::wait(500);
00045     }
00046 }
00047 
00048 
00049 //Main thread
00050 int main() {
00051     redLED    = 0;
00052     yellowLED = 0;
00053     greenLED  = 0;
00054     
00055     //Create and run threads
00056     t1 = new Thread(Function1);           
00057     t2 = new Thread(Function2);    
00058     t3 = new Thread(Function3);     //Dynamically allocated
00059     
00060     while(1) {
00061         //Thread::wait(osWaitForever);
00062         Thread::wait(5000);
00063         printf("Awake\n");  //Should not happen
00064     }
00065 }