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.
main.cpp@1:907cf114c40c, 2016-09-08 (annotated)
- Committer:
- argusbrown
- Date:
- Thu Sep 08 21:33:41 2016 +0000
- Revision:
- 1:907cf114c40c
- Parent:
- 0:677614c5f014
Removed one of the osSignalWait sections to make the demo clearer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
argusbrown | 0:677614c5f014 | 1 | #include "mbed.h" |
argusbrown | 0:677614c5f014 | 2 | |
argusbrown | 0:677614c5f014 | 3 | Serial pc(USBTX, USBRX); |
argusbrown | 0:677614c5f014 | 4 | Ticker tick1; |
argusbrown | 0:677614c5f014 | 5 | Ticker tick2; |
argusbrown | 0:677614c5f014 | 6 | DigitalOut led1(LED_RED,true); |
argusbrown | 0:677614c5f014 | 7 | DigitalOut led2(LED_GREEN,true); |
argusbrown | 0:677614c5f014 | 8 | osThreadId mainThread; |
argusbrown | 0:677614c5f014 | 9 | |
argusbrown | 0:677614c5f014 | 10 | void interrupt1() |
argusbrown | 0:677614c5f014 | 11 | { |
argusbrown | 0:677614c5f014 | 12 | osSignalSet(mainThread, 0x1); |
argusbrown | 0:677614c5f014 | 13 | } |
argusbrown | 0:677614c5f014 | 14 | |
argusbrown | 0:677614c5f014 | 15 | void interrupt2() |
argusbrown | 0:677614c5f014 | 16 | { |
argusbrown | 0:677614c5f014 | 17 | osSignalSet(mainThread, 0x2); |
argusbrown | 0:677614c5f014 | 18 | } |
argusbrown | 0:677614c5f014 | 19 | |
argusbrown | 0:677614c5f014 | 20 | int main() |
argusbrown | 0:677614c5f014 | 21 | { |
argusbrown | 0:677614c5f014 | 22 | pc.baud(115200); |
argusbrown | 0:677614c5f014 | 23 | pc.printf("Init\n\r"); |
argusbrown | 0:677614c5f014 | 24 | |
argusbrown | 0:677614c5f014 | 25 | // Get the id of the main thread |
argusbrown | 0:677614c5f014 | 26 | mainThread = Thread::gettid(); |
argusbrown | 0:677614c5f014 | 27 | |
argusbrown | 0:677614c5f014 | 28 | tick1.attach(&interrupt1, 1.0); // setup ticker to call interrupt1 every 1.0 seconds |
argusbrown | 1:907cf114c40c | 29 | tick2.attach(&interrupt2, 2.4); // setup ticker to call interrupt2 every 1.0 seconds |
argusbrown | 0:677614c5f014 | 30 | |
argusbrown | 0:677614c5f014 | 31 | while (true) { |
argusbrown | 0:677614c5f014 | 32 | // If the event mask is set to 0, then any signal will exit the wait. |
argusbrown | 0:677614c5f014 | 33 | // If nothing happens for 10 seonds, timeout |
argusbrown | 0:677614c5f014 | 34 | // Internaly the code is doing something like this: |
argusbrown | 1:907cf114c40c | 35 | // if (signal1 | signal2) then exit wait |
argusbrown | 0:677614c5f014 | 36 | osEvent v = osSignalWait(0, 10000); |
argusbrown | 1:907cf114c40c | 37 | |
argusbrown | 1:907cf114c40c | 38 | // Use this form to wait for both events |
argusbrown | 1:907cf114c40c | 39 | // Internaly the code is doing something like this: |
argusbrown | 1:907cf114c40c | 40 | // if ((signal1 | signal2) & 0x3 == 0x3) then exit wait |
argusbrown | 1:907cf114c40c | 41 | //osEvent v = osSignalWait(0x3, 10000); |
argusbrown | 1:907cf114c40c | 42 | |
argusbrown | 0:677614c5f014 | 43 | pc.printf("Wait1 exit. Status: 0x%x\n",(int) v.status); |
argusbrown | 0:677614c5f014 | 44 | |
argusbrown | 0:677614c5f014 | 45 | if (v.status == osEventSignal) { |
argusbrown | 0:677614c5f014 | 46 | pc.printf("Signals Event1 Caught: 0x%x\n",(int) v.value.signals); |
argusbrown | 0:677614c5f014 | 47 | |
argusbrown | 0:677614c5f014 | 48 | // Tick1 timeout |
argusbrown | 0:677614c5f014 | 49 | if (v.value.signals & 0x1) { |
argusbrown | 0:677614c5f014 | 50 | led1 = !led1; |
argusbrown | 0:677614c5f014 | 51 | pc.printf("Event1 0x1\n"); |
argusbrown | 0:677614c5f014 | 52 | } |
argusbrown | 0:677614c5f014 | 53 | |
argusbrown | 0:677614c5f014 | 54 | // Tick2 timeout |
argusbrown | 0:677614c5f014 | 55 | if (v.value.signals & 0x2) { |
argusbrown | 0:677614c5f014 | 56 | led2 = !led2; |
argusbrown | 0:677614c5f014 | 57 | pc.printf("Event1 0x2\n"); |
argusbrown | 0:677614c5f014 | 58 | } |
argusbrown | 0:677614c5f014 | 59 | |
argusbrown | 1:907cf114c40c | 60 | } |
argusbrown | 1:907cf114c40c | 61 | |
argusbrown | 0:677614c5f014 | 62 | } |
argusbrown | 0:677614c5f014 | 63 | } |