|
7 | 7 | // Creates an event bound to the specified event queue
|
8 | 8 | EventQueue queue;
|
9 | 9 | 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 | + |
11 | 13 |
|
12 | 14 | void handler(int count)
|
13 | 15 | {
|
14 |
| - printf("Event = %d \n", count); |
| 16 | + unsigned time_ms = equeue_tick(); |
| 17 | + printf("Timestamp = %d Event = %d \n", time_ms, count); |
15 | 18 | return;
|
16 | 19 | }
|
17 | 20 |
|
18 | 21 | void post_events(void)
|
19 | 22 | {
|
20 |
| - |
21 | 23 | // 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 | + |
26 | 39 | }
|
27 | 40 |
|
| 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 | + |
28 | 79 | int main()
|
29 | 80 | {
|
30 |
| - |
| 81 | + // Example 1 Dispatch posted events for a specified period |
31 | 82 | Thread event_thread;
|
32 | 83 |
|
33 | 84 | // 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 |
37 | 91 |
|
38 | 92 | event_thread.start(callback(post_events));
|
39 | 93 |
|
40 | 94 | // 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); |
44 | 100 |
|
45 | 101 | event_thread.join();
|
| 102 | + |
46 | 103 | }
|
0 commit comments