Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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