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.
main.cpp@0:bd08296227ad, 2022-03-17 (annotated)
- Committer:
- cspista
- Date:
- Thu Mar 17 12:43:28 2022 +0000
- Revision:
- 0:bd08296227ad
Final version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cspista | 0:bd08296227ad | 1 | #include "mbed.h" |
| cspista | 0:bd08296227ad | 2 | #include "rtos.h" |
| cspista | 0:bd08296227ad | 3 | |
| cspista | 0:bd08296227ad | 4 | DigitalOut led(LED1); |
| cspista | 0:bd08296227ad | 5 | |
| cspista | 0:bd08296227ad | 6 | void led_thread(void const *argument) { |
| cspista | 0:bd08296227ad | 7 | while (true) { |
| cspista | 0:bd08296227ad | 8 | // Signal flags that are reported as event are automatically cleared. |
| cspista | 0:bd08296227ad | 9 | osEvent evt = Thread::signal_wait(0); //Wait for any signal |
| cspista | 0:bd08296227ad | 10 | switch(evt.status) { |
| cspista | 0:bd08296227ad | 11 | case osOK: |
| cspista | 0:bd08296227ad | 12 | printf("osOK\n"); //no error or event occurred |
| cspista | 0:bd08296227ad | 13 | break; |
| cspista | 0:bd08296227ad | 14 | case osEventSignal: |
| cspista | 0:bd08296227ad | 15 | printf("osEventSignal = %#05x\n",evt.value.signals); //signal event occurred |
| cspista | 0:bd08296227ad | 16 | break; |
| cspista | 0:bd08296227ad | 17 | case osEventTimeout: |
| cspista | 0:bd08296227ad | 18 | printf("osEventTimeout\n"); //timeout occurred |
| cspista | 0:bd08296227ad | 19 | break; |
| cspista | 0:bd08296227ad | 20 | case osErrorValue: |
| cspista | 0:bd08296227ad | 21 | printf("osErrorValue\n"); //value of a parameter is out of range |
| cspista | 0:bd08296227ad | 22 | break; |
| cspista | 0:bd08296227ad | 23 | default: |
| cspista | 0:bd08296227ad | 24 | printf("Unknown error flag: %#08x\n",(uint32_t)evt.status); |
| cspista | 0:bd08296227ad | 25 | break; |
| cspista | 0:bd08296227ad | 26 | }; |
| cspista | 0:bd08296227ad | 27 | led = !led; |
| cspista | 0:bd08296227ad | 28 | } |
| cspista | 0:bd08296227ad | 29 | } |
| cspista | 0:bd08296227ad | 30 | |
| cspista | 0:bd08296227ad | 31 | int main (void) { |
| cspista | 0:bd08296227ad | 32 | int32_t signal_mask = 0x1; |
| cspista | 0:bd08296227ad | 33 | Thread thread2(led_thread); |
| cspista | 0:bd08296227ad | 34 | while (true) { |
| cspista | 0:bd08296227ad | 35 | Thread::wait(1000); |
| cspista | 0:bd08296227ad | 36 | thread2.signal_set(signal_mask); |
| cspista | 0:bd08296227ad | 37 | signal_mask <<=1; |
| cspista | 0:bd08296227ad | 38 | if(signal_mask > 0x8000) signal_mask=0x1; |
| cspista | 0:bd08296227ad | 39 | } |
| cspista | 0:bd08296227ad | 40 | } |