-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimers.c
86 lines (73 loc) · 2.31 KB
/
timers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include "timers.h"
static struct sigaction sa;
static struct itimerval timer;
void (*mainTimerHandler)(int sig );
uint8_t timers_initialize( void(*handlerfptr)(int sig ))
{
mainTimerHandler=handlerfptr;
return 0;
}
uint8_t timers_create_timer( int expireS, int intervalS, int expireUS, int intervalUS)
{
/*
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
// Set up signal handler.
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = mainTimerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
fprintf(stderr, "Failed to setup signal handling for %s.\n", name);
return -2;
}
// Set and enable alarm
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
its.it_interval.tv_sec = intervalS;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = expireS;
its.it_value.tv_nsec = 0;
timer_settime(*timerID, 0, &its, NULL);
*/
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset(&sa, 0, sizeof (sa));
sa.sa_handler = mainTimerHandler;
sigaction (SIGALRM, &sa, NULL);
/* Configure the timer to expire after 250 msec... */
timer.it_value.tv_sec = expireS;
timer.it_value.tv_usec = expireUS;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = intervalUS;
timer.it_interval.tv_usec = intervalS;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_REAL, &timer, NULL);
return 0;
}
uint8_t timers_cancel_timer(void)
{
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = mainTimerHandler;
sigaction (SIGALRM, NULL, NULL);
/* Configure the timer to expire after 250 msec... */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 0;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_REAL, NULL, NULL);
//alarm(0);
return 0;
}