A demonstration that uses multiple signals from the main thread

This example shows how to use multiple signals from the main thread in mbed-os.

The sample sets up two Tickers to generate periodic interrupts. Each ticker sets a signal during an interrupt.

osSignalWait(signal, timeout) is used to wait for an event. Set signal to 0 to exit on any signal or set signal to a mask value to only exit if all of the signals are set.

Files at this revision

API Documentation at this revision

Comitter:
argusbrown
Date:
Thu Sep 08 21:33:41 2016 +0000
Parent:
0:677614c5f014
Commit message:
Removed one of the osSignalWait sections to make the demo clearer.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Sep 08 21:17:19 2016 +0000
+++ b/main.cpp	Thu Sep 08 21:33:41 2016 +0000
@@ -26,14 +26,20 @@
     mainThread = Thread::gettid();
     
     tick1.attach(&interrupt1, 1.0); // setup ticker to call interrupt1 every 1.0 seconds
-    tick2.attach(&interrupt2, 2.0); // setup ticker to call interrupt2 every 1.0 seconds
+    tick2.attach(&interrupt2, 2.4); // setup ticker to call interrupt2 every 1.0 seconds
 
     while (true) {
         // If the event mask is set to 0, then any signal will exit the wait.
         // If nothing happens for 10 seonds, timeout
         // Internaly the code is doing something like this:
-        //   if (signal1 | signal2) then exit wait
+        //   if (signal1 | signal2) then exit wait     
         osEvent v = osSignalWait(0, 10000);
+        
+        // Use this form to wait for both events
+        // Internaly the code is doing something like this:
+        //   if ((signal1 | signal2) & 0x3 == 0x3) then exit wait
+        //osEvent v = osSignalWait(0x3, 10000);
+        
         pc.printf("Wait1 exit. Status: 0x%x\n",(int) v.status);
 
         if (v.status == osEventSignal) {
@@ -51,30 +57,7 @@
                 pc.printf("Event1 0x2\n");
             }
 
-        }
-        
-        // Use this form to wait for both events
-        // Internaly the code is doing something like this:
-        //   if ((signal1 | signal2) & 0x3 == 0x3) then exit wait
-        v = osSignalWait(0x3, 10000);
-        pc.printf("Wait2 exit. Status: 0x%x\n",(int) v.status);
-
-        if (v.status == osEventSignal) {
-            pc.printf("Signals Event2 Caught: 0x%x\n",(int) v.value.signals);
-
-            // Tick1 timeout
-            if (v.value.signals & 0x1) {
-                led1 = !led1;
-                pc.printf("Event2 0x1\n");
-            }
-            
-             // Tick2 timeout
-            if (v.value.signals & 0x2) {
-                led2 = !led2;
-                pc.printf("Event2 0x2\n");
-            }
-
-        }
-        
+        }       
+       
     }
 }
\ No newline at end of file