From aa59e32b4d5a8e4382a11805f387d770faad207b Mon Sep 17 00:00:00 2001 From: Timofey Titovets Date: Sun, 23 Jun 2024 15:31:52 +0200 Subject: [PATCH] sht3x: use periodic measurment with wait_ack Signed-off-by: Timofey Titovets --- klippy/extras/sht3x.py | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/klippy/extras/sht3x.py b/klippy/extras/sht3x.py index 699d3f2095f4..5a8785e8d3b2 100644 --- a/klippy/extras/sht3x.py +++ b/klippy/extras/sht3x.py @@ -27,6 +27,13 @@ 'LOW_REP': [0x24, 0x16], }, }, + 'PERIODIC': { + '2HZ': { + 'HIGH_REP': [0x22, 0x36], + 'MED_REP': [0x22, 0x20], + 'LOW_REP': [0x22, 0x2B], + }, + }, 'OTHER': { 'STATUS': { 'READ': [0xF3, 0x2D], @@ -72,10 +79,12 @@ def get_report_time_delta(self): def _init_sht3x(self): # Device Soft Reset - self.i2c.i2c_write(SHT3X_CMD['OTHER']['SOFTRESET']) - - # Wait 2ms after reset - self.reactor.pause(self.reactor.monotonic() + .02) + self.i2c.i2c_write_wait_ack(SHT3X_CMD['OTHER']['BREAK']) + # Break takes ~ 1ms + self.reactor.pause(self.reactor.monotonic() + .0015) + self.i2c.i2c_write_wait_ack(SHT3X_CMD['OTHER']['SOFTRESET']) + # Wait <=1.5ms after reset + self.reactor.pause(self.reactor.monotonic() + .0015) status = self.i2c.i2c_read(SHT3X_CMD['OTHER']['STATUS']['READ'], 3) response = bytearray(status['response']) @@ -86,17 +95,17 @@ def _init_sht3x(self): if self._crc8(status) != checksum: logging.warning("sht3x: Reading status - checksum error!") + # Enable periodic mode + self.i2c.i2c_write_wait_ack( + SHT3X_CMD['PERIODIC']['2HZ']['HIGH_REP'] + ) + # Wait <=15.5ms for first measurment + self.reactor.pause(self.reactor.monotonic() + .0155) + def _sample_sht3x(self, eventtime): try: - # Read Temeprature - params = self.i2c.i2c_write( - SHT3X_CMD['MEASURE']['STRETCH_ENABLED']['HIGH_REP'] - ) - # Wait - self.reactor.pause(self.reactor.monotonic() - + .20) - - params = self.i2c.i2c_read([], 6) + # Read measurment + params = self.i2c.i2c_read(SHT3X_CMD['OTHER']['FETCH'], 6) response = bytearray(params['response']) rtemp = response[0] << 8