#include "mbed.h" //Include file for all mbed libraries #include "rtos.h" //Include the mbed rtos functions osThreadId mainThreadID; //Declare thread ID for use with signals - main thread osThreadId thead1_ThreadID; //Declare thread ID for use with signals - green led thread osThreadId thread2_ThreadID; //Declare thread ID for use with signals - orange led thread osThreadId thread3_ThreadID; //Declare thread ID for use with signals - red led thread osThreadId thread4_ThreadID; //Declare thread ID for use with signals - blue led thread //Define symbols for signal numbers as signals are numbered in 2 to the power off (binary) #define s1 1 //Use s1 symbol for signal number 1 #define s2 2 //Use s2 symbol for signal number 2 #define s3 4 //Use s3 symbol for signal number 4 #define s4 8 //Use s4 symbol for signal number 8 Mutex print_mutex; //Mutex to protect printf // thread1 void thread1(void const *args) { //Define a thread thead1_ThreadID = osThreadGetId(); //Read green led thread ID needed for thread termination and creation while (true) { //Thread always in infinite loop print_mutex.lock(); //Wait for printf mutex to become available printf("Thread 1 is running\n\n"); //Write message to indicate the thread is running print_mutex.unlock(); //Release printf mutex osSignalSet(mainThreadID, s1); //Send a signal to main thread using thread ID and signal 1 Thread::wait(3000); //Thread put into wait state for time period in mS } } // thread2 void thread2(void const *args) { //Define a thread thread2_ThreadID = osThreadGetId(); //thread ID needed for thread termination and creation while (true) { //Thread always in infinite loop print_mutex.lock(); //Wait for printf mutex to become available printf("Thread 2 is running\n\n"); //Write message to indicate the thread is running print_mutex.unlock(); //Release printf mutex osSignalSet(mainThreadID, s2); //Send a signal to main thread using thread ID and signal 2 Thread::wait(3000); //Thread put into wait state for time period in mS } } // thread3 void thread3(void const *args) { //Define a thread thread3_ThreadID = osThreadGetId(); //thread ID needed for thread termination and creation while (true) { //Thread always in infinite loop print_mutex.lock(); //Wait for printf mutex to become available printf("Thread 3 is running\n\n"); //Write message to indicate the thread is running print_mutex.unlock(); //Release printf mutex osSignalSet(mainThreadID, s3); //Send a signal to main thread using thread ID and signal 3 Thread::wait(3000); //Thread put into wait state for time period in mS } } // thread4 void thread4(void const *args) { //Define a thread thread4_ThreadID = osThreadGetId(); //thread ID needed for thread termination and creation while (true) { //Thread always in infinite loop print_mutex.lock(); //Wait for printf mutex to become available printf("Thread 4 is running\n\n"); //Write message to indicate the thread is running print_mutex.unlock(); //Release printf mutex osSignalSet(mainThreadID, s4); //Send a signal to main thread using thread ID and signal 4 osDelay(3000); //Thread put into wait state for time period in mS } } int main() { //Main function is also a thread - NB mainThreadID = osThreadGetId(); //Thread must have its thread ID to identify the signal sent to it Thread thread_1(thread1, NULL, osPriorityNormal, 2048); //Create and start thread with normal priority and stated stack size. Thread thread_2(thread2); //Create and start thread with default normal priority and default stack size - 2k Thread thread_3(thread3); //Create and start the red led thread Thread thread_4(thread4); //Create and start the blue led thread while (true) { //Infinite loop osEvent evt; //Create variable to store the event status evt = osSignalWait(0, 0); //Check if any signal have been received without any wait period - see RTOS handbook! //Use a switch/case to evaluate the thread signals stored in variable evt.value.signals //Example - if its contents is 7 (binary 111) means that signals 1, 2 and 3 is set switch(evt.value.signals) { //The current status of all the signals sent to this thread is stored in this variable case 1 : //Only signal 1 is set {print_mutex.lock(); //Wait for printf mutex to become available printf("Signal 1 received other 3 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 2 : //Only signal 2 is set {print_mutex.lock(); //Wait for printf mutex to become available printf("Signal 2 received other 3 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 3 : //Signals 1 and 2 is set {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 2 received other 2 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 4 : //Only signal 3 is set {print_mutex.lock(); //Wait for printf mutex to become available printf("Signal 3 received other 3 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 5 : //Signal 1 and 3 is set {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 3 received other 2 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 6 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 2 + 3 received other 2 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 7 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 2 + 3 received other 1 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 8 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signal 4 received other 3 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 9 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 4 received other 2 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 10: {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 2 + 4 received other 2 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 11 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 2 + 4 received other 1 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 12 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 3 + 4 received other 2 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 13 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 3 +4 received other 1 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 14 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 2 + 3 + 4 received other 1 is off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; case 15 : {print_mutex.lock(); //Wait for printf mutex to become available printf("Signals 1 + 2 + 3 + 4 received\n\n" ); print_mutex.unlock(); } //Release printf mutex break; default : {print_mutex.lock(); //Wait for printf mutex to become available printf("All signals are off\n\n" ); print_mutex.unlock(); } //Release printf mutex break; } Thread::wait(1000); } }