You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: events/README.md
+20-38Lines changed: 20 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,35 @@
1
-
##The mbed-events library ##
1
+
# The mbed-events library
2
2
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
4
8
5
9
```cpp
6
10
#include"mbed_events.h"
7
11
#include<stdio.h>
8
12
9
13
intmain() {
10
-
// creates a queue with the default size
14
+
// Creates a queue with the default size
11
15
EventQueue queue;
12
16
13
-
// events are simple callbacks
17
+
// Events are simple callbacks
14
18
queue.call(printf, "called immediately\n");
15
19
queue.call_in(2000, printf, "called in 2 seconds\n");
16
20
queue.call_every(1000, printf, "called every 1 seconds\n");
17
21
18
-
// events are executed by the dispatch method
22
+
// Events are executed by the dispatch method
19
23
queue.dispatch();
20
24
}
21
25
```
22
26
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
33
28
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:
37
30
38
31
```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
40
33
// is the default if no argument was provided. Alternatively the size
// The dispatch function provides the context for the running the queue
42
+
// The dispatch function provides the context for running the queue
50
43
// and can take a millisecond timeout to run for a fixed time or to just
51
44
// dispatch any pending events
52
45
queue.dispatch();
53
46
```
54
47
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:
59
49
60
50
``` cpp
61
51
// Simple call function registers events to be called as soon as possible
queue.call_every(400, printf, "called every 0.4 seconds\n");
74
64
```
75
65
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:
79
67
80
68
```cpp
81
-
// The event id uniquely represents the event in the queue
69
+
// The event ID uniquely represents the event in the queue
82
70
int id = queue.call_in(100, printf, "will this work?\n");
83
71
84
72
// 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
86
74
if (id) {
87
75
error("oh no!");
88
76
}
@@ -92,10 +80,7 @@ if (id) {
92
80
queue.cancel(id);
93
81
```
94
82
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:
99
84
100
85
```cpp
101
86
// Creates an event bound to the specified event queue
@@ -112,7 +97,7 @@ event.period(10000);
112
97
queue.dispatch();
113
98
114
99
// Events can also pass arguments to the underlying callback when both
115
-
// initially constructed and posted.
100
+
// are initially constructed and posted.
116
101
Event<void(int, int)> event(&queue, printf, "recieved %d and %d\n");
117
102
118
103
// Events can be posted multiple times and enqueue gracefully until
@@ -124,10 +109,7 @@ event.post(5, 6);
124
109
queue.dispatch();
125
110
```
126
111
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:
0 commit comments