Go Back   EcoModder Forum > EcoModding > Instrumentation > OpenGauge / MPGuino FE computer
Register Now
 Register Now
 

Reply  Post New Thread
 
Submit Tools LinkBack Thread Tools
Old 10-08-2011, 05:18 AM   #1 (permalink)
EcoModding Lurker
 
Join Date: Oct 2009
Location: Austria
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
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


Last edited by Sebastian; 10-08-2011 at 06:32 AM..
  Reply With Quote
Alt Today
Popular topics

Other popular topics in this forum...

   
Reply  Post New Thread


Thread Tools




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