diff --git a/.github/workflows/build-clang-doxy.yml b/.github/workflows/build-clang-doxy.yml index e5be7e9cd..ceca5a45e 100644 --- a/.github/workflows/build-clang-doxy.yml +++ b/.github/workflows/build-clang-doxy.yml @@ -27,12 +27,34 @@ jobs: [ "funhouse_noota", "esp32s3_devkitc_1_n8", + "magtag", + "metroesp32s2", + "feather_esp32s2", + "feather_esp32s2_tft", + "feather_esp32s2_reverse_tft", + "qtpy_esp32s2", + "feather_esp32s3_reverse_tft", + "qtpy_esp32s3_n4r2", + "feather_esp32s3_4mbflash_2mbpsram", + "feather_esp32s3_tft", "xiao_esp32s3", ] include: - offset: "0x0" - offset: "0x1000" arduino-platform: "funhouse_noota" + - offset: "0x1000" + arduino-platform: "magtag" + - offset: "0x1000" + arduino-platform: "metroesp32s2" + - offset: "0x1000" + arduino-platform: "feather_esp32s2" + - offset: "0x1000" + arduino-platform: "feather_esp32s2_tft" + - offset: "0x1000" + arduino-platform: "feather_esp32s2_reverse_tft" + - offset: "0x1000" + arduino-platform: "qtpy_esp32s2" steps: - name: "skip if unwanted" continue-on-error: true @@ -54,13 +76,14 @@ jobs: - uses: actions/checkout@v4 with: repository: adafruit/ci-arduino - ref: ci-wippersnapper + ref: ci-wippersnapper-2025-07-10-noota path: ci - name: Checkout Board Definitions uses: actions/checkout@v4 with: repository: adafruit/Wippersnapper_Boards path: ws-boards + ref: migrate-4mb-to-esptool - name: Install CI-Arduino run: bash ci/actions_install.sh - name: Install extra Arduino libraries @@ -200,19 +223,9 @@ jobs: matrix: arduino-platform: [ - "magtag", - "metroesp32s2", "metro_esp32s3", - "feather_esp32s2", - "feather_esp32s2_tft", - "feather_esp32s2_reverse_tft", "feather_esp32s3", - "feather_esp32s3_4mbflash_2mbpsram", - "feather_esp32s3_tft", "qtpy_esp32s3", - "qtpy_esp32s2", - "feather_esp32s3_reverse_tft", - "qtpy_esp32s3_n4r2", ] steps: - name: "skip if unwanted" diff --git a/library.properties b/library.properties index 6eb790b8b..ab298edf2 100644 --- a/library.properties +++ b/library.properties @@ -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 diff --git a/platformio.ini b/platformio.ini index a81b8c299..b40e19c4d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 8e8da72df..eeda800e6 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -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()) { diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 6c95eadce..c46c0e356 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -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" @@ -86,6 +88,9 @@ // forward decl. class Wippersnapper; +class WipperSnapper_I2C_Driver_INA260; +class WipperSnapper_I2C_Driver_INA237; +class WipperSnapper_I2C_Driver_INA238; /**************************************************************************/ /*! @@ -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; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h index 7c702b544..e76532719 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h @@ -16,8 +16,10 @@ #ifndef WipperSnapper_I2C_Driver_H #define WipperSnapper_I2C_Driver_H +#include "wippersnapper/i2c/v1/i2c.pb.h" #include #include +#include #define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000)) ///< Used for last sensor read time, initially set 24hrs ago (max period) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp new file mode 100644 index 000000000..a18c70bcc --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp @@ -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 + +/*******************************************************************************/ +/*! + @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; +} \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h new file mode 100644 index 000000000..9422a8f56 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h @@ -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 \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp new file mode 100644 index 000000000..c912dacdb --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.cpp @@ -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 + +/*******************************************************************************/ +/*! + @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; +} \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.h new file mode 100644 index 000000000..93bbb3d99 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA238.h @@ -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 \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp new file mode 100644 index 000000000..243212324 --- /dev/null +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.cpp @@ -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 + +/*******************************************************************************/ +/*! + @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; +} \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.h index 96fb46699..00136c7dc 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA260.h @@ -16,7 +16,9 @@ #define WipperSnapper_I2C_Driver_INA260_H #include "WipperSnapper_I2C_Driver.h" -#include + +// Forward declaration +class Adafruit_INA260; /**************************************************************************/ /*! @@ -25,75 +27,15 @@ /**************************************************************************/ class WipperSnapper_I2C_Driver_INA260 : public WipperSnapper_I2C_Driver { public: - /*******************************************************************************/ - /*! - @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; - } - - /*******************************************************************************/ - /*! - @brief Destructor for an INA260 sensor. - */ - /*******************************************************************************/ - ~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; - } + WipperSnapper_I2C_Driver_INA260(TwoWire *i2c, uint16_t sensorAddress); + ~WipperSnapper_I2C_Driver_INA260(); - /** - * @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; - } + bool begin(); + bool getEventVoltage(sensors_event_t *voltageEvent); + bool getEventCurrent(sensors_event_t *currentEvent); protected: - Adafruit_INA260 *_ina260; ///< Pointer to INA260 sensor object + Adafruit_INA260 *_ina260 = nullptr; ///< Pointer to INA260 sensor object }; #endif // WipperSnapper_I2C_Driver_INA260 \ No newline at end of file