@@ -40,7 +40,8 @@ class drvScd30 : public drvBase {
4040 The name of the driver.
4141 */
4242 /* ******************************************************************************/
43- drvScd30 (TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel, const char * driver_name)
43+ drvScd30 (TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
44+ const char *driver_name)
4445 : drvBase(i2c, sensorAddress, mux_channel, driver_name) {
4546 _i2c = i2c;
4647 _address = sensorAddress;
@@ -60,6 +61,58 @@ class drvScd30 : public drvBase {
6061 return _scd->begin ((uint8_t )_address, _i2c);
6162 }
6263
64+ /* ******************************************************************************/
65+ /* !
66+ @brief Checks if sensor was read within last 1s, or is the first read.
67+ @returns True if the sensor was recently read, False otherwise.
68+ */
69+ bool hasBeenReadInLastSecond () {
70+ return _lastRead != 0 && millis () - _lastRead < 1000 ;
71+ }
72+
73+ /* ******************************************************************************/
74+ /* !
75+ @brief Checks if the sensor is ready to be read
76+ @returns True if the sensor is ready, False otherwise.
77+ */
78+ /* ******************************************************************************/
79+ bool isSensorReady () {
80+ if (!_scd->dataReady ()) {
81+ // failed, one more quick attempt
82+ delay (100 );
83+ if (!_scd->dataReady ()) {
84+ return false ;
85+ }
86+ }
87+ return true ;
88+ }
89+
90+ /* ******************************************************************************/
91+ /* !
92+ @brief Reads the SCD30 sensor.
93+ @returns True if the sensor was read successfully, False otherwise.
94+ */
95+ /* ******************************************************************************/
96+ bool readSensorData () {
97+ // dont read sensor more than once per second
98+ if (hasBeenReadInLastSecond ()) {
99+ return true ;
100+ }
101+
102+ if (!isSensorReady ()) {
103+ return false ;
104+ }
105+
106+ if (!_scd->read ()) {
107+ return false ;
108+ }
109+ _CO2 = _scd->CO2 ;
110+ _humidity = _scd->relative_humidity ;
111+ _temperature = _scd->temperature ;
112+ _lastRead = millis ();
113+ return true ;
114+ }
115+
63116 /* ******************************************************************************/
64117 /* !
65118 @brief Gets the SCD30's current temperature.
@@ -71,14 +124,11 @@ class drvScd30 : public drvBase {
71124 /* ******************************************************************************/
72125 bool getEventAmbientTemp (sensors_event_t *tempEvent) {
73126 // check if sensor is enabled and data is available
74- if (!_scd->dataReady ())
75- return false ;
76-
77- // attempt to get temperature data
78- sensors_event_t humidEvent;
79- if (!_scd->getEvent (&humidEvent, tempEvent))
127+ if (!readSensorData ()) {
80128 return false ;
129+ }
81130
131+ tempEvent->temperature = _temperature;
82132 return true ;
83133 }
84134
@@ -93,14 +143,11 @@ class drvScd30 : public drvBase {
93143 /* ******************************************************************************/
94144 bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
95145 // check if sensor is enabled and data is available
96- if (!_scd->dataReady ())
97- return false ;
98-
99- // attempt to get temperature data
100- sensors_event_t tempEvent;
101- if (!_scd->getEvent (humidEvent, &tempEvent))
146+ if (!readSensorData ()) {
102147 return false ;
148+ }
103149
150+ humidEvent->relative_humidity = _humidity;
104151 return true ;
105152 }
106153
@@ -115,15 +162,20 @@ class drvScd30 : public drvBase {
115162 /* ******************************************************************************/
116163 bool getEventCO2 (sensors_event_t *co2Event) {
117164 // check if sensor is enabled and data is available
118- if (!_scd-> dataReady ())
165+ if (!readSensorData ()) {
119166 return false ;
167+ }
120168
121- co2Event->CO2 = _scd-> CO2 ;
169+ co2Event->CO2 = _CO2 ;
122170 return true ;
123171 }
124172
125173protected:
126- Adafruit_SCD30 *_scd; // /< SCD30 driver object
174+ Adafruit_SCD30 *_scd = nullptr ; // /< SCD30 driver object
175+ ulong _lastRead = 0 ; // /< Last time the sensor was read
176+ float _temperature; // /< Temperature
177+ float _humidity; // /< Relative Humidity
178+ float _CO2; // /< CO2
127179};
128180
129181#endif // drvScd30
0 commit comments