@@ -55,38 +55,74 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
55
55
_scd->begin (*_i2c, _sensorAddress);
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
+ /* ******************************************************************************/
76
+ bool HasBeenReadInLastSecond () {
77
+ return _lastRead != 0 && millis () - _lastRead < 1000 ;
78
+ }
79
+
80
+ /* ******************************************************************************/
69
81
/* !
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.
73
84
*/
74
- /* *******************************************************************************/
75
- bool readSensorMeasurements () {
76
- uint16_t error;
85
+ /* ******************************************************************************/
86
+ bool IsSensorReady () {
77
87
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
+ }
79
97
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 ()) {
83
111
return false ;
112
+ }
84
113
85
114
// 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 ) {
88
120
return false ;
89
-
121
+ }
122
+ _CO2.CO2 = co2;
123
+ _temperature.temperature = temperature;
124
+ _humidity.relative_humidity = humidity;
125
+ _lastRead = millis ();
90
126
return true ;
91
127
}
92
128
@@ -101,10 +137,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
101
137
/* ******************************************************************************/
102
138
bool getEventAmbientTemp (sensors_event_t *tempEvent) {
103
139
// read all sensor measurements
104
- if (!readSensorMeasurements ())
140
+ if (!ReadSensorData ()) {
105
141
return false ;
142
+ }
106
143
107
- tempEvent-> temperature = _temperature;
144
+ tempEvent = & _temperature;
108
145
return true ;
109
146
}
110
147
@@ -119,10 +156,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
119
156
/* ******************************************************************************/
120
157
bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
121
158
// read all sensor measurements
122
- if (!readSensorMeasurements ())
159
+ if (!ReadSensorData ()) {
123
160
return false ;
161
+ }
124
162
125
- humidEvent-> relative_humidity = _humidity;
163
+ humidEvent = & _humidity;
126
164
return true ;
127
165
}
128
166
@@ -137,18 +175,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
137
175
/* ******************************************************************************/
138
176
bool getEventCO2 (sensors_event_t *co2Event) {
139
177
// read all sensor measurements
140
- if (!readSensorMeasurements ())
178
+ if (!ReadSensorData ()) {
141
179
return false ;
180
+ }
142
181
143
- co2Event-> CO2 = ( float )_co2 ;
182
+ co2Event = &_CO2 ;
144
183
return true ;
145
184
}
146
185
147
186
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
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
152
192
};
153
193
154
- #endif // WipperSnapper_I2C_Driver_SCD4X
194
+ #endif // WipperSnapper_I2C_Driver_SCD4X_H
0 commit comments