@@ -55,38 +55,74 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
5555 _scd->begin (*_i2c, _sensorAddress);
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+ /* ******************************************************************************/
76+ bool HasBeenReadInLastSecond () {
77+ return _lastRead != 0 && millis () - _lastRead < 1000 ;
78+ }
79+
80+ /* ******************************************************************************/
6981 /* !
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.
82+ @brief Checks if the sensor is ready to be read
83+ @returns True if the sensor is ready, False otherwise.
7384 */
74- /* *******************************************************************************/
75- bool readSensorMeasurements () {
76- uint16_t error;
85+ /* ******************************************************************************/
86+ bool IsSensorReady () {
7787 bool isDataReady = false ;
78- delay (100 );
88+ for (int i = 0 ; i < 2 ; i++) {
89+ uint16_t error = _scd->getDataReadyStatus (isDataReady);
90+ if (error == 0 && isDataReady) {
91+ return true ;
92+ }
93+ delay (100 );
94+ }
95+ return false ;
96+ }
7997
80- // Check if data is ready
81- error = _scd->getDataReadyStatus (isDataReady);
82- if (error || !isDataReady)
98+ /* ******************************************************************************/
99+ /* !
100+ @brief Reads the sensor.
101+ @returns True if the sensor was read successfully, False otherwise.
102+ */
103+ /* ******************************************************************************/
104+ bool ReadSensorData () {
105+ // dont read sensor more than once per second
106+ if (HasBeenReadInLastSecond ()) {
107+ return true ;
108+ }
109+
110+ if (!IsSensorReady ()) {
83111 return false ;
112+ }
84113
85114 // Read SCD4x measurement
86- error = _scd->readMeasurement (_co2, _temperature, _humidity);
87- if (error || _co2 == 0 )
115+ uint16_t co2 = 0 ;
116+ float temperature = 0 ;
117+ float humidity = 0 ;
118+ int16_t error = _scd->readMeasurement (co2, temperature, humidity);
119+ if (error != 0 || co2 == 0 ) {
88120 return false ;
89-
121+ }
122+ _CO2.CO2 = co2;
123+ _temperature.temperature = temperature;
124+ _humidity.relative_humidity = humidity;
125+ _lastRead = millis ();
90126 return true ;
91127 }
92128
@@ -101,10 +137,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
101137 /* ******************************************************************************/
102138 bool getEventAmbientTemp (sensors_event_t *tempEvent) {
103139 // read all sensor measurements
104- if (!readSensorMeasurements ())
140+ if (!ReadSensorData ()) {
105141 return false ;
142+ }
106143
107- tempEvent-> temperature = _temperature;
144+ tempEvent = & _temperature;
108145 return true ;
109146 }
110147
@@ -119,10 +156,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
119156 /* ******************************************************************************/
120157 bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
121158 // read all sensor measurements
122- if (!readSensorMeasurements ())
159+ if (!ReadSensorData ()) {
123160 return false ;
161+ }
124162
125- humidEvent-> relative_humidity = _humidity;
163+ humidEvent = & _humidity;
126164 return true ;
127165 }
128166
@@ -137,18 +175,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
137175 /* ******************************************************************************/
138176 bool getEventCO2 (sensors_event_t *co2Event) {
139177 // read all sensor measurements
140- if (!readSensorMeasurements ())
178+ if (!ReadSensorData ()) {
141179 return false ;
180+ }
142181
143- co2Event-> CO2 = ( float )_co2 ;
182+ co2Event = &_CO2 ;
144183 return true ;
145184 }
146185
147186protected:
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
187+ SensirionI2cScd4x *_scd = nullptr ; // /< SCD4x driver object
188+ sensors_event_t _temperature = {0 }; // /< Temperature
189+ sensors_event_t _humidity = {0 }; // /< Relative Humidity
190+ sensors_event_t _CO2 = {0 }; // /< CO2
191+ ulong _lastRead = 0uL; // /< Last time the sensor was read
152192};
153193
154- #endif // WipperSnapper_I2C_Driver_SCD4X
194+ #endif // WipperSnapper_I2C_Driver_SCD4X_H
0 commit comments