mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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