diff --git a/platformio.ini b/platformio.ini index dd4acafc2..ea78dd66f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -271,6 +271,16 @@ 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_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 +extra_scripts = pre:rename_usb_config.py + ; Adafruit Funhouse ESP32-S2 [env:adafruit_funhouse_esp32s2] extends = common:esp32 diff --git a/src/components/i2c/WipperSnapper_I2C.cpp b/src/components/i2c/WipperSnapper_I2C.cpp index bd47877e4..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()) { + 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; @@ -1328,8 +1329,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 +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)() > ((*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..03b1f319d 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,9 @@ 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 +438,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 +476,9 @@ 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 +487,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 +527,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorObjectTempPeriodPrv() { + virtual ulong getSensorObjectTempPeriodPrv() { return _objectTempSensorPeriodPrv; } @@ -535,7 +539,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 +579,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorLightPeriodPrv() { return _lightSensorPeriodPrv; } + virtual ulong getSensorLightPeriodPrv() { return _lightSensorPeriodPrv; } /*******************************************************************************/ /*! @@ -585,7 +589,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 +629,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorPM10_STDPeriodPrv() { return _PM10SensorPeriodPrv; } + virtual ulong getSensorPM10_STDPeriodPrv() { return _PM10SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -635,7 +639,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 +679,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorPM25_STDPeriodPrv() { return _PM25SensorPeriodPrv; } + virtual ulong getSensorPM25_STDPeriodPrv() { return _PM25SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -685,7 +689,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 +729,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorPM100_STDPeriodPrv() { return _PM100SensorPeriodPrv; } + virtual ulong getSensorPM100_STDPeriodPrv() { return _PM100SensorPeriodPrv; } /*******************************************************************************/ /*! @@ -735,7 +739,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 +781,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorUnitlessPercentPeriodPrv() { + virtual ulong getSensorUnitlessPercentPeriodPrv() { return _unitlessPercentPeriodPrv; } @@ -789,7 +793,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 +831,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 +840,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 +878,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 +887,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 +925,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 +934,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 +982,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorAmbientTempFPeriodPrv() { + virtual ulong getSensorAmbientTempFPeriodPrv() { return _ambientTempFPeriodPrv; } @@ -991,7 +995,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorAmbientTempFPeriodPrv(long period) { + virtual void setSensorAmbientTempFPeriodPrv(ulong period) { _ambientTempFPeriodPrv = period; } @@ -1036,7 +1040,9 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorObjectTempFPeriodPrv() { return _objectTempFPeriodPrv; } + virtual ulong getSensorObjectTempFPeriodPrv() { + return _objectTempFPeriodPrv; + } /*******************************************************************************/ /*! @@ -1047,7 +1053,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorObjectTempFPeriodPrv(long period) { + virtual void setSensorObjectTempFPeriodPrv(ulong period) { _objectTempFPeriodPrv = period; } @@ -1092,7 +1098,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorGasResistancePeriodPrv() { + virtual ulong getSensorGasResistancePeriodPrv() { return _gasResistancePeriodPrv; } @@ -1105,7 +1111,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorGasResistancePeriodPrv(long period) { + virtual void setSensorGasResistancePeriodPrv(ulong period) { _gasResistancePeriodPrv = period; } @@ -1145,7 +1151,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorNOxIndexPeriodPrv() { return _NOxIndexPeriodPrv; } + virtual ulong getSensorNOxIndexPeriodPrv() { return _NOxIndexPeriodPrv; } /*******************************************************************************/ /*! @@ -1156,7 +1162,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorNOxIndexPeriodPrv(long period) { + virtual void setSensorNOxIndexPeriodPrv(ulong period) { _NOxIndexPeriodPrv = period; } @@ -1196,7 +1202,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long getSensorVOCIndexPeriodPrv() { return _VOCIndexPeriodPrv; } + virtual ulong getSensorVOCIndexPeriodPrv() { return _VOCIndexPeriodPrv; } /*******************************************************************************/ /*! @@ -1207,7 +1213,7 @@ class WipperSnapper_I2C_Driver { last. */ /*******************************************************************************/ - virtual void setSensorVOCIndexPeriodPrv(long period) { + virtual void setSensorVOCIndexPeriodPrv(ulong period) { _VOCIndexPeriodPrv = period; } @@ -1275,7 +1281,7 @@ class WipperSnapper_I2C_Driver { in seconds. */ /*********************************************************************************/ - virtual long SensorProximityPeriodPrv() { return _proximitySensorPeriodPrv; } + virtual ulong SensorProximityPeriodPrv() { return _proximitySensorPeriodPrv; } /*******************************************************************************/ /*! @@ -1285,7 +1291,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_SCD30.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h index fcaa1d94d..00b1d7545 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h @@ -58,8 +58,8 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { @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; + bool hasBeenReadInLastSecond() { + return _lastRead != 0 && millis() - _lastRead < 1000; } /*******************************************************************************/ @@ -68,7 +68,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { @returns True if the sensor is ready, False otherwise. */ /*******************************************************************************/ - bool sensorReady() { + bool isSensorReady() { if (!_scd->dataReady()) { // failed, one more quick attempt delay(100); @@ -87,18 +87,20 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver { /*******************************************************************************/ bool readSensorData() { // dont read sensor more than once per second - if (alreadyRecentlyRead()) { + if (hasBeenReadInLastSecond()) { return true; } - if (!sensorReady()) { + if (!isSensorReady()) { 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 dfe2c1de3..ffef41445 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h @@ -42,51 +42,93 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver { : WipperSnapper_I2C_Driver(i2c, sensorAddress) { _i2c = i2c; _sensorAddress = sensorAddress; + _lastRead = 0; + _temperature = 20.0; + _humidity = 50.0; } /*******************************************************************************/ /*! @brief Initializes the SCD40 sensor and begins I2C. + @param pollPeriod + The sensor's polling period in seconds. @returns True if initialized successfully, False otherwise. */ /*******************************************************************************/ - bool begin() { + bool begin(ulong pollPeriod) { _scd = new SensirionI2CScd4x(); _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; - + } + + // Takes 5seconds to have data ready, don't queue read until then + long currentTime = (long)millis() - ((pollPeriod * 1000) - 5000); // 5s time + this->setSensorCO2PeriodPrv(currentTime); + this->setSensorAmbientTempFPeriodPrv(currentTime); + this->setSensorAmbientTempPeriodPrv(currentTime); + this->setSensorRelativeHumidityPeriodPrv(currentTime); 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 hasBeenReadInLastSecond() { + 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 isSensorReady() { 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 (hasBeenReadInLastSecond()) { + return true; + } - // Check if data is ready - error = _scd->getDataReadyFlag(isDataReady); - if (error || !isDataReady) + if (!isSensorReady()) { 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) { return false; - + } + _lastRead = millis(); return true; } @@ -101,8 +143,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 +162,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 +181,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 + SensirionI2CScd4x *_scd = nullptr; ///< SCD4x driver object + uint16_t _co2 = 0; ///< SCD4x co2 reading + float _temperature; ///< SCD4x temperature reading + float _humidity; ///< SCD4x humidity reading + ulong _lastRead; ///< Last time the sensor was read }; #endif // WipperSnapper_I2C_Driver_SCD4X \ No newline at end of file