![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Task651 Event Queue
Dependencies: ELEC350-Practicals-FZ429
Fork of Task622Solution-mbedos54 by
main.cpp@11:f8dde59c750c, 2017-11-23 (annotated)
- Committer:
- noutram
- Date:
- Thu Nov 23 14:15:12 2017 +0000
- Revision:
- 11:f8dde59c750c
- Parent:
- 10:dd9aa289d656
EventQueue using C Functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
noutram | 10:dd9aa289d656 | 2 | #include <iostream> |
noutram | 10:dd9aa289d656 | 3 | #include "sample_hardware.hpp" |
noutram | 10:dd9aa289d656 | 4 | #include "mbed_events.h" |
noutram | 10:dd9aa289d656 | 5 | void do_rising(); |
noutram | 10:dd9aa289d656 | 6 | void enableRising(); |
noutram | 10:dd9aa289d656 | 7 | |
noutram | 10:dd9aa289d656 | 8 | DigitalOut led(LED1); |
noutram | 10:dd9aa289d656 | 9 | InterruptIn btn(USER_BUTTON); |
noutram | 8:b28defacd894 | 10 | |
noutram | 10:dd9aa289d656 | 11 | // creates a queue with the default size |
noutram | 10:dd9aa289d656 | 12 | EventQueue queue; |
noutram | 10:dd9aa289d656 | 13 | unsigned int buttonCount = 0; |
noutram | 0:f916cefba2f4 | 14 | |
noutram | 10:dd9aa289d656 | 15 | |
noutram | 10:dd9aa289d656 | 16 | int main() { |
noutram | 10:dd9aa289d656 | 17 | |
noutram | 10:dd9aa289d656 | 18 | //Hook up isr |
noutram | 10:dd9aa289d656 | 19 | enableRising(); |
noutram | 8:b28defacd894 | 20 | |
noutram | 10:dd9aa289d656 | 21 | // events are simple callbacks |
noutram | 10:dd9aa289d656 | 22 | queue.call(printf, "called immediately on dispatch\n"); |
noutram | 10:dd9aa289d656 | 23 | queue.call_in(2000, printf, "called after 2 seconds\n"); |
noutram | 10:dd9aa289d656 | 24 | queue.call_every(1000, printf, "called every 1 seconds\n"); |
noutram | 0:f916cefba2f4 | 25 | |
noutram | 10:dd9aa289d656 | 26 | // events are executed by the dispatch method |
noutram | 10:dd9aa289d656 | 27 | queue.dispatch(); |
noutram | 10:dd9aa289d656 | 28 | |
noutram | 10:dd9aa289d656 | 29 | //Unreachable code |
noutram | 10:dd9aa289d656 | 30 | printf("This should never appear!\n"); |
noutram | 8:b28defacd894 | 31 | } |
noutram | 8:b28defacd894 | 32 | |
noutram | 10:dd9aa289d656 | 33 | //ISR |
noutram | 10:dd9aa289d656 | 34 | void do_rising() { |
noutram | 10:dd9aa289d656 | 35 | //ISR response time (fast) |
noutram | 10:dd9aa289d656 | 36 | led = !led; |
noutram | 10:dd9aa289d656 | 37 | |
noutram | 10:dd9aa289d656 | 38 | //Turn off this interrupt (fast) |
noutram | 10:dd9aa289d656 | 39 | btn.rise(NULL); |
noutram | 2:70084af839d3 | 40 | |
noutram | 10:dd9aa289d656 | 41 | //Post the following into the main thread event queue |
noutram | 10:dd9aa289d656 | 42 | |
noutram | 10:dd9aa289d656 | 43 | //Turn back on after a delay |
noutram | 10:dd9aa289d656 | 44 | queue.call_in(1000, enableRising); |
noutram | 10:dd9aa289d656 | 45 | |
noutram | 10:dd9aa289d656 | 46 | //Dispatch to the main thread queue |
noutram | 10:dd9aa289d656 | 47 | queue.call(printf, "Button depressed %5d times\n", ++buttonCount); |
noutram | 0:f916cefba2f4 | 48 | } |
noutram | 0:f916cefba2f4 | 49 | |
noutram | 10:dd9aa289d656 | 50 | //Enable the rising edge |
noutram | 10:dd9aa289d656 | 51 | void enableRising() { |
noutram | 10:dd9aa289d656 | 52 | btn.rise(do_rising); |
noutram | 0:f916cefba2f4 | 53 | } |