Updated for stage-3

Fork of Task613Solution-mbeds54 by Nicholas Outram

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 typedef struct  {
00015     float temp;
00016     float press;
00017     float humid;
00018 } Measurement;
00019 
00020 Measurement buffer[120];
00021 
00022 
00023 //I/O
00024 DigitalOut onBoardLED(LED1);
00025 DigitalOut redLED(PE_15);
00026 DigitalOut yellowLED(PB_10);
00027 DigitalOut greenLED(PB_11);
00028 
00029 DigitalIn  onBoardSwitch(USER_BUTTON);
00030 DigitalIn  SW1(PE_12);
00031 DigitalIn  SW2(PE_14);
00032 
00033 Thread t1;
00034 Thread t2;
00035 Thread t3;
00036 Thread t4;
00037 
00038 //Thread ID
00039 osThreadId idMain;
00040 osThreadId id1;
00041 osThreadId id2;
00042 osThreadId id3;
00043 osThreadId id4;
00044 
00045 void Function1()
00046 {
00047     while (true) {
00048         redLED = !redLED;
00049         if (redLED == 0) {
00050             t2.flags_set(RED_OFF);
00051         }
00052         Thread::wait(1000);
00053     }
00054 }
00055 
00056 void Function2()
00057 {
00058     while (true) {
00059         ThisThread::flags_wait_all(RED_OFF);
00060         yellowLED = !yellowLED;
00061         if (yellowLED == 0) {
00062             t3.flags_set(YELLOW_OFF);
00063         }
00064     }
00065 }
00066 
00067 //Green Flashing
00068 void Function3()
00069 {
00070     while (true) {
00071         ThisThread::flags_wait_all(YELLOW_OFF);
00072         greenLED = !greenLED;
00073         if (greenLED == 0) {
00074             t4.flags_set(GREEN_OFF);
00075         }       
00076     }
00077 }
00078 
00079 //This function waits for signals from all other threads
00080 void Function4()
00081 {
00082     while (true) {
00083         ThisThread::flags_wait_all(GREEN_OFF);
00084         //Signal main thread       
00085         osSignalSet(idMain, ALL_OFF);              
00086     }
00087 }
00088 
00089 //Main thread
00090 int main() {
00091     redLED    = 0;
00092     yellowLED = 0;
00093     greenLED  = 0;
00094     
00095     //Main thread ID
00096     idMain = osThreadGetId();   //CMSIS RTOS call
00097     
00098     // run threads
00099     t4.start(Function4);
00100     t1.start(Function1);           
00101     t2.start(Function2);    
00102     t3.start(Function3);     
00103 
00104     
00105     //Thread ID
00106 //    id1 = t1.gettid();
00107 //    id2 = t2.gettid();
00108 //    id3 = t3.gettid();
00109 //    id4 = t4.gettid();
00110     
00111     while(1) {
00112         //Wait for the ALL_ON signal
00113         osSignalWait(ALL_OFF,osWaitForever);
00114         printf("ALL OFF\n");
00115     }
00116 }