Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2105 - 1
#include "main.h"
2
#include "timer.h"
3
 
4
 
5
struct timeval startTimeSec;            //Set at every start
6
struct timeval travelTime;
7
struct timeval intermitTime;
8
u8 sec_handler;
9
u8 msec_handler;
10
u16 time_in_msec;
11
 
12
//>> Call for intervals in Seconds
13
//------------------------------------------------------------------------------------------------------
14
u8 interval_in_sec(u8 number_of_seconds){
15
        if(sec_handler){
16
                gettimeofday(&travelTime, NULL);
17
                if(!((travelTime.tv_sec - startTimeSec.tv_sec) % number_of_seconds)){
18
                        return(1);
19
                }else{return(0);}
20
        }else{return(0);}
21
}
22
 
23
//>> Call for intervals in Milliseconds
24
//------------------------------------------------------------------------------------------------------
25
u8 interval_in_msec(u16 number_of_milliseconds){
26
        if(msec_handler)
27
        {
28
                if(!(time_in_msec % number_of_milliseconds)){
29
                        return(1);
30
                }else{return(0);}
31
        }else{return(0);}
32
}
33
 
34
//>> Reset Timer for every loop cycle
35
//------------------------------------------------------------------------------------------------------
36
u16 msec_time_compare1;
37
u16 msec_time_compare2;
38
void reset_timer(){
39
        gettimeofday(&intermitTime, NULL);
40
 
41
        msec_time_compare1 = intermitTime.tv_usec/1000;
42
        if(!(msec_time_compare1 == msec_time_compare2)){
43
                time_in_msec++;
44
                msec_handler = 1;
45
        }else{
46
                msec_handler = 0;
47
        }
48
        if(time_in_msec == 10001){
49
                time_in_msec = 1;
50
        }
51
        msec_time_compare2 = intermitTime.tv_usec/1000;
52
 
53
        if(!(intermitTime.tv_sec == travelTime.tv_sec)){
54
                sec_handler = 1;
55
        }else{
56
                sec_handler = 0;
57
        }
58
}
59
 
60
//>> Initialize Timer, store reference Time
61
//------------------------------------------------------------------------------------------------------
62
void timer_init(){
63
        gettimeofday(&startTimeSec, NULL);
64
        sec_handler = 1;
65
        msec_handler = 1;
66
        time_in_msec = 0;
67
 
68
}