diff --git a/LICENSE b/LICENSE index 7511d62..f7acb9d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2017 Radomir Dopieralski, written for Adafruit Industries +Copyright (c) 2017 Radomir Dopieralski, written for Adafruit Industries, and (c) 2016 Chris Balmer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/adafruit_si7021.py b/adafruit_si7021.py index 88b38b0..8004ba1 100644 --- a/adafruit_si7021.py +++ b/adafruit_si7021.py @@ -8,7 +8,7 @@ This is a CircuitPython driver for the SI7021 temperature and humidity sensor. -* Author(s): Radomir Dopieralski +* Author(s): Radomir Dopieralski, Chris Balmer, Ian Grant Implementation Notes -------------------- @@ -40,6 +40,8 @@ _RESET = const(0xFE) _READ_USER1 = const(0xE7) _USER1_VAL = const(0x3A) +_ID1_CMD = bytearray([0xFA, 0x0F]) +_ID2_CMD = bytearray([0xFC, 0xC9]) def _crc(data): @@ -55,6 +57,36 @@ def _crc(data): return crc +def _convert_to_integer(bytes_to_convert): + """Use bitwise operators to convert the bytes into integers.""" + integer = None + for chunk in bytes_to_convert: + if not integer: + integer = chunk + else: + integer = integer << 8 + integer = integer | chunk + return integer + + +def _get_device_identifier(identifier_byte): + """ + Convert the identifier byte to a device identifier (model type). + Values are based on the information from page 24 of the datasheet. + """ + if identifier_byte in (0x00, 0xFF): + identifier_string = "Engineering sample" + elif identifier_byte == 0x0D: + identifier_string = "Si7013" + elif identifier_byte == 0x14: + identifier_string = "Si7020" + elif identifier_byte == 0x15: + identifier_string = "Si7021" + else: + identifier_string = "Unknown" + return identifier_string + + class SI7021: """ A driver for the SI7021 temperature and humidity sensor. @@ -141,3 +173,38 @@ def start_measurement(self, what): elif self._measurement != what: raise RuntimeError("other measurement in progress") self._measurement = what + + @property + def serial_number(self): + """The device's unique ID (serial number).""" + return self._get_device_info()[0] + + @property + def device_identifier(self): + """A device identifier (model type) string.""" + return self._get_device_info()[1] + + def _get_device_info(self): + """ + Get the serial number and the sensor identifier (model type). + The identifier is part of the bytes returned for the serial number. + Source: https://github.com/chrisbalmer/micropython-si7021 + """ + # Serial 1st half + data = _ID1_CMD + id1 = bytearray(8) + with self.i2c_device as i2c: + i2c.write_then_readinto(data, id1) + # Serial 2nd half + data = _ID2_CMD + id2 = bytearray(6) + with self.i2c_device as i2c: + i2c.write_then_readinto(data, id2) + # Combine the two halves + combined_id = bytearray( + [id1[0], id1[2], id1[4], id1[6], id2[0], id2[1], id2[3], id2[4]] + ) + # Convert serial number and extract identifier part + serial = _convert_to_integer(combined_id) + identifier = _get_device_identifier(id2[0]) + return serial, identifier