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