Skip to content

Commit d283c0f

Browse files
authored
Merge pull request #3086 from iriark01/patch-4
Edit
2 parents 775bfa3 + bd5aebe commit d283c0f

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

events/README.md

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,35 @@
1-
## The mbed-events library ##
1+
# The mbed-events library
22

3-
The mbed-events library provides a flexible queue for scheduling events.
3+
The mbed-events library provides a flexible queue for scheduling events. It can be used as a normal event loop, or it can be backgrounded on a single hardware timer or even another event loop. It is both thread and IRQ safe, and provides functions for easily composing independent event queues.
4+
5+
The library can act as a drop-in scheduler, provide synchronization between multiple threads, or just act as a mechanism for moving events out of interrupt contexts.
6+
7+
## Example
48

59
``` cpp
610
#include "mbed_events.h"
711
#include <stdio.h>
812

913
int main() {
10-
// creates a queue with the default size
14+
// Creates a queue with the default size
1115
EventQueue queue;
1216

13-
// events are simple callbacks
17+
// Events are simple callbacks
1418
queue.call(printf, "called immediately\n");
1519
queue.call_in(2000, printf, "called in 2 seconds\n");
1620
queue.call_every(1000, printf, "called every 1 seconds\n");
1721

18-
// events are executed by the dispatch method
22+
// Events are executed by the dispatch method
1923
queue.dispatch();
2024
}
2125
```
2226

23-
The mbed-events library can be used as a normal event loop, or it can be
24-
backgrounded on a single hardware timer or even another event loop. It is
25-
both thread and irq safe, and provides functions for easily composing
26-
independent event queues.
27-
28-
The mbed-events library can act as a drop-in scheduler, provide synchronization
29-
between multiple threads, or just act as a mechanism for moving events out of
30-
interrupt contexts.
31-
32-
### Usage ###
27+
## Usage
3328

34-
The core of the mbed-events library is the [EventQueue](EventQueue.h) class,
35-
which represents a single event queue. The `EventQueue::dispatch` function
36-
runs the queue, providing the context for executing events.
29+
The core of the mbed-events library is the [EventQueue](https://docs.mbed.com/docs/mbed-os-api/en/mbed-os-5.2/api/classevents_1_1EventQueue.html) class, which represents a single event queue. The `EventQueue::dispatch` function runs the queue, providing the context for executing events:
3730

3831
``` cpp
39-
// Creates an event queue enough buffer space for 32 Callbacks. This
32+
// Creates an event queue with enough buffer space for 32 Callbacks. This
4033
// is the default if no argument was provided. Alternatively the size
4134
// can just be specified in bytes.
4235
EventQueue queue(32*EVENTS_EVENT_SIZE);
@@ -46,16 +39,13 @@ EventQueue queue(32*EVENTS_EVENT_SIZE);
4639
queue.call(printf, "hello %d %d %d %d\n", 1, 2, 3, 4);
4740
queue.call(&serial, &Serial::printf, "hi\n");
4841

49-
// The dispatch function provides the context for the running the queue
42+
// The dispatch function provides the context for running the queue
5043
// and can take a millisecond timeout to run for a fixed time or to just
5144
// dispatch any pending events
5245
queue.dispatch();
5346
```
5447
55-
The EventQueue class provides several call functions for posting events
56-
to the underlying event queue. The call functions are thread and irq safe,
57-
don't need the underlying loop to be running, and provide an easy mechanism
58-
for moving events out of interrupt contexts.
48+
The EventQueue class provides several call functions for posting events to the underlying event queue. The call functions are thread and IRQ safe, don't need the underlying loop to be running, and provide an easy mechanism for moving events out of interrupt contexts:
5949
6050
``` cpp
6151
// Simple call function registers events to be called as soon as possible
@@ -73,16 +63,14 @@ queue.call_every(2000, doit_every_two_seconds);
7363
queue.call_every(400, printf, "called every 0.4 seconds\n");
7464
```
7565

76-
The call functions return an id that uniquely represents the event in the
77-
the event queue. This id can be passed to `EventQueue::cancel` to cancel
78-
an in-flight event.
66+
The call functions return an ID that uniquely represents the event in the event queue. This ID can be passed to `EventQueue::cancel` to cancel an in-flight event:
7967

8068
``` cpp
81-
// The event id uniquely represents the event in the queue
69+
// The event ID uniquely represents the event in the queue
8270
int id = queue.call_in(100, printf, "will this work?\n");
8371

8472
// If there was not enough memory necessary to allocate the event,
85-
// an id of 0 is returned from the call functions
73+
// an ID of 0 is returned from the call functions
8674
if (id) {
8775
error("oh no!");
8876
}
@@ -92,10 +80,7 @@ if (id) {
9280
queue.cancel(id);
9381
```
9482

95-
For a more fine-grain control of event dispatch, the `Event` class can be
96-
manually instantiated and configured. An `Event` represents an event as
97-
a C++ style function object and can be directly passed to other APIs that
98-
expect a callback.
83+
For a more fine-grain control of event dispatch, the `Event` class can be manually instantiated and configured. An `Event` represents an event as a C++ style function object and can be directly passed to other APIs that expect a callback:
9984

10085
``` cpp
10186
// Creates an event bound to the specified event queue
@@ -112,7 +97,7 @@ event.period(10000);
11297
queue.dispatch();
11398

11499
// Events can also pass arguments to the underlying callback when both
115-
// initially constructed and posted.
100+
// are initially constructed and posted.
116101
Event<void(int, int)> event(&queue, printf, "recieved %d and %d\n");
117102

118103
// Events can be posted multiple times and enqueue gracefully until
@@ -124,10 +109,7 @@ event.post(5, 6);
124109
queue.dispatch();
125110
```
126111
127-
Event queues easily align with module boundaries, where internal state can
128-
be implicitly synchronized through event dispatch. Multiple modules can
129-
use independent event queues, but still be composed through the
130-
`EventQueue::chain` function.
112+
Event queues easily align with module boundaries, where internal state can be implicitly synchronized through event dispatch. Multiple modules can use independent event queues, but still be composed through the `EventQueue::chain` function:
131113
132114
``` cpp
133115
// Create some event queues with pending events

0 commit comments

Comments
 (0)