2424 @brief Class that provides a driver interface for the SCD30 sensor.
2525*/
2626/* *************************************************************************/
27- class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
27+ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver
28+ {
2829
2930public:
3031 /* ******************************************************************************/
@@ -37,7 +38,8 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
3738 */
3839 /* ******************************************************************************/
3940 WipperSnapper_I2C_Driver_SCD30 (TwoWire *i2c, uint16_t sensorAddress)
40- : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41+ : WipperSnapper_I2C_Driver(i2c, sensorAddress)
42+ {
4143 _i2c = i2c;
4244 _sensorAddress = sensorAddress;
4345 }
@@ -48,11 +50,47 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
4850 @returns True if initialized successfully, False otherwise.
4951 */
5052 /* ******************************************************************************/
51- bool begin () {
53+ bool begin ()
54+ {
5255 _scd = new Adafruit_SCD30 ();
5356 return _scd->begin ((uint8_t )_sensorAddress, _i2c);
5457 }
5558
59+ /* ******************************************************************************/
60+ /* !
61+ @brief Reads the SCD30 sensor.
62+ @returns True if the sensor was read successfully, False otherwise.
63+ */
64+ /* ******************************************************************************/
65+ bool readSensor ()
66+ {
67+ // dont read sensor more than once per second
68+ if (_lastRead != 0 && millis () - _lastRead < 1000 )
69+ {
70+ return true ;
71+ }
72+
73+ if (!_scd->dataReady ())
74+ {
75+ delay (100 );
76+ if (!_scd->dataReady ())
77+ {
78+ return false ;
79+ }
80+ }
81+ sensors_event_t tempEvent;
82+ sensors_event_t humidEvent;
83+ if (!_scd->getEvent (&humidEvent, &tempEvent))
84+ {
85+ return false ;
86+ }
87+ _temperature = tempEvent.temperature ;
88+ _humidity = humidEvent.relative_humidity ;
89+ _CO2 = _scd->CO2 ;
90+ _lastRead = millis ();
91+ return true ;
92+ }
93+
5694 /* ******************************************************************************/
5795 /* !
5896 @brief Gets the SCD30's current temperature.
@@ -62,16 +100,15 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
62100 otherwise.
63101 */
64102 /* ******************************************************************************/
65- bool getEventAmbientTemp (sensors_event_t *tempEvent) {
103+ bool getEventAmbientTemp (sensors_event_t *tempEvent)
104+ {
66105 // 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))
106+ if (!readSensor ())
107+ {
73108 return false ;
109+ }
74110
111+ tempEvent->temperature = _temperature;
75112 return true ;
76113 }
77114
@@ -84,16 +121,15 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
84121 otherwise.
85122 */
86123 /* ******************************************************************************/
87- bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
124+ bool getEventRelativeHumidity (sensors_event_t *humidEvent)
125+ {
88126 // 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))
127+ if (!readSensor ())
128+ {
95129 return false ;
130+ }
96131
132+ humidEvent->relative_humidity = _humidity;
97133 return true ;
98134 }
99135
@@ -106,17 +142,24 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
106142 otherwise.
107143 */
108144 /* ******************************************************************************/
109- bool getEventCO2 (sensors_event_t *co2Event) {
145+ bool getEventCO2 (sensors_event_t *co2Event)
146+ {
110147 // check if sensor is enabled and data is available
111- if (_CO2SensorPeriod != 0 && (!_scd->dataReady ()))
148+ if (!readSensor ())
149+ {
112150 return false ;
151+ }
113152
114- co2Event->CO2 = _scd-> CO2 ;
153+ co2Event->CO2 = _CO2 ;
115154 return true ;
116155 }
117156
118157protected:
119- Adafruit_SCD30 *_scd; // /< SCD30 driver object
158+ Adafruit_SCD30 *_scd = nullptr ; // /< SCD30 driver object
159+ ulong _lastRead = 0 ; // /< Last time the sensor was read
160+ float _temperature = 0 ; // /< Temperature
161+ float _humidity = 0 ; // /< Relative Humidity
162+ float _CO2 = 0 ; // /< CO2
120163};
121164
122165#endif // WipperSnapper_I2C_Driver_SCD30
0 commit comments