Skip to content

Commit 8cd03e5

Browse files
authored
Merge pull request #6 from tyeth/offline-mode-INA-refactor-from-flat0808
Offline mode ina refactor from flat0808
2 parents 00adc77 + f735ef0 commit 8cd03e5

15 files changed

+674
-43
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/Wippersnapper_V2.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
*/
3131
#ifdef WS_DEBUG
3232
#define WS_DEBUG_PRINT(...) \
33-
{ \
34-
WS_PRINTER.print(__VA_ARGS__); \
35-
} /**< Print debug message to serial */
33+
{ WS_PRINTER.print(__VA_ARGS__); } /**< Print debug message to serial */
3634
#define WS_DEBUG_PRINTLN(...) \
3735
{ \
3836
WS_PRINTER.println(__VA_ARGS__); \
@@ -44,11 +42,9 @@
4442
} /**< Print debug message in hexadecimal */
4543
#else
4644
#define WS_DEBUG_PRINT(...) \
47-
{ \
48-
} /**< Debug print */
45+
{} /**< Debug print */
4946
#define WS_DEBUG_PRINTLN(...) \
50-
{ \
51-
} /**< Debug println */
47+
{} /**< Debug println */
5248
#endif
5349

5450
/*!

src/components/i2c/controller.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ 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+
{"ina228",
137+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
138+
const char *driver_name) -> drvBase * {
139+
return new drvIna228(i2c, addr, mux_channel, driver_name);
140+
}},
141+
{"ina237",
142+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
143+
const char *driver_name) -> drvBase * {
144+
return new drvIna237(i2c, addr, mux_channel, driver_name);
145+
}},
146+
{"ina238",
147+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
148+
const char *driver_name) -> drvBase * {
149+
return new drvIna238(i2c, addr, mux_channel, driver_name);
150+
}},
151+
{"ina260",
152+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
153+
const char *driver_name) -> drvBase * {
154+
return new drvIna260(i2c, addr, mux_channel, driver_name);
155+
}},
136156
{"lc709203f",
137157
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
138158
const char *driver_name) -> drvBase * {
@@ -372,11 +392,12 @@ static const std::unordered_map<uint16_t, std::vector<const char *>>
372392
{0x38, {"aht20", "max17048"}},
373393
{0x39, {"tsl2591"}},
374394
{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"}},
395+
{"htu21d", "htu31d", "ina219", "ina228", "ina237", "ina238", "ina260",
396+
"ms8607", "si7021", "stemma_soil"}},
397+
{0x41, {"htu31d", "ina219", "ina228", "ina237", "ina238", "ina260"}},
398+
{0x44,
399+
{"hdc302x", "ina228", "ina237", "ina238", "ina260", "sht3x", "sht4x"}},
400+
{0x45, {"hdc302x", "ina228", "ina237", "ina238", "ina260", "sht3x"}},
380401
{0x46, {"hdc302x"}},
381402
{0x47, {"hdc302x"}},
382403
{0x48, {"adt7410", "pct2075", "tmp117"}},

src/components/i2c/controller.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "drivers/drvHts221.h"
3434
#include "drivers/drvHtu21d.h"
3535
#include "drivers/drvIna219.h"
36+
#include "drivers/drvIna228.h"
37+
#include "drivers/drvIna237.h"
38+
#include "drivers/drvIna238.h"
39+
#include "drivers/drvIna260.h"
3640
#include "drivers/drvLc709203f.h"
3741
#include "drivers/drvLps22hb.h"
3842
#include "drivers/drvLps25hb.h"

src/components/i2c/drivers/drvBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
#ifndef DRV_BASE_H
1717
#define DRV_BASE_H
18+
#include "helpers/ws_helper_macros.h"
1819
#include <Adafruit_Sensor.h>
1920
#include <Arduino.h>
21+
#include <Wire.h>
22+
#include <map>
2023
#include <protos/i2c.pb.h>
2124

2225
#define NO_MUX_CH 0xFFFF; ///< No MUX channel specified
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*!
2+
* @file drvIna228.cpp
3+
*
4+
* Device driver for the INA228 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 "drvIna228.h"
17+
#include <Adafruit_INA228.h>
18+
19+
/*******************************************************************************/
20+
/*!
21+
@brief Destructor for an INA228 sensor.
22+
*/
23+
/*******************************************************************************/
24+
drvIna228::~drvIna228() {
25+
if (_ina228) {
26+
delete _ina228;
27+
_ina228 = nullptr;
28+
}
29+
}
30+
31+
/*******************************************************************************/
32+
/*!
33+
@brief Initializes the INA228 sensor and begins I2C.
34+
@returns True if initialized successfully, False otherwise.
35+
*/
36+
/*******************************************************************************/
37+
bool drvIna228::begin() {
38+
_ina228 = new Adafruit_INA228();
39+
if (!_ina228->begin(_address, _i2c)) {
40+
WS_DEBUG_PRINTLN("INA228 failed to initialise!");
41+
return false;
42+
}
43+
44+
_ina228->setShunt(0.015, 10.0);
45+
if (_ina228->getCurrentConversionTime() != INA228_TIME_280_us) {
46+
_ina228->setCurrentConversionTime(INA228_TIME_280_us);
47+
}
48+
if (_ina228->getAveragingCount() != INA228_COUNT_16) {
49+
_ina228->setAveragingCount(INA228_COUNT_16);
50+
}
51+
if (_ina228->getVoltageConversionTime() != INA228_TIME_150_us) {
52+
_ina228->setVoltageConversionTime(INA228_TIME_150_us);
53+
}
54+
return true;
55+
}
56+
57+
/*******************************************************************************/
58+
/*!
59+
@brief Reads a voltage sensor and converts the
60+
reading into the expected SI unit.
61+
@param voltageEvent
62+
voltage sensor reading, in volts.
63+
@returns True if the sensor event was obtained successfully, False
64+
otherwise.
65+
*/
66+
/*******************************************************************************/
67+
bool drvIna228::getEventVoltage(sensors_event_t *voltageEvent) {
68+
voltageEvent->voltage = _ina228->getBusVoltage_V();
69+
return true;
70+
}
71+
72+
/**
73+
* @brief Get the current sensor event.
74+
*
75+
* @param currentEvent Pointer to the current sensor event.
76+
*
77+
* @returns True if the sensor event was obtained successfully, False
78+
* otherwise.
79+
*/
80+
bool drvIna228::getEventCurrent(sensors_event_t *currentEvent) {
81+
currentEvent->current = _ina228->getCurrent_mA();
82+
return true;
83+
}
84+
85+
void drvIna228::ConfigureDefaultSensorTypes() {
86+
_default_sensor_types_count = 2;
87+
_default_sensor_types[0] =
88+
wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
89+
_default_sensor_types[1] =
90+
wippersnapper_sensor_SensorType_SENSOR_TYPE_CURRENT;
91+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*!
2+
* @file drvIna228.h
3+
*
4+
* Device driver for the INA228 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_INA228_H
16+
#define DRV_INA228_H
17+
18+
#include "drvBase.h"
19+
20+
class Adafruit_INA228;
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a INA228 sensor.
25+
*/
26+
/**************************************************************************/
27+
class drvIna228 : public drvBase {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for a INA228 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+
drvIna228(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 INA228 sensor.
49+
*/
50+
/*******************************************************************************/
51+
~drvIna228();
52+
53+
/*******************************************************************************/
54+
/*!
55+
@brief Initializes the INA228 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_INA228 *_ina228; ///< Pointer to INA228 sensor object
87+
};
88+
89+
#endif // DRV_INA228_H
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
44+
_ina237->setShunt(0.015, 10.0);
45+
if (_ina237->getCurrentConversionTime() != INA2XX_TIME_280_us) {
46+
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);
47+
}
48+
if (_ina237->getAveragingCount() != INA2XX_COUNT_16) {
49+
_ina237->setAveragingCount(INA2XX_COUNT_16);
50+
}
51+
if (_ina237->getVoltageConversionTime() != INA2XX_TIME_150_us) {
52+
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
53+
}
54+
55+
return true;
56+
}
57+
58+
/*******************************************************************************/
59+
/*!
60+
@brief Reads a voltage sensor and converts the
61+
reading into the expected SI unit.
62+
@param voltageEvent
63+
voltage sensor reading, in volts.
64+
@returns True if the sensor event was obtained successfully, False
65+
otherwise.
66+
*/
67+
/*******************************************************************************/
68+
bool drvIna237::getEventVoltage(sensors_event_t *voltageEvent) {
69+
voltageEvent->voltage = _ina237->getBusVoltage_V();
70+
return true;
71+
}
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 drvIna237::getEventCurrent(sensors_event_t *currentEvent) {
82+
currentEvent->current = _ina237->getCurrent_mA();
83+
return true;
84+
}
85+
86+
void drvIna237::ConfigureDefaultSensorTypes() {
87+
_default_sensor_types_count = 2;
88+
_default_sensor_types[0] =
89+
wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
90+
_default_sensor_types[1] =
91+
wippersnapper_sensor_SensorType_SENSOR_TYPE_CURRENT;
92+
}

0 commit comments

Comments
 (0)