From a54e2d9bb2fa98e20587ec68569e881acdfa17ab Mon Sep 17 00:00:00 2001 From: siddacious Date: Tue, 2 Jun 2020 21:38:23 -0700 Subject: [PATCH 1/5] moved lps25 into subclass --- adafruit_lps2x.py | 202 ++++++++++++++++++++++++++++++++--- examples/lps2x_simpletest.py | 2 +- 2 files changed, 187 insertions(+), 17 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index 5f98a95..d34165e 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -59,6 +59,26 @@ _LPS25_CHIP_ID = 0xBD _LPS25_DEFAULT_ADDRESS = 0x5D +# define LPS2X_I2CADDR_DEFAULT 0x5D ///< LPS2X default i2c address +# define LPS2X_WHOAMI 0x0F ///< Chip ID register + +# define LPS22HB_CHIP_ID 0xB1 ///< LPS22 default device id from WHOAMI +# define LPS22_THS_P_L_REG 0x0C ///< Pressure threshold value for int +# define LPS22_CTRL_REG1 0x10 ///< First control register. Includes BD & ODR +# define LPS22_CTRL_REG2 0x11 ///< Second control register. Includes SW Reset +# define LPS22_CTRL_REG3 0x12 ///< Third control register. Includes interrupt polarity + +# define LPS25HB_CHIP_ID 0xBD ///< LPS25HB default device id from WHOAMI +# define LPS25_CTRL_REG1 0x20 ///< First control register. Includes BD & ODR +# define LPS25_CTRL_REG2 0x21 ///< Second control register. Includes SW Reset +# define LPS25_CTRL_REG3 0x22 ///< Third control register. Includes interrupt polarity +# define LPS25_CTRL_REG4 0x23 ///< Fourth control register. Includes DRDY INT control +# define LPS25_INTERRUPT_CFG 0x24 ///< Interrupt control register +# define LPS25_THS_P_L_REG 0xB0 ///< Pressure threshold value for int + +# define LPS2X_PRESS_OUT_XL(0x28 | 0x80) ///< | 0x80 to set auto increment on multi-byte read +# define LPS2X_TEMP_OUT_L (0x2B | 0x80) ///< | 0x80 to set auto increment on + class CV: """struct helper""" @@ -104,21 +124,33 @@ class Rate(CV): 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), - ) -) +# typedef enum { +# LPS25_RATE_ONE_SHOT, +# LPS25_RATE_1_HZ, +# LPS25_RATE_7_HZ, +# LPS25_RATE_12_5_HZ, +# LPS25_RATE_25_HZ, +# } lps25_rate_t; + +# /** +# * @brief +# * +# * Allowed values for `setDataRate`. +# */ +# typedef enum { +# LPS22_RATE_ONE_SHOT, +# LPS22_RATE_1_HZ, +# LPS22_RATE_10_HZ, +# LPS22_RATE_25_HZ, +# LPS22_RATE_50_HZ, +# LPS22_RATE_75_HZ, +# } lps22_rate_t; 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. @@ -126,8 +158,6 @@ class LPS2X: # pylint: disable=too-many-instance-attributes _chip_id = ROUnaryStruct(_WHO_AM_I, " Date: Wed, 3 Jun 2020 11:02:11 -0700 Subject: [PATCH 2/5] added LPS22 --- adafruit_lps2x.py | 106 ++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index d34165e..e4530a4 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -56,28 +56,28 @@ _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 -_LPS25_CHIP_ID = 0xBD -_LPS25_DEFAULT_ADDRESS = 0x5D +_LPS25HB_CHIP_ID = 0xBD +_LPS2X_DEFAULT_ADDRESS = 0x5D -# define LPS2X_I2CADDR_DEFAULT 0x5D ///< LPS2X default i2c address -# define LPS2X_WHOAMI 0x0F ///< Chip ID register +# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address +# _LPS2X_WHOAMI = 0x0F # Chip ID register -# define LPS22HB_CHIP_ID 0xB1 ///< LPS22 default device id from WHOAMI -# define LPS22_THS_P_L_REG 0x0C ///< Pressure threshold value for int -# define LPS22_CTRL_REG1 0x10 ///< First control register. Includes BD & ODR -# define LPS22_CTRL_REG2 0x11 ///< Second control register. Includes SW Reset -# define LPS22_CTRL_REG3 0x12 ///< Third control register. Includes interrupt polarity +_LPS22HB_CHIP_ID = 0xB1 # LPS22 default device id from WHOAMI +# _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 -# define LPS25HB_CHIP_ID 0xBD ///< LPS25HB default device id from WHOAMI -# define LPS25_CTRL_REG1 0x20 ///< First control register. Includes BD & ODR -# define LPS25_CTRL_REG2 0x21 ///< Second control register. Includes SW Reset -# define LPS25_CTRL_REG3 0x22 ///< Third control register. Includes interrupt polarity -# define LPS25_CTRL_REG4 0x23 ///< Fourth control register. Includes DRDY INT control -# define LPS25_INTERRUPT_CFG 0x24 ///< Interrupt control register -# define LPS25_THS_P_L_REG 0xB0 ///< Pressure threshold value for int +# _LPS25HB_CHIP_ID = 0xBD # LPS25HB default device id from WHOAMI +# _LPS25_CTRL_REG1 = 0x20 # First control register. Includes BD & ODR +# _LPS25_CTRL_REG2 = 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 -# define LPS2X_PRESS_OUT_XL(0x28 | 0x80) ///< | 0x80 to set auto increment on multi-byte read -# define LPS2X_TEMP_OUT_L (0x2B | 0x80) ///< | 0x80 to set auto increment on +# _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 class CV: @@ -162,9 +162,9 @@ class LPS2X: # pylint: disable=too-many-instance-attributes _raw_temperature = ROUnaryStruct(_TEMP_OUT_L, " Date: Wed, 3 Jun 2020 11:34:49 -0700 Subject: [PATCH 3/5] ready for testing --- adafruit_lps2x.py | 202 +++++++++++----------------------------------- 1 file changed, 48 insertions(+), 154 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index e4530a4..2d6f3b3 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -44,40 +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 - -_LPS25HB_CHIP_ID = 0xBD -_LPS2X_DEFAULT_ADDRESS = 0x5D - # _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address # _LPS2X_WHOAMI = 0x0F # Chip ID register - -_LPS22HB_CHIP_ID = 0xB1 # LPS22 default device id from WHOAMI -# _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 - -# _LPS25HB_CHIP_ID = 0xBD # LPS25HB default device id from WHOAMI -# _LPS25_CTRL_REG1 = 0x20 # First control register. Includes BD & ODR -# _LPS25_CTRL_REG2 = 0x21 # Second control register. Includes SW Reset +# _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_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 -# _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 + +# _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: @@ -124,29 +125,6 @@ class Rate(CV): pass # pylint: disable=unnecessary-pass -# typedef enum { -# LPS25_RATE_ONE_SHOT, -# LPS25_RATE_1_HZ, -# LPS25_RATE_7_HZ, -# LPS25_RATE_12_5_HZ, -# LPS25_RATE_25_HZ, -# } lps25_rate_t; - -# /** -# * @brief -# * -# * Allowed values for `setDataRate`. -# */ -# typedef enum { -# LPS22_RATE_ONE_SHOT, -# LPS22_RATE_1_HZ, -# LPS22_RATE_10_HZ, -# LPS22_RATE_25_HZ, -# LPS22_RATE_50_HZ, -# LPS22_RATE_75_HZ, -# } lps22_rate_t; - - class LPS2X: # pylint: disable=too-many-instance-attributes """Base class ST LPS2x family of pressure sensors @@ -156,11 +134,9 @@ class LPS2X: # pylint: disable=too-many-instance-attributes """ - _chip_id = ROUnaryStruct(_WHO_AM_I, " Date: Wed, 3 Jun 2020 15:20:13 -0700 Subject: [PATCH 4/5] fixed temp scaling and updated example --- adafruit_lps2x.py | 16 ++++++++-------- examples/lps2x_simpletest.py | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index 2d6f3b3..013df1f 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -144,7 +144,6 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None): raise RuntimeError( "Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id ) - self.reset() self.initialize() sleep(0.010) # delay 10ms for first reading @@ -174,8 +173,11 @@ def pressure(self): @property def temperature(self): """The current temperature measurement in degrees C""" + raw_temperature = self._raw_temperature - return (raw_temperature / self._temp_scaling) + 42.5 # pylint:disable=no-member + return ( + raw_temperature / self._temp_scaling # pylint:disable=no-member + ) + self._temp_offset # pylint:disable=no-member @property def data_rate(self): @@ -222,10 +224,10 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS): ("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): @@ -249,9 +251,7 @@ class LPS22(LPS2X): _reset = RWBit(_LPS22_CTRL_REG2, 2) _data_rate = RWBits(3, _LPS22_CTRL_REG1, 4) - def __init__( - self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=_LPS22HB_CHIP_ID - ): + def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS): # Only adding Class-appropriate rates Rate.add_values( ( @@ -264,12 +264,12 @@ def __init__( ) ) - super().__init__(i2c_bus, address) + 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.enabled = True self.data_rate = Rate.LPS22_RATE_75_HZ # pylint:disable=no-member # void configureInterrupt(bool activelow, bool opendrain, bool data_ready, diff --git a/examples/lps2x_simpletest.py b/examples/lps2x_simpletest.py index 30ae864..a270e38 100644 --- a/examples/lps2x_simpletest.py +++ b/examples/lps2x_simpletest.py @@ -4,6 +4,8 @@ import adafruit_lps2x i2c = busio.I2C(board.SCL, board.SDA) +# 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) From 419dd74fb673a96d8a5b402230383656e9405d76 Mon Sep 17 00:00:00 2001 From: siddacious Date: Wed, 3 Jun 2020 15:49:48 -0700 Subject: [PATCH 5/5] updating docs --- README.rst | 7 ++++--- adafruit_lps2x.py | 49 ++++++++++++++++++++++++++--------------------- docs/index.rst | 2 +- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/README.rst b/README.rst index 6375023..0ea7f42 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/adafruit_lps2x.py b/adafruit_lps2x.py index 013df1f..1b49adb 100644 --- a/adafruit_lps2x.py +++ b/adafruit_lps2x.py @@ -33,7 +33,7 @@ **Hardware:** -* `LPS25HB Breakout `_ +* LPS25HB Breakout https://www.adafruit.com/products/4530 **Software and Dependencies:** * Adafruit CircuitPython firmware for the supported boards: @@ -105,20 +105,31 @@ 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 | + +-----------------------------+------------------------------------------------+ """ @@ -182,10 +193,7 @@ def temperature(self): @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 @@ -195,9 +203,6 @@ def data_rate(self, value): self._data_rate = value - # void setPresThreshold(uint16_t hPa_delta); - # bool getEvent(sensors_event_t *pressure, sensors_event_t *temp); - class LPS25(LPS2X): """Library for the ST LPS25 pressure sensors diff --git a/docs/index.rst b/docs/index.rst index ea983c8..14521ae 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -27,7 +27,7 @@ Table of Contents .. toctree:: :caption: Related Products - * `LPS25HW Breakout `_ + * LPS25HW Breakout .. toctree:: :caption: Other Links