mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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