During coding for my project I found a possible bug in the ISR handler from TIMER2_OVF_vect. It might be harmless in current code design, but it may create strange bugs for persons that add their own Eventfunctions:
Code:
for(byte eventID = 0; eventID < EVENTFUNC_SIZE; eventID++){
if(eventFuncCounts[eventID]!= 0){
eventFuncCounts[eventID]--;
if(eventFuncCounts[eventID] == 0)
eventFuncs[eventID]();
}
}
eventFuncCounts is an unsigned int array width no inital values. So, what does this function do?
First, because of missing initialization of eventFuncCounts, it will fire each (!) eventFuncs[eventID]() at a random time after startup => not very nice.
Second, after eventFuncCountss[eventID] reaches 0, it will fire the event every ms until a new time is set by addevent => even worse!
Idea for improvement widthout affecting other existing code:
Let eventFuncCounts[eventID] roll over after reaching 0 and define 65535 as "event execution disabled".
Code:
for(byte eventID = 0; eventID < EVENTFUNC_SIZE; eventID++){
if(eventFuncCounts[eventID]!= 65535){
eventFuncCounts[eventID]--;
if(eventFuncCounts[eventID] == 0)
eventFuncs[eventID]();
}
}
Second, initialize eventFuncCounts array with all = 65535. First step in setup() should be:
Code:
for(byte eventID = 0; eventID < EVENTFUNC_SIZE; eventID++){
eventFuncCounts[eventID] = 65535;
}
Nice side effet: Now you can cancel an already scheduled event by doing addEvent(ACTION_ID, 65535);
Edit: Mistake from my side, it will not execute an event more than once.. But rest of it may be useful
