AnsweredAssumed Answered

How to adjust a timer interval?

Question asked by Angelo Quattrociocchi on Aug 20, 2014
Latest reply on Oct 4, 2016 by Norberto Jiménez

I have a timer running that was started using _timer_start_periodic_every, and I want to be able to make one-time adjustments to the interval, effectively causing the next event to occur early or late by a certain amount.  Following that it should continue at the originally specified interval.  I don't see an obvious way to do that through the MQX functions, other than canceling and restarting the timer.  Is there a better way to do this?

 

[Edit] Also, I tried doing this (cancel and restart), and it's not working.  I get a success return code when cancelling, but the timer keeps running so after starting a new one (for which I get a different id) I have two running.  It eventually stops working (presumably when I run out of stack space).

 

void restartTimer(uint16_t offsetMs)
{
    TIME_STRUCT offsetTime;
    
    offsetTime.SECONDS = 0;
    offsetTime.MILLISECONDS = offsetMs;
    
    //cancel current timer
    printf("\nCancelling timer id %d, result %d\n", oneHz_timer, _timer_cancel(oneHz_timer));
    
    //restart timer at a new offset
    oneHz_timer = _timer_start_periodic_at(timer1sEvent, 0, TIMER_ELAPSED_TIME_MODE, &offsetTime, 1000);
    
    printf("Timer restarted at %dms, timer id %d...\n", offsetMs, oneHz_timer);
}

 

The result is this:

Cancelling timer id 2, result 0

Timer restarted at 1000ms, timer id 3...

 

I am calling this from the same thread that started the timer initially.

Outcomes