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.

main.cpp

Committer:
argusbrown
Date:
2016-09-08
Revision:
0:677614c5f014
Child:
1:907cf114c40c

File content as of revision 0:677614c5f014:

#include "mbed.h"

Serial pc(USBTX, USBRX);
Ticker tick1;
Ticker tick2;
DigitalOut led1(LED_RED,true);
DigitalOut led2(LED_GREEN,true);
osThreadId mainThread;

void interrupt1()
{
    osSignalSet(mainThread, 0x1);
}

void interrupt2()
{
    osSignalSet(mainThread, 0x2);
}

int main()
{
    pc.baud(115200);
    pc.printf("Init\n\r");
    
    // Get the id of the main thread
    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

    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
        osEvent v = osSignalWait(0, 10000);
        pc.printf("Wait1 exit. Status: 0x%x\n",(int) v.status);

        if (v.status == osEventSignal) {
            pc.printf("Signals Event1 Caught: 0x%x\n",(int) v.value.signals);

            // Tick1 timeout
            if (v.value.signals & 0x1) {
                led1 = !led1;
                pc.printf("Event1 0x1\n");
            }
            
             // Tick2 timeout
            if (v.value.signals & 0x2) {
                led2 = !led2;
                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");
            }

        }
        
    }
}