|
45 | 45 |
|
46 | 46 | __version__ = "0.0.0-auto.0"
|
47 | 47 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MPRLS.git"
|
| 48 | + |
| 49 | + |
| 50 | +import time |
| 51 | +from adafruit_bus_device.i2c_device import I2CDevice |
| 52 | +from digitalio import Direction |
| 53 | +from micropython import const |
| 54 | + |
| 55 | +# pylint: disable=bad-whitespace |
| 56 | +_MPRLS_DEFAULT_ADDR = const(0x18) |
| 57 | +# pylint: enable=bad-whitespace |
| 58 | + |
| 59 | +class MPRLS: |
| 60 | + """ |
| 61 | + Driver base for the MPRLS pressure sensor |
| 62 | + :param i2c_bus: The `busio.I2C` object to use. This is the only required parameter. |
| 63 | + :param int addr: The optional I2C address, defaults to 0x18 |
| 64 | + :param microcontroller.Pin reset_pin: Optional digitalio pin for hardware resetting |
| 65 | + :param microcontroller.Pin eoc_pin: Optional digitalio pin for getting End Of Conversion signal |
| 66 | + :param float psi_min: The minimum pressure in PSI, defaults to 0 |
| 67 | + :param float psi_max: The maximum pressure in PSI, defaults to 25 |
| 68 | + """ |
| 69 | + |
| 70 | + def __init__(self, i2c_bus, *, addr = _MPRLS_DEFAULT_ADDR, |
| 71 | + reset_pin = None, eoc_pin = None, psi_min = 0, psi_max = 25): |
| 72 | + # Init I2C |
| 73 | + self._i2c = I2CDevice(i2c_bus, addr) |
| 74 | + self._buffer = bytearray(4) |
| 75 | + |
| 76 | + # Optional hardware reset pin |
| 77 | + if reset_pin is not None: |
| 78 | + reset_pin.direction = Direction.OUTPUT |
| 79 | + reset_pin.value = True |
| 80 | + reset_pin.value = False |
| 81 | + time.sleep(0.01) |
| 82 | + reset_pin.value = True |
| 83 | + time.sleep(0.005) # Start up timing |
| 84 | + |
| 85 | + # Optional end-of-conversion pin |
| 86 | + self._eoc = eoc_pin |
| 87 | + if eoc_pin is not None: |
| 88 | + self._eoc.direction = Direction.INPUT |
| 89 | + |
| 90 | + self._psimax = psi_max |
| 91 | + self._psimin = psi_min |
| 92 | + # That's pretty much it, there's no ID register :( |
| 93 | + |
| 94 | + @property |
| 95 | + def pressure(self): |
| 96 | + """The measured pressure, in hPa""" |
| 97 | + return self._read_data() |
| 98 | + |
| 99 | + |
| 100 | + def _read_data(self): |
| 101 | + """Read the status & 24-bit data reading""" |
| 102 | + self._buffer[0] = 0xAA |
| 103 | + self._buffer[1] = 0 |
| 104 | + self._buffer[2] = 0 |
| 105 | + with self._i2c as i2c: |
| 106 | + # send command |
| 107 | + i2c.write(self._buffer, end=3) |
| 108 | + # ready busy flag/status |
| 109 | + while True: |
| 110 | + # check End of Convert pin first, if we can |
| 111 | + if self._eoc is not None: |
| 112 | + if self._eoc.value: |
| 113 | + break |
| 114 | + # or you can read the status byte |
| 115 | + i2c.readinto(self._buffer, end=1) |
| 116 | + if not self._buffer[0] & 0x20: |
| 117 | + break |
| 118 | + # no longer busy! |
| 119 | + i2c.readinto(self._buffer, end=4) |
| 120 | + |
| 121 | + # check other status bits |
| 122 | + if self._buffer[0] & 0x01: |
| 123 | + raise RuntimeError("Internal math saturation") |
| 124 | + if self._buffer[0] & 0x04: |
| 125 | + raise RuntimeError("Integrity failure") |
| 126 | + |
| 127 | + # All is good, calculate the PSI and convert to hPA |
| 128 | + raw_psi = (self._buffer[1] << 16) | (self._buffer[2] << 8) | self._buffer[3] |
| 129 | + # use the 10-90 calibration curve |
| 130 | + psi = (raw_psi - 0x19999A) * (self._psimax-self._psimin) |
| 131 | + psi /= 0xE66666 - 0x19999A |
| 132 | + psi += self._psimin |
| 133 | + # convert PSI to hPA |
| 134 | + return psi * 68.947572932 |
0 commit comments