Please find below link and source to an event library:
http://avasig.com/clsEvent.zip
Prototype, clsEvent.h
/**
* File:
* clsEvent.h
*
* Notes:
* This file contains the prototype for the class clsEvent.
*
* Methods:
* clsEvent Class constructor
* debug Enable or disable debugging msgs on serial O/P
* getName Returns the name assigned to this event
* serviceEvents Should be called in main loop to service events
*
* Members:
* m_pTest Test callback should return true if event should occur
* m_pCallback Call-back routine to call when event test expression is true
* m_pData Data to associate with this instance
* m_pstrName The name assigned to this event
* m_pNext Next event in Linked-list
*
* History:
* 29/01/2012 Created by Simon Platten
*/
#ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H
#include <Arduino.h>
class clsEvent {
private:
char* m_pstrName;
bool (*m_pTest)(clsEvent* pEvent);
void (*m_pCallback)(clsEvent* pEvent);
void* m_pData;
clsEvent* m_pNext;
public:
static void debug(bool blnDebug);
static void serviceEvents();
public:
clsEvent(char* pstrName,
bool (*pTest)(clsEvent* pEvent),
void (*pCallback)(clsEvent* pEvent),
void *pData = NULL);
char* getName();
};
#endif
Implementation, clsEvent.cpp
/** /**
* File:
* clsEvent.cpp
*
* Notes:
* This file contains the implementation of the class clsEvent.
*
* History:
* 29/01/2012 Created by Simon Platten
*/
#include "clsEvent.h"
// Pointer to all events linked list
static clsEvent* pAllEvents = NULL;
static bool blnDebugFlag = false;
/**
* Event constructor
*
* Paramters:
* pstrName, A description of this event
* pTest, callback routine to test if event should occur
* pCallback, the routine to call when the timer expires
* pData, pointer to data block accessible for this timer
*/
clsEvent::clsEvent(char* pstrName,
bool (*pTest)(clsEvent* pEvent),
void (*pCallback)(clsEvent* pEvent),
void *pData) {
// Configure this event
m_pstrName = pstrName;
m_pTest = pTest;
m_pCallback = pCallback;
m_pData = pData;
if ( pAllEvents != NULL ) {
// Insert this event into the list of all events
m_pNext = (clsEvent*)pAllEvents;
} else {
// This is the first node in the list, ensure that there is no Next node
m_pNext = NULL;
}
// Insert this timer at the beginning of the linked list
pAllEvents = this;
}
/**
* Enable or disable debugging msgs on serial O/P
*/
void clsEvent::debug(bool blnDebug) {
blnDebugFlag = blnDebug;
}
/**
* Returns the name assigned to this event
*/
char* clsEvent::getName() {
return m_pstrName;
}
/**
* Should be called in main loop to service events
*/
void clsEvent::serviceEvents() {
for( clsEvent* pEvent=pAllEvents; pEvent!=NULL;
pEvent=pEvent->m_pNext ) {
if ( (*pEvent->m_pTest)(pEvent) == true ) {
if ( blnDebugFlag == true ) {
Serial.print(pEvent->getName());
Serial.println(" is true!");
}
(*pEvent->m_pCallback)(pEvent);
}
}
}
The idea behind this class is simple, create your own event using:
new clsEvent(pstrName, pTest, pCallback, pData);
pstrName is a text string that describes the event, used only when debugging enabled, to show output on serial port
pTest, is a pointer to a test function, prototype: bool (pTest)(clsEvent pEvent)
pCallback, is a pointer to a function to call when the result of pTest is true, prototype: void (pCallback)(clsEvent pEvent)
pData, is an option pointer to your own data structure that you can reference via the pEvent pointer passed to your callback.
I am using this to register a test for a push button, the test routine debounces the button and returns true or false, my call back routine then performs an action.
Forgot to mention, you will also need to call the static routine:
clsEvent::serviceEvents();
From within you loop.