|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 | 21 | # THE SOFTWARE.
|
22 | 22 | """
|
23 |
| -`Adafruit_MPRLS` |
| 23 | +`adafruit_mprls` |
24 | 24 | ====================================================
|
25 | 25 |
|
26 |
| -.. todo:: Describe what the module does |
| 26 | +CircuitPython library to support Honeywell MPRLS digital pressure sensors |
27 | 27 |
|
28 | 28 | * Author(s): ladyada
|
29 | 29 |
|
|
32 | 32 |
|
33 | 33 | **Hardware:**
|
34 | 34 |
|
35 |
| -.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST |
36 |
| - inline format: "* `Link Text <url>`_" |
37 |
| -
|
38 | 35 | **Software and Dependencies:**
|
39 | 36 |
|
40 | 37 | * Adafruit CircuitPython firmware for the supported boards:
|
41 | 38 | https://github.com/adafruit/circuitpython/releases
|
42 |
| - |
43 |
| -.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either. |
44 | 39 |
|
45 |
| -# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
46 |
| -# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
| 40 | +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 41 | +
|
47 | 42 | """
|
48 | 43 |
|
49 | 44 | # imports
|
50 | 45 |
|
51 | 46 | __version__ = "0.0.0-auto.0"
|
52 | 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 | + if psi_min >= psi_max: |
| 91 | + raise ValueError("Min PSI must be < max!") |
| 92 | + self._psimax = psi_max |
| 93 | + self._psimin = psi_min |
| 94 | + # That's pretty much it, there's no ID register :( |
| 95 | + |
| 96 | + @property |
| 97 | + def pressure(self): |
| 98 | + """The measured pressure, in hPa""" |
| 99 | + return self._read_data() |
| 100 | + |
| 101 | + |
| 102 | + def _read_data(self): |
| 103 | + """Read the status & 24-bit data reading""" |
| 104 | + self._buffer[0] = 0xAA |
| 105 | + self._buffer[1] = 0 |
| 106 | + self._buffer[2] = 0 |
| 107 | + with self._i2c as i2c: |
| 108 | + # send command |
| 109 | + i2c.write(self._buffer, end=3) |
| 110 | + # ready busy flag/status |
| 111 | + while True: |
| 112 | + # check End of Convert pin first, if we can |
| 113 | + if self._eoc is not None: |
| 114 | + if self._eoc.value: |
| 115 | + break |
| 116 | + # or you can read the status byte |
| 117 | + i2c.readinto(self._buffer, end=1) |
| 118 | + if not self._buffer[0] & 0x20: |
| 119 | + break |
| 120 | + # no longer busy! |
| 121 | + i2c.readinto(self._buffer, end=4) |
| 122 | + |
| 123 | + # check other status bits |
| 124 | + if self._buffer[0] & 0x01: |
| 125 | + raise RuntimeError("Internal math saturation") |
| 126 | + if self._buffer[0] & 0x04: |
| 127 | + raise RuntimeError("Integrity failure") |
| 128 | + |
| 129 | + # All is good, calculate the PSI and convert to hPA |
| 130 | + raw_psi = (self._buffer[1] << 16) | (self._buffer[2] << 8) | self._buffer[3] |
| 131 | + # use the 10-90 calibration curve |
| 132 | + psi = (raw_psi - 0x19999A) * (self._psimax-self._psimin) |
| 133 | + psi /= 0xE66666 - 0x19999A |
| 134 | + psi += self._psimin |
| 135 | + # convert PSI to hPA |
| 136 | + return psi * 68.947572932 |
0 commit comments