@@ -55,38 +55,78 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
55
55
_scd->begin (*_i2c);
56
56
57
57
// stop previously started measurement
58
- if (_scd->stopPeriodicMeasurement ())
58
+ if (_scd->stopPeriodicMeasurement () != 0 ) {
59
59
return false ;
60
+ }
60
61
61
62
// start measurements
62
- if (_scd->startPeriodicMeasurement ())
63
+ if (_scd->startPeriodicMeasurement () != 0 ) {
63
64
return false ;
65
+ }
64
66
65
67
return true ;
66
68
}
67
69
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
+ /* ******************************************************************************/
69
80
/* !
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.
73
83
*/
74
- /* *******************************************************************************/
75
- bool readSensorMeasurements () {
76
- uint16_t error;
84
+ /* ******************************************************************************/
85
+ bool sensorReady () {
77
86
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
+ }
79
110
80
- // Check if data is ready
81
- error = _scd->getDataReadyFlag (isDataReady);
82
- if (error || !isDataReady)
111
+ if (!sensorReady ()) {
83
112
return false ;
113
+ }
114
+
84
115
85
116
// 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);
88
127
return false ;
89
-
128
+ }
129
+ _lastRead = millis ();
90
130
return true ;
91
131
}
92
132
@@ -101,8 +141,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
101
141
/* ******************************************************************************/
102
142
bool getEventAmbientTemp (sensors_event_t *tempEvent) {
103
143
// read all sensor measurements
104
- if (!readSensorMeasurements ())
144
+ if (!readSensorData ()) {
105
145
return false ;
146
+ }
106
147
107
148
tempEvent->temperature = _temperature;
108
149
return true ;
@@ -119,8 +160,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
119
160
/* ******************************************************************************/
120
161
bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
121
162
// read all sensor measurements
122
- if (!readSensorMeasurements ())
163
+ if (!readSensorData ()) {
123
164
return false ;
165
+ }
124
166
125
167
humidEvent->relative_humidity = _humidity;
126
168
return true ;
@@ -137,18 +179,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
137
179
/* ******************************************************************************/
138
180
bool getEventCO2 (sensors_event_t *co2Event) {
139
181
// read all sensor measurements
140
- if (!readSensorMeasurements ())
182
+ if (!readSensorData ()) {
141
183
return false ;
184
+ }
142
185
143
186
co2Event->CO2 = (float )_co2;
144
187
return true ;
145
188
}
146
189
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
152
196
};
153
197
154
198
#endif // WipperSnapper_I2C_Driver_SCD4X
0 commit comments