Skip to content

add Events examples #73

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 28, 2020
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
4 changes: 4 additions & 0 deletions APIs_RTOS/EventQueue_ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"

DigitalOut led1(LED1);
Expand Down
4 changes: 4 additions & 0 deletions APIs_RTOS/EventQueue_ex_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# EventQueue example

This example shows how to queue functions.

20 changes: 20 additions & 0 deletions APIs_RTOS/EventQueue_ex_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed_events.h"
#include <stdio.h>

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");

// events are executed by the dispatch method
queue.dispatch();
}
4 changes: 4 additions & 0 deletions APIs_RTOS/EventQueue_ex_3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# EventQueue example

This example shows how to chain events from more than one EventQueue.

34 changes: 34 additions & 0 deletions APIs_RTOS/EventQueue_ex_3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed_events.h"
#include <stdio.h>

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

int main()
{
// Create some event queues with pending events
EventQueue a;
a.call(printf, "hello from a!\n");

EventQueue b;
b.call(printf, "hello from b!\n");

EventQueue c;
c.call(printf, "hello from c!\n");

// Chain c and b onto a's event queue. Both c and b will be dispatched
// in the context of a's dispatch function.
c.chain(&a);
b.chain(&a);

// Dispatching a will in turn dispatch b and c, printing hello from
// all three queues
a.dispatch();
}
4 changes: 4 additions & 0 deletions APIs_RTOS/Shared_Events_1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Shared event example

Instead of creating a special thread, you can use a shared event queue and dispatch the global event queue from the main thread.

33 changes: 33 additions & 0 deletions APIs_RTOS/Shared_Events_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "mbed_events.h"

DigitalOut led1(LED1);
InterruptIn sw(SW2);

void rise_handler(void)
{
// Toggle LED
led1 = !led1;
}

void fall_handler(void)
{
printf("fall_handler in context %p\r\n", ThisThread::get_id());
// Toggle LED
led1 = !led1;
}

int main()
{
// Request the shared queue
EventQueue *queue = mbed_event_queue();
printf("Starting in context %p\r\n", ThisThread::get_id());
// The 'rise' handler will execute in IRQ context
sw.rise(rise_handler);
// The 'fall' handler will execute in the context of the shared queue thread
sw.fall(queue->event(fall_handler));
}
3 changes: 3 additions & 0 deletions APIs_RTOS/Shared_Events_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Shared event example

Instead of creating a special thread, you can use a shared event queue, which shares the queue with other system components, saving RAM. Because the event queue is shared, you should limit the execution time of your event functions to avoid delaying other users’ events excessively.
35 changes: 35 additions & 0 deletions APIs_RTOS/Shared_Events_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "mbed_events.h"

DigitalOut led1(LED1);
InterruptIn sw(SW2);

void rise_handler(void)
{
// Toggle LED
led1 = !led1;
}

void fall_handler(void)
{
printf("fall_handler in context %p\r\n", ThisThread::get_id());
// Toggle LED
led1 = !led1;
}

int main()
{
// Request the shared queue
EventQueue *queue = mbed_event_queue();
printf("Starting in context %p\r\n", ThisThread::get_id());
// The 'rise' handler will execute in IRQ context
sw.rise(rise_handler);
// The 'fall' handler will execute in the context of the shared queue (actually the main thread)
sw.fall(queue->event(fall_handler));
// Setup complete, so we now dispatch the shared queue from main
queue->dispatch_forever();
}
7 changes: 7 additions & 0 deletions APIs_RTOS/Shared_Events_2/mbed_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"target_overrides": {
"*": {
"events.shared-dispatch-from-application": true
}
}
}
4 changes: 4 additions & 0 deletions APIs_RTOS/mbed-os-example-events/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# EventQueue example

This example shows how to instantiate, configure and post events.

46 changes: 46 additions & 0 deletions APIs_RTOS/mbed-os-example-events/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"

// Creates an event bound to the specified event queue
EventQueue queue;
void handler(int count);
Event<void(int)> event(&queue, handler);

void handler(int count)
{
printf("Event = %d \n", count);
return;
}

void post_events(void)
{

// Events can be posted multiple times and enqueue gracefully until
// the dispatch function is called.
event.post(1);
event.post(2);
event.post(3);
}

int main()
{

Thread event_thread;

// The event can be manually configured for special timing requirements
// specified in milliseconds
event.delay(100); // Starting delay - 100 msec
event.period(200); // Delay between each evet - 200msec

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
// 400 msec - Only 2 set of events will be dispatched as period is 200 msec

event_thread.join();
}