|
1 | 1 | # The MIT License (MIT) |
2 | 2 | # |
3 | | -# Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries |
| 3 | +# Copyright (c) 2017 Radomir Dopieralski for Adafruit Industries. |
4 | 4 | # |
5 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | 6 | # of this software and associated documentation files (the "Software"), to deal |
|
19 | 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21 | 21 | # THE SOFTWARE. |
| 22 | + |
22 | 23 | """ |
23 | | -`adafruit_si7021` |
24 | | -==================================================== |
| 24 | +``adafruit_si7021`` |
| 25 | +=================== |
25 | 26 |
|
26 | | -TODO(description) |
| 27 | +This is a CircuitPython driver for the SI7021 temperature and humidity sensor. |
27 | 28 |
|
28 | | -* Author(s): Radomir Dopieralski |
29 | 29 | """ |
| 30 | + |
| 31 | + |
| 32 | +from micropython import const |
| 33 | +import ustruct |
| 34 | +import sys |
| 35 | + |
| 36 | +from adafruit_bus_device.i2c_device import I2CDevice |
| 37 | + |
| 38 | + |
| 39 | +HUMIDITY = const(0xf5) |
| 40 | +TEMPERATURE = const(0xf3) |
| 41 | +_RESET = const(0xfe) |
| 42 | +_READ_USER1 = const(0xe7) |
| 43 | +_USER1_VAL = const(0x3a) |
| 44 | + |
| 45 | + |
| 46 | +def _crc(data): |
| 47 | + crc = 0 |
| 48 | + for byte in data: |
| 49 | + crc ^= byte |
| 50 | + for i in range(8): |
| 51 | + if crc & 0x80: |
| 52 | + crc <<= 1 |
| 53 | + crc ^= 0x131 |
| 54 | + else: |
| 55 | + crc <<= 1 |
| 56 | + return crc |
| 57 | + |
| 58 | + |
| 59 | +class SI7021: |
| 60 | + """ |
| 61 | + A driver for the SI7021 temperature and humidity sensor. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, i2c, address=0x40): |
| 65 | + self.i2c_device = I2CDevice(i2c, address) |
| 66 | + self.init() |
| 67 | + self._measurement = 0 |
| 68 | + |
| 69 | + def init(self): |
| 70 | + self.reset() |
| 71 | + # Make sure the USER1 settings are correct. |
| 72 | + while True: |
| 73 | + # While restarting, the sensor doesn't respond to reads or writes. |
| 74 | + try: |
| 75 | + data = bytearray([_READ_USER1]) |
| 76 | + with self.i2c_device as i2c: |
| 77 | + i2c.writeto(data, stop=False) |
| 78 | + i2c.readfrom_into(data) |
| 79 | + value = data[0] |
| 80 | + except OSError as e: |
| 81 | + if e.args[0] not in ('I2C bus error', 19): # errno 19 ENODEV |
| 82 | + raise |
| 83 | + else: |
| 84 | + break |
| 85 | + if value != _USER1_VAL: |
| 86 | + raise RuntimeError("bad USER1 register (%x!=%x)" % ( |
| 87 | + value, _USER1_VAL)) |
| 88 | + |
| 89 | + def _command(self, command): |
| 90 | + with self.i2c_device as i2c: |
| 91 | + i2c.writeto(ustruct.pack('B', command)) |
| 92 | + |
| 93 | + def _data(self): |
| 94 | + data = bytearray(3) |
| 95 | + data[0] = 0xff |
| 96 | + while True: |
| 97 | + # While busy, the sensor doesn't respond to reads. |
| 98 | + try: |
| 99 | + with self.i2c_device as i2c: |
| 100 | + i2c.readfrom_into(data) |
| 101 | + except OSError as e: |
| 102 | + if e.args[0] not in ('I2C bus error', 19): # errno 19 ENODEV |
| 103 | + raise |
| 104 | + else: |
| 105 | + if data[0] != 0xff: # Check if read succeeded. |
| 106 | + break |
| 107 | + value, checksum = ustruct.unpack('>HB', data) |
| 108 | + if checksum != _crc(data[:2]): |
| 109 | + raise ValueError("CRC mismatch") |
| 110 | + return value |
| 111 | + |
| 112 | + def reset(self): |
| 113 | + self._command(_RESET) |
| 114 | + |
| 115 | + @property |
| 116 | + def relative_humidity(self): |
| 117 | + """The measured relative humidity in percents.""" |
| 118 | + self.start_measurement(HUMIDITY) |
| 119 | + value = self._data() |
| 120 | + self._measurement = 0 |
| 121 | + return value * 125 / 65536 - 6 |
| 122 | + |
| 123 | + @property |
| 124 | + def temperature(self): |
| 125 | + """The measured temperature in degrees Celcius.""" |
| 126 | + self.start_measurement(TEMPERATURE) |
| 127 | + value = self._data() |
| 128 | + self._measurement = 0 |
| 129 | + return value * 175.72 / 65536 - 46.85 |
| 130 | + |
| 131 | + def start_measurement(self, what): |
| 132 | + """ |
| 133 | + Starts a measurement. |
| 134 | +
|
| 135 | + Starts a measurement of either ``HUMIDITY`` or ``TEMPERATURE`` |
| 136 | + depending on the ``what`` argument. Returns immediately, and the |
| 137 | + result of the measurement can be retrieved with the |
| 138 | + ``temperature`` and ``relative_humidity`` properties. This way it |
| 139 | + will take much less time. |
| 140 | +
|
| 141 | + This can be useful if you want to start the measurement, but don't |
| 142 | + want the call to block until the measurement is ready -- for instance, |
| 143 | + when you are doing other things at the same time. |
| 144 | + """ |
| 145 | + if what not in (HUMIDITY, TEMPERATURE): |
| 146 | + raise ValueError() |
| 147 | + if not self._measurement: |
| 148 | + self._command(what) |
| 149 | + elif self._measurement != what: |
| 150 | + raise RuntimeError("other measurement in progress") |
| 151 | + self._measurement = what |
0 commit comments