Skip to content

Commit

Permalink
Merge pull request #45 from FlavorJ/add-success
Browse files Browse the repository at this point in the history
Success!
  • Loading branch information
ladyada authored Jan 5, 2021
2 parents ef3f6a0 + f02905a commit bb528c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 19 additions & 9 deletions Adafruit_INA219.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int16_t Adafruit_INA219::getBusVoltage_raw() {

Adafruit_BusIO_Register bus_voltage_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_BUSVOLTAGE, 2, MSBFIRST);
bus_voltage_reg.read(&value);
_success = bus_voltage_reg.read(&value);

// Shift to the right 3 to drop CNVR and OVF and multiply by LSB
return (int16_t)((value >> 3) * 4);
Expand All @@ -86,7 +86,7 @@ int16_t Adafruit_INA219::getShuntVoltage_raw() {
uint16_t value;
Adafruit_BusIO_Register shunt_voltage_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_SHUNTVOLTAGE, 2, MSBFIRST);
shunt_voltage_reg.read(&value);
_success = shunt_voltage_reg.read(&value);
return value;
}

Expand All @@ -108,7 +108,7 @@ int16_t Adafruit_INA219::getCurrent_raw() {
// Now we can safely read the CURRENT register!
Adafruit_BusIO_Register current_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CURRENT, 2, MSBFIRST);
current_reg.read(&value);
_success = current_reg.read(&value);
return value;
}

Expand All @@ -130,7 +130,7 @@ int16_t Adafruit_INA219::getPower_raw() {
// Now we can safely read the POWER register!
Adafruit_BusIO_Register power_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_POWER, 2, MSBFIRST);
power_reg.read(&value);
_success = power_reg.read(&value);
return value;
}

Expand Down Expand Up @@ -262,7 +262,7 @@ void Adafruit_INA219::setCalibration_32V_2A() {
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
_success = config_reg.write(config, 2);
}

/*!
Expand All @@ -277,9 +277,9 @@ void Adafruit_INA219::powerSave(bool on) {
Adafruit_BusIO_RegisterBits mode_bits =
Adafruit_BusIO_RegisterBits(&config_reg, 3, 0);
if (on) {
mode_bits.write(INA219_CONFIG_MODE_POWERDOWN);
_success = mode_bits.write(INA219_CONFIG_MODE_POWERDOWN);
} else {
mode_bits.write(INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS);
_success = mode_bits.write(INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS);
}
}

Expand Down Expand Up @@ -373,7 +373,7 @@ void Adafruit_INA219::setCalibration_32V_1A() {
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
_success = config_reg.write(config, 2);
}

/*!
Expand Down Expand Up @@ -465,5 +465,15 @@ void Adafruit_INA219::setCalibration_16V_400mA() {

Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
_success = config_reg.write(config, 2);
}

/*!
* @brief Provides the the underlying return value from the last operation
* called on the device.
* @return true: Last operation was successful false: Last operation failed
* @note For function calls that have intermediary device operations,
* e.g. calibration before read/write, only the final operation's
* result is stored.
*/
bool Adafruit_INA219::success() { return _success; }
3 changes: 3 additions & 0 deletions Adafruit_INA219.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,13 @@ class Adafruit_INA219 {
float getCurrent_mA();
float getPower_mW();
void powerSave(bool on);
bool success();

private:
Adafruit_I2CDevice *i2c_dev = NULL;

bool _success;

uint8_t ina219_i2caddr = -1;
uint32_t ina219_calValue;
// The following multipliers are used to convert raw current and power
Expand Down

0 comments on commit bb528c4

Please sign in to comment.