-
Notifications
You must be signed in to change notification settings - Fork 1
/
ads1110.c
executable file
·113 lines (85 loc) · 2.72 KB
/
ads1110.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
/*
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/>.
*/
#include "ads1110.h"
#include <time.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "def.h"
extern int g_debug;
extern FILE *fp_console;
int ads1110_open(t_ads1110 *sensor, unsigned char i2c_address)
{
// local variables
int fd;
unsigned char buf[10]={0x00};
// try to open I2C Bus
fd = open("/dev/i2c-1", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
return 1;
}
if (ioctl(fd, I2C_SLAVE, i2c_address) < 0) {
fprintf(stderr, "ioctl error: %s\n", strerror(errno));
sensor->present = 0;
return 1;
}
// Try to read from sensor to check if it present
if (read(fd, buf, 3) != 3)
{
sensor->present = 0;
return (1);
}
if (g_debug > 0) printf("Opened ADS1110 on 0x%x\n", i2c_address);
// assign file handle to sensor object
sensor->fd = fd;
sensor->address = i2c_address;
sensor->present = 1;
return (0);
}
int ads1110_init(t_ads1110 *sensor)
{
// set calibration data
//sensor->voltage_factor = 2.;
ddebug_print("%s @ 0x%x: voltage_factor=%f\n", __func__, sensor->address, sensor->voltage_factor);
return(0);
}
int ads1110_measure(t_ads1110 *sensor)
{
//variables
//struct timespec sample_time;
unsigned char buf[10]={0x00};
//int digoutp;
if (read(sensor->fd, buf, 3) != 3) { // Read back data into buf[]
printf("Unable to read from slave\n");
return(1);
}
sensor->voltage_raw = (buf[0] << 8) + buf[1];
ddebug_print("%s @ 0x%x: voltage_raw=%d\n", __func__, sensor->address, sensor->voltage_raw);
return(0);
}
int ads1110_calculate(t_ads1110 *sensor)
{
sensor->voltage_converted = (sensor->voltage_raw/sensor->voltage_factor);
debug_print("%s @ 0x%x: Voltage: %fV\n", __func__, sensor->address, sensor->voltage_converted);
return (0);
}