Skip to content

Commit 4e9d959

Browse files
committed
D6T: Swap to single read for both metrics
1 parent b35c2c2 commit 4e9d959

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_D6T1A.h

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ class WipperSnapper_I2C_Driver_D6T1A : public WipperSnapper_I2C_Driver {
6464
return true;
6565
}
6666

67+
68+
/*******************************************************************************/
69+
/*!
70+
@brief Checks if sensor was read within last 1s, or is the first read.
71+
@returns True if the sensor was recently read, False otherwise.
72+
*/
73+
/*******************************************************************************/
74+
bool HasBeenReadInLast200ms() {
75+
return _lastRead != 0 && millis() - _lastRead < 200;
76+
}
77+
78+
/*******************************************************************************/
79+
/*!
80+
@brief Reads the sensor.
81+
@returns True if the sensor was read successfully, False otherwise.
82+
*/
83+
/*******************************************************************************/
84+
bool ReadSensorData() {
85+
// dont read sensor more than once per 200ms
86+
if (HasBeenReadInLast200ms()) {
87+
return true;
88+
}
89+
90+
_d6t1a->read();
91+
_deviceTemp.temperature = _d6t1a->ambientTempC();
92+
_objectTemp.relative_humidity = _d6t1a->objectTempC();
93+
_lastRead = millis();
94+
return true;
95+
}
96+
97+
98+
6799
/*******************************************************************************/
68100
/*!
69101
@brief Gets the D6T1A's current temperature.
@@ -74,8 +106,12 @@ class WipperSnapper_I2C_Driver_D6T1A : public WipperSnapper_I2C_Driver {
74106
*/
75107
/*******************************************************************************/
76108
bool getEventAmbientTemp(sensors_event_t *tempEvent) {
77-
tempEvent->temperature = _d6t1a->ambientTempC();
78-
return true;
109+
if (ReadSensorData() && _deviceTemp != nan) {
110+
// if the sensor was read recently, return the cached temperature
111+
tempEvent->temperature = _deviceTemp;
112+
return true;
113+
}
114+
return false; // sensor not read recently, return false
79115
}
80116

81117
/*******************************************************************************/
@@ -88,13 +124,20 @@ class WipperSnapper_I2C_Driver_D6T1A : public WipperSnapper_I2C_Driver {
88124
*/
89125
/*******************************************************************************/
90126
bool getEventObjectTemp(sensors_event_t *tempEvent) {
91-
tempEvent->temperature = _d6t1a->objectTempC();
92-
return true;
127+
if (ReadSensorData() && _objectTemp != nan) {
128+
// if the sensor was read recently, return the cached temperature
129+
tempEvent->temperature = _objectTemp;
130+
return true;
131+
}
132+
return false; // sensor not read recently, return false
93133
}
94134

95135

96136
protected:
97-
OmronD6T *_d6t1a; ///< D6T1A object
137+
float _deviceTemp = nan; ///< Device temperature in Celsius
138+
float _objectTemp = nan; ///< Object temperature in Celsius
139+
uint32_t _lastRead = 0; ///< Last time the sensor was read in milliseconds
140+
OmronD6T *_d6t1a = nullptr; ///< D6T1A object
98141
};
99142

100143
#endif // WipperSnapper_I2C_Driver_D6T1A

0 commit comments

Comments
 (0)