Update SHT3X driver to bring it into line with other sensors, e.g., HYT..., and avoid unnecessary I²C reads (#21816)

* Avoid unnecessary I²C reads, bring code in line with other sensors.

* Update xsns_14_sht3x.ino

fix copyright again
This commit is contained in:
Jan-David Förster 2024-07-22 13:58:17 +02:00 committed by GitHub
parent 8c9040919d
commit 9878a96c0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -41,6 +41,9 @@ uint8_t sht3x_addresses[] = { 0x44, 0x45, 0x46, 0x70 };
uint8_t sht3x_count = 0;
struct SHT3XSTRUCT {
float humi = NAN;
float temp = NAN;
uint8_t valid = 0;
uint8_t type; // Sensor type
uint8_t address; // I2C bus address
uint8_t bus;
@ -64,9 +67,8 @@ uint8_t Sht3xComputeCrc(uint8_t data[], uint8_t len) {
return crc;
}
bool Sht3xRead(uint32_t sensor, float &t, float &h) {
t = NAN;
h = NAN;
bool Sht3xRead(uint32_t sensor) {
if (sht3x_sensors[sensor].valid) { sht3x_sensors[sensor].valid--; }
TwoWire& myWire = I2cGetWire(sht3x_sensors[sensor].bus);
if (&myWire == nullptr) { return false; } // No valid I2c bus
@ -95,7 +97,7 @@ bool Sht3xRead(uint32_t sensor, float &t, float &h) {
if (myWire.endTransmission() != 0) { // Stop I2C transmission
return false;
}
delay(30); // Timing verified with logic analyzer (10 is to short)
delay(30); // Timing verified with logic analyzer (10 is too short)
uint8_t data[6];
myWire.requestFrom(i2c_address, (uint8_t)6); // Request 6 bytes of data
for (uint32_t i = 0; i < 6; i++) {
@ -104,21 +106,24 @@ bool Sht3xRead(uint32_t sensor, float &t, float &h) {
if ((Sht3xComputeCrc(&data[0], 2) != data[2]) || (Sht3xComputeCrc(&data[3], 2) != data[5])) {
return false;
}
t = ((float)(((data[0] << 8) | data[1]) * 175) / 65535.0) - 45.0;
float t;
float h;
t = ((((data[0] << 8) | data[1]) * 175) / 65535.0) - 45.0;
if (type == SHT3X_TYPE_SHT4X) {
h = ((float)(((data[3] << 8) | data[4]) * 125) / 65535.0) - 6.0;
h = ((((data[3] << 8) | data[4]) * 125) / 65535.0) - 6.0;
} else {
h = ((float)(((data[3] << 8) | data[4]) * 100) / 65535.0);
h = (((data[3] << 8) | data[4]) * 100) / 65535.0;
}
return (!isnan(t) && !isnan(h));
sht3x_sensors[sensor].temp = ConvertTemp(t);
sht3x_sensors[sensor].humi = ConvertHumidity(h);
if (isnan(sht3x_sensors[sensor].temp) || isnan(sht3x_sensors[sensor].humi)) { return false; }
sht3x_sensors[sensor].valid = SENSOR_MAX_MISS;
return true;
}
/********************************************************************************************/
void Sht3xDetect(void) {
float t;
float h;
for (uint32_t bus = 0; bus < 2; bus++) {
for (uint32_t k = 0; k < SHT3X_TYPES; k++) {
for (uint32_t i = 0; i < SHT3X_ADDRESSES; i++) {
@ -126,7 +131,7 @@ void Sht3xDetect(void) {
sht3x_sensors[sht3x_count].type = k;
sht3x_sensors[sht3x_count].address = sht3x_addresses[i];
sht3x_sensors[sht3x_count].bus = bus;
if (Sht3xRead(sht3x_count, t, h)) {
if (Sht3xRead(sht3x_count)) {
GetTextIndexed(sht3x_sensors[sht3x_count].types, sizeof(sht3x_sensors[sht3x_count].types), sht3x_sensors[sht3x_count].type, kSht3xTypes);
I2cSetActiveFound(sht3x_sensors[sht3x_count].address, sht3x_sensors[sht3x_count].types, sht3x_sensors[sht3x_count].bus);
sht3x_count++;
@ -139,15 +144,20 @@ void Sht3xDetect(void) {
}
}
void Sht3xUpdate(void) {
for (uint32_t idx = 0; idx < sht3x_count; idx++) {
if (!Sht3xRead(idx)) {
AddLogMissed(sht3x_sensors[idx].types, sht3x_sensors[idx].valid);
}
}
}
void Sht3xShow(bool json) {
float t;
float h;
char types[11];
for (uint32_t idx = 0; idx < sht3x_count; idx++) {
if (Sht3xRead(idx, t, h)) {
t = ConvertTemp(t);
h = ConvertHumidity(h);
if (sht3x_sensors[idx].valid) {
strlcpy(types, sht3x_sensors[idx].types, sizeof(types));
if (sht3x_count > 1) {
snprintf_P(types, sizeof(types), PSTR("%s%c%02X"), types, IndexSeparator(), sht3x_sensors[idx].address); // "SHT3X-0xXX"
@ -162,7 +172,7 @@ void Sht3xShow(bool json) {
}
#endif
}
TempHumDewShow(json, ((0 == TasmotaGlobal.tele_period) && (0 == idx)), types, t, h);
TempHumDewShow(json, ((0 == TasmotaGlobal.tele_period) && (0 == idx)), types, sht3x_sensors[idx].temp, sht3x_sensors[idx].humi);
}
}
}
@ -181,6 +191,9 @@ bool Xsns14(uint32_t function) {
}
else if (sht3x_count) {
switch (function) {
case FUNC_EVERY_SECOND:
Sht3xUpdate();
break;
case FUNC_JSON_APPEND:
Sht3xShow(1);
break;