11// FILE: INA3221.cpp
22// AUTHOR: Rob Tillaart
3- // VERSION: 0.4.0
3+ // VERSION: 0.4.1
44// DATE: 2024-02-05
55// PURPOSE: Arduino library for the I2C INA3221 3 channel voltage and current sensor.
66// URL: https://github.com/RobTillaart/INA3221_RT
@@ -49,6 +49,7 @@ INA3221::INA3221(const uint8_t address, TwoWire *wire)
4949 _shunt[0 ] = 0.1 ; // Ohm
5050 _shunt[1 ] = 0.1 ; // Ohm
5151 _shunt[2 ] = 0.1 ; // Ohm
52+ _error = 0 ;
5253}
5354
5455bool INA3221::begin ()
@@ -67,7 +68,7 @@ bool INA3221::isConnected()
6768uint8_t INA3221::getAddress ()
6869{
6970 return _address;
70- };
71+ }
7172
7273
7374// //////////////////////////////////////////////////////
@@ -384,6 +385,18 @@ uint16_t INA3221::getDieID()
384385}
385386
386387
388+ // //////////////////////////////////////////////////////
389+ //
390+ // ERROR HANDLING
391+ //
392+ int INA3221::getLastError ()
393+ {
394+ int e = _error;
395+ _error = 0 ;
396+ return e;
397+ }
398+
399+
387400// //////////////////////////////////////////////////////
388401//
389402// PRIVATE
@@ -392,12 +405,25 @@ uint16_t INA3221::_readRegister(uint8_t reg)
392405{
393406 _wire->beginTransmission (_address);
394407 _wire->write (reg);
395- _wire->endTransmission ();
396-
397- _wire->requestFrom (_address, (uint8_t )2 );
398- uint16_t value = _wire->read ();
399- value <<= 8 ;
400- value |= _wire->read ();
408+ int n = _wire->endTransmission ();
409+ if (n != 0 )
410+ {
411+ _error = -1 ;
412+ return 0 ;
413+ }
414+
415+ uint16_t value = 0 ;
416+ if (2 == _wire->requestFrom (_address, (uint8_t )2 ))
417+ {
418+ value = _wire->read ();
419+ value <<= 8 ;
420+ value |= _wire->read ();
421+ }
422+ else
423+ {
424+ _error = -2 ;
425+ return 0 ;
426+ }
401427 return value;
402428}
403429
@@ -408,7 +434,12 @@ uint16_t INA3221::_writeRegister(uint8_t reg, uint16_t value)
408434 _wire->write (reg);
409435 _wire->write (value >> 8 );
410436 _wire->write (value & 0xFF );
411- return _wire->endTransmission ();
437+ int n = _wire->endTransmission ();
438+ if (n != 0 )
439+ {
440+ _error = -1 ;
441+ }
442+ return n;
412443}
413444
414445
0 commit comments