Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:6ab5b8697bfb, 2018-11-12 (annotated)
- Committer:
- lmottola
- Date:
- Mon Nov 12 12:10:30 2018 +0000
- Revision:
- 0:6ab5b8697bfb
- Child:
- 1:cc715a7c24a5
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lmottola | 0:6ab5b8697bfb | 1 | #include "mbed.h" |
lmottola | 0:6ab5b8697bfb | 2 | |
lmottola | 0:6ab5b8697bfb | 3 | DigitalOut led1(LED1); |
lmottola | 0:6ab5b8697bfb | 4 | InterruptIn button(USER_BUTTON); |
lmottola | 0:6ab5b8697bfb | 5 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
lmottola | 0:6ab5b8697bfb | 6 | Thread t; |
lmottola | 0:6ab5b8697bfb | 7 | |
lmottola | 0:6ab5b8697bfb | 8 | void rise_handler_thread_context(void) { |
lmottola | 0:6ab5b8697bfb | 9 | printf("rise_handler_thread_context in context %p\r\n", Thread::gettid()); |
lmottola | 0:6ab5b8697bfb | 10 | } |
lmottola | 0:6ab5b8697bfb | 11 | |
lmottola | 0:6ab5b8697bfb | 12 | void rise_handler_iterrupt_context(void) { |
lmottola | 0:6ab5b8697bfb | 13 | // Execute the time critical part first |
lmottola | 0:6ab5b8697bfb | 14 | led1 = !led1; |
lmottola | 0:6ab5b8697bfb | 15 | // The rest can execute later in user context (and can contain code that's not interrupt safe) |
lmottola | 0:6ab5b8697bfb | 16 | // We use the 'queue.call' function to add an event (the call to 'rise_handler_user_context') to the queue |
lmottola | 0:6ab5b8697bfb | 17 | queue.call(rise_handler_thread_context); |
lmottola | 0:6ab5b8697bfb | 18 | } |
lmottola | 0:6ab5b8697bfb | 19 | |
lmottola | 0:6ab5b8697bfb | 20 | void fall_handler(void) { |
lmottola | 0:6ab5b8697bfb | 21 | printf("fall_handler in context %p\r\n", Thread::gettid()); |
lmottola | 0:6ab5b8697bfb | 22 | // Toggle LED |
lmottola | 0:6ab5b8697bfb | 23 | led1 = !led1; |
lmottola | 0:6ab5b8697bfb | 24 | } |
lmottola | 0:6ab5b8697bfb | 25 | |
lmottola | 0:6ab5b8697bfb | 26 | int main() { |
lmottola | 0:6ab5b8697bfb | 27 | // Start the event queue |
lmottola | 0:6ab5b8697bfb | 28 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
lmottola | 0:6ab5b8697bfb | 29 | printf("Starting in context %p\r\n", Thread::gettid()); |
lmottola | 0:6ab5b8697bfb | 30 | // The 'rise' handler will execute in IRQ context |
lmottola | 0:6ab5b8697bfb | 31 | button.rise(rise_handler_iterrupt_context); |
lmottola | 0:6ab5b8697bfb | 32 | // The 'fall' handler will execute in the context of thread 't' |
lmottola | 0:6ab5b8697bfb | 33 | button.fall(queue.event(fall_handler)); |
lmottola | 0:6ab5b8697bfb | 34 | } |