updated for stage-3
Fork of Task613-mbedos54 by
Embed:
(wiki syntax)
Show/hide line numbers
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(PE_15); 00017 DigitalOut yellowLED(PB_10); 00018 DigitalOut greenLED(PB_11); 00019 00020 DigitalIn onBoardSwitch(USER_BUTTON); 00021 DigitalIn SW1(PE_12); 00022 DigitalIn SW2(PE_14); 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.flags_set(RED_ON); 00042 } 00043 00044 ThisThread::sleep_for(5100); 00045 } 00046 } 00047 00048 void Function2() 00049 { 00050 while (true) { 00051 yellowLED = !yellowLED; 00052 if (yellowLED == 1) { 00053 t4.flags_set(YELLOW_ON); 00054 } 00055 ThisThread::sleep_for(1900); 00056 } 00057 } 00058 00059 //Green Flashing 00060 void Function3() 00061 { 00062 while (true) { 00063 greenLED = !greenLED; 00064 if (greenLED == 1) { 00065 t4.flags_set(GREEN_ON); 00066 } 00067 ThisThread::sleep_for(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 ThisThread::flags_wait_all(RED_ON); 00078 printf("Red\n"); 00079 ThisThread::flags_wait_all(YELLOW_ON); 00080 printf("Yellow\n"); 00081 ThisThread::flags_wait_all(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 //Run threads 00098 t4.start(Function4); 00099 t1.start(Function1); 00100 t2.start(Function2); 00101 t3.start(Function3); 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 }
Generated on Sat Jul 16 2022 21:35:25 by
1.7.2
