Merge pull request #765 from adafruit/ina238
Ina238 + 237 via a refactor of INA260 too.
This commit is contained in:
commit
895bd13011
11 changed files with 448 additions and 68 deletions
|
|
@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
|
||||||
category=Communication
|
category=Communication
|
||||||
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
|
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
|
||||||
architectures=*
|
architectures=*
|
||||||
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306
|
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ lib_deps =
|
||||||
adafruit/Adafruit DS248x
|
adafruit/Adafruit DS248x
|
||||||
adafruit/Adafruit INA219
|
adafruit/Adafruit INA219
|
||||||
adafruit/Adafruit INA260 Library
|
adafruit/Adafruit INA260 Library
|
||||||
|
adafruit/Adafruit INA237 and INA238 Library
|
||||||
adafruit/Adafruit HDC302x
|
adafruit/Adafruit HDC302x
|
||||||
adafruit/Adafruit HTS221
|
adafruit/Adafruit HTS221
|
||||||
adafruit/Adafruit HTU21DF Library
|
adafruit/Adafruit HTU21DF Library
|
||||||
|
|
|
||||||
|
|
@ -391,6 +391,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
|
||||||
_scd30->configureDriver(msgDeviceInitReq);
|
_scd30->configureDriver(msgDeviceInitReq);
|
||||||
drivers.push_back(_scd30);
|
drivers.push_back(_scd30);
|
||||||
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
|
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
|
||||||
|
} else if (strcmp("ina237", msgDeviceInitReq->i2c_device_name) == 0) {
|
||||||
|
_ina237 = new WipperSnapper_I2C_Driver_INA237(this->_i2c, i2cAddress);
|
||||||
|
if (!_ina237->begin()) {
|
||||||
|
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA237");
|
||||||
|
_busStatusResponse =
|
||||||
|
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_ina237->configureDriver(msgDeviceInitReq);
|
||||||
|
drivers.push_back(_ina237);
|
||||||
|
WS_DEBUG_PRINTLN("INA237 Initialized Successfully!");
|
||||||
|
} else if (strcmp("ina238", msgDeviceInitReq->i2c_device_name) == 0) {
|
||||||
|
_ina238 = new WipperSnapper_I2C_Driver_INA238(this->_i2c, i2cAddress);
|
||||||
|
if (!_ina238->begin()) {
|
||||||
|
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA238");
|
||||||
|
_busStatusResponse =
|
||||||
|
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_ina238->configureDriver(msgDeviceInitReq);
|
||||||
|
drivers.push_back(_ina238);
|
||||||
|
WS_DEBUG_PRINTLN("INA238 Initialized Successfully!");
|
||||||
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
|
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
|
||||||
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
|
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
|
||||||
if (!_ina219->begin()) {
|
if (!_ina219->begin()) {
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,8 @@
|
||||||
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
|
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
|
||||||
#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h"
|
#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h"
|
||||||
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
|
#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_INA260.h"
|
#include "drivers/WipperSnapper_I2C_Driver_INA260.h"
|
||||||
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
|
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
|
||||||
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
|
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
|
||||||
|
|
@ -86,6 +88,9 @@
|
||||||
|
|
||||||
// forward decl.
|
// forward decl.
|
||||||
class Wippersnapper;
|
class Wippersnapper;
|
||||||
|
class WipperSnapper_I2C_Driver_INA260;
|
||||||
|
class WipperSnapper_I2C_Driver_INA237;
|
||||||
|
class WipperSnapper_I2C_Driver_INA238;
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -165,6 +170,8 @@ private:
|
||||||
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
|
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
|
||||||
WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr;
|
WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr;
|
||||||
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
|
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
|
||||||
|
WipperSnapper_I2C_Driver_INA237 *_ina237 = nullptr;
|
||||||
|
WipperSnapper_I2C_Driver_INA238 *_ina238 = nullptr;
|
||||||
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
|
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
|
||||||
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
|
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
|
||||||
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
|
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,10 @@
|
||||||
#ifndef WipperSnapper_I2C_Driver_H
|
#ifndef WipperSnapper_I2C_Driver_H
|
||||||
#define WipperSnapper_I2C_Driver_H
|
#define WipperSnapper_I2C_Driver_H
|
||||||
|
|
||||||
|
#include "wippersnapper/i2c/v1/i2c.pb.h"
|
||||||
#include <Adafruit_Sensor.h>
|
#include <Adafruit_Sensor.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
|
||||||
#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000))
|
#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000))
|
||||||
///< Used for last sensor read time, initially set 24hrs ago (max period)
|
///< Used for last sensor read time, initially set 24hrs ago (max period)
|
||||||
|
|
|
||||||
114
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp
Normal file
114
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
/*!
|
||||||
|
* @file WipperSnapper_I2C_Driver_INA237.cpp
|
||||||
|
*
|
||||||
|
* Device driver implementation for the INA237 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_INA237.h"
|
||||||
|
#include "Wippersnapper.h"
|
||||||
|
#include <Adafruit_INA237.h>
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Constructor for a INA237 sensor.
|
||||||
|
@param i2c
|
||||||
|
The I2C interface.
|
||||||
|
@param sensorAddress
|
||||||
|
The 7-bit I2C address of the sensor.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
WipperSnapper_I2C_Driver_INA237::WipperSnapper_I2C_Driver_INA237(
|
||||||
|
TwoWire *i2c, uint16_t sensorAddress)
|
||||||
|
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina237(nullptr) {
|
||||||
|
_i2c = i2c;
|
||||||
|
_sensorAddress = sensorAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Destructor for an INA237 sensor.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
WipperSnapper_I2C_Driver_INA237::~WipperSnapper_I2C_Driver_INA237() {
|
||||||
|
delete _ina237;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Initializes the INA237 sensor and begins I2C.
|
||||||
|
@returns True if initialized successfully, False otherwise.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
bool WipperSnapper_I2C_Driver_INA237::begin() {
|
||||||
|
_ina237 = new Adafruit_INA237();
|
||||||
|
if (!_ina237->begin(_sensorAddress, _i2c)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configuration based on INA237 example sketch
|
||||||
|
// Set default shunt resistance and maximum current
|
||||||
|
// Default 0.015 ohm shunt, 10A max current
|
||||||
|
_ina237->setShunt(0.015, 10.0);
|
||||||
|
|
||||||
|
// Set averaging for better accuracy (16 samples)
|
||||||
|
_ina237->setAveragingCount(INA2XX_COUNT_16);
|
||||||
|
|
||||||
|
// Set conversion times as per example
|
||||||
|
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
|
||||||
|
_ina237->setCurrentConversionTime(INA2XX_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_INA237::getEventVoltage(
|
||||||
|
sensors_event_t *voltageEvent) {
|
||||||
|
voltageEvent->voltage = _ina237->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_INA237::getEventCurrent(
|
||||||
|
sensors_event_t *currentEvent) {
|
||||||
|
currentEvent->current = _ina237->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_INA237::getEventRaw(sensors_event_t *powerEvent) {
|
||||||
|
powerEvent->data[0] = _ina237->getPower_mW();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
45
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h
Normal file
45
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
/*!
|
||||||
|
* @file WipperSnapper_I2C_Driver_INA237.h
|
||||||
|
*
|
||||||
|
* Device driver for the INA237 DC Current and Voltage Monitor
|
||||||
|
* 16-bit ADC with ±0.3% gain error, ±50µV offset voltage
|
||||||
|
* Cost-effective version, lower precision than INA238
|
||||||
|
*
|
||||||
|
* 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_INA237_H
|
||||||
|
#define WipperSnapper_I2C_Driver_INA237_H
|
||||||
|
|
||||||
|
#include "WipperSnapper_I2C_Driver.h"
|
||||||
|
#include "Wippersnapper.h"
|
||||||
|
|
||||||
|
// Forward declaration
|
||||||
|
class Adafruit_INA237;
|
||||||
|
|
||||||
|
/**************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Class that provides a driver interface for a INA237 sensor.
|
||||||
|
*/
|
||||||
|
/**************************************************************************/
|
||||||
|
class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
|
||||||
|
public:
|
||||||
|
WipperSnapper_I2C_Driver_INA237(TwoWire *i2c, uint16_t sensorAddress);
|
||||||
|
~WipperSnapper_I2C_Driver_INA237();
|
||||||
|
|
||||||
|
bool begin();
|
||||||
|
bool getEventVoltage(sensors_event_t *voltageEvent);
|
||||||
|
bool getEventCurrent(sensors_event_t *currentEvent);
|
||||||
|
bool getEventRaw(sensors_event_t *powerEvent);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Adafruit_INA237 *_ina237; ///< Pointer to INA237 sensor object
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // WipperSnapper_I2C_Driver_INA237
|
||||||
114
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp
Normal file
114
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
/*!
|
||||||
|
* @file WipperSnapper_I2C_Driver_INA238.cpp
|
||||||
|
*
|
||||||
|
* Device driver implementation for the INA238 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_INA238.h"
|
||||||
|
#include "Wippersnapper.h"
|
||||||
|
#include <Adafruit_INA238.h>
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Constructor for a INA238 sensor.
|
||||||
|
@param i2c
|
||||||
|
The I2C interface.
|
||||||
|
@param sensorAddress
|
||||||
|
The 7-bit I2C address of the sensor.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
WipperSnapper_I2C_Driver_INA238::WipperSnapper_I2C_Driver_INA238(
|
||||||
|
TwoWire *i2c, uint16_t sensorAddress)
|
||||||
|
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina238(nullptr) {
|
||||||
|
_i2c = i2c;
|
||||||
|
_sensorAddress = sensorAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Destructor for an INA238 sensor.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
WipperSnapper_I2C_Driver_INA238::~WipperSnapper_I2C_Driver_INA238() {
|
||||||
|
delete _ina238;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Initializes the INA238 sensor and begins I2C.
|
||||||
|
@returns True if initialized successfully, False otherwise.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
bool WipperSnapper_I2C_Driver_INA238::begin() {
|
||||||
|
_ina238 = new Adafruit_INA238();
|
||||||
|
if (!_ina238->begin(_sensorAddress, _i2c)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configuration based on INA238 example sketch
|
||||||
|
// Set default shunt resistance and maximum current
|
||||||
|
// Default 0.015 ohm shunt, 10A max current
|
||||||
|
_ina238->setShunt(0.015, 10.0);
|
||||||
|
|
||||||
|
// Set averaging for better accuracy (16 samples)
|
||||||
|
_ina238->setAveragingCount(INA2XX_COUNT_16);
|
||||||
|
|
||||||
|
// Set conversion times as per example
|
||||||
|
_ina238->setVoltageConversionTime(INA2XX_TIME_150_us);
|
||||||
|
_ina238->setCurrentConversionTime(INA2XX_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_INA238::getEventVoltage(
|
||||||
|
sensors_event_t *voltageEvent) {
|
||||||
|
voltageEvent->voltage = _ina238->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_INA238::getEventCurrent(
|
||||||
|
sensors_event_t *currentEvent) {
|
||||||
|
currentEvent->current = _ina238->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_INA238::getEventRaw(sensors_event_t *powerEvent) {
|
||||||
|
powerEvent->data[0] = _ina238->getPower_mW();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
44
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.h
Normal file
44
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.h
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*!
|
||||||
|
* @file WipperSnapper_I2C_Driver_INA238.h
|
||||||
|
*
|
||||||
|
* Device driver for the INA238 High-precision DC Current and Voltage Monitor
|
||||||
|
* 16-bit ADC with ±0.1% gain error, ±5µV offset voltage
|
||||||
|
* Higher precision version compared to INA237
|
||||||
|
*
|
||||||
|
* 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_INA238_H
|
||||||
|
#define WipperSnapper_I2C_Driver_INA238_H
|
||||||
|
|
||||||
|
#include "WipperSnapper_I2C_Driver.h"
|
||||||
|
|
||||||
|
// Forward declaration
|
||||||
|
class Adafruit_INA238;
|
||||||
|
|
||||||
|
/**************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Class that provides a driver interface for a INA238 sensor.
|
||||||
|
*/
|
||||||
|
/**************************************************************************/
|
||||||
|
class WipperSnapper_I2C_Driver_INA238 : public WipperSnapper_I2C_Driver {
|
||||||
|
public:
|
||||||
|
WipperSnapper_I2C_Driver_INA238(TwoWire *i2c, uint16_t sensorAddress);
|
||||||
|
~WipperSnapper_I2C_Driver_INA238();
|
||||||
|
|
||||||
|
bool begin();
|
||||||
|
bool getEventVoltage(sensors_event_t *voltageEvent);
|
||||||
|
bool getEventCurrent(sensors_event_t *currentEvent);
|
||||||
|
bool getEventRaw(sensors_event_t *powerEvent);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Adafruit_INA238 *_ina238; ///< Pointer to INA238 sensor object
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // WipperSnapper_I2C_Driver_INA238
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
/*!
|
||||||
|
* @file WipperSnapper_I2C_Driver_INA260.cpp
|
||||||
|
*
|
||||||
|
* Device driver implementation for the INA260 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "WipperSnapper_I2C_Driver_INA260.h"
|
||||||
|
#include "Wippersnapper.h"
|
||||||
|
#include <Adafruit_INA260.h>
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Constructor for a INA260 sensor.
|
||||||
|
@param i2c
|
||||||
|
The I2C interface.
|
||||||
|
@param sensorAddress
|
||||||
|
The 7-bit I2C address of the sensor.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
WipperSnapper_I2C_Driver_INA260::WipperSnapper_I2C_Driver_INA260(
|
||||||
|
TwoWire *i2c, uint16_t sensorAddress)
|
||||||
|
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina260(nullptr) {
|
||||||
|
_i2c = i2c;
|
||||||
|
_sensorAddress = sensorAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Destructor for an INA260 sensor.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
WipperSnapper_I2C_Driver_INA260::~WipperSnapper_I2C_Driver_INA260() {
|
||||||
|
delete _ina260;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************/
|
||||||
|
/*!
|
||||||
|
@brief Initializes the INA260 sensor and begins I2C.
|
||||||
|
@returns True if initialized successfully, False otherwise.
|
||||||
|
*/
|
||||||
|
/*******************************************************************************/
|
||||||
|
bool WipperSnapper_I2C_Driver_INA260::begin() {
|
||||||
|
_ina260 = new Adafruit_INA260();
|
||||||
|
if (!_ina260->begin(_sensorAddress, _i2c)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// TODO: use setCalibration()
|
||||||
|
|
||||||
|
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_INA260::getEventVoltage(
|
||||||
|
sensors_event_t *voltageEvent) {
|
||||||
|
voltageEvent->voltage = _ina260->readBusVoltage();
|
||||||
|
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_INA260::getEventCurrent(
|
||||||
|
sensors_event_t *currentEvent) {
|
||||||
|
currentEvent->current = _ina260->readCurrent();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
@ -16,7 +16,9 @@
|
||||||
#define WipperSnapper_I2C_Driver_INA260_H
|
#define WipperSnapper_I2C_Driver_INA260_H
|
||||||
|
|
||||||
#include "WipperSnapper_I2C_Driver.h"
|
#include "WipperSnapper_I2C_Driver.h"
|
||||||
#include <Adafruit_INA260.h>
|
|
||||||
|
// Forward declaration
|
||||||
|
class Adafruit_INA260;
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -25,75 +27,15 @@
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
class WipperSnapper_I2C_Driver_INA260 : public WipperSnapper_I2C_Driver {
|
class WipperSnapper_I2C_Driver_INA260 : public WipperSnapper_I2C_Driver {
|
||||||
public:
|
public:
|
||||||
/*******************************************************************************/
|
WipperSnapper_I2C_Driver_INA260(TwoWire *i2c, uint16_t sensorAddress);
|
||||||
/*!
|
~WipperSnapper_I2C_Driver_INA260();
|
||||||
@brief Constructor for a INA260 sensor.
|
|
||||||
@param i2c
|
|
||||||
The I2C interface.
|
|
||||||
@param sensorAddress
|
|
||||||
The 7-bit I2C address of the sensor.
|
|
||||||
*/
|
|
||||||
/*******************************************************************************/
|
|
||||||
WipperSnapper_I2C_Driver_INA260(TwoWire *i2c, uint16_t sensorAddress)
|
|
||||||
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
|
|
||||||
_i2c = i2c;
|
|
||||||
_sensorAddress = sensorAddress;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*******************************************************************************/
|
bool begin();
|
||||||
/*!
|
bool getEventVoltage(sensors_event_t *voltageEvent);
|
||||||
@brief Destructor for an INA260 sensor.
|
bool getEventCurrent(sensors_event_t *currentEvent);
|
||||||
*/
|
|
||||||
/*******************************************************************************/
|
|
||||||
~WipperSnapper_I2C_Driver_INA260() { delete _ina260; }
|
|
||||||
|
|
||||||
/*******************************************************************************/
|
|
||||||
/*!
|
|
||||||
@brief Initializes the INA260 sensor and begins I2C.
|
|
||||||
@returns True if initialized successfully, False otherwise.
|
|
||||||
*/
|
|
||||||
/*******************************************************************************/
|
|
||||||
bool begin() {
|
|
||||||
_ina260 = new Adafruit_INA260();
|
|
||||||
if (!_ina260->begin(_sensorAddress, _i2c)) {
|
|
||||||
WS_DEBUG_PRINTLN("INA260 failed to initialise!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// TODO: use setCalibration()
|
|
||||||
|
|
||||||
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 getEventVoltage(sensors_event_t *voltageEvent) {
|
|
||||||
voltageEvent->voltage = _ina260->readBusVoltage();
|
|
||||||
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 getEventCurrent(sensors_event_t *currentEvent) {
|
|
||||||
currentEvent->current = _ina260->readCurrent();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Adafruit_INA260 *_ina260; ///< Pointer to INA260 sensor object
|
Adafruit_INA260 *_ina260 = nullptr; ///< Pointer to INA260 sensor object
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WipperSnapper_I2C_Driver_INA260
|
#endif // WipperSnapper_I2C_Driver_INA260
|
||||||
Loading…
Reference in a new issue