Skip to content

Commit 2245f07

Browse files
committed
working code & example
1 parent 21ee013 commit 2245f07

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

README.md

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

README.rst

Lines changed: 17 additions & 1 deletion
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 VEML6075 UVA & UVB sensor.
1717

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

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

3450
Contributing

adafruit_mprls.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,90 @@
4545

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

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)

0 commit comments

Comments
 (0)