Felipe Neves
/
TEN_mbedos_events
TEN MBED OS Course events lab
main.cpp@0:4303da3c75bf, 2017-01-29 (annotated)
- Committer:
- uLipe
- Date:
- Sun Jan 29 20:40:45 2017 +0000
- Revision:
- 0:4303da3c75bf
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uLipe | 0:4303da3c75bf | 1 | /** |
uLipe | 0:4303da3c75bf | 2 | * @brief first event control program with MBED OS |
uLipe | 0:4303da3c75bf | 3 | */ |
uLipe | 0:4303da3c75bf | 4 | #include "mbed.h" |
uLipe | 0:4303da3c75bf | 5 | #include "rtos.h" |
uLipe | 0:4303da3c75bf | 6 | #include "mbed_events.h" |
uLipe | 0:4303da3c75bf | 7 | |
uLipe | 0:4303da3c75bf | 8 | /* declares threads for this demo: */ |
uLipe | 0:4303da3c75bf | 9 | const size_t a_stk_size = 1024; |
uLipe | 0:4303da3c75bf | 10 | uint8_t a_stk[a_stk_size]; |
uLipe | 0:4303da3c75bf | 11 | Thread a_thread(osPriorityNormal, a_stk_size, &a_stk[0]); |
uLipe | 0:4303da3c75bf | 12 | |
uLipe | 0:4303da3c75bf | 13 | /* thread b is used as event loop thread*/ |
uLipe | 0:4303da3c75bf | 14 | Thread b_thread; |
uLipe | 0:4303da3c75bf | 15 | |
uLipe | 0:4303da3c75bf | 16 | /* queue used to handle events */ |
uLipe | 0:4303da3c75bf | 17 | EventQueue ev_queue(32 * EVENTS_EVENT_SIZE); |
uLipe | 0:4303da3c75bf | 18 | |
uLipe | 0:4303da3c75bf | 19 | /* reserve the debbuger uart to shell interface */ |
uLipe | 0:4303da3c75bf | 20 | Serial pc_serial(USBTX,USBRX); |
uLipe | 0:4303da3c75bf | 21 | |
uLipe | 0:4303da3c75bf | 22 | /* allocate HW for button with interrupt */ |
uLipe | 0:4303da3c75bf | 23 | InterruptIn sw(SW2); |
uLipe | 0:4303da3c75bf | 24 | |
uLipe | 0:4303da3c75bf | 25 | |
uLipe | 0:4303da3c75bf | 26 | /** |
uLipe | 0:4303da3c75bf | 27 | * @brief thread a function |
uLipe | 0:4303da3c75bf | 28 | */ |
uLipe | 0:4303da3c75bf | 29 | static void thread_a(void) |
uLipe | 0:4303da3c75bf | 30 | { |
uLipe | 0:4303da3c75bf | 31 | int pressed = 0; |
uLipe | 0:4303da3c75bf | 32 | for(;;) { |
uLipe | 0:4303da3c75bf | 33 | /* halt thread until a signal is received */ |
uLipe | 0:4303da3c75bf | 34 | a_thread.signal_wait(1, osWaitForever); |
uLipe | 0:4303da3c75bf | 35 | pressed++; |
uLipe | 0:4303da3c75bf | 36 | pc_serial.printf("## button press woke up this thread, noof presses: %d !##\n\r", pressed); |
uLipe | 0:4303da3c75bf | 37 | pc_serial.printf("## now this thread will wait for a new press !##\n\r"); |
uLipe | 0:4303da3c75bf | 38 | } |
uLipe | 0:4303da3c75bf | 39 | } |
uLipe | 0:4303da3c75bf | 40 | |
uLipe | 0:4303da3c75bf | 41 | |
uLipe | 0:4303da3c75bf | 42 | /** |
uLipe | 0:4303da3c75bf | 43 | * @brief event callback called from thread context |
uLipe | 0:4303da3c75bf | 44 | */ |
uLipe | 0:4303da3c75bf | 45 | static void button_callback(void) |
uLipe | 0:4303da3c75bf | 46 | { |
uLipe | 0:4303da3c75bf | 47 | /* button callback is used to assert a signal, |
uLipe | 0:4303da3c75bf | 48 | * this is the advantage of event loop, signals are not allowed |
uLipe | 0:4303da3c75bf | 49 | * to use in ISR, but this callback is executed from thread |
uLipe | 0:4303da3c75bf | 50 | * context |
uLipe | 0:4303da3c75bf | 51 | */ |
uLipe | 0:4303da3c75bf | 52 | a_thread.signal_set(1); |
uLipe | 0:4303da3c75bf | 53 | } |
uLipe | 0:4303da3c75bf | 54 | |
uLipe | 0:4303da3c75bf | 55 | |
uLipe | 0:4303da3c75bf | 56 | /** |
uLipe | 0:4303da3c75bf | 57 | * @brief main application loop |
uLipe | 0:4303da3c75bf | 58 | */ |
uLipe | 0:4303da3c75bf | 59 | int main(void) |
uLipe | 0:4303da3c75bf | 60 | { |
uLipe | 0:4303da3c75bf | 61 | pc_serial.baud(115200); |
uLipe | 0:4303da3c75bf | 62 | a_thread.start(thread_a); |
uLipe | 0:4303da3c75bf | 63 | b_thread.start(callback(&ev_queue, &EventQueue::dispatch_forever)); |
uLipe | 0:4303da3c75bf | 64 | sw.rise(ev_queue.event(button_callback)); |
uLipe | 0:4303da3c75bf | 65 | return 0; |
uLipe | 0:4303da3c75bf | 66 | } |