Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 #define RED_OFF 1 00005 #define YELLOW_OFF 2 00006 #define GREEN_OFF 4 00007 #define ALL_OFF 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 == 0) { 00042 t2->signal_set(RED_OFF); 00043 } 00044 Thread::wait(1000); 00045 } 00046 } 00047 00048 void Function2(void const *args) 00049 { 00050 while (true) { 00051 Thread::signal_wait(RED_OFF); 00052 yellowLED = !yellowLED; 00053 if (yellowLED == 0) { 00054 t3->signal_set(YELLOW_OFF); 00055 } 00056 } 00057 } 00058 00059 //Green Flashing 00060 void Function3(void const *args) 00061 { 00062 while (true) { 00063 Thread::signal_wait(YELLOW_OFF); 00064 greenLED = !greenLED; 00065 if (greenLED == 0) { 00066 t4->signal_set(GREEN_OFF); 00067 } 00068 } 00069 } 00070 00071 //This function waits for signals from all other threads 00072 void Function4(void const *args) 00073 { 00074 while (true) { 00075 Thread::signal_wait(GREEN_OFF); 00076 //Signal main thread 00077 osSignalSet(idMain, ALL_OFF); 00078 } 00079 } 00080 00081 //Main thread 00082 int main() { 00083 redLED = 0; 00084 yellowLED = 0; 00085 greenLED = 0; 00086 00087 //Main thread ID 00088 idMain = osThreadGetId(); //CMSIS RTOS call 00089 00090 //Create and run threads 00091 t4 = new Thread(Function4); 00092 t1 = new Thread(Function1); 00093 t2 = new Thread(Function2); 00094 t3 = new Thread(Function3); //Dynamically allocated 00095 00096 00097 //Thread ID 00098 id1 = t1->gettid(); 00099 id2 = t2->gettid(); 00100 id3 = t3->gettid(); 00101 id4 = t4->gettid(); 00102 00103 while(1) { 00104 //Wait for the ALL_ON signal 00105 osSignalWait(ALL_OFF,osWaitForever); 00106 printf("ALL OFF\n"); 00107 } 00108 }
Generated on Fri Jul 15 2022 05:34:19 by
1.7.2