Subversion Repositories Projects

Rev

Blame | Last modification | View Log | RSS feed

#include "main.h"
#include "timer.h"


struct timeval startTimeSec;            //Set at every start
struct timeval travelTime;
struct timeval intermitTime;
u8 sec_handler;
u8 msec_handler;
u16 time_in_msec;

//>> Call for intervals in Seconds
//------------------------------------------------------------------------------------------------------
u8 interval_in_sec(u8 number_of_seconds){
        if(sec_handler){
                gettimeofday(&travelTime, NULL);
                if(!((travelTime.tv_sec - startTimeSec.tv_sec) % number_of_seconds)){
                        return(1);
                }else{return(0);}
        }else{return(0);}
}

//>> Call for intervals in Milliseconds
//------------------------------------------------------------------------------------------------------
u8 interval_in_msec(u16 number_of_milliseconds){
        if(msec_handler)
        {
                if(!(time_in_msec % number_of_milliseconds)){
                        return(1);
                }else{return(0);}
        }else{return(0);}
}

//>> Reset Timer for every loop cycle
//------------------------------------------------------------------------------------------------------
u16 msec_time_compare1;
u16 msec_time_compare2;
void reset_timer(){
        gettimeofday(&intermitTime, NULL);

        msec_time_compare1 = intermitTime.tv_usec/1000;
        if(!(msec_time_compare1 == msec_time_compare2)){
                time_in_msec++;
                msec_handler = 1;
        }else{
                msec_handler = 0;
        }
        if(time_in_msec == 10001){
                time_in_msec = 1;
        }
        msec_time_compare2 = intermitTime.tv_usec/1000;

        if(!(intermitTime.tv_sec == travelTime.tv_sec)){
                sec_handler = 1;
        }else{
                sec_handler = 0;
        }
}

//>> Initialize Timer, store reference Time
//------------------------------------------------------------------------------------------------------
void timer_init(){
        gettimeofday(&startTimeSec, NULL);
        sec_handler = 1;
        msec_handler = 1;
        time_in_msec = 0;

}