Skip to content

Commit 824b044

Browse files
authored
Merge pull request #4 from adafruit/lps22
Adding LPS22 support
2 parents a533f8a + 419dd74 commit 824b044

File tree

4 files changed

+162
-58
lines changed

4 files changed

+162
-58
lines changed

README.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ Usage Example
6262
import adafruit_lps2x
6363
6464
i2c = busio.I2C(board.SCL, board.SDA)
65-
lps = adafruit_lps2x.LPS25HW(i2c)
66-
print("out of reset/init")
65+
# uncomment and comment out the line after to use with the LPS22
66+
# lps = adafruit_lps2x.LPS22(i2c)
67+
lps = adafruit_lps2x.LPS25(i2c)
6768
while True:
6869
print("Pressure: %.2f hPa" % lps.pressure)
69-
print("Temperature: %.2f C"% lps.temperature)
70+
print("Temperature: %.2f C" % lps.temperature)
7071
time.sleep(1)
7172
7273
Contributing

adafruit_lps2x.py

Lines changed: 154 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
3434
**Hardware:**
3535
36-
* `LPS25HB Breakout <https://www.adafruit.com/products/45XX>`_
36+
* LPS25HB Breakout https://www.adafruit.com/products/4530
3737
3838
**Software and Dependencies:**
3939
* Adafruit CircuitPython firmware for the supported boards:
@@ -44,20 +44,41 @@
4444
"""
4545
__version__ = "0.0.0-auto.0"
4646
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LPS2X.git"
47+
from time import sleep
4748
from micropython import const
4849
import adafruit_bus_device.i2c_device as i2cdevice
4950
from adafruit_register.i2c_struct import ROUnaryStruct
5051
from adafruit_register.i2c_bits import RWBits, ROBits
5152
from adafruit_register.i2c_bit import RWBit
5253

53-
_WHO_AM_I = const(0x0F)
54-
_CTRL_REG1 = const(0x20)
55-
_CTRL_REG2 = const(0x21)
56-
_PRESS_OUT_XL = const(0x28 | 0x80) # | 0x80 to set auto increment on multi-byte read
57-
_TEMP_OUT_L = const(0x2B | 0x80) # | 0x80 to set auto increment on multi-byte read
54+
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
55+
# _LPS2X_WHOAMI = 0x0F # Chip ID register
56+
# _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read
57+
# _LPS2X_TEMP_OUT_L = (0x2B # 0x80) ///< | 0x80 to set auto increment on
58+
_LPS2X_WHO_AM_I = const(0x0F)
59+
_LPS2X_PRESS_OUT_XL = const(
60+
0x28 | 0x80
61+
) # | 0x80 to set auto increment on multi-byte read
62+
_LPS2X_TEMP_OUT_L = const(
63+
0x2B | 0x80
64+
) # | 0x80 to set auto increment on multi-byte read
5865

59-
_LPS25_CHIP_ID = 0xBD
60-
_LPS25_DEFAULT_ADDRESS = 0x5D
66+
_LPS25_CTRL_REG1 = const(0x20) # First control register. Includes BD & ODR
67+
_LPS25_CTRL_REG2 = const(0x21) # Second control register. Includes SW Reset
68+
# _LPS25_CTRL_REG3 = 0x22 # Third control register. Includes interrupt polarity
69+
# _LPS25_CTRL_REG4 = 0x23 # Fourth control register. Includes DRDY INT control
70+
# _LPS25_INTERRUPT_CFG = 0x24 # Interrupt control register
71+
# _LPS25_THS_P_L_REG = 0xB0 # Pressure threshold value for int
72+
73+
74+
# _LPS22_THS_P_L_REG = 0x0C # Pressure threshold value for int
75+
_LPS22_CTRL_REG1 = 0x10 # First control register. Includes BD & ODR
76+
_LPS22_CTRL_REG2 = 0x11 # Second control register. Includes SW Reset
77+
# _LPS22_CTRL_REG3 = 0x12 # Third control register. Includes interrupt polarity
78+
79+
_LPS2X_DEFAULT_ADDRESS = 0x5D
80+
_LPS25HB_CHIP_ID = 0xBD
81+
_LPS22HB_CHIP_ID = 0xB1 # LPS22 default device id from WHOAMI
6182

6283

6384
class CV:
@@ -84,64 +105,65 @@ def is_valid(cls, value):
84105
class Rate(CV):
85106
"""Options for ``data_rate``
86107
87-
+-----------------------+------------------------------------------------------------------+
88-
| Rate | Description |
89-
+-----------------------+------------------------------------------------------------------+
90-
| ``Rate.ONE_SHOT`` | Setting `data_rate` to ``Rate.ONE_SHOT`` takes a single pressure |
91-
| | and temperature measurement |
92-
+-----------------------+------------------------------------------------------------------+
93-
| ``Rate.RATE_1_HZ`` | 1 Hz |
94-
+-----------------------+------------------------------------------------------------------+
95-
| ``Rate.RATE_7_HZ`` | 7 Hz |
96-
+-----------------------+------------------------------------------------------------------+
97-
| ``Rate.RATE_12_5_HZ`` | 12.5 Hz |
98-
+-----------------------+------------------------------------------------------------------+
99-
| ``Rate.RATE_25_HZ`` | 25 Hz |
100-
+-----------------------+------------------------------------------------------------------+
108+
+-----------------------------+------------------------------------------------+
109+
| Rate | Description |
110+
+-----------------------------+------------------------------------------------+
111+
| ``Rate.LSP25_SHUTDOWN`` | Setting `data_rate` to ``Rate.LSP25_SHUTDOWN`` |
112+
| | stops measurements from being taken |
113+
+-----------------------------+------------------------------------------------+
114+
| ``Rate.LSP25_RATE_1_HZ`` | 1 Hz |
115+
+-----------------------------+------------------------------------------------+
116+
| ``Rate.LSP25_RATE_7_HZ`` | 7 Hz |
117+
+-----------------------------+------------------------------------------------+
118+
| ``Rate.LSP25_RATE_12_5_HZ`` | 12.5 Hz |
119+
+-----------------------------+------------------------------------------------+
120+
| ``Rate.LSP25_RATE_25_HZ`` | 25 Hz |
121+
+-----------------------------+------------------------------------------------+
122+
| ``Rate.LSP22_SHUTDOWN`` | Setting `data_rate` to ``Rate.LSP22_SHUTDOWN`` |
123+
| | stops measurements from being taken |
124+
+-----------------------------+------------------------------------------------+
125+
| ``Rate.LSP22_RATE_1_HZ`` | 1 Hz |
126+
+-----------------------------+------------------------------------------------+
127+
| ``Rate.LSP22_RATE_10_HZ`` | 10 Hz |
128+
+-----------------------------+------------------------------------------------+
129+
| ``Rate.LSP22_RATE_25_HZ`` | 25 Hz |
130+
+-----------------------------+------------------------------------------------+
131+
| ``Rate.LSP22_RATE_50_HZ`` | 50 Hz |
132+
+-----------------------------+------------------------------------------------+
101133
102134
"""
103135

104136
pass # pylint: disable=unnecessary-pass
105137

106138

107-
Rate.add_values(
108-
(
109-
("RATE_ONE_SHOT", 0, 0, None),
110-
("RATE_1_HZ", 1, 1, None),
111-
("RATE_7_HZ", 2, 7, None),
112-
("RATE_12_5_HZ", 3, 12.5, None),
113-
("RATE_25_HZ", 4, 25, None),
114-
)
115-
)
116-
117-
118139
class LPS2X: # pylint: disable=too-many-instance-attributes
119-
"""Library for the ST LPS2x family of pressure sensors
140+
"""Base class ST LPS2x family of pressure sensors
120141
121-
:param ~busio.I2C i2c_bus: The I2C bus the LPS25HB is connected to.
142+
:param ~busio.I2C i2c_bus: The I2C bus the sensor is connected to.
122143
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
123144
``0x5c`` when the ``SDO`` pin is connected to Ground.
124145
125146
"""
126147

127-
_chip_id = ROUnaryStruct(_WHO_AM_I, "<B")
128-
_reset = RWBit(_CTRL_REG2, 2)
129-
enabled = RWBit(_CTRL_REG1, 7)
130-
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
131-
_data_rate = RWBits(3, _CTRL_REG1, 4)
132-
_raw_temperature = ROUnaryStruct(_TEMP_OUT_L, "<h")
133-
_raw_pressure = ROBits(24, _PRESS_OUT_XL, 0, 3)
148+
_chip_id = ROUnaryStruct(_LPS2X_WHO_AM_I, "<B")
149+
_raw_temperature = ROUnaryStruct(_LPS2X_TEMP_OUT_L, "<h")
150+
_raw_pressure = ROBits(24, _LPS2X_PRESS_OUT_XL, 0, 3)
134151

135-
def __init__(self, i2c_bus, address=_LPS25_DEFAULT_ADDRESS):
152+
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
136153
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
137-
if not self._chip_id in [_LPS25_CHIP_ID]:
154+
if not self._chip_id in [chip_id]:
138155
raise RuntimeError(
139-
"Failed to find LPS25HB! Found chip ID 0x%x" % self._chip_id
156+
"Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id
140157
)
141-
142158
self.reset()
143-
self.enabled = True
144-
self.data_rate = Rate.RATE_25_HZ # pylint:disable=no-member
159+
self.initialize()
160+
sleep(0.010) # delay 10ms for first reading
161+
162+
def initialize(self): # pylint: disable=no-self-use
163+
"""Configure the sensor with the default settings. For use after calling `reset()`"""
164+
raise RuntimeError(
165+
"LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead"
166+
) # override in subclass
145167

146168
def reset(self):
147169
"""Reset the sensor, restoring all configuration registers to their defaults"""
@@ -162,16 +184,16 @@ def pressure(self):
162184
@property
163185
def temperature(self):
164186
"""The current temperature measurement in degrees C"""
187+
165188
raw_temperature = self._raw_temperature
166-
return (raw_temperature / 480) + 42.5
189+
return (
190+
raw_temperature / self._temp_scaling # pylint:disable=no-member
191+
) + self._temp_offset # pylint:disable=no-member
167192

168193
@property
169194
def data_rate(self):
170195
"""The rate at which the sensor measures ``pressure`` and ``temperature``. ``data_rate``
171-
shouldbe set to one of the values of ``adafruit_lps2x.DataRate``. Note that setting
172-
``data_rate``to ``Rate.ONE_SHOT`` places the sensor into a low-power shutdown mode where
173-
measurements toupdate ``pressure`` and ``temperature`` are only taken when
174-
``take_measurement`` is called."""
196+
shouldbe set to one of the values of ``adafruit_lps2x.Rate``."""
175197
return self._data_rate
176198

177199
@data_rate.setter
@@ -180,3 +202,82 @@ def data_rate(self, value):
180202
raise AttributeError("data_rate must be a `Rate`")
181203

182204
self._data_rate = value
205+
206+
207+
class LPS25(LPS2X):
208+
"""Library for the ST LPS25 pressure sensors
209+
210+
:param ~busio.I2C i2c_bus: The I2C bus the LPS25HB is connected to.
211+
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
212+
``0x5c`` when the ``SDO`` pin is connected to Ground.
213+
214+
"""
215+
216+
enabled = RWBit(_LPS25_CTRL_REG1, 7)
217+
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
218+
_reset = RWBit(_LPS25_CTRL_REG2, 2)
219+
_data_rate = RWBits(3, _LPS25_CTRL_REG1, 4)
220+
221+
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
222+
223+
Rate.add_values(
224+
(
225+
("LPS25_RATE_ONE_SHOT", 0, 0, None),
226+
("LPS25_RATE_1_HZ", 1, 1, None),
227+
("LPS25_RATE_7_HZ", 2, 7, None),
228+
("LPS25_RATE_12_5_HZ", 3, 12.5, None),
229+
("LPS25_RATE_25_HZ", 4, 25, None),
230+
)
231+
)
232+
super().__init__(i2c_bus, address, chip_id=_LPS25HB_CHIP_ID)
233+
234+
self._temp_scaling = 480
235+
self._temp_offset = 42.5
236+
# self._inc_spi_flag = 0x40
237+
238+
def initialize(self):
239+
"""Configure the sensor with the default settings. For use after calling `reset()`"""
240+
self.enabled = True
241+
self.data_rate = Rate.LPS25_RATE_25_HZ # pylint:disable=no-member
242+
243+
# void configureInterrupt(bool activelow, bool opendrain,
244+
# bool pres_high = false, bool pres_low = false);
245+
246+
247+
class LPS22(LPS2X):
248+
"""Library for the ST LPS22 pressure sensors
249+
250+
:param ~busio.I2C i2c_bus: The I2C bus the LPS22HB is connected to.
251+
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
252+
``0x5c`` when the ``SDO`` pin is connected to Ground.
253+
254+
"""
255+
256+
_reset = RWBit(_LPS22_CTRL_REG2, 2)
257+
_data_rate = RWBits(3, _LPS22_CTRL_REG1, 4)
258+
259+
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
260+
# Only adding Class-appropriate rates
261+
Rate.add_values(
262+
(
263+
("LPS22_RATE_ONE_SHOT", 0, 0, None),
264+
("LPS22_RATE_1_HZ", 1, 1, None),
265+
("LPS22_RATE_10_HZ", 2, 10, None),
266+
("LPS22_RATE_25_HZ", 3, 25, None),
267+
("LPS22_RATE_50_HZ", 4, 50, None),
268+
("LPS22_RATE_75_HZ", 5, 75, None),
269+
)
270+
)
271+
272+
super().__init__(i2c_bus, address, chip_id=_LPS22HB_CHIP_ID)
273+
self._temp_scaling = 100
274+
self._temp_offset = 0
275+
276+
def initialize(self):
277+
"""Configure the sensor with the default settings. For use after calling `reset()`"""
278+
self.data_rate = Rate.LPS22_RATE_75_HZ # pylint:disable=no-member
279+
280+
# void configureInterrupt(bool activelow, bool opendrain, bool data_ready,
281+
# bool pres_high = false, bool pres_low = false,
282+
# bool fifo_full = false, bool fifo_watermark = false,
283+
# bool fifo_overflow = false);

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Table of Contents
2727
.. toctree::
2828
:caption: Related Products
2929

30-
* `LPS25HW Breakout <https://www.adafruit.com/products/4258>`_
30+
* LPS25HW Breakout <https://www.adafruit.com/products/4258>
3131

3232
.. toctree::
3333
:caption: Other Links

examples/lps2x_simpletest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import adafruit_lps2x
55

66
i2c = busio.I2C(board.SCL, board.SDA)
7-
lps = adafruit_lps2x.LPS2X(i2c)
7+
# uncomment and comment out the line after to use with the LPS22
8+
# lps = adafruit_lps2x.LPS22(i2c)
9+
lps = adafruit_lps2x.LPS25(i2c)
810
while True:
911
print("Pressure: %.2f hPa" % lps.pressure)
1012
print("Temperature: %.2f C" % lps.temperature)

0 commit comments

Comments
 (0)