Stage-1 Students SoCEM / Mbed 2 deprecated Task613

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