![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Using event queues with C++
Dependencies: ELEC350-Practicals-FZ429
Fork of Task622Solution-mbedos54 by
main.cpp@11:c865ef615016, 2017-11-23 (annotated)
- Committer:
- noutram
- Date:
- Thu Nov 23 14:15:38 2017 +0000
- Revision:
- 11:c865ef615016
- Parent:
- 10:c077eefcf371
Uses C++
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:f916cefba2f4 | 1 | #include "mbed.h" |
noutram | 10:c077eefcf371 | 2 | #include <iostream> |
noutram | 10:c077eefcf371 | 3 | #include "sample_hardware.hpp" |
noutram | 10:c077eefcf371 | 4 | #include "mbed_events.h" |
noutram | 10:c077eefcf371 | 5 | void do_rising(); |
noutram | 10:c077eefcf371 | 6 | void enableRising(); |
noutram | 10:c077eefcf371 | 7 | |
noutram | 10:c077eefcf371 | 8 | DigitalOut led(LED1); |
noutram | 10:c077eefcf371 | 9 | InterruptIn btn(USER_BUTTON); |
noutram | 0:f916cefba2f4 | 10 | |
noutram | 10:c077eefcf371 | 11 | class ButtonManager { |
noutram | 10:c077eefcf371 | 12 | private: |
noutram | 10:c077eefcf371 | 13 | InterruptIn& input; |
noutram | 10:c077eefcf371 | 14 | EventQueue& q; |
noutram | 10:c077eefcf371 | 15 | unsigned int count; |
noutram | 10:c077eefcf371 | 16 | unsigned int state; |
noutram | 0:f916cefba2f4 | 17 | |
noutram | 10:c077eefcf371 | 18 | void risingEdgeISR() { |
noutram | 10:c077eefcf371 | 19 | if (state == 0) { |
noutram | 10:c077eefcf371 | 20 | input.rise(NULL); |
noutram | 10:c077eefcf371 | 21 | led = !led; |
noutram | 10:c077eefcf371 | 22 | state = 1; |
noutram | 10:c077eefcf371 | 23 | q.call_in(200, callback(this, &ButtonManager::risingEdgeHasSettled)); |
noutram | 10:c077eefcf371 | 24 | q.call(printf, "Pressed %d times\n", ++count); |
noutram | 10:c077eefcf371 | 25 | } |
noutram | 10:c077eefcf371 | 26 | } |
noutram | 10:c077eefcf371 | 27 | |
noutram | 10:c077eefcf371 | 28 | void risingEdgeHasSettled() { |
noutram | 10:c077eefcf371 | 29 | state = 2; |
noutram | 10:c077eefcf371 | 30 | input.fall(callback(this, &ButtonManager::fallingEdgeISR)); |
noutram | 10:c077eefcf371 | 31 | printf("Rising edge has settled\n"); |
noutram | 10:c077eefcf371 | 32 | } |
noutram | 10:c077eefcf371 | 33 | |
noutram | 10:c077eefcf371 | 34 | void fallingEdgeISR() { |
noutram | 10:c077eefcf371 | 35 | if (state == 2) { |
noutram | 10:c077eefcf371 | 36 | input.fall(NULL); |
noutram | 10:c077eefcf371 | 37 | state = 3; |
noutram | 10:c077eefcf371 | 38 | q.call_in(200, callback(this, &ButtonManager::fallingEdgeHasSettled)); |
noutram | 10:c077eefcf371 | 39 | q.call(printf, "Falling edge detected\n"); |
noutram | 10:c077eefcf371 | 40 | } |
noutram | 10:c077eefcf371 | 41 | } |
noutram | 10:c077eefcf371 | 42 | |
noutram | 10:c077eefcf371 | 43 | void fallingEdgeHasSettled() { |
noutram | 10:c077eefcf371 | 44 | state = 0; |
noutram | 10:c077eefcf371 | 45 | input.rise(callback(this, &ButtonManager::risingEdgeISR)); |
noutram | 10:c077eefcf371 | 46 | printf("Falling edge has settled\n"); |
noutram | 10:c077eefcf371 | 47 | } |
noutram | 10:c077eefcf371 | 48 | |
noutram | 10:c077eefcf371 | 49 | public: |
noutram | 10:c077eefcf371 | 50 | |
noutram | 10:c077eefcf371 | 51 | ButtonManager(InterruptIn& ip, EventQueue& queue) : input(ip), q(queue) { |
noutram | 10:c077eefcf371 | 52 | count=0; |
noutram | 10:c077eefcf371 | 53 | state=0; |
noutram | 10:c077eefcf371 | 54 | input.rise(callback(this, &ButtonManager::risingEdgeISR)); |
noutram | 10:c077eefcf371 | 55 | printf("Initialised\n"); |
noutram | 10:c077eefcf371 | 56 | } |
noutram | 10:c077eefcf371 | 57 | |
noutram | 10:c077eefcf371 | 58 | }; |
noutram | 8:b28defacd894 | 59 | |
noutram | 10:c077eefcf371 | 60 | |
noutram | 10:c077eefcf371 | 61 | int main() { |
noutram | 10:c077eefcf371 | 62 | //Creates a queue with the default size |
noutram | 10:c077eefcf371 | 63 | EventQueue queue; |
noutram | 10:c077eefcf371 | 64 | |
noutram | 10:c077eefcf371 | 65 | //Map button state machine onto the main queue |
noutram | 10:c077eefcf371 | 66 | ButtonManager bm(btn, queue); |
noutram | 2:70084af839d3 | 67 | |
noutram | 10:c077eefcf371 | 68 | // events are executed by the dispatch method |
noutram | 10:c077eefcf371 | 69 | queue.dispatch(); |
noutram | 10:c077eefcf371 | 70 | |
noutram | 10:c077eefcf371 | 71 | //Unreachable code |
noutram | 10:c077eefcf371 | 72 | printf("This should never appear!\n"); |
noutram | 0:f916cefba2f4 | 73 | } |
noutram | 0:f916cefba2f4 | 74 | |
noutram | 4:dae8898e55fe | 75 |