mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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