24
24
@brief Class that provides a driver interface for the SCD30 sensor.
25
25
*/
26
26
/* *************************************************************************/
27
- class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
27
+ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver
28
+ {
28
29
29
30
public:
30
31
/* ******************************************************************************/
@@ -37,7 +38,8 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
37
38
*/
38
39
/* ******************************************************************************/
39
40
WipperSnapper_I2C_Driver_SCD30 (TwoWire *i2c, uint16_t sensorAddress)
40
- : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress)
42
+ {
41
43
_i2c = i2c;
42
44
_sensorAddress = sensorAddress;
43
45
}
@@ -48,11 +50,47 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
48
50
@returns True if initialized successfully, False otherwise.
49
51
*/
50
52
/* ******************************************************************************/
51
- bool begin () {
53
+ bool begin ()
54
+ {
52
55
_scd = new Adafruit_SCD30 ();
53
56
return _scd->begin ((uint8_t )_sensorAddress, _i2c);
54
57
}
55
58
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
+
56
94
/* ******************************************************************************/
57
95
/* !
58
96
@brief Gets the SCD30's current temperature.
@@ -62,16 +100,15 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
62
100
otherwise.
63
101
*/
64
102
/* ******************************************************************************/
65
- bool getEventAmbientTemp (sensors_event_t *tempEvent) {
103
+ bool getEventAmbientTemp (sensors_event_t *tempEvent)
104
+ {
66
105
// 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
+ {
73
108
return false ;
109
+ }
74
110
111
+ tempEvent->temperature = _temperature;
75
112
return true ;
76
113
}
77
114
@@ -84,16 +121,15 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
84
121
otherwise.
85
122
*/
86
123
/* ******************************************************************************/
87
- bool getEventRelativeHumidity (sensors_event_t *humidEvent) {
124
+ bool getEventRelativeHumidity (sensors_event_t *humidEvent)
125
+ {
88
126
// 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
+ {
95
129
return false ;
130
+ }
96
131
132
+ humidEvent->relative_humidity = _humidity;
97
133
return true ;
98
134
}
99
135
@@ -106,17 +142,24 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
106
142
otherwise.
107
143
*/
108
144
/* ******************************************************************************/
109
- bool getEventCO2 (sensors_event_t *co2Event) {
145
+ bool getEventCO2 (sensors_event_t *co2Event)
146
+ {
110
147
// check if sensor is enabled and data is available
111
- if (_CO2SensorPeriod != 0 && (!_scd->dataReady ()))
148
+ if (!readSensor ())
149
+ {
112
150
return false ;
151
+ }
113
152
114
- co2Event->CO2 = _scd-> CO2 ;
153
+ co2Event->CO2 = _CO2 ;
115
154
return true ;
116
155
}
117
156
118
157
protected:
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
120
163
};
121
164
122
165
#endif // WipperSnapper_I2C_Driver_SCD30
0 commit comments