Event class example to manually instantiate, configure and post events.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 // Creates an event bound to the specified event queue
00003 EventQueue queue;
00004 void handler(int count);
00005 Event<void(int)> event(&queue, handler);
00006 
00007 void handler(int count) {
00008     printf("Event = %d \n", count);
00009     return;
00010 }
00011 
00012 void post_events(void) {
00013 
00014     // Events can be posted multiple times and enqueue gracefully until
00015     // the dispatch function is called.
00016     event.post(1);
00017     event.post(2);
00018     event.post(3);    
00019 }
00020 
00021 int main() {
00022 
00023     Thread event_thread;
00024         
00025     // The event can be manually configured for special timing requirements
00026     // specified in milliseconds
00027     event.delay(100);       // Starting delay - 100 msec
00028     event.period(200);      // Delay between each evet - 200msec
00029     
00030     event_thread.start(callback(post_events));
00031     
00032     // Posted events are dispatched in the context of the queue's
00033     // dispatch function
00034     queue.dispatch(400);        // Dispatch time - 400msec
00035     // 400 msec - Only 2 set of events will be dispatched as period is 200 msec
00036     
00037     event_thread.join();
00038 }