Skip to content

Commit 736d1ba

Browse files
committed
Fix SCD4x and SCD30 (alreadyRecentlyRead) + MetroS3 debug target
1 parent 270344d commit 736d1ba

File tree

3 files changed

+78
-25
lines changed

3 files changed

+78
-25
lines changed

platformio.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ build_flags = -DARDUINO_METRO_ESP32S3 -DBOARD_HAS_PSRAM
271271
board_build.partitions = tinyuf2-partitions-16MB.csv
272272
extra_scripts = pre:rename_usb_config.py
273273

274+
; Adafruit Metro ESP32-S3
275+
[env:adafruit_metro_esp32s3_debug]
276+
extends = common:esp32
277+
board = adafruit_metro_esp32s3
278+
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
279+
;set partition to tinyuf2-partitions-16MB.csv as of idf 5.1
280+
board_build.partitions = tinyuf2-partitions-16MB.csv
281+
extra_scripts = pre:rename_usb_config.py
282+
274283
; Adafruit Funhouse ESP32-S2
275284
[env:adafruit_funhouse_esp32s2]
276285
extends = common:esp32

src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
5959
@returns True if the sensor was recently read, False otherwise.
6060
*/
6161
bool alreadyRecentlyRead() {
62-
return (_lastRead != 0 && millis() - _lastRead) < 1000;
62+
return _lastRead != 0 && millis() - _lastRead < 1000;
6363
}
6464

6565
/*******************************************************************************/

src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,78 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
5555
_scd->begin(*_i2c);
5656

5757
// stop previously started measurement
58-
if (_scd->stopPeriodicMeasurement())
58+
if (_scd->stopPeriodicMeasurement() != 0) {
5959
return false;
60+
}
6061

6162
// start measurements
62-
if (_scd->startPeriodicMeasurement())
63+
if (_scd->startPeriodicMeasurement() != 0) {
6364
return false;
65+
}
6466

6567
return true;
6668
}
6769

68-
/********************************************************************************/
70+
/*******************************************************************************/
71+
/*!
72+
@brief Checks if sensor was read within last 1s, or is the first read.
73+
@returns True if the sensor was recently read, False otherwise.
74+
*/
75+
bool alreadyRecentlyRead() {
76+
return _lastRead != 0 && millis() - _lastRead < 1000;
77+
}
78+
79+
/*******************************************************************************/
6980
/*!
70-
@brief Attempts to read the SCD4x's sensor measurements
71-
@returns True if the measurements were read without errors, False
72-
if read errors occured or if sensor did not have data ready.
81+
@brief Checks if the sensor is ready to be read
82+
@returns True if the sensor is ready, False otherwise.
7383
*/
74-
/********************************************************************************/
75-
bool readSensorMeasurements() {
76-
uint16_t error;
84+
/*******************************************************************************/
85+
bool sensorReady() {
7786
bool isDataReady = false;
78-
delay(100);
87+
uint16_t error = _scd->getDataReadyFlag(isDataReady);
88+
if (error != 0 || !isDataReady) {
89+
// failed, one more quick attempt
90+
delay(100);
91+
error = _scd->getDataReadyFlag(isDataReady);
92+
if (error != 0 || !isDataReady) {
93+
return false;
94+
}
95+
}
96+
return true;
97+
}
98+
99+
/*******************************************************************************/
100+
/*!
101+
@brief Reads the sensor.
102+
@returns True if the sensor was read successfully, False otherwise.
103+
*/
104+
/*******************************************************************************/
105+
bool readSensorData() {
106+
// dont read sensor more than once per second
107+
if (alreadyRecentlyRead()) {
108+
return true;
109+
}
79110

80-
// Check if data is ready
81-
error = _scd->getDataReadyFlag(isDataReady);
82-
if (error || !isDataReady)
111+
if (!sensorReady()) {
83112
return false;
113+
}
114+
84115

85116
// Read SCD4x measurement
86-
error = _scd->readMeasurement(_co2, _temperature, _humidity);
87-
if (error || _co2 == 0)
117+
uint16_t error = _scd->readMeasurement(_co2, _temperature, _humidity);
118+
if (error != 0 || _co2 == 0) {
119+
WS_DEBUG_PRINT("Error reading SCD4x measurements: #");
120+
WS_DEBUG_PRINT(error);
121+
WS_DEBUG_PRINT(" CO2: ");
122+
WS_DEBUG_PRINTLN(_co2);
123+
WS_DEBUG_PRINT("Temp: ");
124+
WS_DEBUG_PRINT(_temperature);
125+
WS_DEBUG_PRINT(" Humidity: ");
126+
WS_DEBUG_PRINTLN(_humidity);
88127
return false;
89-
128+
}
129+
_lastRead = millis();
90130
return true;
91131
}
92132

@@ -101,8 +141,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
101141
/*******************************************************************************/
102142
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
103143
// read all sensor measurements
104-
if (!readSensorMeasurements())
144+
if (!readSensorData()) {
105145
return false;
146+
}
106147

107148
tempEvent->temperature = _temperature;
108149
return true;
@@ -119,8 +160,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
119160
/*******************************************************************************/
120161
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
121162
// read all sensor measurements
122-
if (!readSensorMeasurements())
163+
if (!readSensorData()) {
123164
return false;
165+
}
124166

125167
humidEvent->relative_humidity = _humidity;
126168
return true;
@@ -137,18 +179,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
137179
/*******************************************************************************/
138180
bool getEventCO2(sensors_event_t *co2Event) {
139181
// read all sensor measurements
140-
if (!readSensorMeasurements())
182+
if (!readSensorData()) {
141183
return false;
184+
}
142185

143186
co2Event->CO2 = (float)_co2;
144187
return true;
145188
}
146189

147-
protected:
148-
SensirionI2CScd4x *_scd; ///< SCD4x driver object
149-
uint16_t _co2; ///< SCD4x co2 reading
150-
float _temperature; ///< SCD4x temperature reading
151-
float _humidity; ///< SCD4x humidity reading
190+
private:
191+
SensirionI2CScd4x *_scd = nullptr; ///< SCD4x driver object
192+
uint16_t _co2 = 0; ///< SCD4x co2 reading
193+
float _temperature = 20.0f; ///< SCD4x temperature reading
194+
float _humidity = 50.0f; ///< SCD4x humidity reading
195+
ulong _lastRead = 0; ///< Last time the sensor was read
152196
};
153197

154198
#endif // WipperSnapper_I2C_Driver_SCD4X

0 commit comments

Comments
 (0)