@@ -53,32 +53,52 @@ 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
+
56
82
/* ******************************************************************************/
57
83
/* !
58
84
@brief Reads the SCD30 sensor.
59
85
@returns True if the sensor was read successfully, False otherwise.
60
86
*/
61
87
/* ******************************************************************************/
62
- bool readSensor () {
88
+ bool readSensorData () {
63
89
// dont read sensor more than once per second
64
- if (_lastRead != 0 && millis () - _lastRead < 1000 ) {
90
+ if (alreadyRecentlyRead () ) {
65
91
return true ;
66
92
}
67
93
68
- if (!_scd->dataReady ()) {
69
- delay (100 );
70
- if (!_scd->dataReady ()) {
71
- return false ;
72
- }
94
+ if (!sensorReady ()) {
95
+ return false ;
73
96
}
74
- sensors_event_t tempEvent;
75
- sensors_event_t humidEvent;
76
- if (!_scd->getEvent (&humidEvent, &tempEvent)) {
97
+
98
+ if (_scd->getEvent (&_humidity, &_temperature)) {
77
99
return false ;
78
100
}
79
- _temperature = tempEvent.temperature ;
80
- _humidity = humidEvent.relative_humidity ;
81
- _CO2 = _scd->CO2 ;
101
+ _CO2.CO2 = _scd->CO2 ;
82
102
_lastRead = millis ();
83
103
return true ;
84
104
}
@@ -94,11 +114,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
94
114
/* ******************************************************************************/
95
115
bool getEventAmbientTemp (sensors_event_t *tempEvent) {
96
116
// check if sensor is enabled and data is available
97
- if (!readSensor ()) {
117
+ if (!readSensorData ()) {
98
118
return false ;
99
119
}
100
120
101
- tempEvent-> temperature = _temperature;
121
+ tempEvent = & _temperature;
102
122
return true ;
103
123
}
104
124
@@ -113,11 +133,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
113
133
/* ******************************************************************************/
114
134
bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
115
135
// check if sensor is enabled and data is available
116
- if (!readSensor ()) {
136
+ if (!readSensorData ()) {
117
137
return false ;
118
138
}
119
139
120
- humidEvent-> relative_humidity = _humidity;
140
+ humidEvent = & _humidity;
121
141
return true ;
122
142
}
123
143
@@ -132,20 +152,20 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
132
152
/* ******************************************************************************/
133
153
bool getEventCO2 (sensors_event_t *co2Event) {
134
154
// check if sensor is enabled and data is available
135
- if (!readSensor ()) {
155
+ if (!readSensorData ()) {
136
156
return false ;
137
157
}
138
158
139
- co2Event-> CO2 = _CO2;
159
+ co2Event = & _CO2;
140
160
return true ;
141
161
}
142
162
143
163
protected:
144
164
Adafruit_SCD30 *_scd = nullptr ; // /< SCD30 driver object
145
165
ulong _lastRead = 0 ; // /< Last time the sensor was read
146
- float _temperature = 0 ; // /< Temperature
147
- float _humidity = 0 ; // /< Relative Humidity
148
- float _CO2 = 0 ; // /< CO2
166
+ sensors_event_t _temperature; // /< Temperature
167
+ sensors_event_t _humidity; // /< Relative Humidity
168
+ sensors_event_t _CO2; // /< CO2
149
169
};
150
170
151
171
#endif // WipperSnapper_I2C_Driver_SCD30
0 commit comments