Skip to content

Commit 98056b5

Browse files
add Events examples
1 parent 73f5e9f commit 98056b5

File tree

12 files changed

+199
-0
lines changed

12 files changed

+199
-0
lines changed

APIs_RTOS/EventQueue_ex_1/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
15
#include "mbed.h"
26

37
DigitalOut led1(LED1);

APIs_RTOS/EventQueue_ex_2/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# EventQueue example
2+
3+
This example shows how to queue functions.
4+

APIs_RTOS/EventQueue_ex_2/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed_events.h"
6+
#include <stdio.h>
7+
8+
int main()
9+
{
10+
// creates a queue with the default size
11+
EventQueue queue;
12+
13+
// events are simple callbacks
14+
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");
17+
18+
// events are executed by the dispatch method
19+
queue.dispatch();
20+
}

APIs_RTOS/EventQueue_ex_3/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# EventQueue example
2+
3+
This example shows how to chain events from more than one EventQueue.
4+

APIs_RTOS/EventQueue_ex_3/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed_events.h"
6+
#include <stdio.h>
7+
8+
/**
9+
Event queues easily align with module boundaries, where internal state can be
10+
implicitly synchronized through event dispatch. Multiple modules can use
11+
independent event queues, but still be composed through the EventQueue::chain function.
12+
**/
13+
14+
int main()
15+
{
16+
// Create some event queues with pending events
17+
EventQueue a;
18+
a.call(printf, "hello from a!\n");
19+
20+
EventQueue b;
21+
b.call(printf, "hello from b!\n");
22+
23+
EventQueue c;
24+
c.call(printf, "hello from c!\n");
25+
26+
// Chain c and b onto a's event queue. Both c and b will be dispatched
27+
// in the context of a's dispatch function.
28+
c.chain(&a);
29+
b.chain(&a);
30+
31+
// Dispatching a will in turn dispatch b and c, printing hello from
32+
// all three queues
33+
a.dispatch();
34+
}

APIs_RTOS/Shared_Events_1/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Shared event example
2+
3+
Instead of creating special thread user can use shared event queue and dispatch the global event queue from main thread.
4+

APIs_RTOS/Shared_Events_1/main.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "mbed_events.h"
7+
8+
DigitalOut led1(LED1);
9+
InterruptIn sw(SW2);
10+
11+
void rise_handler(void)
12+
{
13+
// Toggle LED
14+
led1 = !led1;
15+
}
16+
17+
void fall_handler(void)
18+
{
19+
printf("fall_handler in context %p\r\n", ThisThread::get_id());
20+
// Toggle LED
21+
led1 = !led1;
22+
}
23+
24+
int main()
25+
{
26+
// Request the shared queue
27+
EventQueue *queue = mbed_event_queue();
28+
printf("Starting in context %p\r\n", ThisThread::get_id());
29+
// The 'rise' handler will execute in IRQ context
30+
sw.rise(rise_handler);
31+
// The 'fall' handler will execute in the context of the shared queue thread
32+
sw.fall(queue->event(fall_handler));
33+
}

APIs_RTOS/Shared_Events_2/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Shared event example
2+
3+
Instead of creating special thread user can use shared event queue i.e. sharing it with other system components and saving RAM. As the event queue is shared, you should limit the execution time of your event functions to avoid delaying other users’ events excessively.
4+

APIs_RTOS/Shared_Events_2/main.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
#include "mbed_events.h"
7+
8+
DigitalOut led1(LED1);
9+
InterruptIn sw(SW2);
10+
11+
void rise_handler(void)
12+
{
13+
// Toggle LED
14+
led1 = !led1;
15+
}
16+
17+
void fall_handler(void)
18+
{
19+
printf("fall_handler in context %p\r\n", ThisThread::get_id());
20+
// Toggle LED
21+
led1 = !led1;
22+
}
23+
24+
int main()
25+
{
26+
// Request the shared queue
27+
EventQueue *queue = mbed_event_queue();
28+
printf("Starting in context %p\r\n", ThisThread::get_id());
29+
// The 'rise' handler will execute in IRQ context
30+
sw.rise(rise_handler);
31+
// The 'fall' handler will execute in the context of the shared queue (actually the main thread)
32+
sw.fall(queue->event(fall_handler));
33+
// Setup complete, so we now dispatch the shared queue from main
34+
queue->dispatch_forever();
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"target_overrides": {
3+
"*": {
4+
"events.shared-dispatch-from-application": true
5+
}
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# EventQueue example
2+
3+
This example shows how to instantiate, configure and post events.
4+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
// Creates an event bound to the specified event queue
8+
EventQueue queue;
9+
void handler(int count);
10+
Event<void(int)> event(&queue, handler);
11+
12+
void handler(int count)
13+
{
14+
printf("Event = %d \n", count);
15+
return;
16+
}
17+
18+
void post_events(void)
19+
{
20+
21+
// Events can be posted multiple times and enqueue gracefully until
22+
// the dispatch function is called.
23+
event.post(1);
24+
event.post(2);
25+
event.post(3);
26+
}
27+
28+
int main()
29+
{
30+
31+
Thread event_thread;
32+
33+
// The event can be manually configured for special timing requirements
34+
// specified in milliseconds
35+
event.delay(100); // Starting delay - 100 msec
36+
event.period(200); // Delay between each evet - 200msec
37+
38+
event_thread.start(callback(post_events));
39+
40+
// Posted events are dispatched in the context of the queue's
41+
// dispatch function
42+
queue.dispatch(400); // Dispatch time - 400msec
43+
// 400 msec - Only 2 set of events will be dispatched as period is 200 msec
44+
45+
event_thread.join();
46+
}

0 commit comments

Comments
 (0)