EcoModder.com

EcoModder.com (https://ecomodder.com/forum/)
-   OpenGauge / MPGuino FE computer (https://ecomodder.com/forum/opengauge-mpguino-fe-computer.html)
-   -   Small improvement to ISR(TIMER2_OVF_vect) (https://ecomodder.com/forum/showthread.php/small-improvement-isr-timer2_ovf_vect-19080.html)

Sebastian 10-08-2011 04:18 AM

Small improvement to ISR(TIMER2_OVF_vect)
 
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 ;)


All times are GMT -4. The time now is 02:49 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Content Relevant URLs by vBSEO 3.5.2
All content copyright EcoModder.com