25 #include <i2c/smbus.h>
26 #include <linux/i2c-dev.h>
27 #include <sys/ioctl.h>
41 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return false; }
60 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return false; }
63 if (res < 0) {
return -1; }
78 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return NAN; }
82 if (res < 0) {
return NAN; }
86 if ((sres & 0x8000)) {
87 uint16_t t = (sres & 0x7FFF) + 1;
90 shuntV = (sres & 0x7FFF) * 1E-2;
93 if (shuntV > 320.0 || shuntV < -320.0) {
return NAN; }
97 if ((t < o->min) || (t > o->
max)) {
return NAN; }
116 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return NAN; }
119 int32_t res = i2c_smbus_read_word_data(busHandle,
INA219_REG_BUS);
120 if (res < 0) {
return NAN; }
123 uint8_t flags = (sres & 0x03);
124 if ((flags & 0x01) || !(flags & 0x02)) {
return NAN; }
126 float busV = (sres >> 3) * 4E-3;
130 if ((t < o->min) || (t > o->
max)) {
return NAN; }
149 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return NAN; }
153 if (res < 0) {
return NAN; }
155 float power = sres * 2E-3;
159 if ((t < o->min) || (t > o->
max)) {
return NAN; }
174 if (ioctl(busHandle, I2C_SLAVE, devAddr) < 0) {
return NAN; }
178 if (res < 0) {
return NAN; }
181 if ((sres & 0x8000)) {
182 uint16_t t = ~(sres & 0x7FFF) + 1;
185 current = (sres & 0x7FFF) * 1E-4;
190 if ((t < o->min) || (t > o->
max)) {
return NAN; }
int16_t i2c_swapbytes(const int16_t in)
Swap word byte order.
float i2c_ina219_read_shuntVoltage(const int busHandle, const int devAddr, const void *opts)
Get voltage across the shunt resistor in millivolts.
bool i2c_ina219_configure(const int busHandle, const int devAddr)
Send configuration command to the specified device.
float i2c_ina219_read_current(const int busHandle, const int devAddr, const void *opts)
Get current flow through shunt resistor in amps.
float i2c_ina219_read_busVoltage(const int busHandle, const int devAddr, const void *opts)
Get bus voltage (at V- terminal) in volts.
uint16_t i2c_ina219_read_configuration(const int busHandle, const int devAddr)
Read configuration from device.
float i2c_ina219_read_power(const int busHandle, const int devAddr, const void *opts)
Get power consumption in watts.
#define INA219_CONFIG_RESET
#define INA219_CONFIG_DEF
Default sensor configuration for this library.
#define INA219_REG_SHUNT
INA219 shunt voltage register.
#define INA219_REG_CALIBRATION
INA219 calibration registor.
#define INA219_REG_CONFIG
INA219 configuration register.
#define INA219_REG_BUS
INA219 bus voltage register.
#define INA219_REG_POWER
INA219 calculated power register.
#define INA219_REG_CURRENT
INA219 current register.
float offset
Add this amount to received value.
float max
If not NaN, largest value considered valid.
float scale
Scale received value by this quantity.