11// FILE: INA228.cpp
22// AUTHOR: Rob Tillaart
3- // VERSION: 0.2 .0
3+ // VERSION: 0.3 .0
44// DATE: 2024-05-09
55// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
66// URL: https://github.com/RobTillaart/INA228
@@ -162,7 +162,7 @@ double INA228::getEnergy()
162162 // read 40 bit UNSIGNED as a double to prevent 64 bit integers
163163 // double might be 8 or 4 byte, depends on platform
164164 // 40 bit ==> O(10^12)
165- double value = _readRegisterF (INA228_ENERGY);
165+ double value = _readRegisterF (INA228_ENERGY, ' U ' );
166166 // PAGE 31 (8.1.2)
167167 return value * (16 * 3.2 ) * _current_LSB;
168168}
@@ -174,7 +174,7 @@ double INA228::getCharge()
174174 // read 40 bit SIGNED as a float to prevent 64 bit integers
175175 // double might be 8 or 4 byte, depends on platform
176176 // 40 bit ==> O(10^12)
177- double value = _readRegisterF (INA228_CHARGE);
177+ double value = _readRegisterF (INA228_CHARGE, ' S ' );
178178 // PAGE 32 (8.1.2)
179179 return value * _current_LSB;
180180}
@@ -598,7 +598,7 @@ uint32_t INA228::_readRegister(uint8_t reg, uint8_t bytes)
598598
599599
600600// always 5 bytes
601- double INA228::_readRegisterF (uint8_t reg)
601+ double INA228::_readRegisterF (uint8_t reg, char mode )
602602{
603603 _error = 0 ;
604604 _wire->beginTransmission (_address);
@@ -611,28 +611,23 @@ double INA228::_readRegisterF(uint8_t reg)
611611 }
612612
613613 double value = 0 ;
614-
615- int32_t ival = 0 ;
616614 if (5 == _wire->requestFrom (_address, (uint8_t )5 ))
617615 {
616+ uint32_t val = 0 ;
618617 // fetch 4 MSB bytes first.
619618 for (int i = 0 ; i < 4 ; i++)
620619 {
621- ival <<= 8 ;
622- ival |= _wire->read ();
620+ val <<= 8 ;
621+ val |= _wire->read ();
623622 }
624- value = ival;
623+ // handle signed / unsigned by casting.
624+ if (mode == ' U' ) value = val;
625+ else value = (int32_t ) val;
626+ // process last byte
625627 value *= 256 ;
626628 // note: mar05c
627629 uint8_t n = _wire->read ();
628630 value += n;
629-
630- // ORG
631- // for (int i = 0; i < bytes; i++)
632- // {
633- // value *= 256.0;
634- // value += _wire->read();
635- // }
636631 }
637632 else
638633 {
0 commit comments