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