Skip to content

Adding LPS22 support #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ Usage Example
import adafruit_lps2x

i2c = busio.I2C(board.SCL, board.SDA)
lps = adafruit_lps2x.LPS25HW(i2c)
print("out of reset/init")
# uncomment and comment out the line after to use with the LPS22
# lps = adafruit_lps2x.LPS22(i2c)
lps = adafruit_lps2x.LPS25(i2c)
while True:
print("Pressure: %.2f hPa" % lps.pressure)
print("Temperature: %.2f C"% lps.temperature)
print("Temperature: %.2f C" % lps.temperature)
time.sleep(1)

Contributing
Expand Down
207 changes: 154 additions & 53 deletions adafruit_lps2x.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

**Hardware:**

* `LPS25HB Breakout <https://www.adafruit.com/products/45XX>`_
* LPS25HB Breakout https://www.adafruit.com/products/4530

**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
Expand All @@ -44,20 +44,41 @@
"""
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LPS2X.git"
from time import sleep
from micropython import const
import adafruit_bus_device.i2c_device as i2cdevice
from adafruit_register.i2c_struct import ROUnaryStruct
from adafruit_register.i2c_bits import RWBits, ROBits
from adafruit_register.i2c_bit import RWBit

_WHO_AM_I = const(0x0F)
_CTRL_REG1 = const(0x20)
_CTRL_REG2 = const(0x21)
_PRESS_OUT_XL = const(0x28 | 0x80) # | 0x80 to set auto increment on multi-byte read
_TEMP_OUT_L = const(0x2B | 0x80) # | 0x80 to set auto increment on multi-byte read
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
# _LPS2X_WHOAMI = 0x0F # Chip ID register
# _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read
# _LPS2X_TEMP_OUT_L = (0x2B # 0x80) ///< | 0x80 to set auto increment on
_LPS2X_WHO_AM_I = const(0x0F)
_LPS2X_PRESS_OUT_XL = const(
0x28 | 0x80
) # | 0x80 to set auto increment on multi-byte read
_LPS2X_TEMP_OUT_L = const(
0x2B | 0x80
) # | 0x80 to set auto increment on multi-byte read

_LPS25_CHIP_ID = 0xBD
_LPS25_DEFAULT_ADDRESS = 0x5D
_LPS25_CTRL_REG1 = const(0x20) # First control register. Includes BD & ODR
_LPS25_CTRL_REG2 = const(0x21) # Second control register. Includes SW Reset
# _LPS25_CTRL_REG3 = 0x22 # Third control register. Includes interrupt polarity
# _LPS25_CTRL_REG4 = 0x23 # Fourth control register. Includes DRDY INT control
# _LPS25_INTERRUPT_CFG = 0x24 # Interrupt control register
# _LPS25_THS_P_L_REG = 0xB0 # Pressure threshold value for int


# _LPS22_THS_P_L_REG = 0x0C # Pressure threshold value for int
_LPS22_CTRL_REG1 = 0x10 # First control register. Includes BD & ODR
_LPS22_CTRL_REG2 = 0x11 # Second control register. Includes SW Reset
# _LPS22_CTRL_REG3 = 0x12 # Third control register. Includes interrupt polarity

_LPS2X_DEFAULT_ADDRESS = 0x5D
_LPS25HB_CHIP_ID = 0xBD
_LPS22HB_CHIP_ID = 0xB1 # LPS22 default device id from WHOAMI


class CV:
Expand All @@ -84,64 +105,65 @@ def is_valid(cls, value):
class Rate(CV):
"""Options for ``data_rate``

+-----------------------+------------------------------------------------------------------+
| Rate | Description |
+-----------------------+------------------------------------------------------------------+
| ``Rate.ONE_SHOT`` | Setting `data_rate` to ``Rate.ONE_SHOT`` takes a single pressure |
| | and temperature measurement |
+-----------------------+------------------------------------------------------------------+
| ``Rate.RATE_1_HZ`` | 1 Hz |
+-----------------------+------------------------------------------------------------------+
| ``Rate.RATE_7_HZ`` | 7 Hz |
+-----------------------+------------------------------------------------------------------+
| ``Rate.RATE_12_5_HZ`` | 12.5 Hz |
+-----------------------+------------------------------------------------------------------+
| ``Rate.RATE_25_HZ`` | 25 Hz |
+-----------------------+------------------------------------------------------------------+
+-----------------------------+------------------------------------------------+
| Rate | Description |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP25_SHUTDOWN`` | Setting `data_rate` to ``Rate.LSP25_SHUTDOWN`` |
| | stops measurements from being taken |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP25_RATE_1_HZ`` | 1 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP25_RATE_7_HZ`` | 7 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP25_RATE_12_5_HZ`` | 12.5 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP25_RATE_25_HZ`` | 25 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP22_SHUTDOWN`` | Setting `data_rate` to ``Rate.LSP22_SHUTDOWN`` |
| | stops measurements from being taken |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP22_RATE_1_HZ`` | 1 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP22_RATE_10_HZ`` | 10 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP22_RATE_25_HZ`` | 25 Hz |
+-----------------------------+------------------------------------------------+
| ``Rate.LSP22_RATE_50_HZ`` | 50 Hz |
+-----------------------------+------------------------------------------------+

"""

pass # pylint: disable=unnecessary-pass


Rate.add_values(
(
("RATE_ONE_SHOT", 0, 0, None),
("RATE_1_HZ", 1, 1, None),
("RATE_7_HZ", 2, 7, None),
("RATE_12_5_HZ", 3, 12.5, None),
("RATE_25_HZ", 4, 25, None),
)
)


class LPS2X: # pylint: disable=too-many-instance-attributes
"""Library for the ST LPS2x family of pressure sensors
"""Base class ST LPS2x family of pressure sensors

:param ~busio.I2C i2c_bus: The I2C bus the LPS25HB is connected to.
:param ~busio.I2C i2c_bus: The I2C bus the sensor is connected to.
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
``0x5c`` when the ``SDO`` pin is connected to Ground.

"""

_chip_id = ROUnaryStruct(_WHO_AM_I, "<B")
_reset = RWBit(_CTRL_REG2, 2)
enabled = RWBit(_CTRL_REG1, 7)
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
_data_rate = RWBits(3, _CTRL_REG1, 4)
_raw_temperature = ROUnaryStruct(_TEMP_OUT_L, "<h")
_raw_pressure = ROBits(24, _PRESS_OUT_XL, 0, 3)
_chip_id = ROUnaryStruct(_LPS2X_WHO_AM_I, "<B")
_raw_temperature = ROUnaryStruct(_LPS2X_TEMP_OUT_L, "<h")
_raw_pressure = ROBits(24, _LPS2X_PRESS_OUT_XL, 0, 3)

def __init__(self, i2c_bus, address=_LPS25_DEFAULT_ADDRESS):
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
if not self._chip_id in [_LPS25_CHIP_ID]:
if not self._chip_id in [chip_id]:
raise RuntimeError(
"Failed to find LPS25HB! Found chip ID 0x%x" % self._chip_id
"Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id
)

self.reset()
self.enabled = True
self.data_rate = Rate.RATE_25_HZ # pylint:disable=no-member
self.initialize()
sleep(0.010) # delay 10ms for first reading

def initialize(self): # pylint: disable=no-self-use
"""Configure the sensor with the default settings. For use after calling `reset()`"""
raise RuntimeError(
"LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead"
) # override in subclass

def reset(self):
"""Reset the sensor, restoring all configuration registers to their defaults"""
Expand All @@ -162,16 +184,16 @@ def pressure(self):
@property
def temperature(self):
"""The current temperature measurement in degrees C"""

raw_temperature = self._raw_temperature
return (raw_temperature / 480) + 42.5
return (
raw_temperature / self._temp_scaling # pylint:disable=no-member
) + self._temp_offset # pylint:disable=no-member

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

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

self._data_rate = value


class LPS25(LPS2X):
"""Library for the ST LPS25 pressure sensors

:param ~busio.I2C i2c_bus: The I2C bus the LPS25HB is connected to.
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
``0x5c`` when the ``SDO`` pin is connected to Ground.

"""

enabled = RWBit(_LPS25_CTRL_REG1, 7)
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
_reset = RWBit(_LPS25_CTRL_REG2, 2)
_data_rate = RWBits(3, _LPS25_CTRL_REG1, 4)

def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):

Rate.add_values(
(
("LPS25_RATE_ONE_SHOT", 0, 0, None),
("LPS25_RATE_1_HZ", 1, 1, None),
("LPS25_RATE_7_HZ", 2, 7, None),
("LPS25_RATE_12_5_HZ", 3, 12.5, None),
("LPS25_RATE_25_HZ", 4, 25, None),
)
)
super().__init__(i2c_bus, address, chip_id=_LPS25HB_CHIP_ID)

self._temp_scaling = 480
self._temp_offset = 42.5
# self._inc_spi_flag = 0x40

def initialize(self):
"""Configure the sensor with the default settings. For use after calling `reset()`"""
self.enabled = True
self.data_rate = Rate.LPS25_RATE_25_HZ # pylint:disable=no-member

# void configureInterrupt(bool activelow, bool opendrain,
# bool pres_high = false, bool pres_low = false);


class LPS22(LPS2X):
"""Library for the ST LPS22 pressure sensors

:param ~busio.I2C i2c_bus: The I2C bus the LPS22HB is connected to.
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
``0x5c`` when the ``SDO`` pin is connected to Ground.

"""

_reset = RWBit(_LPS22_CTRL_REG2, 2)
_data_rate = RWBits(3, _LPS22_CTRL_REG1, 4)

def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
# Only adding Class-appropriate rates
Rate.add_values(
(
("LPS22_RATE_ONE_SHOT", 0, 0, None),
("LPS22_RATE_1_HZ", 1, 1, None),
("LPS22_RATE_10_HZ", 2, 10, None),
("LPS22_RATE_25_HZ", 3, 25, None),
("LPS22_RATE_50_HZ", 4, 50, None),
("LPS22_RATE_75_HZ", 5, 75, None),
)
)

super().__init__(i2c_bus, address, chip_id=_LPS22HB_CHIP_ID)
self._temp_scaling = 100
self._temp_offset = 0

def initialize(self):
"""Configure the sensor with the default settings. For use after calling `reset()`"""
self.data_rate = Rate.LPS22_RATE_75_HZ # pylint:disable=no-member

# void configureInterrupt(bool activelow, bool opendrain, bool data_ready,
# bool pres_high = false, bool pres_low = false,
# bool fifo_full = false, bool fifo_watermark = false,
# bool fifo_overflow = false);
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Table of Contents
.. toctree::
:caption: Related Products

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

.. toctree::
:caption: Other Links
Expand Down
4 changes: 3 additions & 1 deletion examples/lps2x_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import adafruit_lps2x

i2c = busio.I2C(board.SCL, board.SDA)
lps = adafruit_lps2x.LPS2X(i2c)
# uncomment and comment out the line after to use with the LPS22
# lps = adafruit_lps2x.LPS22(i2c)
lps = adafruit_lps2x.LPS25(i2c)
while True:
print("Pressure: %.2f hPa" % lps.pressure)
print("Temperature: %.2f C" % lps.temperature)
Expand Down