-
Notifications
You must be signed in to change notification settings - Fork 54
Ina238 + 237 via a refactor of INA260 too. #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
af62bcc
Specify 8MB partition for S3 feather (no PSRAM)
tyeth b391585
Add INA237 + INA238
tyeth 1c6813f
Resolve dependency chain - Split INA260 + 2XX to cpp files, forward d…
tyeth b4bf436
QTPY S3 No PSRAM uses 8MB partition
tyeth 7f0f1a2
clang format
tyeth c5b8706
clang format?
tyeth 3e2f7e4
Merge remote-tracking branch 'upstream/main' into ina238
tyeth 00deb17
Cleanup includes and unnecessary prints
tyeth 50b924c
Cleanup header docstrings, and remove <wire.h> (breaks pio)
tyeth a1cdc01
Add Wire include back to I2C Driver (for cpp include chain)
tyeth c9e728c
Merge branch 'upstream-main' into ina238
tyeth 08fa3ac
Migrate 4MB flash devices to No OTA esptool install method
tyeth 29ef4c2
make esptool not pinned
tyeth 860822a
Temporarily swap boards branch to migrate-4mb-to-esptool
tyeth 51b9335
Temporarily swap ci-arduino to ci-wippersnapper-2025-07-10-noota
tyeth 398fb7d
Merge branch 'main' into ina238
tyeth 94bcecc
Revert "Temporarily swap boards branch to migrate-4mb-to-esptool"
tyeth 51dcd25
Revert "Temporarily swap ci-arduino to ci-wippersnapper-2025-07-10-no…
tyeth 2e97574
Merge branch 'main' into ina238
tyeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
* | ||
tyeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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
45
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
114
src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/*! | ||
* @file WipperSnapper_I2C_Driver_INA238.cpp | ||
tyeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* 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; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.