Skip to content

Commit e1a54bf

Browse files
authored
Update Event/EventQueue snippets to mirror updates to the API changes (#125)
* Update Events example to use Chrono times Also show how to use non_periodic and cancel. Add timestamp to event printf and add expected results * Update EventQueue snippets for the chrono dispatch changes Mbed OS is being updated to bring the event queue dispatch function in line with other event functions in moving to using chrono times. This commit updates the associated snippet examples accordingly. It also fixes the use of the new split dispatch functions: dispatch_once(), dispatch_for(duration ms), dispatch_forever()
1 parent a377eba commit e1a54bf

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

APIs_RTOS/EventQueue_ex_2/main.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@
44
*/
55
#include "mbed_events.h"
66
#include <stdio.h>
7+
using namespace std::chrono_literals;
78

9+
// In the example below there are 3 types of event callbacks
10+
// 1) Call the provided function immediately
11+
// 2) Call the provided function once (after the specified period)
12+
// 3) Call the provided function every specified period
13+
//
14+
// Expected output:
15+
//
16+
// called immediately
17+
// called every 1 seconds
18+
// called in 2 seconds
19+
// called every 1 seconds
20+
// called every 1 seconds
21+
// ^ repeated forever
22+
//
823
int main()
924
{
1025
// creates a queue with the default size
1126
EventQueue queue;
1227

1328
// events are simple callbacks
1429
queue.call(printf, "called immediately\n");
15-
queue.call_in(2000, printf, "called in 2 seconds\n");
16-
queue.call_every(1000, printf, "called every 1 seconds\n");
30+
queue.call_in(2000ms, printf, "called in 2 seconds\n");
31+
queue.call_every(1000ms, printf, "called every 1 seconds\n");
1732

1833
// events are executed by the dispatch_forever method
1934
queue.dispatch_forever();

APIs_RTOS/EventQueue_ex_3/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@
44
*/
55
#include "mbed_events.h"
66
#include <stdio.h>
7+
using namespace std::chrono_literals;
78

89
/**
910
Event queues easily align with module boundaries, where internal state can be
1011
implicitly synchronized through event dispatch. Multiple modules can use
1112
independent event queues, but still be composed through the EventQueue::chain function.
13+
14+
Note the call() methods in this example only dispatch once as no period is
15+
set and thus defaults to dispatch_once(). The ordering of the chaining dictates
16+
the order of the dispatching.
17+
18+
Expected output:
19+
20+
hello from a!
21+
hello from c!
22+
hello from b!
1223
**/
1324

1425
int main()
@@ -30,5 +41,5 @@ int main()
3041

3142
// Dispatching a will in turn dispatch b and c, printing hello from
3243
// all three queues
33-
a.dispatch();
44+
a.dispatch_forever();
3445
}

APIs_RTOS/Events_ex_1/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int main()
9696
// converted to using Chrono times and thus times are still specified
9797
// in integer millisecond units.
9898
// 800 ms will allow the posted events to be dispatched multiple times
99-
queue.dispatch(800);
99+
queue.dispatch_for(800ms);
100100

101101
event_thread.join();
102102

APIs_RTOS/UserAllocatedEvent_ex_1/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ int main()
7070
printf("*** start ***\n");
7171
Thread event_thread;
7272

73-
// The event can be manually configured for special timing requirements
74-
// specified in milliseconds
73+
// The event can be manually configured for special timing requirements.
74+
// Timings are specified in milliseconds.
7575
// Starting delay - 100 msec
7676
// Delay between each event - 200msec
7777
event1.delay(100);
@@ -86,7 +86,7 @@ int main()
8686
event_thread.start(callback(post_events));
8787

8888
// Posted events are dispatched in the context of the queue's dispatch function
89-
queue.dispatch(400); // Dispatch time - 400msec
89+
queue.dispatch_for(400ms); // Dispatch time - 400msec
9090
// 400 msec - Only 2 set of events will be dispatched as period is 200 msec
9191

9292
event_thread.join();

0 commit comments

Comments
 (0)