Skip to content

Commit 186a256

Browse files
committed
0.3.0 INA228
1 parent 22e5744 commit 186a256

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

libraries/INA228/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.0] - 2025-03-08
10+
- redo #20, INA.getEnergy => keep it UNsigned. Broken in 0.2.0
11+
12+
----
13+
914
## [0.2.0] - 2025-03-05
1015
- fix #20, INA.getCharge => make it signed.
1116
- add **getLastError()** low level (I2C) error handling

libraries/INA228/INA228.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
{

libraries/INA228/INA228.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA228.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.0
4+
// VERSION: 0.3.0
55
// DATE: 2024-05-09
66
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA228
@@ -16,7 +16,7 @@
1616
#include "Wire.h"
1717

1818

19-
#define INA228_LIB_VERSION (F("0.2.0"))
19+
#define INA228_LIB_VERSION (F("0.3.0"))
2020

2121

2222
// for setMode() and getMode()
@@ -250,8 +250,8 @@ class INA228
250250
private:
251251
// max 4 bytes
252252
uint32_t _readRegister(uint8_t reg, uint8_t bytes);
253-
// always 5 bytes
254-
double _readRegisterF(uint8_t reg);
253+
// always 5 bytes, mode == U ==> unsigned, otherwise signed
254+
double _readRegisterF(uint8_t reg, char mode);
255255
uint16_t _writeRegister(uint8_t reg, uint16_t value);
256256

257257
float _current_LSB;

libraries/INA228/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Finally the INA228 has an **energy** and **charge** register.
3535
These are values accumulated over time, and only work in continuous mode.
3636
(to be investigated what those mean ).
3737

38-
The INA228 also provides an ALERT line, to generate an interrupt
38+
The INA228 also provides an **ALERT** line, to generate an interrupt
3939
in case a predefined threshold has been met.
4040
This can be an under- or over-voltage, temperature or power limit.
4141
The library does not handle these interrupts.
@@ -46,11 +46,13 @@ The library is limited tested and verified with hardware.
4646

4747
Feedback as always is welcome.
4848

49-
### Breaking change 0.2.0
49+
50+
### Breaking change 0.3.0
5051

5152
The function **getCharge()** is updated as the value can be negative too.
5253
The previous versions assumed it could only be positive. See #22.
53-
These are obsolete now.
54+
0.3.0 fixed the **getEnergy()** which only can have positive values
55+
and was broken in 0.2.0. Versions before 0.3.0 are obsolete now.
5456

5557

5658
### Details

libraries/INA228/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ getManufacturer KEYWORD2
109109
getDieID KEYWORD2
110110
getRevision KEYWORD2
111111

112+
getLastError KEYWORD2
112113

113114
# Constants (LITERAL1)
114115
INA228_LIB_VERSION LITERAL1

libraries/INA228/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/INA228.git"
1717
},
18-
"version": "0.2.0",
18+
"version": "0.3.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/INA228/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=INA228
2-
version=0.2.0
2+
version=0.3.0
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.

0 commit comments

Comments
 (0)