|
23 | 23 | `adafruit_lps35hw`
|
24 | 24 | ================================================================================
|
25 | 25 |
|
26 |
| -A driver for the ST LPS35HW water resistant mems pressure sensor |
| 26 | +A driver for the ST LPS35HW water resistant MEMS pressure sensor |
27 | 27 |
|
28 | 28 |
|
29 | 29 | * Author(s): Bryan Siepert
|
|
36 | 36 | **Software and Dependencies:**
|
37 | 37 |
|
38 | 38 | * Adafruit CircuitPython firmware for the supported boards:
|
39 |
| - https://github.com/adafruit/circuitpython/releases |
40 |
| -
|
41 |
| -
|
| 39 | + https://github.com/adafruit/circuitpython/releases |
42 | 40 | * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
|
43 | 41 | * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
|
| 42 | +
|
44 | 43 | """
|
45 | 44 |
|
46 | 45 | # imports
|
47 | 46 |
|
48 | 47 | __version__ = "0.0.0-auto.0"
|
49 | 48 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LPS35HW.git"
|
| 49 | + |
| 50 | +from micropython import const |
| 51 | +import adafruit_bus_device.i2c_device as i2cdevice |
| 52 | +from adafruit_register.i2c_struct import UnaryStruct |
| 53 | +from adafruit_register.i2c_bits import RWBits, ROBits |
| 54 | +from adafruit_register.i2c_bit import RWBit |
| 55 | +# pylint: disable=bad-whitespace |
| 56 | +_INTERRUPT_CFG = const(0x0B) |
| 57 | +_THS_P_L = const(0x0C) |
| 58 | +_THS_P_H = const(0x0D) |
| 59 | +_WHO_AM_I = const(0x0F) |
| 60 | +_CTRL_REG1 = const(0x10) |
| 61 | +_CTRL_REG2 = const(0x11) |
| 62 | +_CTRL_REG3 = const(0x12) |
| 63 | +_FIFO_CTRL = const(0x14) |
| 64 | +_REF_P_XL = const(0x15) |
| 65 | +_REF_P_L = const(0x16) |
| 66 | +_REF_P_H = const(0x17) |
| 67 | +_RPDS_L = const(0x18) |
| 68 | +_RPDS_H = const(0x19) |
| 69 | +_RES_CONF = const(0x1A) |
| 70 | +_INT_SOURCE = const(0x25) |
| 71 | +_FIFO_STATUS = const(0x26) |
| 72 | +_STATUS = const(0x27) |
| 73 | +_PRESS_OUT_XL = const(0x28) |
| 74 | +_PRESS_OUT_L = const(0x29) |
| 75 | +_PRESS_OUT_H = const(0x2A) |
| 76 | +_TEMP_OUT_L = const(0x2B) |
| 77 | +_TEMP_OUT_H = const(0x2C) |
| 78 | +_LPFP_RES = const(0x33) |
| 79 | +# pylint: enable=bad-whitespace |
| 80 | + |
| 81 | +class DataRate: # pylint: disable=too-few-public-methods |
| 82 | + """Options for ``data_rate`` |
| 83 | +
|
| 84 | + +---------------------------+-------------------------+ |
| 85 | + | ``DataRate`` | Time | |
| 86 | + +===========================+=========================+ |
| 87 | + | ``DataRate.ONE_SHOT`` | One shot mode | |
| 88 | + +---------------------------+-------------------------+ |
| 89 | + | ``DataRate.RATE_1_hz`` | 1 hz | |
| 90 | + +---------------------------+-------------------------+ |
| 91 | + | ``DataRate.RATE_10_hz`` | 10 hz (Default) | |
| 92 | + +---------------------------+-------------------------+ |
| 93 | + | ``DataRate.RATE_25_hz`` | 25 hz | |
| 94 | + +---------------------------+-------------------------+ |
| 95 | + | ``DataRate.RATE_50_hz`` | 50 hz | |
| 96 | + +---------------------------+-------------------------+ |
| 97 | + | ``DataRate.RATE_75_hz`` | 75 hz | |
| 98 | + +---------------------------+-------------------------+ |
| 99 | +
|
| 100 | + """ |
| 101 | + ONE_SHOT = const(0x00) |
| 102 | + RATE_1_hz = const(0x01) |
| 103 | + RATE_10_hz = const(0x02) |
| 104 | + RATE_25_hz = const(0x03) |
| 105 | + RATE_50_hz = const(0x04) |
| 106 | + RATE_75_hz = const(0x05) |
| 107 | + |
| 108 | +class LPS35HW: |
| 109 | + """Driver for the ST LPS35HW MEMS pressure sensor |
| 110 | +
|
| 111 | + :param ~busio.I2C i2c_bus: The I2C bus the INA260 is connected to. |
| 112 | + :param address: The I2C device address for the sensor. Default is ``0x5d``. |
| 113 | +
|
| 114 | + """ |
| 115 | + |
| 116 | + |
| 117 | + reset_pressure = RWBit(_INTERRUPT_CFG, 4) |
| 118 | + """Reset ``pressure`` to be reported as the measured absolute value""" |
| 119 | + |
| 120 | + |
| 121 | + data_rate = RWBits(3, _CTRL_REG1, 4) |
| 122 | + """The rate at which the sensor measures ``pressure`` and ``temperature``. ``data_rate`` should |
| 123 | + be set to one of the values of ``adafruit_lps35hw.DataRate``. Note that setting ``data_rate`` |
| 124 | + to ``DataRate.ONE_SHOT`` places the sensor into a low-power shutdown mode where measurements to |
| 125 | + update ``pressure`` and ``temperature`` are only taken when ``take_measurement`` is called.""" |
| 126 | + |
| 127 | + low_pass_enabled = RWBit(_CTRL_REG1, 3) |
| 128 | + """True if the low pass filter is enabled. Setting to `True` will reduce the sensor bandwidth |
| 129 | + from ``data_rate/2`` to ``data_rate/9``, filtering out high-frequency noise.""" |
| 130 | + |
| 131 | + _raw_temperature = ROBits(16, _TEMP_OUT_L, 0, 2) |
| 132 | + _raw_pressure = ROBits(24, _PRESS_OUT_XL, 0, 3) |
| 133 | + _reference_pressure = RWBits(24, _REF_P_XL, 0, 3) |
| 134 | + _pressure_offset = RWBits(16, _RPDS_L, 0, 2) |
| 135 | + |
| 136 | + _block_updates = RWBit(_CTRL_REG1, 1) |
| 137 | + |
| 138 | + _reset = RWBit(_CTRL_REG2, 2) |
| 139 | + _one_shot = RWBit(_CTRL_REG2, 0) |
| 140 | + |
| 141 | + _interrupt_active_low = RWBit(_CTRL_REG3, 7) |
| 142 | + _interrupt_open_drain = RWBit(_CTRL_REG3, 6) |
| 143 | + _int_pin_on_high = RWBit(_CTRL_REG3, 1) |
| 144 | + _int_pin_on_low = RWBit(_CTRL_REG3, 0) |
| 145 | + |
| 146 | + _interrupt_active = RWBit(_INT_SOURCE, 2) |
| 147 | + _pressure_low = RWBit(_INT_SOURCE, 1) |
| 148 | + _pressure_high = RWBit(_INT_SOURCE, 0) |
| 149 | + |
| 150 | + _auto_zero = RWBit(_INTERRUPT_CFG, 5) |
| 151 | + _interrupts_enabled = RWBit(_INTERRUPT_CFG, 3) |
| 152 | + _interrupt_latch = RWBit(_INTERRUPT_CFG, 2) |
| 153 | + _interrupt_low = RWBit(_INTERRUPT_CFG, 1) |
| 154 | + _interrupt_high = RWBit(_INTERRUPT_CFG, 0) |
| 155 | + |
| 156 | + _reset_filter = ROBits(8, _LPFP_RES, 0, 1) |
| 157 | + |
| 158 | + _chip_id = UnaryStruct(_WHO_AM_I, "<B") |
| 159 | + _pressure_threshold = UnaryStruct(_THS_P_L, "<H") |
| 160 | + |
| 161 | + # def __init__(self, i2c_bus, address=0x5c): |
| 162 | + def __init__(self, i2c_bus, address=0x5d): |
| 163 | + self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address) |
| 164 | + if self._chip_id != 0xb1: |
| 165 | + raise RuntimeError('Failed to find LPS35HW! Chip ID 0x%x' % self._chip_id) |
| 166 | + |
| 167 | + self.reset() |
| 168 | + |
| 169 | + # set data_rate to put the sensor in continuous mode |
| 170 | + self.data_rate = DataRate.RATE_10_hz |
| 171 | + |
| 172 | + self._block_updates = True |
| 173 | + self._interrupt_latch = True |
| 174 | + |
| 175 | + @property |
| 176 | + def pressure(self): |
| 177 | + """The current pressure measurement in hPa""" |
| 178 | + # reset the filter to prevent spurious readings |
| 179 | + self._reset_filter # pylint: disable=pointless-statement |
| 180 | + |
| 181 | + # check for negative and convert |
| 182 | + raw = self._raw_pressure |
| 183 | + if raw & (1<<23) != 0: |
| 184 | + raw = (raw - (1<<24)) |
| 185 | + return raw / 4096.0 |
| 186 | + |
| 187 | + @property |
| 188 | + def temperature(self): |
| 189 | + """The current temperature measurement in degrees C""" |
| 190 | + |
| 191 | + return self._raw_temperature / 100.0 |
| 192 | + |
| 193 | + def reset(self): |
| 194 | + """Reset the sensor, restoring all configuration registers to their defaults""" |
| 195 | + self._reset = True |
| 196 | + # wait for the reset to finish |
| 197 | + while self._reset: |
| 198 | + pass |
| 199 | + |
| 200 | + def take_measurement(self): |
| 201 | + """Update the value of ``pressure`` and ``temperature`` by taking a single measurement. |
| 202 | + Only meaningful if ``data_rate`` is set to ``ONE_SHOT``""" |
| 203 | + self._one_shot = True |
| 204 | + while self._one_shot: |
| 205 | + pass |
| 206 | + |
| 207 | + def zero_pressure(self): |
| 208 | + """Set the current pressure as zero and report the ``pressure`` relative to it""" |
| 209 | + self._auto_zero = True |
| 210 | + while self._auto_zero: |
| 211 | + pass |
| 212 | + |
| 213 | + # def reset_pressure(self): |
| 214 | + # """Reset ``pressure`` to be reported as the measured absolute value""" |
| 215 | + # self._reset_zero = True |
| 216 | + |
| 217 | + @property |
| 218 | + def pressure_threshold(self): |
| 219 | + """The high presure threshold. Use ``high_threshold_enabled`` to use it""" |
| 220 | + return self._pressure_threshold / 16 |
| 221 | + |
| 222 | + @pressure_threshold.setter |
| 223 | + def pressure_threshold(self, value): |
| 224 | + """The high value threshold""" |
| 225 | + self._pressure_threshold = (value * 16) |
| 226 | + |
| 227 | + @property |
| 228 | + def high_threshold_enabled(self): |
| 229 | + """Set to `True` or `False` to enable or disable the high pressure threshold""" |
| 230 | + return self._interrupts_enabled and self._interrupt_high |
| 231 | + |
| 232 | + @high_threshold_enabled.setter |
| 233 | + def high_threshold_enabled(self, value): |
| 234 | + if value: |
| 235 | + self._interrupts_enabled = True |
| 236 | + self._interrupt_high = True |
| 237 | + else: |
| 238 | + self._interrupts_enabled = False |
| 239 | + self._interrupt_high = False |
| 240 | + |
| 241 | + @property |
| 242 | + def high_threshold_exceeded(self): |
| 243 | + """Returns `True` if the pressure high threshold has been exceeded. Must be enabled by |
| 244 | + setting ``high_threshold_enabled`` to `True` and setting a ``pressure_threshold``.""" |
| 245 | + return self._pressure_high |
0 commit comments