Skip to content

Commit 560d707

Browse files
committed
Fix DPS, scd, memset issue
1 parent c95047d commit 560d707

File tree

3 files changed

+113
-27
lines changed

3 files changed

+113
-27
lines changed

src/components/i2c/controller.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,12 @@ void I2cController::update() {
726726
// Read the driver's sensors
727727
_i2c_model->ClearI2cDeviceEvent();
728728
for (size_t i = 0; i < sensor_count; i++) {
729-
sensors_event_t event;
730-
// Call the driver's handler function for the SensorType
731-
drv->GetSensorEvent(drv->_sensors[i], &event);
729+
sensors_event_t event = {0};
730+
// Attempt to call driver's read handler function
731+
if (!drv->GetSensorEvent(drv->_sensors[i], &event)) {
732+
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to read sensor!");
733+
continue;
734+
}
732735
// Fill the I2cDeviceEvent's sensor_event array submsg.
733736
_i2c_model->AddI2cDeviceSensorEvent(event, drv->_sensors[i]);
734737
}

src/components/i2c/drivers/drvDps310.h

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class drvDps310 : public drvBase {
4848
_i2c_mux_channel = mux_channel;
4949
strncpy(_name, driver_name, sizeof(_name) - 1);
5050
_name[sizeof(_name) - 1] = '\0';
51+
_last_read = 0;
5152
}
5253

5354
/*******************************************************************************/
@@ -81,8 +82,38 @@ class drvDps310 : public drvBase {
8182
if (_dps_pressure == NULL) {
8283
return false;
8384
}
84-
// Wait for the first reading to complete
85-
delay(1000);
85+
return true;
86+
}
87+
88+
/*******************************************************************************/
89+
/*!
90+
@brief Reads the DPS310's temperature and pressure.
91+
@returns True if the measurements were read successfully, False
92+
otherwise.
93+
*/
94+
/*******************************************************************************/
95+
bool alreadyRecentlyRead() {
96+
return (_last_read != 0 && (millis() - _last_read < 1000));
97+
}
98+
99+
/*******************************************************************************/
100+
/*!
101+
@brief Reads the DPS310's temperature and pressure.
102+
@returns True if the measurements were read successfully, False
103+
otherwise.
104+
*/
105+
/*******************************************************************************/
106+
bool ReadMeasurements() {
107+
if (alreadyRecentlyRead())
108+
return true;
109+
110+
while (!_dps310->temperatureAvailable() || !_dps310->pressureAvailable())
111+
return false;
112+
113+
if (!_dps310->getEvents(&_temp_event, &_pressure_event))
114+
return false;
115+
116+
_last_read = millis();
86117
return true;
87118
}
88119

@@ -96,11 +127,10 @@ class drvDps310 : public drvBase {
96127
*/
97128
/*******************************************************************************/
98129
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
99-
if (!_dps310->temperatureAvailable()) {
130+
if (!ReadMeasurements()) {
100131
return false;
101132
}
102-
103-
_dps_temp->getEvent(tempEvent);
133+
tempEvent->temperature = _temp_event.temperature;
104134
return true;
105135
}
106136

@@ -114,15 +144,16 @@ class drvDps310 : public drvBase {
114144
*/
115145
/*******************************************************************************/
116146
bool getEventPressure(sensors_event_t *pressureEvent) {
117-
if (!_dps310->pressureAvailable()) {
147+
if (!ReadMeasurements()) {
118148
return false;
119149
}
120-
121-
_dps_pressure->getEvent(pressureEvent);
150+
pressureEvent->pressure = _pressure_event.pressure;
122151
return true;
123152
}
124153

125154
protected:
155+
sensors_event_t _temp_event, _pressure_event;
156+
ulong _last_read;
126157
Adafruit_DPS310 *_dps310; ///< DPS310 driver object
127158
Adafruit_Sensor *_dps_temp =
128159
NULL; ///< Holds data for the DPS310's temperature sensor

src/components/i2c/drivers/drvScd30.h

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

125173
protected:
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

Comments
 (0)