@@ -42,6 +42,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
42
42
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
43
43
_i2c = i2c;
44
44
_sensorAddress = sensorAddress;
45
+ _lastRead = 0 ;
46
+ _temperature = 20.0 ;
47
+ _humidity = 50.0 ;
45
48
}
46
49
47
50
/* ******************************************************************************/
@@ -55,38 +58,69 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
55
58
_scd->begin (*_i2c, _sensorAddress);
56
59
57
60
// stop previously started measurement
58
- if (_scd->stopPeriodicMeasurement ())
61
+ if (_scd->stopPeriodicMeasurement () != 0 ) {
59
62
return false ;
63
+ }
60
64
61
65
// start measurements
62
- if (_scd->startPeriodicMeasurement ())
66
+ if (_scd->startPeriodicMeasurement () != 0 ) {
63
67
return false ;
68
+ }
64
69
65
70
return true ;
66
71
}
67
72
68
- /* *******************************************************************************/
73
+ /* ******************************************************************************/
74
+ /* !
75
+ @brief Checks if sensor was read within last 1s, or is the first read.
76
+ @returns True if the sensor was recently read, False otherwise.
77
+ */
78
+ bool hasBeenReadInLastSecond () {
79
+ return _lastRead != 0 && millis () - _lastRead < 1000 ;
80
+ }
81
+
82
+ /* ******************************************************************************/
69
83
/* !
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.
84
+ @brief Checks if the sensor is ready to be read
85
+ @returns True if the sensor is ready, False otherwise.
73
86
*/
74
- /* *******************************************************************************/
75
- bool readSensorMeasurements () {
76
- uint16_t error;
87
+ /* ******************************************************************************/
88
+ bool isSensorReady () {
77
89
bool isDataReady = false ;
78
- delay (100 );
90
+ uint16_t error = _scd->getDataReadyStatus (isDataReady);
91
+ if (error != 0 || !isDataReady) {
92
+ // failed, one more quick attempt
93
+ delay (100 );
94
+ error = _scd->getDataReadyStatus (isDataReady);
95
+ if (error != 0 || !isDataReady) {
96
+ return false ;
97
+ }
98
+ }
99
+ return true ;
100
+ }
79
101
80
- // Check if data is ready
81
- error = _scd->getDataReadyStatus (isDataReady);
82
- if (error || !isDataReady)
102
+ /* ******************************************************************************/
103
+ /* !
104
+ @brief Reads the sensor.
105
+ @returns True if the sensor was read successfully, False otherwise.
106
+ */
107
+ /* ******************************************************************************/
108
+ bool readSensorData () {
109
+ // dont read sensor more than once per second
110
+ if (hasBeenReadInLastSecond ()) {
111
+ return true ;
112
+ }
113
+
114
+ if (!isSensorReady ()) {
83
115
return false ;
116
+ }
84
117
85
118
// Read SCD4x measurement
86
- error = _scd->readMeasurement (_co2, _temperature, _humidity);
87
- if (error || _co2 == 0 )
119
+ uint16_t error = _scd->readMeasurement (_co2, _temperature, _humidity);
120
+ if (error != 0 || _co2 == 0 ) {
88
121
return false ;
89
-
122
+ }
123
+ _lastRead = millis ();
90
124
return true ;
91
125
}
92
126
@@ -101,8 +135,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
101
135
/* ******************************************************************************/
102
136
bool getEventAmbientTemp (sensors_event_t *tempEvent) {
103
137
// read all sensor measurements
104
- if (!readSensorMeasurements ())
138
+ if (!readSensorData ()) {
105
139
return false ;
140
+ }
106
141
107
142
tempEvent->temperature = _temperature;
108
143
return true ;
@@ -119,8 +154,9 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
119
154
/* ******************************************************************************/
120
155
bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
121
156
// read all sensor measurements
122
- if (!readSensorMeasurements ())
157
+ if (!readSensorData ()) {
123
158
return false ;
159
+ }
124
160
125
161
humidEvent->relative_humidity = _humidity;
126
162
return true ;
@@ -137,18 +173,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
137
173
/* ******************************************************************************/
138
174
bool getEventCO2 (sensors_event_t *co2Event) {
139
175
// read all sensor measurements
140
- if (!readSensorMeasurements ())
176
+ if (!readSensorData ()) {
141
177
return false ;
178
+ }
142
179
143
180
co2Event->CO2 = (float )_co2;
144
181
return true ;
145
182
}
146
183
147
184
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
185
+ SensirionI2cScd4x *_scd = nullptr ; // /< SCD4x driver object
186
+ uint16_t _co2 = 0 ; // /< SCD4x co2 reading
187
+ float _temperature; // /< SCD4x temperature reading
188
+ float _humidity; // /< SCD4x humidity reading
189
+ ulong _lastRead; // /< Last time the sensor was read
152
190
};
153
191
154
- #endif // WipperSnapper_I2C_Driver_SCD4X
192
+ #endif // WipperSnapper_I2C_Driver_SCD4X_H
0 commit comments