10ina219_drv::ina219_drv(
i2c_interface & i2c, uint8_t i2c_addr) : _i2c(i2c), _i2c_addr(i2c_addr){
16void ina219_drv::setConfig(
bool reset, INA219::BG busgain, INA219::PG powergain, INA219::ADC busadc, INA219::ADC shuntadc, INA219::MODE mode) {
19 conf |= busgain << 13;
20 conf |= powergain << 11;
22 conf |= shuntadc << 3;
24 writeRegister(INA219::REG_CONF, conf);
26 this->busgain = busgain;
27 this->shuntgain = shuntgain;
28 this->busadc = busadc;
29 this->shuntadc = shuntadc;
37void ina219_drv::setCalibration(
unsigned int maxCurrent,
float shuntR) {
38 currentLSB = (maxCurrent / 32767.);
39 calibration = (uint16_t)(40.96 / (currentLSB * shuntR));
40 writeRegister(INA219::REG_CAL, calibration);
46float ina219_drv::getShuntVoltage(){
47 int16_t raw = (int16_t)readRegister(INA219::REG_SHUNT_V);
55float ina219_drv::getBusVoltage(){
56 uint16_t raw = readRegister(INA219::REG_BUS_V);
61 return INA219::OVERFLOW;
64 float result = (int16_t)((raw >> 3) * 4);
73float ina219_drv::getCurrent() {
74 int16_t raw = (int16_t)readRegister(INA219::REG_CURRENT);
75 return currentLSB * raw;
78void ina219_drv::writeRegister(uint8_t reg, uint16_t value){
81 txbuf[1] = value >> 8;
82 txbuf[2] = value & 0xFF;
83 _i2c.i2cWrite(_i2c_addr, txbuf, 3);
86uint16_t ina219_drv::readRegister(uint8_t reg){
87 uint8_t txbuf[] = { reg };
89 _i2c.i2cWrite(_i2c_addr, txbuf, 1);
90 _i2c.i2cRead (_i2c_addr, rxbuf, 2);
91 return ((rxbuf[0] << 8) | rxbuf[1]);