Skip to content

Update Event/EventQueue snippets to mirror updates to the API changes #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions APIs_RTOS/EventQueue_ex_2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@
*/
#include "mbed_events.h"
#include <stdio.h>
using namespace std::chrono_literals;

// In the example below there are 3 types of event callbacks
// 1) Call the provided function immediately
// 2) Call the provided function once (after the specified period)
// 3) Call the provided function every specified period
//
// Expected output:
//
// called immediately
// called every 1 seconds
// called in 2 seconds
// called every 1 seconds
// called every 1 seconds
// ^ repeated forever
//
int main()
{
// creates a queue with the default size
EventQueue queue;

// events are simple callbacks
queue.call(printf, "called immediately\n");
queue.call_in(2000, printf, "called in 2 seconds\n");
queue.call_every(1000, printf, "called every 1 seconds\n");
queue.call_in(2000ms, printf, "called in 2 seconds\n");
queue.call_every(1000ms, printf, "called every 1 seconds\n");

// events are executed by the dispatch_forever method
queue.dispatch_forever();
Expand Down
13 changes: 12 additions & 1 deletion APIs_RTOS/EventQueue_ex_3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@
*/
#include "mbed_events.h"
#include <stdio.h>
using namespace std::chrono_literals;

/**
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.

Note the call() methods in this example only dispatch once as no period is
set and thus defaults to dispatch_once(). The ordering of the chaining dictates
the order of the dispatching.

Expected output:

hello from a!
hello from c!
hello from b!
**/

int main()
Expand All @@ -30,5 +41,5 @@ int main()

// Dispatching a will in turn dispatch b and c, printing hello from
// all three queues
a.dispatch();
a.dispatch_forever();
}
2 changes: 1 addition & 1 deletion APIs_RTOS/Events_ex_1/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int main()
// converted to using Chrono times and thus times are still specified
// in integer millisecond units.
// 800 ms will allow the posted events to be dispatched multiple times
queue.dispatch(800);
queue.dispatch_for(800ms);

event_thread.join();

Expand Down
6 changes: 3 additions & 3 deletions APIs_RTOS/UserAllocatedEvent_ex_1/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ int main()
printf("*** start ***\n");
Thread event_thread;

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

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

event_thread.join();
Expand Down