Skip to content

Commit 4c664fa

Browse files
committed
0.4.0 INA229
1 parent 186a256 commit 4c664fa

File tree

7 files changed

+62
-25
lines changed

7 files changed

+62
-25
lines changed

libraries/INA229/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ 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.4.0] - 2025-03-08
10+
- redo #8, INA.getEnergy => keep it UNsigned. Broken in 0.3.0
11+
- update keywords.txt
12+
13+
----
14+
915
## [0.3.0] - 2025-03-05
1016
- fix #8,INA.getCharge => make it signed.
1117

libraries/INA229/INA229.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA229.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.3.0
3+
// VERSION: 0.4.0
44
// DATE: 2025-01-22
55
// PURPOSE: Arduino library for the INA229, SPI, 20 bit, voltage, current and power sensor.
66
// URL: https://github.com/RobTillaart/INA229
@@ -190,10 +190,10 @@ float INA229::getTemperature()
190190
// PAGE 24 + 8.1.2
191191
double INA229::getEnergy()
192192
{
193-
// read 40 bit unsigned as a double to prevent 64 bit integers
193+
// read 40 bit UNSIGNED as a double to prevent 64 bit integers
194194
// double might be 8 or 4 byte, depends on platform
195195
// 40 bit ==> O(10^12)
196-
double value = _readRegisterF(INA229_ENERGY);
196+
double value = _readRegisterF(INA229_ENERGY, 'U');
197197
// PAGE 31 (8.1.2)
198198
return value * (16 * 3.2) * _current_LSB;
199199
}
@@ -202,10 +202,10 @@ double INA229::getEnergy()
202202
// PAGE 24 + 8.1.2
203203
double INA229::getCharge()
204204
{
205-
// read 40 bit unsigned as a float to prevent 64 bit integers
205+
// read 40 bit SIGNED as a float to prevent 64 bit integers
206206
// double might be 8 or 4 byte, depends on platform
207207
// 40 bit ==> O(10^12)
208-
double value = _readRegisterF(INA229_CHARGE);
208+
double value = _readRegisterF(INA229_CHARGE, 'S');
209209
// PAGE 32 (8.1.2)
210210
return value * _current_LSB;
211211
}
@@ -650,25 +650,27 @@ uint32_t INA229::_readRegister(uint8_t reg, uint8_t bytes) // bytes = 2 or 3.
650650
}
651651

652652

653-
double INA229::_readRegisterF(uint8_t reg)
653+
double INA229::_readRegisterF(uint8_t reg, char mode)
654654
{
655655
// Dedicated SPI code
656656
double value = 0;
657-
int32_t ival = 0;
658657
uint8_t addr = (reg << 2) + 1; // 1 = Read flag. P18 datasheet
659658

660659
digitalWrite(_select, LOW);
661660
if (_hwSPI)
662661
{
663662
_mySPI->beginTransaction(_spi_settings);
664-
ival = _mySPI->transfer(addr);
663+
uint32_t val = _mySPI->transfer(addr);
665664
// fetch 4 MSB bytes
666665
for (int i = 0; i < 4; i++)
667666
{
668-
ival <<= 8;
669-
ival |= _mySPI->transfer(0x00);
667+
val <<= 8;
668+
val |= _mySPI->transfer(0x00);
670669
}
671-
value = ival;
670+
// handle signed / unsigned by casting.
671+
if (mode == 'U') value = val;
672+
else value = (int32_t) val;
673+
// process last byte
672674
value *= 256;
673675
// fetch last LSB byte
674676
uint8_t n = _mySPI->transfer(0x00);
@@ -677,14 +679,17 @@ double INA229::_readRegisterF(uint8_t reg)
677679
}
678680
else // Software SPI
679681
{
680-
ival = swSPI_transfer(addr);
682+
uint32_t val = swSPI_transfer(addr);
681683
// fetch 4 MSB bytes
682684
for (int i = 0; i < 4; i++)
683685
{
684-
ival <<= 8;
685-
ival |= swSPI_transfer(0x00);
686+
val <<= 8;
687+
val |= swSPI_transfer(0x00);
686688
}
687-
value = ival;
689+
// handle signed / unsigned by casting.
690+
if (mode == 'U') value = val;
691+
else value = (int32_t) val;
692+
// process last byte
688693
value *= 256;
689694
// fetch last LSB byte
690695
uint8_t n = swSPI_transfer(0x00);

libraries/INA229/INA229.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA229.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.0
4+
// VERSION: 0.4.0
55
// DATE: 2025-01-22
66
// PURPOSE: Arduino library for the INA229, SPI, 20 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA229
@@ -13,7 +13,7 @@
1313
#include "SPI.h"
1414

1515

16-
#define INA229_LIB_VERSION (F("0.3.0"))
16+
#define INA229_LIB_VERSION (F("0.4.0"))
1717

1818

1919
#ifndef __SPI_CLASS__
@@ -265,8 +265,8 @@ class INA229
265265
private:
266266
// max 4 bytes
267267
uint32_t _readRegister(uint8_t reg, uint8_t bytes);
268-
// 5 bytes or more
269-
double _readRegisterF(uint8_t reg);
268+
// always 5 bytes, mode == U ==> unsigned, otherwise signed
269+
double _readRegisterF(uint8_t reg, char mode);
270270
uint16_t _writeRegister(uint8_t reg, uint16_t value);
271271

272272
float _current_LSB;

libraries/INA229/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ ends in the code, please report them.
4848
Feedback as always is welcome.
4949

5050

51-
### Breaking change 0.3.0
51+
### Breaking change 0.4.0
5252

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

5758

5859
### Update 0.2.0

libraries/INA229/keywords.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ getBusVolt KEYWORD2
1313
getBusMilliVolt KEYWORD2
1414
getBusMicroVolt KEYWORD2
1515

16+
1617
getShuntVoltage KEYWORD2
1718
getShuntVolt KEYWORD2
1819
getShuntMilliVolt KEYWORD2
@@ -24,21 +25,40 @@ getAmpere KEYWORD2
2425
getMilliAmpere KEYWORD2
2526
getMicroAmpere KEYWORD2
2627

28+
2729
getPower KEYWORD2
2830
getWatt KEYWORD2
2931
getMilliWatt KEYWORD2
3032
getMicroWatt KEYWORD2
3133
getKiloWatt KEYWORD2
3234

35+
3336
getTemperature KEYWORD2
3437

3538

36-
#########################
39+
getEnergy KEYWORD2
40+
getJoule KEYWORD2
41+
getMegaJoule KEYWORD2
42+
getKiloJoule KEYWORD2
43+
getMilliJoule KEYWORD2
44+
getMicroJoule KEYWORD2
45+
getWattHour KEYWORD2
46+
getKiloWattHour KEYWORD2
47+
48+
49+
getCharge KEYWORD2
50+
getCoulomb KEYWORD2
51+
getMilliCoulomb KEYWORD2
52+
getMicroCoulomb KEYWORD2
3753

3854

3955
reset KEYWORD2
56+
setAccumulation KEYWORD2
57+
getAccumulation KEYWORD2
4058
setConversionDelay KEYWORD2
4159
getConversionDelay KEYWORD2
60+
setTemperatureCompensation KEYWORD2
61+
getTemperatureCompensation KEYWORD2
4262
setADCRange KEYWORD2
4363
getADCRange KEYWORD2
4464

@@ -88,6 +108,11 @@ getDieID KEYWORD2
88108
getRevision KEYWORD2
89109

90110

111+
setSPIspeed KEYWORD2
112+
getSPIspeed KEYWORD2
113+
usesHWSPI KEYWORD2
114+
115+
91116
# Constants (LITERAL1)
92117
INA229_LIB_VERSION LITERAL1
93118

libraries/INA229/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/INA229.git"
1717
},
18-
"version": "0.3.0",
18+
"version": "0.4.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/INA229/library.properties

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

0 commit comments

Comments
 (0)