|
| 1 | +/* |
| 2 | +* Copyright (c) 2019 ARM Limited. All rights reserved. |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +* Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | +* not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 12 | +* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "mbed.h" |
| 18 | + |
| 19 | + |
| 20 | +// Creates static event queue |
| 21 | +static EventQueue queue(0); |
| 22 | + |
| 23 | +void handler(int count); |
| 24 | + |
| 25 | +// Creates events for later bound |
| 26 | +auto event1 = make_user_allocated_event(handler, 1); |
| 27 | +auto event2 = make_user_allocated_event(handler, 2); |
| 28 | + |
| 29 | +// Creates event bound to the specified event queue |
| 30 | +auto event3 = queue.make_user_allocated_event(handler, 3); |
| 31 | +auto event4 = queue.make_user_allocated_event(handler, 4); |
| 32 | + |
| 33 | +void handler(int count) { |
| 34 | + printf("UserAllocatedEvent = %d \n", count); |
| 35 | + return; |
| 36 | +} |
| 37 | + |
| 38 | +void post_events(void) |
| 39 | +{ |
| 40 | + // Single instance of user allocated event can be posted only once. |
| 41 | + // Event can be posted again if the previous dispatch has finished or event has been canceled. |
| 42 | + |
| 43 | + // bind & post |
| 44 | + event1.call_on(&queue); |
| 45 | + |
| 46 | + // event cannot be posted again until dispatched or canceled |
| 47 | + bool post_succeed = event1.try_call(); |
| 48 | + assert(!post_succeed); |
| 49 | + |
| 50 | + queue.cancel(&event1); |
| 51 | + |
| 52 | + // try to post |
| 53 | + post_succeed = event1.try_call(); |
| 54 | + assert(post_succeed); |
| 55 | + |
| 56 | + // bind & post |
| 57 | + post_succeed = event2.try_call_on(&queue); |
| 58 | + assert(post_succeed); |
| 59 | + |
| 60 | + // post |
| 61 | + event3.call(); |
| 62 | + |
| 63 | + // post |
| 64 | + event4(); |
| 65 | +} |
| 66 | + |
| 67 | +int main() |
| 68 | +{ |
| 69 | + printf("*** start ***\n"); |
| 70 | + Thread event_thread; |
| 71 | + |
| 72 | + // The event can be manually configured for special timing requirements |
| 73 | + // specified in milliseconds |
| 74 | + // Starting delay - 100 msec |
| 75 | + // Delay between each event - 200msec |
| 76 | + event1.delay(100); |
| 77 | + event1.period(200); |
| 78 | + event2.delay(100); |
| 79 | + event2.period(200); |
| 80 | + event3.delay(100); |
| 81 | + event3.period(200); |
| 82 | + event4.delay(100); |
| 83 | + event4.period(200); |
| 84 | + |
| 85 | + event_thread.start(callback(post_events)); |
| 86 | + |
| 87 | + // Posted events are dispatched in the context of the queue's dispatch function |
| 88 | + queue.dispatch(400); // Dispatch time - 400msec |
| 89 | + // 400 msec - Only 2 set of events will be dispatched as period is 200 msec |
| 90 | + |
| 91 | + event_thread.join(); |
| 92 | +} |
0 commit comments