Skip to content

Commit 0b61e2b

Browse files
committed
Add INA228 (included as base lib for INA23x)
1 parent 895bd13 commit 0b61e2b

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
413413
_ina238->configureDriver(msgDeviceInitReq);
414414
drivers.push_back(_ina238);
415415
WS_DEBUG_PRINTLN("INA238 Initialized Successfully!");
416+
} else if (strcmp("ina228", msgDeviceInitReq->i2c_device_name) == 0) {
417+
_ina228 = new WipperSnapper_I2C_Driver_INA228(this->_i2c, i2cAddress);
418+
if (!_ina228->begin()) {
419+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA228");
420+
_busStatusResponse =
421+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
422+
return false;
423+
}
424+
_ina228->configureDriver(msgDeviceInitReq);
425+
drivers.push_back(_ina228);
426+
WS_DEBUG_PRINTLN("INA228 Initialized Successfully!");
416427
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
417428
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
418429
if (!_ina219->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
3939
#include "drivers/WipperSnapper_I2C_Driver_INA237.h"
4040
#include "drivers/WipperSnapper_I2C_Driver_INA238.h"
41+
#include "drivers/WipperSnapper_I2C_Driver_INA228.h"
4142
#include "drivers/WipperSnapper_I2C_Driver_INA260.h"
4243
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
4344
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
@@ -91,6 +92,7 @@ class Wippersnapper;
9192
class WipperSnapper_I2C_Driver_INA260;
9293
class WipperSnapper_I2C_Driver_INA237;
9394
class WipperSnapper_I2C_Driver_INA238;
95+
class WipperSnapper_I2C_Driver_INA228;
9496

9597
/**************************************************************************/
9698
/*!
@@ -172,6 +174,7 @@ class WipperSnapper_Component_I2C {
172174
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
173175
WipperSnapper_I2C_Driver_INA237 *_ina237 = nullptr;
174176
WipperSnapper_I2C_Driver_INA238 *_ina238 = nullptr;
177+
WipperSnapper_I2C_Driver_INA228 *_ina228 = nullptr;
175178
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
176179
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
177180
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA228.cpp
3+
*
4+
* Device driver implementation for the INA228 High Precision DC Current and
5+
* Voltage Monitor (Avoids import conflict with INA260 typedef enum _mode etc)
6+
*
7+
* Adafruit invests time and resources providing this open source code,
8+
* please support Adafruit and open-source hardware by purchasing
9+
* products from Adafruit!
10+
*
11+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
12+
*
13+
* MIT license, all text here must be included in any redistribution.
14+
*
15+
*/
16+
17+
#include "WipperSnapper_I2C_Driver_INA228.h"
18+
#include "Wippersnapper.h"
19+
#include <Adafruit_INA228.h>
20+
21+
/*******************************************************************************/
22+
/*!
23+
@brief Constructor for a INA228 sensor.
24+
@param i2c
25+
The I2C interface.
26+
@param sensorAddress
27+
The 7-bit I2C address of the sensor.
28+
*/
29+
/*******************************************************************************/
30+
WipperSnapper_I2C_Driver_INA228::WipperSnapper_I2C_Driver_INA228(
31+
TwoWire *i2c, uint16_t sensorAddress)
32+
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina228(nullptr) {
33+
_i2c = i2c;
34+
_sensorAddress = sensorAddress;
35+
}
36+
37+
/*******************************************************************************/
38+
/*!
39+
@brief Destructor for an INA228 sensor.
40+
*/
41+
/*******************************************************************************/
42+
WipperSnapper_I2C_Driver_INA228::~WipperSnapper_I2C_Driver_INA228() {
43+
delete _ina228;
44+
}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Initializes the INA228 sensor and begins I2C.
49+
@returns True if initialized successfully, False otherwise.
50+
*/
51+
/*******************************************************************************/
52+
bool WipperSnapper_I2C_Driver_INA228::begin() {
53+
_ina228 = new Adafruit_INA228();
54+
if (!_ina228->begin(_sensorAddress, _i2c)) {
55+
return false;
56+
}
57+
58+
// Default shunt: 0.015 ohm, 10A max current
59+
_ina228->setShunt(0.015, 10.0);
60+
61+
_ina228->setAveragingCount(INA228_COUNT_16);
62+
_ina228->setVoltageConversionTime(INA228_TIME_150_us);
63+
_ina228->setCurrentConversionTime(INA228_TIME_280_us);
64+
65+
return true;
66+
}
67+
68+
/*******************************************************************************/
69+
/*!
70+
@brief Reads a voltage sensor and converts the
71+
reading into the expected SI unit.
72+
@param voltageEvent
73+
voltage sensor reading, in volts.
74+
@returns True if the sensor event was obtained successfully, False
75+
otherwise.
76+
*/
77+
/*******************************************************************************/
78+
bool WipperSnapper_I2C_Driver_INA228::getEventVoltage(
79+
sensors_event_t *voltageEvent) {
80+
voltageEvent->voltage = _ina228->getBusVoltage_V();
81+
return true;
82+
}
83+
84+
/**
85+
* @brief Get the current sensor event.
86+
*
87+
* @param currentEvent Pointer to the current sensor event.
88+
*
89+
* @returns True if the sensor event was obtained successfully, False
90+
* otherwise.
91+
*/
92+
bool WipperSnapper_I2C_Driver_INA228::getEventCurrent(
93+
sensors_event_t *currentEvent) {
94+
currentEvent->current = _ina228->getCurrent_mA();
95+
return true;
96+
}
97+
98+
/**
99+
* @brief Get the raw (power) sensor event.
100+
*
101+
* @param powerEvent Pointer to the power sensor event.
102+
*
103+
* @returns True if the sensor event was obtained successfully, False
104+
* otherwise.
105+
*/
106+
bool WipperSnapper_I2C_Driver_INA228::getEventRaw(sensors_event_t *powerEvent) {
107+
powerEvent->data[0] = _ina228->getPower_mW();
108+
return true;
109+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA228.h
3+
*
4+
* Device driver for the INA228 High Precision DC Current and Voltage Monitor
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_INA228_H
16+
#define WipperSnapper_I2C_Driver_INA228_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include "Wippersnapper.h"
20+
21+
// Forward declaration
22+
class Adafruit_INA228;
23+
24+
/**************************************************************************/
25+
/*!
26+
@brief Class that provides a driver interface for a INA228 sensor.
27+
*/
28+
/**************************************************************************/
29+
class WipperSnapper_I2C_Driver_INA228 : public WipperSnapper_I2C_Driver {
30+
public:
31+
WipperSnapper_I2C_Driver_INA228(TwoWire *i2c, uint16_t sensorAddress);
32+
~WipperSnapper_I2C_Driver_INA228();
33+
34+
bool begin();
35+
bool getEventVoltage(sensors_event_t *voltageEvent);
36+
bool getEventCurrent(sensors_event_t *currentEvent);
37+
bool getEventRaw(sensors_event_t *powerEvent);
38+
39+
protected:
40+
Adafruit_INA228 *_ina228; ///< Pointer to INA228 sensor object
41+
};
42+
43+
#endif // WipperSnapper_I2C_Driver_INA228_H

0 commit comments

Comments
 (0)