Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
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
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lib_deps =
adafruit/Adafruit DS248x
adafruit/Adafruit INA219
adafruit/Adafruit INA260 Library
adafruit/Adafruit INA237 and INA238 Library
adafruit/Adafruit HDC302x
adafruit/Adafruit HTS221
adafruit/Adafruit HTU21DF Library
Expand Down
22 changes: 22 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_scd30->configureDriver(msgDeviceInitReq);
drivers.push_back(_scd30);
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) {
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
if (!_ina219->begin()) {
Expand Down
7 changes: 7 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#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_INA237.h"
#include "drivers/WipperSnapper_I2C_Driver_INA238.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 @@ -86,6 +88,9 @@

// forward decl.
class Wippersnapper;
class WipperSnapper_I2C_Driver_INA260;
class WipperSnapper_I2C_Driver_INA237;
class WipperSnapper_I2C_Driver_INA238;

/**************************************************************************/
/*!
Expand Down Expand Up @@ -165,6 +170,8 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
WipperSnapper_I2C_Driver_HTU31D *_htu31d = 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_LTR329_LTR303 *_ltr329 = nullptr;
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#ifndef WipperSnapper_I2C_Driver_H
#define WipperSnapper_I2C_Driver_H

#include "wippersnapper/i2c/v1/i2c.pb.h"
#include <Adafruit_Sensor.h>
#include <Arduino.h>
#include <Wire.h>

#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000))
///< Used for last sensor read time, initially set 24hrs ago (max period)
Expand Down
114 changes: 114 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 45 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h
Original file line number Diff line number Diff line change
@@ -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 changes: 114 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading
Loading