Skip to content

Commit c1bf34d

Browse files
add Events examples
1 parent 73f5e9f commit c1bf34d

File tree

11 files changed

+175
-0
lines changed

11 files changed

+175
-0
lines changed

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "mbed_events.h"
2+
#include <stdio.h>
3+
4+
int main()
5+
{
6+
// creates a queue with the default size
7+
EventQueue queue;
8+
9+
// events are simple callbacks
10+
queue.call(printf, "called immediately\n");
11+
queue.call_in(2000, printf, "called in 2 seconds\n");
12+
queue.call_every(1000, printf, "called every 1 seconds\n");
13+
14+
// events are executed by the dispatch method
15+
queue.dispatch();
16+
}

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

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

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

0 commit comments

Comments
 (0)