Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_ina238->configureDriver(msgDeviceInitReq);
drivers.push_back(_ina238);
WS_DEBUG_PRINTLN("INA238 Initialized Successfully!");
} else if (strcmp("ina228", msgDeviceInitReq->i2c_device_name) == 0) {
_ina228 = new WipperSnapper_I2C_Driver_INA228(this->_i2c, i2cAddress);
if (!_ina228->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA228");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_ina228->configureDriver(msgDeviceInitReq);
drivers.push_back(_ina228);
WS_DEBUG_PRINTLN("INA228 Initialized Successfully!");
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
if (!_ina219->begin()) {
Expand Down
3 changes: 3 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
#include "drivers/WipperSnapper_I2C_Driver_INA237.h"
#include "drivers/WipperSnapper_I2C_Driver_INA238.h"
#include "drivers/WipperSnapper_I2C_Driver_INA228.h"
#include "drivers/WipperSnapper_I2C_Driver_INA260.h"
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
Expand Down Expand Up @@ -91,6 +92,7 @@ class Wippersnapper;
class WipperSnapper_I2C_Driver_INA260;
class WipperSnapper_I2C_Driver_INA237;
class WipperSnapper_I2C_Driver_INA238;
class WipperSnapper_I2C_Driver_INA228;

/**************************************************************************/
/*!
Expand Down Expand Up @@ -172,6 +174,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
WipperSnapper_I2C_Driver_INA237 *_ina237 = nullptr;
WipperSnapper_I2C_Driver_INA238 *_ina238 = nullptr;
WipperSnapper_I2C_Driver_INA228 *_ina228 = nullptr;
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
Expand Down
109 changes: 109 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*!
* @file WipperSnapper_I2C_Driver_INA228.cpp
*
* Device driver implementation for the INA228 High Precision DC Current and
* Voltage Monitor (Avoids import conflict with INA260 typedef enum _mode etc)
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/

#include "WipperSnapper_I2C_Driver_INA228.h"
#include "Wippersnapper.h"
#include <Adafruit_INA228.h>

/*******************************************************************************/
/*!
@brief Constructor for a INA228 sensor.
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_INA228::WipperSnapper_I2C_Driver_INA228(
TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina228(nullptr) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an INA228 sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_INA228::~WipperSnapper_I2C_Driver_INA228() {
delete _ina228;
}

/*******************************************************************************/
/*!
@brief Initializes the INA228 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool WipperSnapper_I2C_Driver_INA228::begin() {
_ina228 = new Adafruit_INA228();
if (!_ina228->begin(_sensorAddress, _i2c)) {
return false;
}

// Default shunt: 0.015 ohm, 10A max current
_ina228->setShunt(0.015, 10.0);

_ina228->setAveragingCount(INA228_COUNT_16);
_ina228->setVoltageConversionTime(INA228_TIME_150_us);
_ina228->setCurrentConversionTime(INA228_TIME_280_us);

return true;
}

/*******************************************************************************/
/*!
@brief Reads a voltage sensor and converts the
reading into the expected SI unit.
@param voltageEvent
voltage sensor reading, in volts.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool WipperSnapper_I2C_Driver_INA228::getEventVoltage(
sensors_event_t *voltageEvent) {
voltageEvent->voltage = _ina228->getBusVoltage_V();
return true;
}

/**
* @brief Get the current sensor event.
*
* @param currentEvent Pointer to the current sensor event.
*
* @returns True if the sensor event was obtained successfully, False
* otherwise.
*/
bool WipperSnapper_I2C_Driver_INA228::getEventCurrent(
sensors_event_t *currentEvent) {
currentEvent->current = _ina228->getCurrent_mA();
return true;
}

/**
* @brief Get the raw (power) sensor event.
*
* @param powerEvent Pointer to the power sensor event.
*
* @returns True if the sensor event was obtained successfully, False
* otherwise.
*/
bool WipperSnapper_I2C_Driver_INA228::getEventRaw(sensors_event_t *powerEvent) {
powerEvent->data[0] = _ina228->getPower_mW();
return true;
}
43 changes: 43 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*!
* @file WipperSnapper_I2C_Driver_INA228.h
*
* Device driver for the INA228 High Precision DC Current and Voltage Monitor
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_INA228_H
#define WipperSnapper_I2C_Driver_INA228_H

#include "WipperSnapper_I2C_Driver.h"
#include "Wippersnapper.h"

// Forward declaration
class Adafruit_INA228;

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a INA228 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_INA228 : public WipperSnapper_I2C_Driver {
public:
WipperSnapper_I2C_Driver_INA228(TwoWire *i2c, uint16_t sensorAddress);
~WipperSnapper_I2C_Driver_INA228();

bool begin();
bool getEventVoltage(sensors_event_t *voltageEvent);
bool getEventCurrent(sensors_event_t *currentEvent);
bool getEventRaw(sensors_event_t *powerEvent);

protected:
Adafruit_INA228 *_ina228; ///< Pointer to INA228 sensor object
};

#endif // WipperSnapper_I2C_Driver_INA228_H
Loading