Skip to content

Commit 0bf00bc

Browse files
authored
Merge pull request #709 from tyeth/scd30-scd4x-others-poll-min-1sec
Scd30 scd4x others poll min 1sec
2 parents 712e434 + 16c6723 commit 0bf00bc

File tree

6 files changed

+272
-273
lines changed

6 files changed

+272
-273
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
},
66
"C_Cpp.dimInactiveRegions": true,
77
"dotnet.defaultSolution": "disable",
8-
"cmake.configureOnOpen": false
8+
"cmake.configureOnOpen": false,
9+
"C_Cpp.clang_format_fallbackStyle": "Google",
10+
"C_Cpp.clang_format_style": "file"
911
}

src/components/i2c/drivers/WipperSnapper_I2C_Driver_HDC302X.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
6868
@returns True if the data was read successfully, False otherwise.
6969
*/
7070
/*******************************************************************************/
71-
bool readSensorData() {
71+
bool ReadSensorData() {
7272
uint16_t status = _hdc302x->readStatus();
7373
if (status & 0x0010) {
7474
WS_DEBUG_PRINTLN(F("Device Reset Detected"));
@@ -99,7 +99,7 @@ class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
9999
*/
100100
/*******************************************************************************/
101101
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
102-
if (readSensorData() == false)
102+
if (ReadSensorData() == false)
103103
return false;
104104
tempEvent->temperature = _temp;
105105
return true;
@@ -115,7 +115,7 @@ class WipperSnapper_I2C_Driver_HDC302X : public WipperSnapper_I2C_Driver {
115115
*/
116116
/*******************************************************************************/
117117
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
118-
if (readSensorData() == false)
118+
if (ReadSensorData() == false)
119119
return false;
120120
humidEvent->relative_humidity = _humidity;
121121
return true;

src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD30.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
5858
@brief Checks if sensor was read within last 1s, or is the first read.
5959
@returns True if the sensor was recently read, False otherwise.
6060
*/
61-
bool alreadyRecentlyRead() {
62-
return (_lastRead != 0 && millis() - _lastRead) < 1000;
61+
bool HasBeenReadInLastSecond() {
62+
return _lastRead != 0 && millis() - _lastRead < 1000;
6363
}
6464

6565
/*******************************************************************************/
@@ -68,7 +68,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
6868
@returns True if the sensor is ready, False otherwise.
6969
*/
7070
/*******************************************************************************/
71-
bool sensorReady() {
71+
bool IsSensorReady() {
7272
if (!_scd->dataReady()) {
7373
// failed, one more quick attempt
7474
delay(100);
@@ -85,13 +85,13 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
8585
@returns True if the sensor was read successfully, False otherwise.
8686
*/
8787
/*******************************************************************************/
88-
bool readSensorData() {
88+
bool ReadSensorData() {
8989
// dont read sensor more than once per second
90-
if (alreadyRecentlyRead()) {
90+
if (HasBeenReadInLastSecond()) {
9191
return true;
9292
}
9393

94-
if (!sensorReady()) {
94+
if (!IsSensorReady()) {
9595
return false;
9696
}
9797

@@ -114,7 +114,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
114114
/*******************************************************************************/
115115
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
116116
// check if sensor is enabled and data is available
117-
if (!readSensorData()) {
117+
if (!ReadSensorData()) {
118118
return false;
119119
}
120120

@@ -133,7 +133,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
133133
/*******************************************************************************/
134134
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
135135
// check if sensor is enabled and data is available
136-
if (!readSensorData()) {
136+
if (!ReadSensorData()) {
137137
return false;
138138
}
139139

@@ -152,7 +152,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
152152
/*******************************************************************************/
153153
bool getEventCO2(sensors_event_t *co2Event) {
154154
// check if sensor is enabled and data is available
155-
if (!readSensorData()) {
155+
if (!ReadSensorData()) {
156156
return false;
157157
}
158158

@@ -162,7 +162,7 @@ class WipperSnapper_I2C_Driver_SCD30 : public WipperSnapper_I2C_Driver {
162162

163163
protected:
164164
Adafruit_SCD30 *_scd = nullptr; ///< SCD30 driver object
165-
ulong _lastRead = 0ul; ///< Last time the sensor was read
165+
ulong _lastRead = 0uL; ///< Last time the sensor was read
166166
sensors_event_t _temperature = {0}; ///< Temperature
167167
sensors_event_t _humidity = {0}; ///< Relative Humidity
168168
sensors_event_t _CO2 = {0}; ///< CO2

src/components/i2c/drivers/WipperSnapper_I2C_Driver_SCD4X.h

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,74 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
5555
_scd->begin(*_i2c, _sensorAddress);
5656

5757
// stop previously started measurement
58-
if (_scd->stopPeriodicMeasurement())
58+
if (_scd->stopPeriodicMeasurement() != 0) {
5959
return false;
60+
}
6061

6162
// start measurements
62-
if (_scd->startPeriodicMeasurement())
63+
if (_scd->startPeriodicMeasurement() != 0) {
6364
return false;
65+
}
6466

6567
return true;
6668
}
6769

68-
/********************************************************************************/
70+
/*******************************************************************************/
71+
/*!
72+
@brief Checks if sensor was read within last 1s, or is the first read.
73+
@returns True if the sensor was recently read, False otherwise.
74+
*/
75+
/*******************************************************************************/
76+
bool HasBeenReadInLastSecond() {
77+
return _lastRead != 0 && millis() - _lastRead < 1000;
78+
}
79+
80+
/*******************************************************************************/
6981
/*!
70-
@brief Attempts to read the SCD4x's sensor measurements
71-
@returns True if the measurements were read without errors, False
72-
if read errors occured or if sensor did not have data ready.
82+
@brief Checks if the sensor is ready to be read
83+
@returns True if the sensor is ready, False otherwise.
7384
*/
74-
/********************************************************************************/
75-
bool readSensorMeasurements() {
76-
uint16_t error;
85+
/*******************************************************************************/
86+
bool IsSensorReady() {
7787
bool isDataReady = false;
78-
delay(100);
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+
}
7997

80-
// Check if data is ready
81-
error = _scd->getDataReadyStatus(isDataReady);
82-
if (error || !isDataReady)
98+
/*******************************************************************************/
99+
/*!
100+
@brief Reads the sensor.
101+
@returns True if the sensor was read successfully, False otherwise.
102+
*/
103+
/*******************************************************************************/
104+
bool ReadSensorData() {
105+
// dont read sensor more than once per second
106+
if (HasBeenReadInLastSecond()) {
107+
return true;
108+
}
109+
110+
if (!IsSensorReady()) {
83111
return false;
112+
}
84113

85114
// Read SCD4x measurement
86-
error = _scd->readMeasurement(_co2, _temperature, _humidity);
87-
if (error || _co2 == 0)
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) {
88120
return false;
89-
121+
}
122+
_CO2.CO2 = co2;
123+
_temperature.temperature = temperature;
124+
_humidity.relative_humidity = humidity;
125+
_lastRead = millis();
90126
return true;
91127
}
92128

@@ -101,10 +137,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
101137
/*******************************************************************************/
102138
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
103139
// read all sensor measurements
104-
if (!readSensorMeasurements())
140+
if (!ReadSensorData()) {
105141
return false;
142+
}
106143

107-
tempEvent->temperature = _temperature;
144+
tempEvent = &_temperature;
108145
return true;
109146
}
110147

@@ -119,10 +156,11 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
119156
/*******************************************************************************/
120157
bool getEventRelativeHumidity(sensors_event_t *humidEvent) {
121158
// read all sensor measurements
122-
if (!readSensorMeasurements())
159+
if (!ReadSensorData()) {
123160
return false;
161+
}
124162

125-
humidEvent->relative_humidity = _humidity;
163+
humidEvent = &_humidity;
126164
return true;
127165
}
128166

@@ -137,18 +175,20 @@ class WipperSnapper_I2C_Driver_SCD4X : public WipperSnapper_I2C_Driver {
137175
/*******************************************************************************/
138176
bool getEventCO2(sensors_event_t *co2Event) {
139177
// read all sensor measurements
140-
if (!readSensorMeasurements())
178+
if (!ReadSensorData()) {
141179
return false;
180+
}
142181

143-
co2Event->CO2 = (float)_co2;
182+
co2Event = &_CO2;
144183
return true;
145184
}
146185

147186
protected:
148-
SensirionI2cScd4x *_scd; ///< SCD4x driver object
149-
uint16_t _co2; ///< SCD4x co2 reading
150-
float _temperature; ///< SCD4x temperature reading
151-
float _humidity; ///< SCD4x humidity reading
187+
SensirionI2cScd4x *_scd = nullptr; ///< SCD4x driver object
188+
sensors_event_t _temperature = {0}; ///< Temperature
189+
sensors_event_t _humidity = {0}; ///< Relative Humidity
190+
sensors_event_t _CO2 = {0}; ///< CO2
191+
ulong _lastRead = 0uL; ///< Last time the sensor was read
152192
};
153193

154-
#endif // WipperSnapper_I2C_Driver_SCD4X
194+
#endif // WipperSnapper_I2C_Driver_SCD4X_H

0 commit comments

Comments
 (0)