AVRToolsPlus
A library wof higher-level tools for AVR ATmega328 and ATmega2560 Microcontrollers
Typedefs | Enumerations | Functions
EventManager Namespace Reference

This namespace bundles the Event Manager functionality. It provides logical cohesion for functions implement the Event Manager and prevents namespace collisions. More...

Typedefs

typedef void(* EventListener) (int eventCode, int eventParam)
 Type for an event listener (a.k.a. callback) function.
 

Enumerations

enum  GenericEvents
 This enum provides common event names, purely for user convenience.
 
enum  EventPriority
 EventManager recognizes two kinds of events. By default, events are are queued as low priority, but these constants can be used to explicitly set the priority when queueing events. More...
 

Functions

bool addListener (int eventCode, EventListener listener)
 Add an (event, listener) pair listener to the dispatch table. More...
 
bool removeListener (int eventCode, EventListener listener)
 Remove this (event, listener) pair from the dispatch table. Other listener pairs with the same function or event code will not be affected. More...
 
int removeListener (EventListener listener)
 Remove all occurrances of a listener from the dispatch table, regardless of the event code. returns number removed. More...
 
bool enableListener (int eventCode, EventListener listener, bool enable)
 Enable or disable an (event, listener) pair entry in the dispatch table. More...
 
bool isListenerEnabled (int eventCode, EventListener listener)
 Obtain the the current enabled/disabled state of an (eventCode, listener) pair. More...
 
bool setDefaultListener (EventListener listener)
 Set a default listener. The default listener is a callback function that is called when an event with no listener is processed. More...
 
void removeDefaultListener ()
 Remvoes the default listener. The default listener is a callback function that is called when an event with no listener is processed.
 
void enableDefaultListener (bool enable)
 Enable or disable the default listener. The default listener is a callback function that is called when an event with no listener is processed. More...
 
bool isListenerListEmpty ()
 Check if the listener list (a.k.a., dispatch table) is empty. More...
 
bool isListenerListFull ()
 Check if the listener list (a.k.a., dispatch table) is full. More...
 
int numListeners ()
 Get the number of listeners in the dispatch table. More...
 
bool isEventQueueEmpty (EventPriority pri=kLowPriority)
 Check if the event queue is empty. More...
 
bool isEventQueueFull (EventPriority pri=kLowPriority)
 Check if the event queue is full. More...
 
int getNumEventsInQueue (EventPriority pri=kLowPriority)
 Get the number of events in the event queue. More...
 
bool queueEvent (int eventCode, int eventParam, EventPriority pri=kLowPriority)
 Tries to add an event into the event queue. More...
 
int processEvent ()
 Processes one event from the event queue and dispatches it to the corresponding listeners stored in the dispatch table. More...
 
int processAllEvents ()
 Processes all the events in the event queues and dispatches them to the corresponding listeners stored in the dispatch table. More...
 

Detailed Description

This namespace bundles the Event Manager functionality. It provides logical cohesion for functions implement the Event Manager and prevents namespace collisions.

Enumeration Type Documentation

§ EventPriority

EventManager recognizes two kinds of events. By default, events are are queued as low priority, but these constants can be used to explicitly set the priority when queueing events.

Note
High priority events are always handled before any low priority events.

Function Documentation

§ addListener()

bool EventManager::addListener ( int  eventCode,
EventListener  listener 
)

Add an (event, listener) pair listener to the dispatch table.

  • eventCode the event code this listener listens for.
  • listener the listener to be called when there is an event with this eventCode.
Returns
True if (the event, listener) pair is successfully installed in the dispatch table, false otherwise (e.g. the dispatch table is full).

§ enableDefaultListener()

void EventManager::enableDefaultListener ( bool  enable)

Enable or disable the default listener. The default listener is a callback function that is called when an event with no listener is processed.

  • enable pass true to enable the default listener, false to disable it.

§ enableListener()

bool EventManager::enableListener ( int  eventCode,
EventListener  listener,
bool  enable 
)

Enable or disable an (event, listener) pair entry in the dispatch table.

  • eventCode the event code of the (event, listener) pair to be enabled or disabled.
  • listener the listener of the (event, listener) pair to be enabled or disabled.
  • enable pass true to enable the (event, listener) pair, false to disable it.
Returns
True if the (event, listener) pair was successfully enabled or disabled, false if the (event, listener) pair was not found.

§ getNumEventsInQueue()

int EventManager::getNumEventsInQueue ( EventPriority  pri = kLowPriority)

Get the number of events in the event queue.

  • pri the desired event queue: kLowPriority or kHighPriority. Defaults to kLowPriority.
Returns
The number of events in the specified event queue.

§ isEventQueueEmpty()

bool EventManager::isEventQueueEmpty ( EventPriority  pri = kLowPriority)

Check if the event queue is empty.

  • pri the desired event queue: kLowPriority or kHighPriority. Defaults to kLowPriority.
Returns
True if the specified event queue is empty.

§ isEventQueueFull()

bool EventManager::isEventQueueFull ( EventPriority  pri = kLowPriority)

Check if the event queue is full.

  • pri the desired event queue: kLowPriority or kHighPriority. Defaults to kLowPriority.
Returns
True if the specified event queue is full.

§ isListenerEnabled()

bool EventManager::isListenerEnabled ( int  eventCode,
EventListener  listener 
)

Obtain the the current enabled/disabled state of an (eventCode, listener) pair.

  • eventCode the event code of the (event, listener) pair.
  • listener the listener of the (event, listener) pair.
Returns
The current enabled/disabled state of the (eventCode, listener) pair.

§ isListenerListEmpty()

bool EventManager::isListenerListEmpty ( )

Check if the listener list (a.k.a., dispatch table) is empty.

Returns
True if the listener list (dispatch table) is empty, false if not.

§ isListenerListFull()

bool EventManager::isListenerListFull ( )

Check if the listener list (a.k.a., dispatch table) is full.

Returns
True if the listener list (dispatch table) is full, false if not.

§ numListeners()

int EventManager::numListeners ( )

Get the number of listeners in the dispatch table.

Returns
The number of entries in the listener list (a.k.a., dispatch table).

§ processAllEvents()

int EventManager::processAllEvents ( )

Processes all the events in the event queues and dispatches them to the corresponding listeners stored in the dispatch table.

Events are taken preferentially from the high priority queue. If the high priority queue is empty, then events are taken from the low prioirty queue.

All listeners associated with the event that are enabled will be called. Disabled listeners are not called.

The event processed is removed from the event queue (even if there is no listener to handle it).

Note
If interrupts or other asynchronous processes are adding events as they are being processed, this function might not return for a long time. If events are added as quickly as this function processes them, this function will never return. .
Returns
The number of event handlers called.

§ processEvent()

int EventManager::processEvent ( )

Processes one event from the event queue and dispatches it to the corresponding listeners stored in the dispatch table.

Events are taken preferentially from the high priority queue. If the high priority queue is empty, then events are taken from the low prioirty queue.

All listeners associated with the event that are enabled will be called. Disabled listeners are not called.

The event processed is removed from the event queue (even if there is no listener to handle it).

Note
This function should be called regularly to keep the event queues from getting full. This is usually done by calling it inside an event processing loop.
Returns
The number of event handlers called.

§ queueEvent()

bool EventManager::queueEvent ( int  eventCode,
int  eventParam,
EventPriority  pri = kLowPriority 
)

Tries to add an event into the event queue.

  • eventCode identifies the event to be added.
  • eventParam an integer parameter associated with this event.
  • pri specifies which queue gets the event: kLowPriority or kHighPriority. Defaults to kLowPriority.
Returns
True if successful; false if the queue is full and the event cannot be added.

§ removeListener() [1/2]

bool EventManager::removeListener ( int  eventCode,
EventListener  listener 
)

Remove this (event, listener) pair from the dispatch table. Other listener pairs with the same function or event code will not be affected.

  • eventCode the event code of the (event, listener) pair to be removed.
  • listener the listener of the (event, listener) pair to be removed.
Returns
True if the (event, listener) pair is successfully removed, false otherwise.

§ removeListener() [2/2]

int EventManager::removeListener ( EventListener  listener)

Remove all occurrances of a listener from the dispatch table, regardless of the event code. returns number removed.

This function is useful when one listener handles many different events.

  • listener the listener to be removed.
Returns
The number of entries removed from the dispatch table.

§ setDefaultListener()

bool EventManager::setDefaultListener ( EventListener  listener)

Set a default listener. The default listener is a callback function that is called when an event with no listener is processed.

  • listener the listener to be set as the default listener.
Returns
True if the default listener is successfully installed, false if listener is null.