Adafruit Library
WipperSnapper_I2C_Driver_SCD4X.h
Go to the documentation of this file.
1 
17 #ifndef WipperSnapper_I2C_Driver_SCD4X_H
18 #define WipperSnapper_I2C_Driver_SCD4X_H
19 
21 #include <SensirionI2cScd4x.h>
22 #include <Wire.h>
23 
24 /**************************************************************************/
28 /**************************************************************************/
30 
31 public:
32  /*******************************************************************************/
40  /*******************************************************************************/
41  WipperSnapper_I2C_Driver_SCD4X(TwoWire *i2c, uint16_t sensorAddress)
42  : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
43  _i2c = i2c;
44  _sensorAddress = sensorAddress;
45  }
46 
47  /*******************************************************************************/
52  /*******************************************************************************/
53  bool begin() {
54  _scd = new SensirionI2cScd4x();
55  _scd->begin(*_i2c, _sensorAddress);
56 
57  // stop previously started measurement
58  if (_scd->stopPeriodicMeasurement() != 0) {
59  return false;
60  }
61 
62  // start measurements
63  if (_scd->startPeriodicMeasurement() != 0) {
64  return false;
65  }
66 
67  return true;
68  }
69 
70  /*******************************************************************************/
75  /*******************************************************************************/
77  return _lastRead != 0 && millis() - _lastRead < ONE_SECOND_IN_MILLIS;
78  }
79 
80  /*******************************************************************************/
85  /*******************************************************************************/
86  bool IsSensorReady() {
87  bool isDataReady = false;
88  for (int i = 0; i < 2; i++) {
89  uint16_t error = _scd->getDataReadyStatus(isDataReady);
90  if (error == 0 && isDataReady) {
91  return true;
92  }
93  delay(100);
94  }
95  return false;
96  }
97 
98  /*******************************************************************************/
103  /*******************************************************************************/
104  bool ReadSensorData() {
105  // dont read sensor more than once per second
106  if (HasBeenReadInLastSecond()) {
107  return true;
108  }
109 
110  if (!IsSensorReady()) {
111  return false;
112  }
113 
114  // Read SCD4x measurement
115  uint16_t co2 = 0;
116  float temperature = 0;
117  float humidity = 0;
118  int16_t error = _scd->readMeasurement(co2, temperature, humidity);
119  if (error != 0 || co2 == 0) {
120  return false;
121  }
122  _CO2.CO2 = co2;
123  _temperature.temperature = temperature;
124  _humidity.relative_humidity = humidity;
125  _lastRead = millis();
126  return true;
127  }
128 
129  /*******************************************************************************/
137  /*******************************************************************************/
138  bool getEventAmbientTemp(sensors_event_t *tempEvent) {
139  // read all sensor measurements
140  if (!ReadSensorData()) {
141  return false;
142  }
143 
144  *tempEvent = _temperature;
145  return true;
146  }
147 
148  /*******************************************************************************/
156  /*******************************************************************************/
157  bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
158  // read all sensor measurements
159  if (!ReadSensorData()) {
160  return false;
161  }
162 
163  *humidEvent = _humidity;
164  return true;
165  }
166 
167  /*******************************************************************************/
175  /*******************************************************************************/
176  bool getEventCO2(sensors_event_t *co2Event) {
177  // read all sensor measurements
178  if (!ReadSensorData()) {
179  return false;
180  }
181 
182  *co2Event = _CO2;
183  return true;
184  }
185 
186 protected:
187  SensirionI2cScd4x *_scd = nullptr;
188  sensors_event_t _temperature = {0};
189  sensors_event_t _humidity = {0};
190  sensors_event_t _CO2 = {0};
191  ulong _lastRead = 0uL;
192 };
193 
194 #endif // WipperSnapper_I2C_Driver_SCD4X_H
Class that provides a driver interface for the SCD40 sensor.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:29
SensirionI2cScd4x * _scd
SCD4x driver object.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:187
WipperSnapper_I2C_Driver_SCD4X(TwoWire *i2c, uint16_t sensorAddress)
Constructor for a SCD40 sensor.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:41
sensors_event_t _humidity
Relative Humidity.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:189
bool getEventCO2(sensors_event_t *co2Event)
Gets the SCD40&#39;s current CO2 reading.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:176
bool getEventAmbientTemp(sensors_event_t *tempEvent)
Gets the SCD40&#39;s current temperature.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:138
bool ReadSensorData()
Reads the sensor.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:104
Base class for I2C Drivers.
Definition: WipperSnapper_I2C_Driver.h:33
uint16_t _sensorAddress
The I2C driver&#39;s unique I2C address.
Definition: WipperSnapper_I2C_Driver.h:1340
bool begin()
Initializes the SCD40 sensor and begins I2C.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:53
ulong _lastRead
Last time the sensor was read.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:191
sensors_event_t _temperature
Temperature.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:188
bool HasBeenReadInLastSecond()
Checks if sensor was read within last 1s, or is the first read.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:76
bool getEventRelativeHumidity(sensors_event_t *humidEvent)
Gets the SCD40&#39;s current relative humidity reading.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:157
bool IsSensorReady()
Checks if the sensor is ready to be read.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:86
#define ONE_SECOND_IN_MILLIS
Used for period checks.
Definition: WipperSnapper_I2C_Driver.h:26
TwoWire * _i2c
Pointer to the I2C driver&#39;s Wire object.
Definition: WipperSnapper_I2C_Driver.h:1339
sensors_event_t _CO2
CO2.
Definition: WipperSnapper_I2C_Driver_SCD4X.h:190