Skip to content

Commit caecd33

Browse files
committed
CRC working
1 parent 1de8623 commit caecd33

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

adafruit_shtc3.py

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@
7575
_SHTC3_SLEEP = 0xB098 # Enter sleep mode
7676
_SHTC3_WAKEUP = 0x3517 # Wakeup mode
7777
_SHTC3_CHIP_ID = 0x807
78-
# const uint8_t POLYNOMIAL(0x31)
79-
_POLYNOMIAL = 0x31
8078

8179

8280
class SHTC3:
@@ -165,68 +163,62 @@ def measurements(self):
165163
"""both `temperature` and `relative_humidity`, read simultaneously"""
166164

167165
self.sleep = False
168-
raw_readings = []
169-
166+
temperature = None
167+
humidity = None
168+
# send correct command for the current power state
170169
if self.low_power:
171170
self._write_command(_SHTC3_LOWPOW_MEAS_TFIRST)
172171
time.sleep(0.001)
173172
else:
174173
self._write_command(_SHTC3_NORMAL_MEAS_TFIRST)
175174
time.sleep(0.013)
176175

177-
self._buffer = bytearray(6)
176+
# self._buffer = bytearray(6)
177+
# read the measured data into our buffer
178178
with self.i2c_device as i2c:
179179
i2c.readinto(self._buffer)
180-
raw_readings = unpack_from(">hbh", self._buffer)
181180

182-
# print("CRC8-ing some beef:", hex(self._crc8(bytearray([0xBE, 0xEF]), 2)))
183-
# check CRC of bytes
184-
# if (self._buffer[2] != crc8(self._buffer, 2) or
185-
# self._buffer[5] != crc8(self._buffer + 3, 2)):
186-
# print("NOT CHECKING")
181+
# separate the read data
182+
temp_data = self._buffer[0:2]
183+
temp_crc = self._buffer[2]
184+
humidity_data = self._buffer[3:5]
185+
humidity_crc = self._buffer[5]
187186

188-
raw_temp = raw_readings[0]
187+
# check CRC of bytes
188+
if temp_crc != self._crc8(temp_data) or humidity_crc != self._crc8(
189+
humidity_data
190+
):
191+
return (temperature, humidity)
192+
193+
# decode data into human values:
194+
# convert bytes into 16-bit signed integer
195+
# convert the LSB value to a human value according to the datasheet
196+
raw_temp = unpack_from(">h", temp_data)
189197
raw_temp = ((4375 * raw_temp) >> 14) - 4500
190198
temperature = raw_temp / 100.0
191199

192-
raw_humidity = raw_readings[2]
200+
# repeat above steps for humidity data
201+
raw_humidity = unpack_from(">h", humidity_data)
193202
raw_humidity = (625 * raw_humidity) >> 12
194203
humidity = raw_humidity / 100.0
195204

196205
self.sleep = True
197206
return (temperature, humidity)
198207

199-
## CRC-8 formula from page 14 of SHT spec pdf
200-
#
201-
# Test data 0xBE, 0xEF should yield 0x92
202-
#
203-
# Initialization data 0xFF
204-
# Polynomial 0x31 (x8 + x5 +x4 +1)
205-
# Final XOR 0x00
206-
207-
# @staticmethod
208-
# def _crc8(buffer, length):
209-
# print("buffer:", buffer)
210-
# crc = 0xFF
211-
# print("0: crc = ", format(crc, "#010b"))
212-
213-
# buff_index = 0
214-
# for i in range(length):
215-
# curr_buff = buffer[i]
216-
# print("buffer[%d]" % i, hex(curr_buff), format(curr_buff, "#010b"))
217-
# crc ^= curr_buff
218-
# print("1 crc = ", format(crc, "#010b"))
219-
220-
# for i in range(8):
221-
# print("crc & 0x80: ", format(crc & 0x80, "#010b"))
222-
# crc_shift_one = (crc << 1)
223-
# if crc & 0x80: # if crc top bit is set
224-
# print("yes")
225-
# crc = crc_shift_one ^ _POLYNOMIAL
226-
# else:
227-
# print("no")
228-
# crc = crc_shift_one
229-
# print("3 crc = crc << 1", format(crc, "#010b"))
230-
# print("\t*** NEXT BIT **")
231-
# print("*** NEXT BYTE **")
232-
# return crc
208+
## CRC-8 formula from page 14 of SHTC3 datasheet
209+
# https://media.digikey.com/pdf/Data%20Sheets/Sensirion%20PDFs/HT_DS_SHTC3_D1.pdf
210+
# Test data [0xBE, 0xEF] should yield 0x92
211+
212+
@staticmethod
213+
def _crc8(buffer):
214+
print("\t\tbuff", [hex(i) for i in buffer])
215+
crc = 0xFF
216+
for byte in buffer:
217+
crc ^= byte
218+
for _ in range(8):
219+
if crc & 0x80:
220+
crc = (crc << 1) ^ 0x31
221+
else:
222+
crc = crc << 1
223+
print("\t\tcrc:", hex(crc & 0xFF))
224+
return crc & 0xFF # return the bottom 8 bits

0 commit comments

Comments
 (0)