@@ -53,6 +53,56 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
53
53
return _scd->begin ((uint8_t )_sensorAddress, _i2c);
54
54
}
55
55
56
+ /* ******************************************************************************/
57
+ /* !
58
+ @brief Checks if sensor was read within last 1s, or is the first read.
59
+ @returns True if the sensor was recently read, False otherwise.
60
+ */
61
+ bool alreadyRecentlyRead () {
62
+ return (_lastRead != 0 && millis () - _lastRead) < 1000 ;
63
+ }
64
+
65
+ /* ******************************************************************************/
66
+ /* !
67
+ @brief Checks if the sensor is ready to be read
68
+ @returns True if the sensor is ready, False otherwise.
69
+ */
70
+ /* ******************************************************************************/
71
+ bool sensorReady () {
72
+ if (!_scd->dataReady ()) {
73
+ // failed, one more quick attempt
74
+ delay (100 );
75
+ if (!_scd->dataReady ()) {
76
+ return false ;
77
+ }
78
+ }
79
+ return true ;
80
+ }
81
+
82
+ /* ******************************************************************************/
83
+ /* !
84
+ @brief Reads the SCD30 sensor.
85
+ @returns True if the sensor was read successfully, False otherwise.
86
+ */
87
+ /* ******************************************************************************/
88
+ bool readSensorData () {
89
+ // dont read sensor more than once per second
90
+ if (alreadyRecentlyRead ()) {
91
+ return true ;
92
+ }
93
+
94
+ if (!sensorReady ()) {
95
+ return false ;
96
+ }
97
+
98
+ if (_scd->getEvent (&_humidity, &_temperature)) {
99
+ return false ;
100
+ }
101
+ _CO2.CO2 = _scd->CO2 ;
102
+ _lastRead = millis ();
103
+ return true ;
104
+ }
105
+
56
106
/* ******************************************************************************/
57
107
/* !
58
108
@brief Gets the SCD30's current temperature.
@@ -64,14 +114,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
64
114
/* ******************************************************************************/
65
115
bool getEventAmbientTemp (sensors_event_t *tempEvent) {
66
116
// check if sensor is enabled and data is available
67
- if (_tempSensorPeriod != 0 && (!_scd->dataReady ()))
68
- return false ;
69
-
70
- // attempt to get temperature data
71
- sensors_event_t humidEvent;
72
- if (!_scd->getEvent (&humidEvent, tempEvent))
117
+ if (!readSensorData ()) {
73
118
return false ;
119
+ }
74
120
121
+ tempEvent = &_temperature;
75
122
return true ;
76
123
}
77
124
@@ -86,14 +133,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
86
133
/* ******************************************************************************/
87
134
bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
88
135
// check if sensor is enabled and data is available
89
- if (_humidSensorPeriod != 0 && (!_scd->dataReady ()))
90
- return false ;
91
-
92
- // attempt to get temperature data
93
- sensors_event_t tempEvent;
94
- if (!_scd->getEvent (humidEvent, &tempEvent))
136
+ if (!readSensorData ()) {
95
137
return false ;
138
+ }
96
139
140
+ humidEvent = &_humidity;
97
141
return true ;
98
142
}
99
143
@@ -108,15 +152,20 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
108
152
/* ******************************************************************************/
109
153
bool getEventCO2 (sensors_event_t *co2Event) {
110
154
// check if sensor is enabled and data is available
111
- if (_CO2SensorPeriod != 0 && (!_scd-> dataReady ()))
155
+ if (! readSensorData ()) {
112
156
return false ;
157
+ }
113
158
114
- co2Event-> CO2 = _scd-> CO2 ;
159
+ co2Event = &_CO2 ;
115
160
return true ;
116
161
}
117
162
118
163
protected:
119
- Adafruit_SCD30 *_scd; // /< SCD30 driver object
164
+ Adafruit_SCD30 *_scd = nullptr ; // /< SCD30 driver object
165
+ ulong _lastRead = 0 ; // /< Last time the sensor was read
166
+ sensors_event_t _temperature; // /< Temperature
167
+ sensors_event_t _humidity; // /< Relative Humidity
168
+ sensors_event_t _CO2; // /< CO2
120
169
};
121
170
122
171
#endif // WipperSnapper_I2C_Driver_SCD30
0 commit comments