Merge pull request #15755 from stibus/bugfix/SHT4x

Fix issues introduced to SHT3x driver during merge of #15349
This commit is contained in:
Theo Arends 2022-06-05 10:53:48 +02:00 committed by GitHub
commit c22227c918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 34 deletions

View File

@ -31,32 +31,24 @@
#define XSNS_14 14
#define XI2C_15 15 // See I2CDEVICES.md
#define SHT3X_ADDRESSES 2 // 2 addresses for SHT3x
#define SHT3X_ADDR_GND 0x44 // Address A pin low (GND)
#define SHT3X_ADDR_VDD 0x45 // Address B pin high (VDD)
#define SHTCX_ADDRESSES 1 // 1 address for SHTCx
#define SHTCX_ADDR 0x70 // Address for SHTCx sensors
#define SHT4X_ADDRESSES 2 // 2 addresses for SHT4x
#define SHT4X_ADDR_A 0x44 // Address SHT4x A
#define SHT4X_ADDR_B 0x45 // Address SHT4x B
#define SHT3X_MAX_SENSORS 3 // Only one of 0x44, 0x45 and 0x70
#define SHT3X_TYPES 3 // SHT3X, SHTCX and SHT4X
#define SHT3X_ADDRESSES 3 // 0x44, 0x45 and 0x70
enum SHT3X_Types {
SHT3X_TYPE_SHT3X,
SHT3X_TYPE_SHTCX,
SHT3X_TYPE_SHT4X
};
const char kSht3xTypes[] PROGMEM = "SHT3X|SHTC3|SHT4X";
uint8_t sht3x_addresses[] = { SHT3X_ADDR_GND, SHT3X_ADDR_VDD, SHTCX_ADDR };
uint8_t sht3x_addresses[] = { 0x44, 0x45, 0x70 };
uint8_t sht3x_count = 0;
struct SHT3XSTRUCT {
uint8_t type; // Sensor type
uint8_t address; // I2C bus address
char types[6]; // Sensor type name and address, e.g. "SHT3X"
} sht3x_sensors[SHT3X_MAX_SENSORS];
} sht3x_sensors[SHT3X_ADDRESSES];
uint8_t Sht3xComputeCrc(uint8_t data[], uint8_t len) {
// Compute CRC as per datasheet
@ -112,8 +104,12 @@ bool Sht3xRead(uint32_t type, float &t, float &h, uint8_t i2c_address) {
if ((Sht3xComputeCrc(&data[0], 2) != data[2]) || (Sht3xComputeCrc(&data[3], 2) != data[5])) {
return false;
}
t = ConvertTemp((float)((((data[0] << 8) | data[1]) * 175) / 65535.0) - 45);
h = ConvertHumidity((float)((((data[3] << 8) | data[4]) * 100) / 65535.0));
t = ((float)(((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;
} else {
h = ((float)(((data[3] << 8) | data[4]) * 100) / 65535.0);
}
return (!isnan(t) && !isnan(h));
}
@ -123,26 +119,17 @@ void Sht3xDetect(void) {
float t;
float h;
for (uint32_t i = 0; i < SHT3X_MAX_SENSORS; i++) {
if (!I2cSetDevice(sht3x_addresses[i])) { continue; }
if (i < 2) { // 0x44 and 0x45
sht3x_sensors[sht3x_count].type = SHT3X_TYPE_SHT3X;
if (!Sht3xRead(sht3x_sensors[sht3x_count].type, t, h, sht3x_addresses[i])) {
sht3x_sensors[sht3x_count].type = SHT3X_TYPE_SHT4X;
if (!Sht3xRead(sht3x_sensors[sht3x_count].type, t, h, sht3x_addresses[i])) {
continue;
}
}
} else { // 0x70
sht3x_sensors[sht3x_count].type = SHT3X_TYPE_SHTCX;
if (!Sht3xRead(sht3x_sensors[sht3x_count].type, t, h, sht3x_addresses[i])) {
continue;
for (uint32_t k = 0; k < SHT3X_TYPES; k++) {
sht3x_sensors[sht3x_count].type = k;
for (uint32_t i = 0; i < SHT3X_ADDRESSES; i++) {
if (!I2cSetDevice(sht3x_addresses[i])) { continue; }
sht3x_sensors[sht3x_count].address = sht3x_addresses[i];
if (Sht3xRead(sht3x_sensors[sht3x_count].type, t, h, sht3x_sensors[sht3x_count].address)) {
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_count++;
}
}
sht3x_sensors[sht3x_count].address = sht3x_addresses[i];
GetTextIndexed(sht3x_sensors[sht3x_count].types, sizeof(sht3x_sensors[sht3x_count].types), SHT3X_TYPE_SHT3X, kSht3xTypes);
I2cSetActiveFound(sht3x_sensors[sht3x_count].address, sht3x_sensors[sht3x_count].types);
sht3x_count++;
}
}
@ -192,4 +179,4 @@ bool Xsns14(uint8_t function) {
}
#endif // USE_SHT3X
#endif // USE_I2C
#endif // USE_I2C