From 736d1ba927a8a5ecf00bead489dd0e1b430a9cbc Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 7 Nov 2024 20:30:20 +0000 Subject: [PATCH 1/7] Fix SCD4x and SCD30 (alreadyRecentlyRead) + MetroS3 debug target --- platformio.ini | 9 ++ .../drivers/WipperSnapper_I2C_Driver_SCD30.h | 2 +- .../drivers/WipperSnapper_I2C_Driver_SCD4X.h | 92 ++++++++++++++----- 3 files changed, 78 insertions(+), 25 deletions(-) diff --git a/platformio.ini b/platformio.ini index dd4acafc2..50c184da2 100644 --- a/platformio.ini +++ b/platformio.ini @@ -271,6 +271,15 @@ build_flags = -DARDUINO_METRO_ESP32S3 -DBOARD_HAS_PSRAM board_build.partitions = tinyuf2-partitions-16MB.csv extra_scripts = pre:rename_usb_config.py +; Adafruit Metro ESP32-S3 +[env:adafruit_metro_esp32s3_debug] +extends = common:esp32 +board = adafruit_metro_esp32s3 +build_flags = -DARDUINO_METRO_ESP32S3 -DBOARD_HAS_PSRAM -DCFG_TUSB_DEBUG=1 -DDEBUG=1 -DESP_LOG_LEVEL=ESP_LOG_VERBOSE -DARDUINO_CORE_DEBUG_LEVEL=5 -DCORE_DEBUG_LEVEL=5 -DARDUHAL_LOG_LEVEL=5 -DARDUINO_USB_CDC_ON_BOOT=1 +;set partition to tinyuf2-partitions-16MB.csv as of idf 5.1 +board_build.partitions = tinyuf2-partitions-16MB.csv +extra_scripts = pre:rename_usb_config.py + ; Adafruit Funhouse ESP32-S2 [env:adafruit_funhouse_esp32s2] extends = common:esp32 diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h index fcaa1d94d..ff9b5386d 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h @@ -59,7 +59,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { @returns True if the sensor was recently read, False otherwise. */ bool alreadyRecentlyRead() { - return (_lastRead != 0 && millis() - _lastRead) < 1000; + return _lastRead != 0 && millis() - _lastRead < 1000; } /*******************************************************************************/ diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h index dfe2c1de3..9b2db0f8b 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -55,38 +55,78 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { _scd->begin(*_i2c); // stop previously started measurement - if (_scd->stopPeriodicMeasurement()) + if (_scd->stopPeriodicMeasurement() != 0) { return false; + } // start measurements - if (_scd->startPeriodicMeasurement()) + if (_scd->startPeriodicMeasurement() != 0) { return false; + } return true; } - /********************************************************************************/ + /*******************************************************************************/ + /*! + @brief Checks if sensor was read within last 1s, or is the first read. + @returns True if the sensor was recently read, False otherwise. + */ + bool alreadyRecentlyRead() { + return _lastRead != 0 && millis() - _lastRead < 1000; + } + + /*******************************************************************************/ /*! - @brief Attempts to read the SCD4x's sensor measurements - @returns True if the measurements were read without errors, False - if read errors occured or if sensor did not have data ready. + @brief Checks if the sensor is ready to be read + @returns True if the sensor is ready, False otherwise. */ - /********************************************************************************/ - bool readSensorMeasurements() { - uint16_t error; + /*******************************************************************************/ + bool sensorReady() { bool isDataReady = false; - delay(100); + uint16_t error = _scd->getDataReadyFlag(isDataReady); + if (error != 0 || !isDataReady) { + // failed, one more quick attempt + delay(100); + error = _scd->getDataReadyFlag(isDataReady); + if (error != 0 || !isDataReady) { + return false; + } + } + return true; + } + + /*******************************************************************************/ + /*! + @brief Reads the sensor. + @returns True if the sensor was read successfully, False otherwise. + */ + /*******************************************************************************/ + bool readSensorData() { + // dont read sensor more than once per second + if (alreadyRecentlyRead()) { + return true; + } - // Check if data is ready - error = _scd->getDataReadyFlag(isDataReady); - if (error || !isDataReady) + if (!sensorReady()) { return false; + } + // Read SCD4x measurement - error = _scd->readMeasurement(_co2, _temperature, _humidity); - if (error || _co2 == 0) + uint16_t error = _scd->readMeasurement(_co2, _temperature, _humidity); + if (error != 0 || _co2 == 0) { + WS_DEBUG_PRINT("Error reading SCD4x measurements: #"); + WS_DEBUG_PRINT(error); + WS_DEBUG_PRINT(" CO2: "); + WS_DEBUG_PRINTLN(_co2); + WS_DEBUG_PRINT("Temp: "); + WS_DEBUG_PRINT(_temperature); + WS_DEBUG_PRINT(" Humidity: "); + WS_DEBUG_PRINTLN(_humidity); return false; - + } + _lastRead = millis(); return true; } @@ -101,8 +141,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { /*******************************************************************************/ bool getEventAmbientTemp(sensors_event_t *tempEvent) { // read all sensor measurements - if (!readSensorMeasurements()) + if (!readSensorData()) { return false; + } tempEvent->temperature = _temperature; return true; @@ -119,8 +160,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { /*******************************************************************************/ bool getEventRelativeHumidity(sensors_event_t *humidEvent) { // read all sensor measurements - if (!readSensorMeasurements()) + if (!readSensorData()) { return false; + } humidEvent->relative_humidity = _humidity; return true; @@ -137,18 +179,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { /*******************************************************************************/ bool getEventCO2(sensors_event_t *co2Event) { // read all sensor measurements - if (!readSensorMeasurements()) + if (!readSensorData()) { return false; + } co2Event->CO2 = (float)_co2; return true; } -protected: - SensirionI2CScd4x *_scd; ///< SCD4x driver object - uint16_t _co2; ///< SCD4x co2 reading - float _temperature; ///< SCD4x temperature reading - float _humidity; ///< SCD4x humidity reading +private: + SensirionI2CScd4x *_scd = nullptr; ///< SCD4x driver object + uint16_t _co2 = 0; ///< SCD4x co2 reading + float _temperature = 20.0f; ///< SCD4x temperature reading + float _humidity = 50.0f; ///< SCD4x humidity reading + ulong _lastRead = 0; ///< Last time the sensor was read }; #endif // WipperSnapper_I2C_Driver_SCD4X \ No newline at end of file From cdb4d0258d3486acf46dd939280e7729c23d5d5e Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 7 Nov 2024 23:32:20 +0000 Subject: [PATCH 2/7] Swap Previous Period helper functions to ulong --- platformio.ini | 1 + src/components/i2c/WipperSnapper_I2C.cpp | 6 +- src/components/i2c/WipperSnapper_I2C.h | 4 +- .../i2c/drivers/WipperSnapper_I2C_Driver.h | 88 +++++++++---------- .../drivers/WipperSnapper_I2C_Driver_SCD4X.h | 10 +-- 5 files changed, 51 insertions(+), 58 deletions(-) diff --git a/platformio.ini b/platformio.ini index 50c184da2..ea78dd66f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -275,6 +275,7 @@ extra_scripts = pre:rename_usb_config.py [env:adafruit_metro_esp32s3_debug] extends = common:esp32 board = adafruit_metro_esp32s3 +build_type = debug build_flags = -DARDUINO_METRO_ESP32S3 -DBOARD_HAS_PSRAM -DCFG_TUSB_DEBUG=1 -DDEBUG=1 -DESP_LOG_LEVEL=ESP_LOG_VERBOSE -DARDUINO_CORE_DEBUG_LEVEL=5 -DCORE_DEBUG_LEVEL=5 -DARDUHAL_LOG_LEVEL=5 -DARDUINO_USB_CDC_ON_BOOT=1 ;set partition to tinyuf2-partitions-16MB.csv as of idf 5.1 board_build.partitions = tinyuf2-partitions-16MB.csv diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index bd47877e4..3c82369bd 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -1328,8 +1328,8 @@ void WipperSnapper_Component_I2C::sensorEventRead( unsigned long curTime, wippersnapper_signal_v1_I2CResponse *msgi2cResponse, bool (WipperSnapper_I2C_Driver::*getEventFunc)(sensors_event_t *), long (WipperSnapper_I2C_Driver::*getPeriodFunc)(), - long (WipperSnapper_I2C_Driver::*getPeriodPrvFunc)(), - void (WipperSnapper_I2C_Driver::*setPeriodPrvFunc)(long), + ulong (WipperSnapper_I2C_Driver::*getPeriodPrvFunc)(), + void (WipperSnapper_I2C_Driver::*setPeriodPrvFunc)(ulong), wippersnapper_i2c_v1_SensorType sensorType, const char *sensorName, const char *unit, sensors_event_t event, float sensors_event_t::*valueMember, bool &sensorsReturningFalse, @@ -1337,7 +1337,7 @@ void WipperSnapper_Component_I2C::sensorEventRead( // sensorName used for prefix + error message, units is value suffix curTime = millis(); if (((*iter)->*getPeriodFunc)() != 0L && - curTime - ((*iter)->*getPeriodPrvFunc)() > ((*iter)->*getPeriodFunc)()) { + curTime - ((*iter)->*getPeriodPrvFunc)() > (ulong)((*iter)->*getPeriodFunc)()) { // within the period, read the sensor if (((*iter)->*getEventFunc)(&event)) { float value; diff --git a/src/components/i2c/WipperSnapper_I2C.h b/src/components/i2c/WipperSnapper_I2C.h index 58cd5c5fc..c82c11c08 100644 --- a/src/components/i2c/WipperSnapper_I2C.h +++ b/src/components/i2c/WipperSnapper_I2C.h @@ -106,8 +106,8 @@ class WipperSnapper_Component_I2C { wippersnapper_signal_v1_I2CResponse *msgi2cResponse, bool (WipperSnapper_I2C_Driver::*getEventFunc)(sensors_event_t *), long (WipperSnapper_I2C_Driver::*getPeriodFunc)(), - long (WipperSnapper_I2C_Driver::*getPeriodPrvFunc)(), - void (WipperSnapper_I2C_Driver::*setPeriodPrvFunc)(long), + ulong (WipperSnapper_I2C_Driver::*getPeriodPrvFunc)(), + void (WipperSnapper_I2C_Driver::*setPeriodPrvFunc)(ulong), wippersnapper_i2c_v1_SensorType sensorType, const char *sensorName, const char *unit, sensors_event_t event, float sensors_event_t::*valueMember, bool &sensorsReturningFalse, diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h index 809b03b56..cca7adfa8 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h @@ -190,7 +190,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the co2 sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorCO2PeriodPrv() { return _CO2SensorPeriodPrv; } + virtual ulong getSensorCO2PeriodPrv() { return _CO2SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -199,7 +199,7 @@ class WipperSnapper_I2C_Driver { The time when the co2 sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorCO2PeriodPrv(long period) { + virtual void setSensorCO2PeriodPrv(ulong period) { _CO2SensorPeriodPrv = period; } @@ -236,7 +236,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the eCO2 sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorECO2PeriodPrv() { return _ECO2SensorPeriodPrv; } + virtual ulong getSensorECO2PeriodPrv() { return _ECO2SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -245,7 +245,7 @@ class WipperSnapper_I2C_Driver { The time when the CO2 sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorECO2PeriodPrv(long period) { + virtual void setSensorECO2PeriodPrv(ulong period) { _ECO2SensorPeriodPrv = period; } @@ -282,7 +282,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the TVOC sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorTVOCPeriodPrv() { return _TVOCSensorPeriodPrv; } + virtual ulong getSensorTVOCPeriodPrv() { return _TVOCSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -291,7 +291,7 @@ class WipperSnapper_I2C_Driver { The time when the TVOC sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorTVOCPeriodPrv(long period) { + virtual void setSensorTVOCPeriodPrv(ulong period) { _TVOCSensorPeriodPrv = period; } @@ -329,7 +329,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorAmbientTempPeriodPrv() { return _tempSensorPeriodPrv; } + virtual ulong getSensorAmbientTempPeriodPrv() { return _tempSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -340,7 +340,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorAmbientTempPeriodPrv(long periodPrv) { + virtual void setSensorAmbientTempPeriodPrv(ulong periodPrv) { _tempSensorPeriodPrv = periodPrv; } @@ -378,7 +378,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the humidity sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorRelativeHumidityPeriodPrv() { + virtual ulong getSensorRelativeHumidityPeriodPrv() { return _humidSensorPeriodPrv; } @@ -389,7 +389,7 @@ class WipperSnapper_I2C_Driver { The time when the temperature sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorRelativeHumidityPeriodPrv(long periodPrv) { + virtual void setSensorRelativeHumidityPeriodPrv(ulong periodPrv) { _humidSensorPeriodPrv = periodPrv; } @@ -427,7 +427,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the pressure sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorPressurePeriodPrv() { return _pressureSensorPeriodPrv; } + virtual ulong getSensorPressurePeriodPrv() { return _pressureSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -436,7 +436,7 @@ class WipperSnapper_I2C_Driver { The time when the pressure sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorPressurePeriodPrv(long period) { + virtual void setSensorPressurePeriodPrv(ulong period) { _pressureSensorPeriodPrv = period; } @@ -474,7 +474,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the Altitude sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorAltitudePeriodPrv() { return _altitudeSensorPeriodPrv; } + virtual ulong getSensorAltitudePeriodPrv() { return _altitudeSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -483,7 +483,7 @@ class WipperSnapper_I2C_Driver { The time when the Altitude sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorAltitudePeriodPrv(long period) { + virtual void setSensorAltitudePeriodPrv(ulong period) { _altitudeSensorPeriodPrv = period; } @@ -523,7 +523,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorObjectTempPeriodPrv() { + virtual ulong getSensorObjectTempPeriodPrv() { return _objectTempSensorPeriodPrv; } @@ -535,7 +535,7 @@ class WipperSnapper_I2C_Driver { The time when the object temperature sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorObjectTempPeriodPrv(long period) { + virtual void setSensorObjectTempPeriodPrv(ulong period) { _objectTempSensorPeriodPrv = period; } @@ -575,7 +575,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorLightPeriodPrv() { return _lightSensorPeriodPrv; } + virtual ulong getSensorLightPeriodPrv() { return _lightSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -585,7 +585,7 @@ class WipperSnapper_I2C_Driver { The time when the light sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorLightPeriodPrv(long period) { + virtual void setSensorLightPeriodPrv(ulong period) { _lightSensorPeriodPrv = period; } @@ -625,7 +625,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorPM10_STDPeriodPrv() { return _PM10SensorPeriodPrv; } + virtual ulong getSensorPM10_STDPeriodPrv() { return _PM10SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -635,7 +635,7 @@ class WipperSnapper_I2C_Driver { The time when the light sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorPM10_STDPeriodPrv(long period) { + virtual void setSensorPM10_STDPeriodPrv(ulong period) { _PM10SensorPeriodPrv = period; } @@ -675,7 +675,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorPM25_STDPeriodPrv() { return _PM25SensorPeriodPrv; } + virtual ulong getSensorPM25_STDPeriodPrv() { return _PM25SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -685,7 +685,7 @@ class WipperSnapper_I2C_Driver { The time when the light sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorPM25_STDPeriodPrv(long period) { + virtual void setSensorPM25_STDPeriodPrv(ulong period) { _PM25SensorPeriodPrv = period; } @@ -725,7 +725,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorPM100_STDPeriodPrv() { return _PM100SensorPeriodPrv; } + virtual ulong getSensorPM100_STDPeriodPrv() { return _PM100SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -735,7 +735,7 @@ class WipperSnapper_I2C_Driver { The time when the light sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorPM100_STDPeriodPrv(long period) { + virtual void setSensorPM100_STDPeriodPrv(ulong period) { _PM100SensorPeriodPrv = period; } @@ -777,7 +777,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorUnitlessPercentPeriodPrv() { + virtual ulong getSensorUnitlessPercentPeriodPrv() { return _unitlessPercentPeriodPrv; } @@ -789,7 +789,7 @@ class WipperSnapper_I2C_Driver { The time when the unitless % sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorUnitlessPercentPeriodPrv(long period) { + virtual void setSensorUnitlessPercentPeriodPrv(ulong period) { _unitlessPercentPeriodPrv = period; } @@ -827,7 +827,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the voltage sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorVoltagePeriodPrv() { return _voltagePeriodPrv; } + virtual ulong getSensorVoltagePeriodPrv() { return _voltagePeriodPrv; } /*******************************************************************************/ /*! @@ -836,7 +836,7 @@ class WipperSnapper_I2C_Driver { The time when the voltage sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorVoltagePeriodPrv(long period) { + virtual void setSensorVoltagePeriodPrv(ulong period) { _voltagePeriodPrv = period; } @@ -874,7 +874,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the current sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorCurrentPeriodPrv() { return _currentPeriodPrv; } + virtual ulong getSensorCurrentPeriodPrv() { return _currentPeriodPrv; } /*******************************************************************************/ /*! @@ -883,7 +883,7 @@ class WipperSnapper_I2C_Driver { The time when the current sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorCurrentPeriodPrv(long period) { + virtual void setSensorCurrentPeriodPrv(ulong period) { _currentPeriodPrv = period; } @@ -921,7 +921,7 @@ class WipperSnapper_I2C_Driver { @returns Time when the raw sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual long getSensorRawPeriodPrv() { return _rawSensorPeriodPrv; } + virtual ulong getSensorRawPeriodPrv() { return _rawSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -930,7 +930,7 @@ class WipperSnapper_I2C_Driver { The time when the raw sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorRawPeriodPrv(long period) { + virtual void setSensorRawPeriodPrv(ulong period) { _rawSensorPeriodPrv = period; } @@ -978,7 +978,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorAmbientTempFPeriodPrv() { + virtual ulong getSensorAmbientTempFPeriodPrv() { return _ambientTempFPeriodPrv; } @@ -991,7 +991,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorAmbientTempFPeriodPrv(long period) { + virtual void setSensorAmbientTempFPeriodPrv(ulong period) { _ambientTempFPeriodPrv = period; } @@ -1036,7 +1036,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorObjectTempFPeriodPrv() { return _objectTempFPeriodPrv; } + virtual ulong getSensorObjectTempFPeriodPrv() { return _objectTempFPeriodPrv; } /*******************************************************************************/ /*! @@ -1047,7 +1047,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorObjectTempFPeriodPrv(long period) { + virtual void setSensorObjectTempFPeriodPrv(ulong period) { _objectTempFPeriodPrv = period; } @@ -1092,7 +1092,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorGasResistancePeriodPrv() { + virtual ulong getSensorGasResistancePeriodPrv() { return _gasResistancePeriodPrv; } @@ -1105,7 +1105,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorGasResistancePeriodPrv(long period) { + virtual void setSensorGasResistancePeriodPrv(ulong period) { _gasResistancePeriodPrv = period; } @@ -1145,7 +1145,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorNOxIndexPeriodPrv() { return _NOxIndexPeriodPrv; } + virtual ulong getSensorNOxIndexPeriodPrv() { return _NOxIndexPeriodPrv; } /*******************************************************************************/ /*! @@ -1156,7 +1156,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorNOxIndexPeriodPrv(long period) { + virtual void setSensorNOxIndexPeriodPrv(ulong period) { _NOxIndexPeriodPrv = period; } @@ -1196,7 +1196,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorVOCIndexPeriodPrv() { return _VOCIndexPeriodPrv; } + virtual ulong getSensorVOCIndexPeriodPrv() { return _VOCIndexPeriodPrv; } /*******************************************************************************/ /*! @@ -1207,7 +1207,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorVOCIndexPeriodPrv(long period) { + virtual void setSensorVOCIndexPeriodPrv(ulong period) { _VOCIndexPeriodPrv = period; } @@ -1275,7 +1275,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long SensorProximityPeriodPrv() { return _proximitySensorPeriodPrv; } + virtual ulong SensorProximityPeriodPrv() { return _proximitySensorPeriodPrv; } /*******************************************************************************/ /*! @@ -1285,7 +1285,7 @@ class WipperSnapper_I2C_Driver { The time when the proximity sensor was queried last. */ /*******************************************************************************/ - virtual void setSensorProximityPeriodPrv(long period) { + virtual void setSensorProximityPeriodPrv(ulong period) { _proximitySensorPeriodPrv = period; } diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h index 9b2db0f8b..1eccdc79a 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -116,14 +116,6 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { // Read SCD4x measurement uint16_t error = _scd->readMeasurement(_co2, _temperature, _humidity); if (error != 0 || _co2 == 0) { - WS_DEBUG_PRINT("Error reading SCD4x measurements: #"); - WS_DEBUG_PRINT(error); - WS_DEBUG_PRINT(" CO2: "); - WS_DEBUG_PRINTLN(_co2); - WS_DEBUG_PRINT("Temp: "); - WS_DEBUG_PRINT(_temperature); - WS_DEBUG_PRINT(" Humidity: "); - WS_DEBUG_PRINTLN(_humidity); return false; } _lastRead = millis(); @@ -187,7 +179,7 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { return true; } -private: +protected: SensirionI2CScd4x *_scd = nullptr; ///< SCD4x driver object uint16_t _co2 = 0; ///< SCD4x co2 reading float _temperature = 20.0f; ///< SCD4x temperature reading From b4c83b820850aaca7ee479a1175763049b796ecb Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 7 Nov 2024 23:32:30 +0000 Subject: [PATCH 3/7] Set SCD4x to not read until T+5s --- src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h index 1eccdc79a..53dc04f05 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -64,6 +64,12 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { return false; } + // Takes 5seconds to have data ready, don't queue read until then + ulong currentTime = millis() - 25000uL; // T-25s, so 5s time for 30s polling + this->setSensorCO2PeriodPrv(currentTime); + this->setSensorAmbientTempFPeriodPrv(currentTime); + this->setSensorAmbientTempPeriodPrv(currentTime); + this->setSensorRelativeHumidityPeriodPrv(currentTime); return true; } From 20526213fc0af004c664f87282367289002b8b4b Mon Sep 17 00:00:00 2001 From: tyeth Date: Thu, 7 Nov 2024 23:50:59 +0000 Subject: [PATCH 4/7] Switch to using sensor period for T-5sec delay (pollPeriod begin arg) --- src/components/i2c/WipperSnapper_I2C.cpp | 2 +- src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 3c82369bd..03056ec1a 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -565,7 +565,7 @@ bool WipperSnapper_Component_I2C::initI2CDevice( WS_DEBUG_PRINTLN("VEML7700 Initialized Successfully!"); } else if (strcmp("scd40", msgDeviceInitReq->i2c_device_name) == 0) { _scd40 = new WipperSnapper_I2C_Driver_SCD4X(this->_i2c, i2cAddress); - if (!_scd40->begin()) { + if (!_scd40->begin(msgDeviceInitReq->i2c_device_properties[0].sensor_period)) { WS_DEBUG_PRINTLN("ERROR: Failed to initialize SCD4x!"); _busStatusResponse = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h index 53dc04f05..b48deb24d 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -47,10 +47,12 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { /*******************************************************************************/ /*! @brief Initializes the SCD40 sensor and begins I2C. + @param pollPeriod + The sensor's polling period in milliseconds. @returns True if initialized successfully, False otherwise. */ /*******************************************************************************/ - bool begin() { + bool begin(ulong pollPeriod) { _scd = new SensirionI2CScd4x(); _scd->begin(*_i2c); @@ -65,7 +67,7 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { } // Takes 5seconds to have data ready, don't queue read until then - ulong currentTime = millis() - 25000uL; // T-25s, so 5s time for 30s polling + ulong currentTime = millis() - (pollPeriod * 1000 - 5000); // 5s time this->setSensorCO2PeriodPrv(currentTime); this->setSensorAmbientTempFPeriodPrv(currentTime); this->setSensorAmbientTempPeriodPrv(currentTime); From 7104bf6717edc75a4e050ee46c184092beb1ff14 Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 8 Nov 2024 15:33:02 +0000 Subject: [PATCH 5/7] Switch SCD30 to use read() instead of getEvent() --- .../drivers/WipperSnapper_I2C_Driver_SCD30.h | 18 ++++++++++-------- .../drivers/WipperSnapper_I2C_Driver_SCD4X.h | 5 ++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h index ff9b5386d..d9fe5869e 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h @@ -95,10 +95,12 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { return false; } - if (_scd->getEvent(&_humidity, &_temperature)) { + if (!_scd->read()) { return false; } - _CO2.CO2 = _scd->CO2; + _CO2 = _scd->CO2; + _humidity = _scd->relative_humidity; + _temperature = _scd->temperature; _lastRead = millis(); return true; } @@ -118,7 +120,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { return false; } - tempEvent = &_temperature; + tempEvent->temperature = _temperature; return true; } @@ -137,7 +139,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { return false; } - humidEvent = &_humidity; + humidEvent->relative_humidity = _humidity; return true; } @@ -156,16 +158,16 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { return false; } - co2Event = &_CO2; + co2Event->CO2 = _CO2; return true; } protected: Adafruit_SCD30 *_scd = nullptr; ///< SCD30 driver object ulong _lastRead = 0; ///< Last time the sensor was read - sensors_event_t _temperature; ///< Temperature - sensors_event_t _humidity; ///< Relative Humidity - sensors_event_t _CO2; ///< CO2 + float _temperature; ///< Temperature + float _humidity; ///< Relative Humidity + float _CO2; ///< CO2 }; #endif // WipperSnapper_I2C_Driver_SCD30 \ No newline at end of file diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h index b48deb24d..020e48b42 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -120,7 +120,6 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { return false; } - // Read SCD4x measurement uint16_t error = _scd->readMeasurement(_co2, _temperature, _humidity); if (error != 0 || _co2 == 0) { @@ -190,8 +189,8 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { protected: SensirionI2CScd4x *_scd = nullptr; ///< SCD4x driver object uint16_t _co2 = 0; ///< SCD4x co2 reading - float _temperature = 20.0f; ///< SCD4x temperature reading - float _humidity = 50.0f; ///< SCD4x humidity reading + float _temperature = 20.0f; ///< SCD4x temperature reading + float _humidity = 50.0f; ///< SCD4x humidity reading ulong _lastRead = 0; ///< Last time the sensor was read }; From 8eab8928bb4120930b6d5ca65e752a0df1297ba1 Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 8 Nov 2024 15:49:18 +0000 Subject: [PATCH 6/7] clang format --- src/components/i2c/WipperSnapper_I2C.cpp | 6 ++++-- .../i2c/drivers/WipperSnapper_I2C_Driver.h | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index 03056ec1a..786004faa 100644 --- a/src/components/i2c/WipperSnapper_I2C.cpp +++ b/src/components/i2c/WipperSnapper_I2C.cpp @@ -565,7 +565,8 @@ bool WipperSnapper_Component_I2C::initI2CDevice( WS_DEBUG_PRINTLN("VEML7700 Initialized Successfully!"); } else if (strcmp("scd40", msgDeviceInitReq->i2c_device_name) == 0) { _scd40 = new WipperSnapper_I2C_Driver_SCD4X(this->_i2c, i2cAddress); - if (!_scd40->begin(msgDeviceInitReq->i2c_device_properties[0].sensor_period)) { + if (!_scd40->begin( + msgDeviceInitReq->i2c_device_properties[0].sensor_period)) { WS_DEBUG_PRINTLN("ERROR: Failed to initialize SCD4x!"); _busStatusResponse = wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL; @@ -1337,7 +1338,8 @@ void WipperSnapper_Component_I2C::sensorEventRead( // sensorName used for prefix + error message, units is value suffix curTime = millis(); if (((*iter)->*getPeriodFunc)() != 0L && - curTime - ((*iter)->*getPeriodPrvFunc)() > (ulong)((*iter)->*getPeriodFunc)()) { + curTime - ((*iter)->*getPeriodPrvFunc)() > + (ulong)((*iter)->*getPeriodFunc)()) { // within the period, read the sensor if (((*iter)->*getEventFunc)(&event)) { float value; diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h index cca7adfa8..18baebd6e 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h @@ -427,7 +427,9 @@ class WipperSnapper_I2C_Driver { @returns Time when the pressure sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual ulong getSensorPressurePeriodPrv() { return _pressureSensorPeriodPrv; } + virtual ulong getSensorPressurePeriodPrv() { + return _pressureSensorPeriodPrv; + } /*******************************************************************************/ /*! @@ -474,7 +476,9 @@ class WipperSnapper_I2C_Driver { @returns Time when the Altitude sensor was last queried, in seconds. */ /*********************************************************************************/ - virtual ulong getSensorAltitudePeriodPrv() { return _altitudeSensorPeriodPrv; } + virtual ulong getSensorAltitudePeriodPrv() { + return _altitudeSensorPeriodPrv; + } /*******************************************************************************/ /*! @@ -1036,7 +1040,9 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual ulong getSensorObjectTempFPeriodPrv() { return _objectTempFPeriodPrv; } + virtual ulong getSensorObjectTempFPeriodPrv() { + return _objectTempFPeriodPrv; + } /*******************************************************************************/ /*! @@ -1234,7 +1240,7 @@ class WipperSnapper_I2C_Driver { @brief Enables the device's proximity sensor, if it exists. */ /*******************************************************************************/ - virtual void enableSensorProximity(){}; + virtual void enableSensorProximity() {}; /*******************************************************************************/ /*! @@ -1397,8 +1403,8 @@ class WipperSnapper_I2C_Driver { long _ambientTempFPeriod = 0L; ///< The time period between reading the ///< ambient temp. (°F) sensor's value. long _ambientTempFPeriodPrv = - PERIOD_24HRS_AGO_MILLIS; ///< The time when the ambient temp. (°F) sensor - ///< was last read. + PERIOD_24HRS_AGO_MILLIS; ///< The time when the ambient temp. (°F) sensor + ///< was last read. long _objectTempFPeriod = 0L; ///< The time period between reading the object ///< temp. (°F) sensor's value. long _objectTempFPeriodPrv = From 7ee24c3b982d2f0f87fcee3e08535c361732d885 Mon Sep 17 00:00:00 2001 From: tyeth Date: Fri, 8 Nov 2024 16:08:27 +0000 Subject: [PATCH 7/7] clang again --- src/components/i2c/drivers/WipperSnapper_I2C_Driver.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h index 18baebd6e..03b1f319d 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver.h @@ -1240,7 +1240,7 @@ class WipperSnapper_I2C_Driver { @brief Enables the device's proximity sensor, if it exists. */ /*******************************************************************************/ - virtual void enableSensorProximity() {}; + virtual void enableSensorProximity(){}; /*******************************************************************************/ /*! @@ -1403,8 +1403,8 @@ class WipperSnapper_I2C_Driver { long _ambientTempFPeriod = 0L; ///< The time period between reading the ///< ambient temp. (°F) sensor's value. long _ambientTempFPeriodPrv = - PERIOD_24HRS_AGO_MILLIS; ///< The time when the ambient temp. (°F) sensor - ///< was last read. + PERIOD_24HRS_AGO_MILLIS; ///< The time when the ambient temp. (°F) sensor + ///< was last read. long _objectTempFPeriod = 0L; ///< The time period between reading the object ///< temp. (°F) sensor's value. long _objectTempFPeriodPrv =