Skip to content

Commit 0c34fe5

Browse files
committed
Refactor INA260 and add INA237/238
1 parent 00adc77 commit 0c34fe5

File tree

12 files changed

+461
-36
lines changed

12 files changed

+461
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ src/.vscode/settings.json
4242
.DS_STORE
4343
examples/Wippersnapper_demo/build/
4444
src/Wippersnapper_demo.ino.cpp
45+
build_output_*.txt
4546

4647
# Virtual environment directories
4748
.venv/

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
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, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, 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, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ lib_deps =
3939
adafruit/Adafruit DPS310
4040
adafruit/Adafruit DS248x
4141
adafruit/Adafruit INA219
42+
adafruit/Adafruit INA237 and INA238 Library
4243
adafruit/Adafruit INA260 Library
4344
adafruit/Adafruit HDC302x
4445
adafruit/Adafruit HTS221

src/components/i2c/controller.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ static const std::map<std::string, FnCreateI2CSensorDriver> I2cFactorySensor = {
133133
const char *driver_name) -> drvBase * {
134134
return new drvIna219(i2c, addr, mux_channel, driver_name);
135135
}},
136+
{"ina237",
137+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
138+
const char *driver_name) -> drvBase * {
139+
return new drvIna237(i2c, addr, mux_channel, driver_name);
140+
}},
141+
{"ina238",
142+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
143+
const char *driver_name) -> drvBase * {
144+
return new drvIna238(i2c, addr, mux_channel, driver_name);
145+
}},
146+
{"ina260",
147+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
148+
const char *driver_name) -> drvBase * {
149+
return new drvIna260(i2c, addr, mux_channel, driver_name);
150+
}},
136151
{"lc709203f",
137152
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
138153
const char *driver_name) -> drvBase * {
@@ -372,11 +387,11 @@ static const std::unordered_map<uint16_t, std::vector<const char *>>
372387
{0x38, {"aht20", "max17048"}},
373388
{0x39, {"tsl2591"}},
374389
{0x40,
375-
{"htu21d", "htu31d", "ina219", "ina260", "ms8607", "si7021",
376-
"stemma_soil"}},
377-
{0x41, {"htu31d", "ina219", "ina260"}},
378-
{0x44, {"hdc302x", "ina260", "sht3x", "sht4x"}},
379-
{0x45, {"hdc302x", "ina260", "sht3x"}},
390+
{"htu21d", "htu31d", "ina219", "ina237", "ina238", "ina260",
391+
"ms8607", "si7021", "stemma_soil"}},
392+
{0x41, {"htu31d", "ina219", "ina237", "ina238", "ina260"}},
393+
{0x44, {"hdc302x", "ina237", "ina238", "ina260", "sht3x", "sht4x"}},
394+
{0x45, {"hdc302x", "ina237", "ina238", "ina260", "sht3x"}},
380395
{0x46, {"hdc302x"}},
381396
{0x47, {"hdc302x"}},
382397
{0x48, {"adt7410", "pct2075", "tmp117"}},

src/components/i2c/controller.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#include "drivers/drvHts221.h"
3434
#include "drivers/drvHtu21d.h"
3535
#include "drivers/drvIna219.h"
36+
#include "drivers/drvIna237.h"
37+
#include "drivers/drvIna238.h"
38+
#include "drivers/drvIna260.h"
3639
#include "drivers/drvLc709203f.h"
3740
#include "drivers/drvLps22hb.h"
3841
#include "drivers/drvLps25hb.h"

src/components/i2c/drivers/drvBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
#define DRV_BASE_H
1818
#include <Adafruit_Sensor.h>
1919
#include <Arduino.h>
20+
#include <Wire.h>
21+
#include <map>
2022
#include <protos/i2c.pb.h>
23+
#include "helpers/ws_helper_macros.h"
2124

2225
#define NO_MUX_CH 0xFFFF; ///< No MUX channel specified
2326
#define DEFAULT_SENSOR_PERIOD 15.0f ///< Default sensor period, in seconds
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*!
2+
* @file drvIna237.cpp
3+
*
4+
* Device driver for the INA237 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+
16+
#include "drvIna237.h"
17+
#include <Adafruit_INA237.h>
18+
19+
/*******************************************************************************/
20+
/*!
21+
@brief Destructor for an INA237 sensor.
22+
*/
23+
/*******************************************************************************/
24+
drvIna237::~drvIna237() {
25+
if (_ina237) {
26+
delete _ina237;
27+
_ina237 = nullptr;
28+
}
29+
}
30+
31+
/*******************************************************************************/
32+
/*!
33+
@brief Initializes the INA237 sensor and begins I2C.
34+
@returns True if initialized successfully, False otherwise.
35+
*/
36+
/*******************************************************************************/
37+
bool drvIna237::begin() {
38+
_ina237 = new Adafruit_INA237();
39+
if (!_ina237->begin(_address, _i2c)) {
40+
WS_DEBUG_PRINTLN("INA237 failed to initialise!");
41+
return false;
42+
}
43+
// TODO: use setCalibration()
44+
45+
return true;
46+
}
47+
48+
/*******************************************************************************/
49+
/*!
50+
@brief Reads a voltage sensor and converts the
51+
reading into the expected SI unit.
52+
@param voltageEvent
53+
voltage sensor reading, in volts.
54+
@returns True if the sensor event was obtained successfully, False
55+
otherwise.
56+
*/
57+
/*******************************************************************************/
58+
bool drvIna237::getEventVoltage(sensors_event_t *voltageEvent) {
59+
voltageEvent->voltage = _ina237->getBusVoltage_V();
60+
return true;
61+
}
62+
63+
/**
64+
* @brief Get the current sensor event.
65+
*
66+
* @param currentEvent Pointer to the current sensor event.
67+
*
68+
* @returns True if the sensor event was obtained successfully, False
69+
* otherwise.
70+
*/
71+
bool drvIna237::getEventCurrent(sensors_event_t *currentEvent) {
72+
currentEvent->current = _ina237->getCurrent_mA();
73+
return true;
74+
}
75+
76+
void drvIna237::ConfigureDefaultSensorTypes() {
77+
_default_sensor_types_count = 2;
78+
_default_sensor_types[0] =
79+
wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
80+
_default_sensor_types[1] =
81+
wippersnapper_sensor_SensorType_SENSOR_TYPE_CURRENT;
82+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*!
2+
* @file drvIna237.h
3+
*
4+
* Device driver for the INA237 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 DRV_INA237_H
16+
#define DRV_INA237_H
17+
18+
#include "drvBase.h"
19+
20+
class Adafruit_INA237;
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a INA237 sensor.
25+
*/
26+
/**************************************************************************/
27+
class drvIna237 : public drvBase {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for a INA237 sensor.
32+
@param i2c
33+
The I2C interface.
34+
@param sensorAddress
35+
7-bit device address.
36+
@param mux_channel
37+
The I2C multiplexer channel.
38+
@param driver_name
39+
The name of the driver.
40+
*/
41+
/*******************************************************************************/
42+
drvIna237(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
43+
const char *driver_name)
44+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Destructor for an INA237 sensor.
49+
*/
50+
/*******************************************************************************/
51+
~drvIna237();
52+
53+
/*******************************************************************************/
54+
/*!
55+
@brief Initializes the INA237 sensor and begins I2C.
56+
@returns True if initialized successfully, False otherwise.
57+
*/
58+
/*******************************************************************************/
59+
bool begin();
60+
61+
/*******************************************************************************/
62+
/*!
63+
@brief Reads a voltage sensor and converts the
64+
reading into the expected SI unit.
65+
@param voltageEvent
66+
voltage sensor reading, in volts.
67+
@returns True if the sensor event was obtained successfully, False
68+
otherwise.
69+
*/
70+
/*******************************************************************************/
71+
bool getEventVoltage(sensors_event_t *voltageEvent);
72+
73+
/**
74+
* @brief Get the current sensor event.
75+
*
76+
* @param currentEvent Pointer to the current sensor event.
77+
*
78+
* @returns True if the sensor event was obtained successfully, False
79+
* otherwise.
80+
*/
81+
bool getEventCurrent(sensors_event_t *currentEvent);
82+
83+
void ConfigureDefaultSensorTypes() override;
84+
85+
protected:
86+
Adafruit_INA237 *_ina237; ///< Pointer to INA237 sensor object
87+
};
88+
89+
#endif // DRV_INA237_H
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*!
2+
* @file drvIna238.cpp
3+
*
4+
* Device driver for the INA238 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+
16+
#include "drvIna238.h"
17+
#include <Adafruit_INA238.h>
18+
19+
/*******************************************************************************/
20+
/*!
21+
@brief Destructor for an INA238 sensor.
22+
*/
23+
/*******************************************************************************/
24+
drvIna238::~drvIna238() {
25+
if (_ina238) {
26+
delete _ina238;
27+
_ina238 = nullptr;
28+
}
29+
}
30+
31+
/*******************************************************************************/
32+
/*!
33+
@brief Initializes the INA238 sensor and begins I2C.
34+
@returns True if initialized successfully, False otherwise.
35+
*/
36+
/*******************************************************************************/
37+
bool drvIna238::begin() {
38+
_ina238 = new Adafruit_INA238();
39+
if (!_ina238->begin(_address, _i2c)) {
40+
WS_DEBUG_PRINTLN("INA238 failed to initialise!");
41+
return false;
42+
}
43+
// TODO: use setCalibration()
44+
45+
return true;
46+
}
47+
48+
/*******************************************************************************/
49+
/*!
50+
@brief Reads a voltage sensor and converts the
51+
reading into the expected SI unit.
52+
@param voltageEvent
53+
voltage sensor reading, in volts.
54+
@returns True if the sensor event was obtained successfully, False
55+
otherwise.
56+
*/
57+
/*******************************************************************************/
58+
bool drvIna238::getEventVoltage(sensors_event_t *voltageEvent) {
59+
voltageEvent->voltage = _ina238->getBusVoltage_V();
60+
return true;
61+
}
62+
63+
/**
64+
* @brief Get the current sensor event.
65+
*
66+
* @param currentEvent Pointer to the current sensor event.
67+
*
68+
* @returns True if the sensor event was obtained successfully, False
69+
* otherwise.
70+
*/
71+
bool drvIna238::getEventCurrent(sensors_event_t *currentEvent) {
72+
currentEvent->current = _ina238->getCurrent_mA();
73+
return true;
74+
}
75+
76+
void drvIna238::ConfigureDefaultSensorTypes() {
77+
_default_sensor_types_count = 2;
78+
_default_sensor_types[0] =
79+
wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
80+
_default_sensor_types[1] =
81+
wippersnapper_sensor_SensorType_SENSOR_TYPE_CURRENT;
82+
}

0 commit comments

Comments
 (0)