|
16 | 16 |
|
17 | 17 | **Hardware:**
|
18 | 18 |
|
19 |
| -.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST |
20 |
| - inline format: "* `Link Text <url>`_" |
| 19 | +* `Adafruit AS7341 Breakout <https://www.adafruit.com/products/45XX>`_ |
21 | 20 |
|
22 | 21 | **Software and Dependencies:**
|
23 | 22 |
|
24 | 23 | * Adafruit CircuitPython firmware for the supported boards:
|
25 | 24 | https://github.com/adafruit/circuitpython/releases
|
26 | 25 |
|
27 |
| -.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either. |
| 26 | + * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 27 | + * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
28 | 28 |
|
29 |
| -# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
30 |
| -# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
31 | 29 | """
|
32 | 30 |
|
33 | 31 | # imports
|
34 | 32 |
|
35 | 33 | __version__ = "0.0.0-auto.0"
|
36 | 34 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MS8607.git"
|
| 35 | + |
| 36 | + |
| 37 | +from struct import unpack_from |
| 38 | +from time import sleep |
| 39 | +from micropython import const |
| 40 | +import adafruit_bus_device.i2c_device as i2c_device |
| 41 | + |
| 42 | +# // HSENSOR device commands |
| 43 | +_MS8607_RESET_COMMAND = const(0xFE) # |
| 44 | +_MS8607_READ_HUMIDITY_W_HOLD_COMMAND = const(0xE5) # |
| 45 | +_MS8607_READ_HUMIDITY_WO_HOLD_COMMAND = const(0xF5) # |
| 46 | +_MS8607_READ_SERIAL_FIRST_8BYTES_COMMAND = const(0xFA0F) # |
| 47 | +_MS8607_READ_SERIAL_LAST_6BYTES_COMMAND = const(0xFCC9) # |
| 48 | +_MS8607_WRITE_USER_REG_COMMAND = const(0xE6) # |
| 49 | +_MS8607_READ_USER_REG_COMMAND = const(0xE7) # |
| 50 | + |
| 51 | +_MS8607_HSENSOR_ADDR = const(0x40) # |
| 52 | + |
| 53 | +# enum MS8607_humidity_resolution |
| 54 | +# { |
| 55 | +# MS8607_humidity_resolution_12b = 0, |
| 56 | +# MS8607_humidity_resolution_8b, |
| 57 | +# MS8607_humidity_resolution_10b, |
| 58 | +# MS8607_humidity_resolution_11b |
| 59 | +# };] |
| 60 | + |
| 61 | + |
| 62 | +class MS8607Humidity: |
| 63 | + """Library for the MS8607 Humidity Sensor |
| 64 | +
|
| 65 | +
|
| 66 | + :param ~busio.I2C i2c_bus: The I2C bus the MS8607 is connected to. |
| 67 | +
|
| 68 | + """ |
| 69 | + |
| 70 | + def __init__(self, i2c_bus): |
| 71 | + |
| 72 | + self.i2c_device = i2c_device.I2CDevice(i2c_bus, _MS8607_HSENSOR_ADDR) |
| 73 | + |
| 74 | + # self.reset() |
| 75 | + # self.initialize() |
| 76 | + self._buffer = bytearray(3) |
| 77 | + |
| 78 | + @property |
| 79 | + def relative_humidity(self): |
| 80 | + """The current relative humidity in % rH""" |
| 81 | + |
| 82 | + # self._buffer.clear() |
| 83 | + self._buffer[0] = _MS8607_READ_HUMIDITY_WO_HOLD_COMMAND |
| 84 | + with self.i2c_device as i2c: |
| 85 | + i2c.write(self._buffer, end=1) |
| 86 | + sleep(0.1) # _i2cPort->requestFrom((uint8_t)MS8607_HSENSOR_ADDR, 3U); |
| 87 | + |
| 88 | + with self.i2c_device as i2c: |
| 89 | + i2c.readinto(self._buffer, end=3) |
| 90 | + |
| 91 | + # _adc = (buffer[0] << 8) | buffer[1]; |
| 92 | + # crc = buffer[2]; |
| 93 | + raw_humidity = unpack_from(">H", self._buffer) |
| 94 | + |
| 95 | + # *humidity = |
| 96 | + # adc * HUMIDITY_COEFF_MUL / (1UL << 16) + HUMIDITY_COEFF_ADD; |
| 97 | + return raw_humidity |
| 98 | + # // compute CRC |
| 99 | + # uint8_t crc; |
| 100 | + # status = hsensor_crc_check(_adc, crc); |
| 101 | + # if (status != MS8607_status_ok) |
| 102 | + # return status; |
| 103 | + |
| 104 | + # *adc = _adc; |
| 105 | + |
| 106 | + # return status; |
| 107 | + |
| 108 | + |
| 109 | +# read_temperature_pressure_humidity(float *t, float *p, |
| 110 | + |
| 111 | +# set_humidity_resolution(enum MS8607_humidity_resolution res); |
| 112 | +# void set_humidity_i2c_master_mode(enum MS8607_humidity_i2c_master_mode mode); |
| 113 | + |
| 114 | +# get_compensated_humidity(float temperature, float relative_humidity, float *compensated_humidity); |
| 115 | + |
| 116 | +# /* |
| 117 | +# private: |
| 118 | +# bool hsensor_is_connected(void); |
| 119 | +# hsensor_reset(void); |
| 120 | +# hsensor_crc_check(uint16_t value, uint8_t crc); |
| 121 | +# hsensor_read_user_register(uint8_t *value); |
| 122 | +# hsensor_write_user_register(uint8_t value); |
| 123 | +# enum MS8607_humidity_i2c_master_mode hsensor_i2c_master_mode; |
| 124 | +# hsensor_humidity_conversion_and_read_adc(uint16_t *adc); |
| 125 | +# hsensor_read_relative_humidity(float *humidity); |
| 126 | +# int err = set_humidity_resolution(MS8607_humidity_resolution_12b); // 12 bits |
| 127 | +# hsensor_conversion_time = HSENSOR_CONVERSION_TIME_12b; |
| 128 | +# hsensor_i2c_master_mode = MS8607_i2c_no_hold; |
| 129 | +# float humidity = getHumidity(); |
| 130 | +# float temperature = getTemperature(); |
| 131 | +# float compensated_RH= get_compensated_humidity(temperature, humidity, &compensated_RH); |
| 132 | +# float dew_point = get_dew_point(temperature, humidity, &dew_point); |
0 commit comments