diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index eeda800e6..16616f441 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -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()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index c46c0e356..c594e0bf5 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -36,6 +36,7 @@ #include "drivers/WipperSnapper_I2C_Driver_HTU21D.h" #include "drivers/WipperSnapper_I2C_Driver_HTU31D.h" #include "drivers/WipperSnapper_I2C_Driver_INA219.h" +#include "drivers/WipperSnapper_I2C_Driver_INA228.h" #include "drivers/WipperSnapper_I2C_Driver_INA237.h" #include "drivers/WipperSnapper_I2C_Driver_INA238.h" #include "drivers/WipperSnapper_I2C_Driver_INA260.h" @@ -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; /**************************************************************************/ /*! @@ -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; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.cpp b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.cpp new file mode 100644 index 000000000..d6eaa60e5 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.cpp @@ -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 + +/*******************************************************************************/ +/*! + @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; +} diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.h new file mode 100644 index 000000000..509287d03 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA228.h @@ -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 diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp index 243212324..dcf5bd39b 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp @@ -70,7 +70,7 @@ bool WipperSnapper_I2C_Driver_INA260::begin() { /*******************************************************************************/ bool WipperSnapper_I2C_Driver_INA260::getEventVoltage( sensors_event_t *voltageEvent) { - voltageEvent->voltage = _ina260->readBusVoltage(); + voltageEvent->voltage = _ina260->readBusVoltage() / 1000.0f; return true; }