Skip to content

Commit e7543ae

Browse files
authored
Merge pull request #2 from ladyada/master
mini lint
2 parents c94be3c + 68f3d7d commit e7543ae

File tree

8 files changed

+135
-20
lines changed

8 files changed

+135
-20
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ deploy:
2323
tags: true
2424

2525
install:
26+
- pip install -r requirements.txt
2627
- pip install circuitpython-build-tools Sphinx sphinx-rtd-theme
2728
- pip install --force-reinstall pylint==1.9.2
2829

README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Introduction
1313
:target: https://travis-ci.org/adafruit/Adafruit_CircuitPython_MPRLS
1414
:alt: Build Status
1515

16-
.. todo:: Describe what the library does.
16+
CircuitPython library to support Honeywell MPRLS digital pressure sensors.
1717

1818
Dependencies
1919
=============
@@ -29,7 +29,22 @@ This is easily achieved by downloading
2929
Usage Example
3030
=============
3131

32-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
32+
.. code-block:: python
33+
34+
import time
35+
import board
36+
import busio
37+
import adafruit_mprls
38+
39+
i2c = busio.I2C(board.SCL, board.SDA)
40+
41+
# Simplest use, connect to default over I2C
42+
mpr = adafruit_mprls.MPRLS(i2c, psi_min=0, psi_max=25)
43+
44+
45+
while True:
46+
print((mpr.pressure,))
47+
time.sleep(1)
3348
3449
Contributing
3550
============

adafruit_mprls.py

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`Adafruit_MPRLS`
23+
`adafruit_mprls`
2424
====================================================
2525
26-
.. todo:: Describe what the module does
26+
CircuitPython library to support Honeywell MPRLS digital pressure sensors
2727
2828
* Author(s): ladyada
2929
@@ -32,21 +32,105 @@
3232
3333
**Hardware:**
3434
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-
3835
**Software and Dependencies:**
3936
4037
* Adafruit CircuitPython firmware for the supported boards:
4138
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.
4439
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+
4742
"""
4843

4944
# imports
5045

5146
__version__ = "0.0.0-auto.0"
5247
__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

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
# autodoc_mock_imports = ["digitalio", "busio"]
23+
autodoc_mock_imports = ["digitalio", "micropython"]
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

docs/index.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,9 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
27-
the toctree above for use later.
28-
2926
.. toctree::
3027
:caption: Related Products
3128

32-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
33-
the toctree above for use later.
34-
3529
.. toctree::
3630
:caption: Other Links
3731

examples/mprls_simpletest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
import board
3+
import busio
4+
import adafruit_mprls
5+
6+
i2c = busio.I2C(board.SCL, board.SDA)
7+
8+
# Simplest use, connect to default over I2C
9+
mpr = adafruit_mprls.MPRLS(i2c, psi_min=0, psi_max=25)
10+
11+
# You can also specify both reset and eoc pins
12+
"""
13+
import digitalio
14+
reset = digitalio.DigitalInOut(board.D5)
15+
eoc = digitalio.DigitalInOut(board.D6)
16+
mpr = adafruit_mprls.MPRLS(i2c, eoc_pin=eoc, reset_pin=reset,
17+
psi_min=0, psi_max=25)
18+
"""
19+
20+
while True:
21+
print((mpr.pressure,))
22+
time.sleep(1)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
Adafruit-Blinka
12
adafruit-circuitpython-busdevice

0 commit comments

Comments
 (0)