26 #include <i2c/smbus.h>
27 #include <linux/i2c-dev.h>
28 #include <sys/ioctl.h>
30 #include "I2C-ADS1015.h"
41 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return false; }
43 int32_t res = i2c_smbus_read_word_data(busHandle, ADS1015_REG_CONFIG);
44 if (res < 0) {
return -1; }
56 uint16_t pga = (config & ADS1015_CONFIG_PGA_SELECT);
57 if (pga == ADS1015_CONFIG_PGA_6144MV) {
59 }
else if (pga == ADS1015_CONFIG_PGA_4096MV) {
61 }
else if (pga == ADS1015_CONFIG_PGA_2048MV) {
63 }
else if (pga == ADS1015_CONFIG_PGA_1024MV) {
65 }
else if (pga == ADS1015_CONFIG_PGA_0512MV) {
67 }
else if (pga == ADS1015_CONFIG_PGA_0256MV) {
69 }
else if (pga == ADS1015_CONFIG_PGA_0256MV2) {
71 }
else if (pga == ADS1015_CONFIG_PGA_0256MV3) {
93 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return NAN; }
95 uint16_t pga = ADS1015_CONFIG_PGA_DEFAULT;
96 float min = -INFINITY;
113 uint16_t confClear = (ADS1015_CONFIG_DEFAULT & ADS1015_CONFIG_MUX_CLEAR & ADS1015_CONFIG_PGA_CLEAR);
114 uint16_t muxToSet = (mux & ADS1015_CONFIG_MUX_SELECT);
115 uint16_t pgaToSet = (pga & ADS1015_CONFIG_PGA_SELECT);
124 if (i2c_smbus_write_word_data(
125 busHandle, ADS1015_REG_CONFIG,
126 (uint16_t)
i2c_swapbytes(confClear | muxToSet | pgaToSet | ADS1015_CONFIG_STATE_CONVERT)) < 0) {
133 int32_t res = i2c_smbus_read_word_data(busHandle, ADS1015_REG_RESULT);
134 if (res < 0) {
return NAN; }
137 sres = (sres & 0xFFF0) >> 4;
142 if ((sres & 0x800)) {
143 uint16_t t = (sres & 0x7FF) + 1;
146 adcV = (sres & 0x7FF) * sv;
149 adcV = scale * adcV + offset;
150 if ((adcV < min) || (adcV > max)) { adcV = NAN; }
int16_t i2c_swapbytes(const int16_t in)
Swap word byte order.
float i2c_ads1015_read_ch1(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 1.
float i2c_ads1015_read_ch2(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 2.
float i2c_ads1015_read_mux(const int busHandle, const int devAddr, const uint16_t mux, const i2c_ads1015_options *opts)
Generic ADS1015 read function.
float i2c_ads1015_pga_to_scale_factor(const uint16_t config)
Get number of millivolts represented by LSB at a given PGA value.
float i2c_ads1015_read_diff_ch0_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 0 and 3.
float i2c_ads1015_read_diff_ch0_ch1(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 0 and 1.
float i2c_ads1015_read_ch0(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 0.
float i2c_ads1015_read_ch3(const int busHandle, const int devAddr, const void *opts)
Get single-ended voltage measurement from channel 3.
float i2c_ads1015_read_diff_ch2_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 2 and 3.
uint16_t i2c_ads1015_read_configuration(const int busHandle, const int devAddr)
Read configuration from device.
float i2c_ads1015_read_diff_ch1_ch3(const int busHandle, const int devAddr, const void *opts)
Get differential voltage measurement between channels 1 and 3.
float offset
Add this amount to received value.
float scale
Scale received value by this quantity.
float max
If not NaN, largest value considered valid.
float min
If not NaN, smallest value considered valid.