Brandon Boesch
/
events_ex_1
Test
Fork of events_ex_1 by
main.cpp@2:716e1b448b48, 2017-05-24 (annotated)
- Committer:
- brandonboesch
- Date:
- Wed May 24 14:49:41 2017 +0000
- Revision:
- 2:716e1b448b48
- Parent:
- 1:3ee1c217f3cb
Now has a safer background thread that does not call printf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:aea2e03f5625 | 1 | #include "mbed.h" |
mab5449 | 0:aea2e03f5625 | 2 | #include "mbed_events.h" |
mab5449 | 0:aea2e03f5625 | 3 | |
mab5449 | 0:aea2e03f5625 | 4 | DigitalOut led1(LED1); |
mab5449 | 0:aea2e03f5625 | 5 | InterruptIn sw(SW2); |
mab5449 | 0:aea2e03f5625 | 6 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
mab5449 | 0:aea2e03f5625 | 7 | Thread t; |
mab5449 | 0:aea2e03f5625 | 8 | |
brandonboesch | 2:716e1b448b48 | 9 | void rise_handler_user_context(void) { |
brandonboesch | 2:716e1b448b48 | 10 | printf("rise_handler_user_context in context %p\r\n", Thread::gettid()); |
brandonboesch | 2:716e1b448b48 | 11 | } |
brandonboesch | 2:716e1b448b48 | 12 | |
mab5449 | 0:aea2e03f5625 | 13 | void rise_handler(void) { |
brandonboesch | 2:716e1b448b48 | 14 | // Execute the time critical part first |
mab5449 | 0:aea2e03f5625 | 15 | led1 = !led1; |
brandonboesch | 2:716e1b448b48 | 16 | // The rest can execute later in user context (and can contain code that's not interrupt safe). |
brandonboesch | 2:716e1b448b48 | 17 | // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue. |
brandonboesch | 2:716e1b448b48 | 18 | queue.call(rise_handler_user_context); |
mab5449 | 0:aea2e03f5625 | 19 | } |
mab5449 | 0:aea2e03f5625 | 20 | |
mab5449 | 0:aea2e03f5625 | 21 | void fall_handler(void) { |
mab5449 | 0:aea2e03f5625 | 22 | printf("fall_handler in context %p\r\n", Thread::gettid()); |
mab5449 | 0:aea2e03f5625 | 23 | // Toggle LED |
mab5449 | 0:aea2e03f5625 | 24 | led1 = !led1; |
mab5449 | 0:aea2e03f5625 | 25 | } |
mab5449 | 0:aea2e03f5625 | 26 | |
mab5449 | 0:aea2e03f5625 | 27 | int main() { |
mab5449 | 0:aea2e03f5625 | 28 | // Start the event queue |
mab5449 | 0:aea2e03f5625 | 29 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
mab5449 | 0:aea2e03f5625 | 30 | printf("Starting in context %p\r\n", Thread::gettid()); |
mab5449 | 0:aea2e03f5625 | 31 | // The 'rise' handler will execute in IRQ context |
mab5449 | 0:aea2e03f5625 | 32 | sw.rise(rise_handler); |
mab5449 | 0:aea2e03f5625 | 33 | // The 'fall' handler will execute in the context of thread 't' |
mab5449 | 0:aea2e03f5625 | 34 | sw.fall(queue.event(fall_handler)); |
mab5449 | 0:aea2e03f5625 | 35 | } |