Nicholas Outram / Mbed OS Task613-mbedos54

Fork of Task613-mbedos54 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_ON 1
00004 #define YELLOW_ON 2
00005 #define GREEN_ON 4
00006 #define ALL_ON 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 == 1) {
00041             t4.signal_set(RED_ON);
00042         }
00043         
00044         Thread::wait(5100);
00045     }
00046 }
00047 
00048 void Function2()
00049 {
00050     while (true) {
00051         yellowLED = !yellowLED;
00052         if (yellowLED == 1) {
00053             t4.signal_set(YELLOW_ON);
00054         }
00055         Thread::wait(1900);
00056     }
00057 }
00058 
00059 //Green Flashing
00060 void Function3()
00061 {
00062     while (true) {
00063         greenLED = !greenLED;
00064         if (greenLED == 1) {
00065             t4.signal_set(GREEN_ON);
00066         }       
00067         Thread::wait(700);
00068     }
00069 }
00070 
00071 //This function waits for signals from all other threads
00072 void Function4()
00073 {
00074     while (true) {
00075         
00076         Thread::signal_wait(RED_ON);
00077         printf("Red\n");
00078         Thread::signal_wait(YELLOW_ON);
00079         printf("Yellow\n"); 
00080         Thread::signal_wait(GREEN_ON);
00081         printf("Green\n");
00082         //Signal main thread       
00083         osSignalSet(idMain, ALL_ON);              
00084     }
00085 }
00086 
00087 //Main thread
00088 int main() {
00089     redLED    = 1;
00090     yellowLED = 0;
00091     greenLED  = 0;
00092     
00093     //Main thread ID
00094     idMain = osThreadGetId();   //CMSIS RTOS call
00095     
00096     //Run threads
00097     t4.start(Function4);
00098     t1.start(Function1);           
00099     t2.start(Function2);    
00100     t3.start(Function3);     
00101 
00102     
00103     //Thread ID
00104     id1 = t1.gettid();
00105     id2 = t2.gettid();
00106     id3 = t3.gettid();
00107     id4 = t4.gettid();
00108     
00109     while(1) {
00110         //Wait for the ALL_ON signal
00111         osSignalWait(ALL_ON,osWaitForever);
00112         printf("ALL\n");
00113     }
00114 }