@@ -53,6 +53,56 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
5353 return _scd->begin ((uint8_t )_sensorAddress, _i2c);
5454 }
5555
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+
56106 /* ******************************************************************************/
57107 /* !
58108 @brief Gets the SCD30's current temperature.
@@ -64,14 +114,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
64114 /* ******************************************************************************/
65115 bool getEventAmbientTemp (sensors_event_t *tempEvent) {
66116 // 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 ()) {
73118 return false ;
119+ }
74120
121+ tempEvent = &_temperature;
75122 return true ;
76123 }
77124
@@ -86,14 +133,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
86133 /* ******************************************************************************/
87134 bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
88135 // 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 ()) {
95137 return false ;
138+ }
96139
140+ humidEvent = &_humidity;
97141 return true ;
98142 }
99143
@@ -108,15 +152,20 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
108152 /* ******************************************************************************/
109153 bool getEventCO2 (sensors_event_t *co2Event) {
110154 // check if sensor is enabled and data is available
111- if (_CO2SensorPeriod != 0 && (!_scd-> dataReady ()))
155+ if (! readSensorData ()) {
112156 return false ;
157+ }
113158
114- co2Event-> CO2 = _scd-> CO2 ;
159+ co2Event = &_CO2 ;
115160 return true ;
116161 }
117162
118163protected:
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
120169};
121170
122171#endif // WipperSnapper_I2C_Driver_SCD30
0 commit comments