Skip to content

Commit

Permalink
Fix #8 refactor API (#9)
Browse files Browse the repository at this point in the history
- Fix #8, refactor API - support ESP32-S3
- update readme.md
- update examples
- add **getAddress()**
  • Loading branch information
RobTillaart authored Dec 4, 2023
1 parent d41d271 commit 7fa38ef
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 44 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.0] - 2023-12-04
- Fix #8, refactor API - support ESP32-S3
- update readme.md
- update examples
- add **getAddress()**

----

## [0.1.5] - 2023-09-24
- Add Wire1 support for ESP32
- update readme.md


## [0.1.4] - 2023-06-12
- improve RP2040 support
- add address test in isConnected()
Expand Down
28 changes: 7 additions & 21 deletions INA219.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FILE: INA219.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// VERSION: 0.2.0
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
// URL: https://github.com/RobTillaart/INA219
Expand Down Expand Up @@ -47,28 +47,8 @@ INA219::INA219(const uint8_t address, TwoWire *wire)
}


#if defined (ESP8266) || defined(ESP32)
bool INA219::begin(const uint8_t sda, const uint8_t scl)
{
_wire->begin(sda, scl);
if (! isConnected()) return false;
return true;
}
#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__)
bool INA219::begin(const uint8_t sda, const uint8_t scl)
{
_wire->setSDA(sda);
_wire->setSCL(scl);
_wire->begin();
if (! isConnected()) return false;
return true;
}
#endif


bool INA219::begin()
{
_wire->begin();
if (! isConnected()) return false;
return true;
}
Expand All @@ -82,6 +62,12 @@ bool INA219::isConnected()
}


uint8_t INA219::getAddress()
{
return _address;
}


////////////////////////////////////////////////////////
//
// CORE FUNCTIONS
Expand Down
10 changes: 3 additions & 7 deletions INA219.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// FILE: INA219.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.5
// VERSION: 0.2.0
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
// URL: https://github.com/RobTillaart/INA219
Expand All @@ -14,7 +14,7 @@
#include "Wire.h"


#define INA219_LIB_VERSION (F("0.1.5"))
#define INA219_LIB_VERSION (F("0.2.0"))


class INA219
Expand All @@ -23,13 +23,9 @@ class INA219
// address between 0x40 and 0x4F
explicit INA219(const uint8_t address, TwoWire *wire = &Wire);

#if defined (ESP8266) || defined(ESP32)
bool begin(const uint8_t sda, const uint8_t scl);
#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__)
bool begin(const uint8_t sda, const uint8_t scl);
#endif
bool begin();
bool isConnected();
uint8_t getAddress();


// CORE FUNCTIONS // Register
Expand Down
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Arduino library for INA219 voltage, current and power sensor.
## Description

**Experimental** library for the INA219 power sensor.

Minimal tested, so usage remarks and comments are welcome.

Read datasheet for details.
Expand All @@ -33,15 +32,27 @@ Maxima, see datasheet, chapter 7, esp 7.5
| shunt voltage | 320 | mVolt | depends on PGA setting


#### 0.2.0 Breaking change

Version 0.2.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.


#### Special characters

- Ω == Ohm = ALT-234 (Windows)
- µ == micro = ALT-0181 (Windows)


#### Links
#### Related

Relates to https://github.com/RobTillaart/INA226
- https://www.ti.com/product/INA219#tech-docs
- https://www.ti.com/document-viewer/INA219/datasheet
- https://github.com/RobTillaart/INA219
- https://github.com/RobTillaart/INA226


## I2C
Expand Down Expand Up @@ -83,9 +94,6 @@ use **INA219_test_I2C.ino**
#### Constructor

- **INA219(const uint8_t address, TwoWire \*wire = Wire)** Constructor to set address and optional Wire interface.
- **bool begin(const uint8_t sda, const uint8_t scl)** for ESP32 and ESP8266 et al.
Initializes the class and sets I2C pins.
Returns true if the INA219 address (set in the constructor) is on the I2C bus.
- **bool begin()** UNO ea. initializes the class.
Returns true if the INA219 address (set in the constructor) is on the I2C bus.
- **bool isConnected()** Returns true if the INA219 address (set in the constructor) is on the I2C bus.
Expand All @@ -103,11 +111,17 @@ Also the value is not meaningful if there is no shunt connected.

The library has helper functions to convert above output to a more appropriate scale of units.

Helper functions for the milli scale.

- **float getBusVoltage_mV()** idem, returns millivolts.
Note: returns -100 if the math overflow bit is set.
- **float getShuntVoltage_mV()** idem, returns millivolts.
- **float getCurrent_mA()** idem in milliAmpere.
- **float getPower_mW()** idem in milliWatt.

Helper functions for the micro scale.

- **float getBusVoltage_uV()** idem microVolt.
- **float getShuntVoltage_uV()** idem microVolt.
- **float getCurrent_uA()** idem in microAmpere.
- **float getPower_uW()** idem, in microWatt.
Expand Down Expand Up @@ -170,8 +184,8 @@ minus - == don't care

See details datasheet,

- **bool setMode(uint8_t mode = 7)** mode = 0..7
The value 7 == ShuntBusContinuous mode
- **bool setMode(uint8_t mode = 7)** mode = 0..7.
The value 7 == ShuntBusContinuous mode.
- **uint8_t getMode()** returns the mode (0..7) set.

Descriptive mode functions (convenience wrappers).
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/INA219.git"
},
"version": "0.1.5",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=INA219
version=0.1.5
version=0.2.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for INA219 voltage, current and power sensor.
Expand Down
11 changes: 6 additions & 5 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ unittest(test_constructor)
Wire.begin();
assertTrue(INA.begin());
assertTrue(INA.isConnected());
assertEqual(0x40, INA.getAddress());

// default is not calibrated.
assertFalse(INA.isCalibrated());
Expand All @@ -73,7 +74,7 @@ unittest(test_constructor)
unittest(test_core_functions)
{
INA219 INA(0x40);
// assertTrue(INA.begin());
// assertTrue(INA.begin());

fprintf(stderr, "need mock up\n");
/*
Expand All @@ -88,9 +89,9 @@ unittest(test_core_functions)
unittest(test_configuration)
{
INA219 INA(0x40);
// assertTrue(INA.begin());
// assertTrue(INA.begin());

// only errors can be tested
// only errors can be tested
assertFalse(INA.setBusVoltageRange(33));
assertFalse(INA.setGain(3));
assertFalse(INA.setGain(5));
Expand All @@ -105,9 +106,9 @@ unittest(test_configuration)
unittest(test_calibration)
{
INA219 INA(0x40);
// assertTrue(INA.begin());
// assertTrue(INA.begin());

// only errors can be tested
// only errors can be tested
assertFalse(INA.setMaxCurrentShunt(0.0009));
assertFalse(INA.setMaxCurrentShunt(0));
assertFalse(INA.setMaxCurrentShunt(-1));
Expand Down

0 comments on commit 7fa38ef

Please sign in to comment.