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