mbed_example
/
events_ex_1
Additional events example
main.cpp@2:6ae734681f16, 2017-06-23 (annotated)
- Committer:
- sarahmarshy
- Date:
- Fri Jun 23 13:59:05 2017 -0500
- Revision:
- 2:6ae734681f16
- Parent:
- 1:3ee1c217f3cb
- Child:
- 3:4f5870c9c712
"Update mbed-os"
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 | |
mab5449 | 0:aea2e03f5625 | 9 | void rise_handler(void) { |
mab5449 | 0:aea2e03f5625 | 10 | // Toggle LED |
mab5449 | 0:aea2e03f5625 | 11 | led1 = !led1; |
mab5449 | 0:aea2e03f5625 | 12 | } |
mab5449 | 0:aea2e03f5625 | 13 | |
mab5449 | 0:aea2e03f5625 | 14 | void fall_handler(void) { |
mab5449 | 0:aea2e03f5625 | 15 | printf("fall_handler in context %p\r\n", Thread::gettid()); |
mab5449 | 0:aea2e03f5625 | 16 | // Toggle LED |
mab5449 | 0:aea2e03f5625 | 17 | led1 = !led1; |
mab5449 | 0:aea2e03f5625 | 18 | } |
mab5449 | 0:aea2e03f5625 | 19 | |
mab5449 | 0:aea2e03f5625 | 20 | int main() { |
mab5449 | 0:aea2e03f5625 | 21 | // Start the event queue |
mab5449 | 0:aea2e03f5625 | 22 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
mab5449 | 0:aea2e03f5625 | 23 | printf("Starting in context %p\r\n", Thread::gettid()); |
mab5449 | 0:aea2e03f5625 | 24 | // The 'rise' handler will execute in IRQ context |
mab5449 | 0:aea2e03f5625 | 25 | sw.rise(rise_handler); |
mab5449 | 0:aea2e03f5625 | 26 | // The 'fall' handler will execute in the context of thread 't' |
mab5449 | 0:aea2e03f5625 | 27 | sw.fall(queue.event(fall_handler)); |
mab5449 | 0:aea2e03f5625 | 28 | } |
mab5449 | 0:aea2e03f5625 | 29 |