forked from Openvario/sensord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
147 lines (119 loc) · 3.6 KB
/
timer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
sensord - Sensor Interface for XCSoar Glide Computer - http://www.openvario.org/
Copyright (C) 2014 The openvario project
A detailed list of copyright holders can be found in the file "AUTHORS"
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*
//Set the timer
timer_t SetTimer(int signo, int sec, int mode)
{
struct sigevent sigev;
timer_t timerid;
struct itimerspec itval;
struct itimerspec oitval;
// Create the POSIX timer to generate signo
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = signo;
sigev.sigev_value.sival_ptr = &timerid;
//Create the Timer using timer_create signal
if (timer_create(CLOCK_REALTIME, &sigev, &timerid) == 0)
{
itval.it_value.tv_sec = sec / 1000;
itval.it_value.tv_nsec = (long)(sec % 1000) * (1000000L);
if (mode == 1)
{
itval.it_interval.tv_sec = itval.it_value.tv_sec;
itval.it_interval.tv_nsec = itval.it_value.tv_nsec;
}
else
{
itval.it_interval.tv_sec = 0;
itval.it_interval.tv_nsec = 0;
}
//Set the Timer when to expire through timer_settime
if (timer_settime(timerid, 0, &itval, &oitval) != 0)
{
perror("time_settime error!");
}
}
else
{
perror("timer_create error!");
return -1;
}
return timerid;
}
*/
/*
void SignalHandler(int signo, siginfo_t * info, void *context)
{
unsigned char buf[50];
//printf("wake\n");
if (signo == MEASTIMER)
{
switch(state)
{
case sample_d1:
// start conversion
buf[0] = 0x48; // This is the register we want to read from
if ((write(fd, buf, 1)) != 1) { // Send register we want to read from
printf("Error writing to i2c slave\n");
exit(1);
}
state = get_d1;
break;
case get_d1:
// read result
buf[0] = 0x00;
if ((write(fd, buf, 1)) != 1) { // Send register we want to read from
printf("Error writing to i2c slave\n");
exit(1);
}
if (read(fd, buf, 3) != 3) { // Read back data into buf[]
printf("Unable to read from slave\n");
exit(1);
}
static_sensor.D1 = buf[0] * 65536 + buf[1] * 256 + buf[2];
printf("D1 = %lu\n", static_sensor.D1);
state = sample_d2;
break;
case sample_d2:
// start conversion
buf[0] = 0x58; // This is the register we want to read from
if ((write(fd, buf, 1)) != 1) { // Send register we want to read from
printf("Error writing to i2c slave\n");
exit(1);
}
state = get_d2;
break;
case get_d2:
// read result
buf[0] = 0x00;
if ((write(fd, buf, 1)) != 1) { // Send register we want to read from
printf("Error writing to i2c slave\n");
exit(1);
}
if (read(fd, buf, 3) != 3) { // Read back data into buf[]
printf("Unable to read from slave\n");
exit(1);
}
static_sensor.D2 = buf[0] * 65536 + buf[1] * 256 + buf[2];
printf("D2 = %lu\n", static_sensor.D2);
state = sample_d1;
break;
default:
break;
}
}
}
*/