Daniel Vizcaya / Mbed OS 04_RTOS_Embebidos
Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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