@@ -53,32 +53,52 @@ 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+
5682 /* ******************************************************************************/
5783 /* !
5884 @brief Reads the SCD30 sensor.
5985 @returns True if the sensor was read successfully, False otherwise.
6086 */
6187 /* ******************************************************************************/
62- bool readSensor () {
88+ bool readSensorData () {
6389 // dont read sensor more than once per second
64- if (_lastRead != 0 && millis () - _lastRead < 1000 ) {
90+ if (alreadyRecentlyRead () ) {
6591 return true ;
6692 }
6793
68- if (!_scd->dataReady ()) {
69- delay (100 );
70- if (!_scd->dataReady ()) {
71- return false ;
72- }
94+ if (!sensorReady ()) {
95+ return false ;
7396 }
74- sensors_event_t tempEvent;
75- sensors_event_t humidEvent;
76- if (!_scd->getEvent (&humidEvent, &tempEvent)) {
97+
98+ if (_scd->getEvent (&_humidity, &_temperature)) {
7799 return false ;
78100 }
79- _temperature = tempEvent.temperature ;
80- _humidity = humidEvent.relative_humidity ;
81- _CO2 = _scd->CO2 ;
101+ _CO2.CO2 = _scd->CO2 ;
82102 _lastRead = millis ();
83103 return true ;
84104 }
@@ -94,11 +114,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
94114 /* ******************************************************************************/
95115 bool getEventAmbientTemp (sensors_event_t *tempEvent) {
96116 // check if sensor is enabled and data is available
97- if (!readSensor ()) {
117+ if (!readSensorData ()) {
98118 return false ;
99119 }
100120
101- tempEvent-> temperature = _temperature;
121+ tempEvent = & _temperature;
102122 return true ;
103123 }
104124
@@ -113,11 +133,11 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
113133 /* ******************************************************************************/
114134 bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
115135 // check if sensor is enabled and data is available
116- if (!readSensor ()) {
136+ if (!readSensorData ()) {
117137 return false ;
118138 }
119139
120- humidEvent-> relative_humidity = _humidity;
140+ humidEvent = & _humidity;
121141 return true ;
122142 }
123143
@@ -132,20 +152,20 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
132152 /* ******************************************************************************/
133153 bool getEventCO2 (sensors_event_t *co2Event) {
134154 // check if sensor is enabled and data is available
135- if (!readSensor ()) {
155+ if (!readSensorData ()) {
136156 return false ;
137157 }
138158
139- co2Event-> CO2 = _CO2;
159+ co2Event = & _CO2;
140160 return true ;
141161 }
142162
143163protected:
144164 Adafruit_SCD30 *_scd = nullptr ; // /< SCD30 driver object
145165 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
149169};
150170
151171#endif // WipperSnapper_I2C_Driver_SCD30
0 commit comments