Skip to content

Commit a377eba

Browse files
authored
Update Events example to use Chrono times (#124)
* Update Events example to use Chrono times Also show how to use non_periodic and cancel. * Fix astyle spurious whitespace issues * Further whitespace removal * Add timestamp to event printf and add expected results
1 parent c0f6625 commit a377eba

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

APIs_RTOS/Events_ex_1/main.cpp

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,97 @@
77
// Creates an event bound to the specified event queue
88
EventQueue queue;
99
void handler(int count);
10-
Event<void(int)> event(&queue, handler);
10+
Event<void(int)> event1(&queue, handler);
11+
Event<void(int)> event2(&queue, handler);
12+
1113

1214
void handler(int count)
1315
{
14-
printf("Event = %d \n", count);
16+
unsigned time_ms = equeue_tick();
17+
printf("Timestamp = %d Event = %d \n", time_ms, count);
1518
return;
1619
}
1720

1821
void post_events(void)
1922
{
20-
2123
// 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);
24+
// the dispatch function is called. Each event will be processed
25+
// subject to any delay and period specified. Each time an event has
26+
// been dispatched it will be re-queued ready for the next dispatch
27+
// period.
28+
event1.post(1); // Event1 with a value of 1
29+
event1.post(2); // Event1 with a value of 2
30+
event1.post(3); // Event1 with a value of 3
31+
32+
// Cancel the last event posted ie Event1 with a value of 3
33+
event1.cancel();
34+
35+
event1.post(4); // Event1 with a value of 4
36+
37+
event2.post(5); // Event2 with a value of 5
38+
2639
}
2740

41+
42+
// Example demonstrates the following:
43+
// 1. Post 5 different events to a queue
44+
// 2. Configure the event delay and period for each
45+
// 3. Invoke the dispatcher to dispatch events for a specified period
46+
//
47+
// | 100ms | 200ms | 100ms | 100ms | 200ms |
48+
// ^ ^ ^ ^ ^
49+
// Events 1,2,4 ^ ^ ^ ^
50+
// dispatched ^ ^ ^ ^
51+
// ^ ^ ^ ^
52+
// Events 1,2,4 ^ ^ ^
53+
// dispatched ^ ^ ^
54+
// ^ ^ ^
55+
// Event 5 ^ ^
56+
// ^ ^
57+
// Event 1,2,4 ^
58+
// dispatched ^
59+
// ^
60+
// Events 1,2,4
61+
// dispatched
62+
//
63+
// Expected Output
64+
//
65+
// Timestamp = 100 Event = 1
66+
// Timestamp = 127 Event = 2
67+
// Timestamp = 156 Event = 4
68+
// Timestamp = 300 Event = 1
69+
// Timestamp = 327 Event = 2
70+
// Timestamp = 356 Event = 4
71+
// Timestamp = 400 Event = 5
72+
// Timestamp = 500 Event = 1
73+
// Timestamp = 527 Event = 2
74+
// Timestamp = 556 Event = 4
75+
// Timestamp = 700 Event = 1
76+
// Timestamp = 727 Event = 2
77+
// Timestamp = 756 Event = 4
78+
2879
int main()
2980
{
30-
81+
// Example 1 Dispatch posted events for a specified period
3182
Thread event_thread;
3283

3384
// 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
85+
// specified in milliseconds (using Chrono durations)
86+
event1.delay(100ms); // Starting delay - 100 msec
87+
event1.period(200ms); // Delay between each event - 200msec
88+
89+
event2.delay(400ms); // Starting delay - 400 msec
90+
event2.period(non_periodic); // Single non periodic event
3791

3892
event_thread.start(callback(post_events));
3993

4094
// 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
95+
// dispatch function. Note that the EventQueue library has yet to be
96+
// converted to using Chrono times and thus times are still specified
97+
// in integer millisecond units.
98+
// 800 ms will allow the posted events to be dispatched multiple times
99+
queue.dispatch(800);
44100

45101
event_thread.join();
102+
46103
}

0 commit comments

Comments
 (0)