@@ -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