Skip to content

Commit

Permalink
sht3x: use periodic measurment with wait_ack
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Titovets <[email protected]>
  • Loading branch information
nefelim4ag committed Jul 6, 2024
1 parent b1cd734 commit aa59e32
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions klippy/extras/sht3x.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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'])
Expand All @@ -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
Expand Down

0 comments on commit aa59e32

Please sign in to comment.