Browse Source

sht3x: formatting, fix visibility of virtual

also experiment a bit with status register in log
pull/2525/head
Maxim Prokhorov 2 years ago
parent
commit
946f5f0dfa
1 changed files with 65 additions and 52 deletions
  1. +65
    -52
      code/espurna/sensors/SHT3XI2CSensor.h

+ 65
- 52
code/espurna/sensors/SHT3XI2CSensor.h View File

@ -29,7 +29,6 @@ class SHT3XI2CSensor : public I2CSensor<> {
// Initialization method, must be idempotent
void begin() override {
if (!_dirty) return;
// I2C auto-discover
@ -46,7 +45,6 @@ class SHT3XI2CSensor : public I2CSensor<> {
_ready = true;
_dirty = false;
}
// Descriptive name of the sensor
@ -66,7 +64,6 @@ class SHT3XI2CSensor : public I2CSensor<> {
// Pre-read hook (usually to populate registers with up-to-date data)
void pre() override {
_error = SENSOR_ERROR_OK;
const auto address = lockedAddress();
@ -78,82 +75,98 @@ class SHT3XI2CSensor : public I2CSensor<> {
unsigned char buffer[6];
i2c_read_buffer(address, buffer, std::size(buffer));
if ((CRC8(buffer[0],buffer[1],buffer[2])) && (CRC8(buffer[3],buffer[4],buffer[5]))) {
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
// result bytes are as follows
// cTemp msb, cTemp lsb, cTemp crc, humidity msb, humidity lsb, humidity crc
if ((_crc8(buffer[0], buffer[1], buffer[2])) && (_crc8(buffer[3], buffer[4], buffer[5]))) {
_temperature = ((((buffer[0] * 256.0) + buffer[1]) * 175) / 65535.0) - 45;
_humidity = ((((buffer[3] * 256.0) + buffer[4]) * 100) / 65535.0);
}
else {
} else {
_error = SENSOR_ERROR_CRC;
}
#if SENSOR_DEBUG
status_register();
#endif
#if SENSOR_DEBUG
_statusRegister();
#endif
}
// Current value for slot # index
double value(unsigned char index) override {
if (index == 0) return _temperature;
if (index == 1) return _humidity;
return 0;
}
private:
// Read the status register and output to Debug log
void status_register() {
void _statusRegister() {
const auto address = lockedAddress();
unsigned char buffer[3];
bool crc, cmd, htr;
// Read status register
// Request status register reading
i2c_write_uint8(address, 0xF3, 0x2D);
espurna::time::blockingDelay(
espurna::duration::Milliseconds(20));
unsigned char buffer[3];
i2c_read_buffer(address, buffer, std::size(buffer));
if (CRC8(buffer[0],buffer[1],buffer[2])) {
if (_crc8(buffer[0],buffer[1],buffer[2])) {
// see https://sensirion.com/resource/datasheet/sht3x-d
crc = buffer[1] & 0b00000001;
cmd = buffer[1] & 0b00000010;
htr = buffer[0] & 0b00100000;
DEBUG_MSG_P(PSTR("[SHT3X] Status %02X%02X crc:%u cmd:%u htr:%u\n"), buffer[0], buffer[1], crc, cmd, htr);
uint16_t status = (buffer[0] << 8) | (buffer[1] & 0xff);
auto bit = [&](uint16_t offset) {
return (status & (1 << offset)) ? '1' : '0';
};
DEBUG_MSG_P(PSTR("[SHT3X] Status 0x%04X - crc:%c cmd:%c rst:%c tta:%c rhta:%c htr:%c alrt:%c\n"),
status,
bit(0), // last write crc status
bit(1), // last command status
bit(4), // system reset detected
bit(10), // T tracking alert
bit(11), // RH tracking alert
bit(13), // heater status
bit(15)); // alert pending status (1 when at least one alert)
}
else {
DEBUG_MSG_P(PSTR("[SHT3X] Checksum error\n"));
DEBUG_MSG_P(PSTR("[SHT3X] Checksum error when reading status register\n"));
}
// Clear status register
i2c_write_uint8(address, 0x30, 0x41);
}
// Current value for slot # index
double value(unsigned char index) override {
if (index == 0) return _temperature;
if (index == 1) return _humidity;
return 0;
}
protected:
double _temperature = 0;
unsigned char _humidity = 0;
bool CRC8(uint8_t MSB, uint8_t LSB, uint8_t CRC) {
bool _crc8(uint8_t msb, uint8_t lsb, uint8_t expected) {
/*
* adapted from https://github.com/Risele/SHT3x/blob/master/SHT3x.cpp
* Name : CRC-8
* Poly : 0x31 x^8 + x^5 + x^4 + 1
* Init : 0xFF
* Revert: false
* XorOut: 0x00
* Check : for 0xBE,0xEF CRC is 0x92
* Name : CRC-8
* Poly : 0x31 x^8 + x^5 + x^4 + 1
* Init : 0xFF
* Revert: false
* XorOut: 0x00
* Check : for 0xBE,0xEF CRC is 0x92
*/
uint8_t crc = 0xFF;
uint8_t i;
crc ^= MSB;
static constexpr uint8_t Init { 0xFF };
static constexpr uint8_t Mask { 0x80 };
static constexpr uint8_t Poly { 0x31 };
for (i = 0; i < 8; i++)
crc = crc & 0x80 ? (crc << 1) ^ 0x31 : crc << 1;
uint8_t crc = Init;
crc ^= LSB;
for (i = 0; i < 8; i++)
crc = crc & 0x80 ? (crc << 1) ^ 0x31 : crc << 1;
crc ^= msb;
for (size_t i = 0; i < 8; ++i) {
crc = crc & Mask ? (crc << 1) ^ Poly : crc << 1;
}
if (crc == CRC) return true;
return false;
crc ^= lsb;
for (size_t i = 0; i < 8; ++i) {
crc = crc & Mask ? (crc << 1) ^ Poly : crc << 1;
}
return crc == expected;
}
double _temperature = 0;
unsigned char _humidity = 0;
};
#endif // SENSOR_SUPPORT && SHT3X_I2C_SUPPORT

Loading…
Cancel
Save