Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
events/README.md@3:1198227e6421, 2016-12-16 (annotated)
- Committer:
- switches
- Date:
- Fri Dec 16 16:27:57 2016 +0000
- Revision:
- 3:1198227e6421
- Parent:
- 0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | ## The mbed-events library ## |
switches | 0:5c4d7b2438d3 | 2 | |
switches | 0:5c4d7b2438d3 | 3 | The mbed-events library provides a flexible queue for scheduling events. |
switches | 0:5c4d7b2438d3 | 4 | |
switches | 0:5c4d7b2438d3 | 5 | ``` cpp |
switches | 0:5c4d7b2438d3 | 6 | #include "mbed_events.h" |
switches | 0:5c4d7b2438d3 | 7 | #include <stdio.h> |
switches | 0:5c4d7b2438d3 | 8 | |
switches | 0:5c4d7b2438d3 | 9 | int main() { |
switches | 0:5c4d7b2438d3 | 10 | // creates a queue with the default size |
switches | 0:5c4d7b2438d3 | 11 | EventQueue queue; |
switches | 0:5c4d7b2438d3 | 12 | |
switches | 0:5c4d7b2438d3 | 13 | // events are simple callbacks |
switches | 0:5c4d7b2438d3 | 14 | queue.call(printf, "called immediately\n"); |
switches | 0:5c4d7b2438d3 | 15 | queue.call_in(2000, printf, "called in 2 seconds\n"); |
switches | 0:5c4d7b2438d3 | 16 | queue.call_every(1000, printf, "called every 1 seconds\n"); |
switches | 0:5c4d7b2438d3 | 17 | |
switches | 0:5c4d7b2438d3 | 18 | // events are executed by the dispatch method |
switches | 0:5c4d7b2438d3 | 19 | queue.dispatch(); |
switches | 0:5c4d7b2438d3 | 20 | } |
switches | 0:5c4d7b2438d3 | 21 | ``` |
switches | 0:5c4d7b2438d3 | 22 | |
switches | 0:5c4d7b2438d3 | 23 | The mbed-events library can be used as a normal event loop, or it can be |
switches | 0:5c4d7b2438d3 | 24 | backgrounded on a single hardware timer or even another event loop. It is |
switches | 0:5c4d7b2438d3 | 25 | both thread and irq safe, and provides functions for easily composing |
switches | 0:5c4d7b2438d3 | 26 | independent event queues. |
switches | 0:5c4d7b2438d3 | 27 | |
switches | 0:5c4d7b2438d3 | 28 | The mbed-events library can act as a drop-in scheduler, provide synchronization |
switches | 0:5c4d7b2438d3 | 29 | between multiple threads, or just act as a mechanism for moving events out of |
switches | 0:5c4d7b2438d3 | 30 | interrupt contexts. |
switches | 0:5c4d7b2438d3 | 31 | |
switches | 0:5c4d7b2438d3 | 32 | ### Usage ### |
switches | 0:5c4d7b2438d3 | 33 | |
switches | 0:5c4d7b2438d3 | 34 | The core of the mbed-events library is the [EventQueue](EventQueue.h) class, |
switches | 0:5c4d7b2438d3 | 35 | which represents a single event queue. The `EventQueue::dispatch` function |
switches | 0:5c4d7b2438d3 | 36 | runs the queue, providing the context for executing events. |
switches | 0:5c4d7b2438d3 | 37 | |
switches | 0:5c4d7b2438d3 | 38 | ``` cpp |
switches | 0:5c4d7b2438d3 | 39 | // Creates an event queue enough buffer space for 32 Callbacks. This |
switches | 0:5c4d7b2438d3 | 40 | // is the default if no argument was provided. Alternatively the size |
switches | 0:5c4d7b2438d3 | 41 | // can just be specified in bytes. |
switches | 0:5c4d7b2438d3 | 42 | EventQueue queue(32*EVENTS_EVENT_SIZE); |
switches | 0:5c4d7b2438d3 | 43 | |
switches | 0:5c4d7b2438d3 | 44 | // Events can be posted to the underlying event queue with dynamic |
switches | 0:5c4d7b2438d3 | 45 | // context allocated from the specified buffer |
switches | 0:5c4d7b2438d3 | 46 | queue.call(printf, "hello %d %d %d %d\n", 1, 2, 3, 4); |
switches | 0:5c4d7b2438d3 | 47 | queue.call(&serial, &Serial::printf, "hi\n"); |
switches | 0:5c4d7b2438d3 | 48 | |
switches | 0:5c4d7b2438d3 | 49 | // The dispatch function provides the context for the running the queue |
switches | 0:5c4d7b2438d3 | 50 | // and can take a millisecond timeout to run for a fixed time or to just |
switches | 0:5c4d7b2438d3 | 51 | // dispatch any pending events |
switches | 0:5c4d7b2438d3 | 52 | queue.dispatch(); |
switches | 0:5c4d7b2438d3 | 53 | ``` |
switches | 0:5c4d7b2438d3 | 54 | |
switches | 0:5c4d7b2438d3 | 55 | The EventQueue class provides several call functions for posting events |
switches | 0:5c4d7b2438d3 | 56 | to the underlying event queue. The call functions are thread and irq safe, |
switches | 0:5c4d7b2438d3 | 57 | don't need the underlying loop to be running, and provide an easy mechanism |
switches | 0:5c4d7b2438d3 | 58 | for moving events out of interrupt contexts. |
switches | 0:5c4d7b2438d3 | 59 | |
switches | 0:5c4d7b2438d3 | 60 | ``` cpp |
switches | 0:5c4d7b2438d3 | 61 | // Simple call function registers events to be called as soon as possible |
switches | 0:5c4d7b2438d3 | 62 | queue.call(doit); |
switches | 0:5c4d7b2438d3 | 63 | queue.call(printf, "called immediately\n"); |
switches | 0:5c4d7b2438d3 | 64 | |
switches | 0:5c4d7b2438d3 | 65 | // The call_in function registers events to be called after a delay |
switches | 0:5c4d7b2438d3 | 66 | // specified in milliseconds |
switches | 0:5c4d7b2438d3 | 67 | queue.call_in(2000, doit_in_two_seconds); |
switches | 0:5c4d7b2438d3 | 68 | queue.call_in(300, printf, "called in 0.3 seconds\n"); |
switches | 0:5c4d7b2438d3 | 69 | |
switches | 0:5c4d7b2438d3 | 70 | // The call_every function registers events to be called repeatedly |
switches | 0:5c4d7b2438d3 | 71 | // with a period specified in milliseconds |
switches | 0:5c4d7b2438d3 | 72 | queue.call_every(2000, doit_every_two_seconds); |
switches | 0:5c4d7b2438d3 | 73 | queue.call_every(400, printf, "called every 0.4 seconds\n"); |
switches | 0:5c4d7b2438d3 | 74 | ``` |
switches | 0:5c4d7b2438d3 | 75 | |
switches | 0:5c4d7b2438d3 | 76 | The call functions return an id that uniquely represents the event in the |
switches | 0:5c4d7b2438d3 | 77 | the event queue. This id can be passed to `EventQueue::cancel` to cancel |
switches | 0:5c4d7b2438d3 | 78 | an in-flight event. |
switches | 0:5c4d7b2438d3 | 79 | |
switches | 0:5c4d7b2438d3 | 80 | ``` cpp |
switches | 0:5c4d7b2438d3 | 81 | // The event id uniquely represents the event in the queue |
switches | 0:5c4d7b2438d3 | 82 | int id = queue.call_in(100, printf, "will this work?\n"); |
switches | 0:5c4d7b2438d3 | 83 | |
switches | 0:5c4d7b2438d3 | 84 | // If there was not enough memory necessary to allocate the event, |
switches | 0:5c4d7b2438d3 | 85 | // an id of 0 is returned from the call functions |
switches | 0:5c4d7b2438d3 | 86 | if (id) { |
switches | 0:5c4d7b2438d3 | 87 | error("oh no!"); |
switches | 0:5c4d7b2438d3 | 88 | } |
switches | 0:5c4d7b2438d3 | 89 | |
switches | 0:5c4d7b2438d3 | 90 | // Events can be cancelled as long as they have not been dispatched. If the |
switches | 0:5c4d7b2438d3 | 91 | // event has already expired, cancel has no side-effects. |
switches | 0:5c4d7b2438d3 | 92 | queue.cancel(id); |
switches | 0:5c4d7b2438d3 | 93 | ``` |
switches | 0:5c4d7b2438d3 | 94 | |
switches | 0:5c4d7b2438d3 | 95 | For a more fine-grain control of event dispatch, the `Event` class can be |
switches | 0:5c4d7b2438d3 | 96 | manually instantiated and configured. An `Event` represents an event as |
switches | 0:5c4d7b2438d3 | 97 | a C++ style function object and can be directly passed to other APIs that |
switches | 0:5c4d7b2438d3 | 98 | expect a callback. |
switches | 0:5c4d7b2438d3 | 99 | |
switches | 0:5c4d7b2438d3 | 100 | ``` cpp |
switches | 0:5c4d7b2438d3 | 101 | // Creates an event bound to the specified event queue |
switches | 0:5c4d7b2438d3 | 102 | EventQueue queue; |
switches | 0:5c4d7b2438d3 | 103 | Event<void()> event(&queue, doit); |
switches | 0:5c4d7b2438d3 | 104 | |
switches | 0:5c4d7b2438d3 | 105 | // The event can be manually configured for special timing requirements |
switches | 0:5c4d7b2438d3 | 106 | // specified in milliseconds |
switches | 0:5c4d7b2438d3 | 107 | event.delay(10); |
switches | 0:5c4d7b2438d3 | 108 | event.period(10000); |
switches | 0:5c4d7b2438d3 | 109 | |
switches | 0:5c4d7b2438d3 | 110 | // Posted events are dispatched in the context of the queue's |
switches | 0:5c4d7b2438d3 | 111 | // dispatch function |
switches | 0:5c4d7b2438d3 | 112 | queue.dispatch(); |
switches | 0:5c4d7b2438d3 | 113 | |
switches | 0:5c4d7b2438d3 | 114 | // Events can also pass arguments to the underlying callback when both |
switches | 0:5c4d7b2438d3 | 115 | // initially constructed and posted. |
switches | 0:5c4d7b2438d3 | 116 | Event<void(int, int)> event(&queue, printf, "recieved %d and %d\n"); |
switches | 0:5c4d7b2438d3 | 117 | |
switches | 0:5c4d7b2438d3 | 118 | // Events can be posted multiple times and enqueue gracefully until |
switches | 0:5c4d7b2438d3 | 119 | // the dispatch function is called. |
switches | 0:5c4d7b2438d3 | 120 | event.post(1, 2); |
switches | 0:5c4d7b2438d3 | 121 | event.post(3, 4); |
switches | 0:5c4d7b2438d3 | 122 | event.post(5, 6); |
switches | 0:5c4d7b2438d3 | 123 | |
switches | 0:5c4d7b2438d3 | 124 | queue.dispatch(); |
switches | 0:5c4d7b2438d3 | 125 | ``` |
switches | 0:5c4d7b2438d3 | 126 | |
switches | 0:5c4d7b2438d3 | 127 | Event queues easily align with module boundaries, where internal state can |
switches | 0:5c4d7b2438d3 | 128 | be implicitly synchronized through event dispatch. Multiple modules can |
switches | 0:5c4d7b2438d3 | 129 | use independent event queues, but still be composed through the |
switches | 0:5c4d7b2438d3 | 130 | `EventQueue::chain` function. |
switches | 0:5c4d7b2438d3 | 131 | |
switches | 0:5c4d7b2438d3 | 132 | ``` cpp |
switches | 0:5c4d7b2438d3 | 133 | // Create some event queues with pending events |
switches | 0:5c4d7b2438d3 | 134 | EventQueue a; |
switches | 0:5c4d7b2438d3 | 135 | a.call(printf, "hello from a!\n"); |
switches | 0:5c4d7b2438d3 | 136 | |
switches | 0:5c4d7b2438d3 | 137 | EventQueue b; |
switches | 0:5c4d7b2438d3 | 138 | b.call(printf, "hello from b!\n"); |
switches | 0:5c4d7b2438d3 | 139 | |
switches | 0:5c4d7b2438d3 | 140 | EventQueue c; |
switches | 0:5c4d7b2438d3 | 141 | c.call(printf, "hello from c!\n"); |
switches | 0:5c4d7b2438d3 | 142 | |
switches | 0:5c4d7b2438d3 | 143 | // Chain c and b onto a's event queue. Both c and b will be dispatched |
switches | 0:5c4d7b2438d3 | 144 | // in the context of a's dispatch function. |
switches | 0:5c4d7b2438d3 | 145 | c.chain(&a); |
switches | 0:5c4d7b2438d3 | 146 | b.chain(&a); |
switches | 0:5c4d7b2438d3 | 147 | |
switches | 0:5c4d7b2438d3 | 148 | // Dispatching a will in turn dispatch b and c, printing hello from |
switches | 0:5c4d7b2438d3 | 149 | // all three queues |
switches | 0:5c4d7b2438d3 | 150 | a.dispatch(); |
switches | 0:5c4d7b2438d3 | 151 | ``` |
switches | 0:5c4d7b2438d3 | 152 | |
switches | 0:5c4d7b2438d3 | 153 |