In this example the first thread (the main () function) sends signals to the second thread at regular intervals. The second thread waits until an event signal does not arrive. Each time the signal arrives, the program toggles the status of LED1. Note, that the return value of the Thread :: signal_wait () function is examined and is printed out as well. Just for fun thread2: accepts any signal, and thead1 provides all possible signals from 1 to 32768.

Dependencies:   mbed mbed-rtos

Files at this revision

API Documentation at this revision

Comitter:
cspista
Date:
Thu Mar 17 12:43:28 2022 +0000
Commit message:
Final version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 17 12:43:28 2022 +0000
@@ -0,0 +1,40 @@
+#include "mbed.h"
+#include "rtos.h"
+
+DigitalOut led(LED1);
+
+void led_thread(void const *argument) {
+  while (true) {
+    // Signal flags that are reported as event are automatically cleared.
+    osEvent evt = Thread::signal_wait(0);            //Wait for any signal
+    switch(evt.status) {
+      case osOK:
+        printf("osOK\n");                            //no error or event occurred
+        break;
+      case osEventSignal:
+        printf("osEventSignal = %#05x\n",evt.value.signals); //signal event occurred
+        break;              
+      case osEventTimeout:
+        printf("osEventTimeout\n");                 //timeout occurred
+        break;               
+      case osErrorValue:
+        printf("osErrorValue\n");                   //value of a parameter is out of range
+        break;               
+      default:
+        printf("Unknown error flag: %#08x\n",(uint32_t)evt.status);
+        break;
+    };
+    led = !led;
+  }
+}
+
+int main (void) {
+  int32_t signal_mask = 0x1;
+  Thread thread2(led_thread);
+  while (true) {
+    Thread::wait(1000);
+    thread2.signal_set(signal_mask);
+    signal_mask <<=1;
+    if(signal_mask > 0x8000) signal_mask=0x1;
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Mar 17 12:43:28 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed-rtos/#5713cbbdb706
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 17 12:43:28 2022 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file