Skip to content

Commit 499c17b

Browse files
committed
initial working version
1 parent 44d42e0 commit 499c17b

8 files changed

+285
-6
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ To install in a virtual environment in your current project:
5555
Usage Example
5656
=============
5757

58-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
58+
See the guide at: https://learn.adafruit.com
5959

6060
Contributing
6161
============

adafruit_lps35hw.py

Lines changed: 200 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
`adafruit_lps35hw`
2424
================================================================================
2525
26-
A driver for the ST LPS35HW water resistant mems pressure sensor
26+
A driver for the ST LPS35HW water resistant MEMS pressure sensor
2727
2828
2929
* Author(s): Bryan Siepert
@@ -36,14 +36,210 @@
3636
**Software and Dependencies:**
3737
3838
* Adafruit CircuitPython firmware for the supported boards:
39-
https://github.com/adafruit/circuitpython/releases
40-
41-
39+
https://github.com/adafruit/circuitpython/releases
4240
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4341
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
42+
4443
"""
4544

4645
# imports
4746

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

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 = ["micropython", "adafruit_bus_device", "adafruit_register"]
2424

2525

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

examples/lps35hw_data_rate.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import board
2+
from adafruit_lps35hw import LPS35HW, DataRate
3+
import time
4+
5+
i2c = board.I2C()
6+
lps = LPS35HW(i2c)
7+
8+
lps.data_rate = DataRate.ONE_SHOT
9+
lps.take_measurement()
10+
11+
12+
while True:
13+
print("Pressure: %.2f hPa" % lps.pressure)
14+
print("")
15+
time.sleep(1)
16+
print("Pressure: %.2f hPa" % lps.pressure)
17+
print("")
18+
time.sleep(1)
19+
print("Pressure: %.2f hPa" % lps.pressure)
20+
print("")
21+
time.sleep(1)
22+
23+
#take another measurement
24+
lps.take_measurement()
25+
26+
print("New Pressure: %.2f hPa" % lps.pressure)
27+
print("")
28+
time.sleep(1)

examples/lps35hw_filter.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import board
2+
import adafruit_lps35hw
3+
import time
4+
5+
i2c = board.I2C()
6+
lps = adafruit_lps35hw.LPS35HW(i2c)
7+
8+
lps.low_pass_enabled = True
9+
while True:
10+
print("Pressure: %.2f hPa" % lps.pressure)
11+
print("")
12+
time.sleep(0.125)

examples/lps35hw_high_threshold.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import board
2+
import adafruit_lps35hw
3+
import time
4+
i2c = board.I2C()
5+
lps = adafruit_lps35hw.LPS35HW(i2c)
6+
7+
# You may need to adjust the threshold to something closer
8+
# to the current pressure where the sensor is
9+
lps.pressure_threshold = 1030
10+
11+
lps.high_threshold_enabled = True
12+
13+
while True:
14+
print("Pressure: %.2f hPa" % lps.pressure)
15+
print("Threshhold exceeded: %s" % lps.high_threshold_exceeded)
16+
print("")
17+
time.sleep(1)

examples/lps35hw_relative.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import board
2+
import adafruit_lps35hw
3+
import time
4+
5+
i2c = board.I2C()
6+
lps = adafruit_lps35hw.LPS35HW(i2c)
7+
8+
# set the current pressure as zero hPa and make measurements
9+
# relative to that pressure, even negative!
10+
lps.zero_pressure()
11+
while True:
12+
print("Pressure: %.2f hPa" % lps.pressure)
13+
print("")
14+
time.sleep(0.5)

examples/lps35hw_simpletest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import board
2+
import adafruit_lps35hw
3+
import time
4+
5+
i2c = board.I2C()
6+
lps = adafruit_lps35hw.LPS35HW(i2c)
7+
8+
while True:
9+
print("Pressure: %.2f hPa" % lps.pressure)
10+
print("Temperature: %.2f C"% lps.temperature)
11+
print("")
12+
time.sleep(1)

0 commit comments

Comments
 (0)