Nicholas Outram / Mbed OS Task613Solution-mbeds54

Fork of Task613Solution-mbeds54 by Stage-1 Students SoCEM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define RED_OFF    1
00004 #define YELLOW_OFF 2
00005 #define GREEN_OFF  4
00006 #define ALL_OFF    7
00007 
00008 //Function declarations
00009 void Function1();
00010 void Function2();
00011 void Function3();
00012 void Function4();
00013 
00014 //I/O
00015 DigitalOut onBoardLED(LED1);
00016 DigitalOut redLED(D7);
00017 DigitalOut yellowLED(D6);
00018 DigitalOut greenLED(D5);
00019 
00020 DigitalIn  onBoardSwitch(USER_BUTTON);
00021 DigitalIn  SW1(D4);
00022 DigitalIn  SW2(D3);
00023 
00024 Thread t1;
00025 Thread t2;
00026 Thread t3;
00027 Thread t4;
00028 
00029 //Thread ID
00030 osThreadId idMain;
00031 osThreadId id1;
00032 osThreadId id2;
00033 osThreadId id3;
00034 osThreadId id4;
00035 
00036 void Function1()
00037 {
00038     while (true) {
00039         redLED = !redLED;
00040         if (redLED == 0) {
00041             t2.signal_set(RED_OFF);
00042         }
00043         Thread::wait(1000);
00044     }
00045 }
00046 
00047 void Function2()
00048 {
00049     while (true) {
00050         Thread::signal_wait(RED_OFF);
00051         yellowLED = !yellowLED;
00052         if (yellowLED == 0) {
00053             t3.signal_set(YELLOW_OFF);
00054         }
00055     }
00056 }
00057 
00058 //Green Flashing
00059 void Function3()
00060 {
00061     while (true) {
00062         Thread::signal_wait(YELLOW_OFF);
00063         greenLED = !greenLED;
00064         if (greenLED == 0) {
00065             t4.signal_set(GREEN_OFF);
00066         }       
00067     }
00068 }
00069 
00070 //This function waits for signals from all other threads
00071 void Function4()
00072 {
00073     while (true) {
00074         Thread::signal_wait(GREEN_OFF);
00075         //Signal main thread       
00076         osSignalSet(idMain, ALL_OFF);              
00077     }
00078 }
00079 
00080 //Main thread
00081 int main() {
00082     redLED    = 0;
00083     yellowLED = 0;
00084     greenLED  = 0;
00085     
00086     //Main thread ID
00087     idMain = osThreadGetId();   //CMSIS RTOS call
00088     
00089     // run threads
00090     t4.start(Function4);
00091     t1.start(Function1);           
00092     t2.start(Function2);    
00093     t3.start(Function3);     
00094 
00095     
00096     //Thread ID
00097     id1 = t1.gettid();
00098     id2 = t2.gettid();
00099     id3 = t3.gettid();
00100     id4 = t4.gettid();
00101     
00102     while(1) {
00103         //Wait for the ALL_ON signal
00104         osSignalWait(ALL_OFF,osWaitForever);
00105         printf("ALL OFF\n");
00106     }
00107 }