Skip to content

Commit a54e2d9

Browse files
committed
moved lps25 into subclass
1 parent a533f8a commit a54e2d9

File tree

2 files changed

+187
-17
lines changed

2 files changed

+187
-17
lines changed

adafruit_lps2x.py

Lines changed: 186 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@
5959
_LPS25_CHIP_ID = 0xBD
6060
_LPS25_DEFAULT_ADDRESS = 0x5D
6161

62+
# define LPS2X_I2CADDR_DEFAULT 0x5D ///< LPS2X default i2c address
63+
# define LPS2X_WHOAMI 0x0F ///< Chip ID register
64+
65+
# define LPS22HB_CHIP_ID 0xB1 ///< LPS22 default device id from WHOAMI
66+
# define LPS22_THS_P_L_REG 0x0C ///< Pressure threshold value for int
67+
# define LPS22_CTRL_REG1 0x10 ///< First control register. Includes BD & ODR
68+
# define LPS22_CTRL_REG2 0x11 ///< Second control register. Includes SW Reset
69+
# define LPS22_CTRL_REG3 0x12 ///< Third control register. Includes interrupt polarity
70+
71+
# define LPS25HB_CHIP_ID 0xBD ///< LPS25HB default device id from WHOAMI
72+
# define LPS25_CTRL_REG1 0x20 ///< First control register. Includes BD & ODR
73+
# define LPS25_CTRL_REG2 0x21 ///< Second control register. Includes SW Reset
74+
# define LPS25_CTRL_REG3 0x22 ///< Third control register. Includes interrupt polarity
75+
# define LPS25_CTRL_REG4 0x23 ///< Fourth control register. Includes DRDY INT control
76+
# define LPS25_INTERRUPT_CFG 0x24 ///< Interrupt control register
77+
# define LPS25_THS_P_L_REG 0xB0 ///< Pressure threshold value for int
78+
79+
# define LPS2X_PRESS_OUT_XL(0x28 | 0x80) ///< | 0x80 to set auto increment on multi-byte read
80+
# define LPS2X_TEMP_OUT_L (0x2B | 0x80) ///< | 0x80 to set auto increment on
81+
6282

6383
class CV:
6484
"""struct helper"""
@@ -104,30 +124,40 @@ class Rate(CV):
104124
pass # pylint: disable=unnecessary-pass
105125

106126

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-
)
127+
# typedef enum {
128+
# LPS25_RATE_ONE_SHOT,
129+
# LPS25_RATE_1_HZ,
130+
# LPS25_RATE_7_HZ,
131+
# LPS25_RATE_12_5_HZ,
132+
# LPS25_RATE_25_HZ,
133+
# } lps25_rate_t;
134+
135+
# /**
136+
# * @brief
137+
# *
138+
# * Allowed values for `setDataRate`.
139+
# */
140+
# typedef enum {
141+
# LPS22_RATE_ONE_SHOT,
142+
# LPS22_RATE_1_HZ,
143+
# LPS22_RATE_10_HZ,
144+
# LPS22_RATE_25_HZ,
145+
# LPS22_RATE_50_HZ,
146+
# LPS22_RATE_75_HZ,
147+
# } lps22_rate_t;
116148

117149

118150
class LPS2X: # pylint: disable=too-many-instance-attributes
119-
"""Library for the ST LPS2x family of pressure sensors
151+
"""Base class ST LPS2x family of pressure sensors
120152
121-
:param ~busio.I2C i2c_bus: The I2C bus the LPS25HB is connected to.
153+
:param ~busio.I2C i2c_bus: The I2C bus the sensor is connected to.
122154
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
123155
``0x5c`` when the ``SDO`` pin is connected to Ground.
124156
125157
"""
126158

127159
_chip_id = ROUnaryStruct(_WHO_AM_I, "<B")
128160
_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"""
131161
_data_rate = RWBits(3, _CTRL_REG1, 4)
132162
_raw_temperature = ROUnaryStruct(_TEMP_OUT_L, "<h")
133163
_raw_pressure = ROBits(24, _PRESS_OUT_XL, 0, 3)
@@ -136,12 +166,17 @@ def __init__(self, i2c_bus, address=_LPS25_DEFAULT_ADDRESS):
136166
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
137167
if not self._chip_id in [_LPS25_CHIP_ID]:
138168
raise RuntimeError(
139-
"Failed to find LPS25HB! Found chip ID 0x%x" % self._chip_id
169+
"Failed to find LPS2X! Found chip ID 0x%x" % self._chip_id
140170
)
141171

142172
self.reset()
143-
self.enabled = True
144-
self.data_rate = Rate.RATE_25_HZ # pylint:disable=no-member
173+
self.initialize()
174+
175+
def initialize(self): # pylint: disable=no-self-use
176+
"""Configure the sensor with the default settings. For use after calling `reset()`"""
177+
raise RuntimeError(
178+
"LPS2X Base class cannot be instantiated directly. Use LPS22 or LPS25 instead"
179+
) # override in subclass
145180

146181
def reset(self):
147182
"""Reset the sensor, restoring all configuration registers to their defaults"""
@@ -180,3 +215,138 @@ def data_rate(self, value):
180215
raise AttributeError("data_rate must be a `Rate`")
181216

182217
self._data_rate = value
218+
219+
220+
# /**
221+
# * @brief
222+
# *
223+
# * Allowed values for `setDataRate`.
224+
# */
225+
# typedef enum {
226+
# LPS22_RATE_ONE_SHOT,
227+
# LPS22_RATE_1_HZ,
228+
# LPS22_RATE_10_HZ,
229+
# LPS22_RATE_25_HZ,
230+
# LPS22_RATE_50_HZ,
231+
# LPS22_RATE_75_HZ,
232+
# } lps22_rate_t;
233+
234+
235+
class LPS25(LPS2X):
236+
"""Library for the ST LPS25 pressure sensors
237+
238+
:param ~busio.I2C i2c_bus: The I2C bus the LPS25HB is connected to.
239+
:param address: The I2C device address for the sensor. Default is ``0x5d`` but will accept
240+
``0x5c`` when the ``SDO`` pin is connected to Ground.
241+
242+
"""
243+
244+
enabled = RWBit(_CTRL_REG1, 7)
245+
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
246+
247+
def __init__(self, i2c_bus, address=_LPS25_DEFAULT_ADDRESS):
248+
249+
Rate.add_values(
250+
(
251+
# leave these for backwards compatibility? nah
252+
("LPS25_RATE_ONE_SHOT", 0, 0, None),
253+
("LPS25_RATE_1_HZ", 1, 1, None),
254+
("LPS25_RATE_7_HZ", 2, 7, None),
255+
("LPS25_RATE_12_5_HZ", 3, 12.5, None),
256+
("LPS25_RATE_25_HZ", 4, 25, None),
257+
)
258+
)
259+
260+
super().__init__(i2c_bus, address)
261+
self.initialize()
262+
263+
def initialize(self):
264+
"""Configure the sensor with the default settings. For use after calling `reset()`"""
265+
self.enabled = True
266+
self.data_rate = Rate.LPS25_RATE_25_HZ # pylint:disable=no-member
267+
268+
269+
# """
270+
# class Adafruit_LPS2X {
271+
# public:
272+
# Adafruit_LPS2X();
273+
# ~Adafruit_LPS2X();
274+
275+
# bool begin_I2C(uint8_t i2c_addr = LPS2X_I2CADDR_DEFAULT,
276+
# TwoWire *wire = &Wire, int32_t sensor_id = 0);
277+
278+
# bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI,
279+
# int32_t sensor_id = 0);
280+
# bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
281+
# int8_t mosi_pin, int32_t sensor_id = 0);
282+
283+
# void setPresThreshold(uint16_t hPa_delta);
284+
# bool getEvent(sensors_event_t *pressure, sensors_event_t *temp);
285+
# void reset(void);
286+
287+
# Adafruit_Sensor *getTemperatureSensor(void);
288+
# Adafruit_Sensor *getPressureSensor(void);
289+
290+
# protected:
291+
# /**! @brief The subclasses' hardware initialization function
292+
# @param sensor_id The unique sensor id we want to assign it
293+
# @returns True on success, false if something went wrong! **/
294+
# virtual bool _init(int32_t sensor_id) = 0;
295+
296+
# void _read(void);
297+
298+
# float _temp, ///< Last reading's temperature (C)
299+
# _pressure; ///< Last reading's pressure (hPa)
300+
301+
# uint16_t _sensorid_pressure, ///< ID number for pressure
302+
# _sensorid_temp; ///< ID number for temperature
303+
# float temp_scaling = 1; ///< Different chips have different scalings
304+
# uint8_t inc_spi_flag =
305+
# 0; ///< If this chip has a bitflag for incrementing SPI registers
306+
307+
308+
# Adafruit_BusIO_Register *ctrl1_reg = NULL; ///< The first control register
309+
# Adafruit_BusIO_Register *ctrl2_reg = NULL; ///< The second control register
310+
# Adafruit_BusIO_Register *ctrl3_reg = NULL; ///< The third control register
311+
# Adafruit_BusIO_Register *threshp_reg = NULL; ///< Pressure threshold
312+
313+
# private:
314+
# friend class Adafruit_LPS2X_Temp; ///< Gives access to private members to
315+
# ///< Temp data object
316+
# friend class Adafruit_LPS2X_Pressure; ///< Gives access to private
317+
# ///< members to Pressure data
318+
# ///< object
319+
320+
321+
# };
322+
323+
# /** Specific subclass for LPS25 variant */
324+
# class Adafruit_LPS25 : public Adafruit_LPS2X {
325+
# public:
326+
# lps25_rate_t getDataRate(void);
327+
# void setDataRate(lps25_rate_t data_rate);
328+
# void powerDown(bool power_down);
329+
# void configureInterrupt(bool activelow, bool opendrain,
330+
# bool pres_high = false, bool pres_low = false);
331+
332+
# protected:
333+
# bool _init(int32_t sensor_id);
334+
# };
335+
336+
# /** Specific subclass for LPS22 variant */
337+
# class Adafruit_LPS22 : public Adafruit_LPS2X {
338+
# public:
339+
# lps22_rate_t getDataRate(void);
340+
# void setDataRate(lps22_rate_t data_rate);
341+
# void configureInterrupt(bool activelow, bool opendrain, bool data_ready,
342+
# bool pres_high = false, bool pres_low = false,
343+
# bool fifo_full = false, bool fifo_watermark = false,
344+
# bool fifo_overflow = false);
345+
346+
# protected:
347+
# bool _init(int32_t sensor_id);
348+
# };
349+
350+
# #endif
351+
352+
# """

examples/lps2x_simpletest.py

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

66
i2c = busio.I2C(board.SCL, board.SDA)
7-
lps = adafruit_lps2x.LPS2X(i2c)
7+
lps = adafruit_lps2x.LPS25(i2c)
88
while True:
99
print("Pressure: %.2f hPa" % lps.pressure)
1010
print("Temperature: %.2f C" % lps.temperature)

0 commit comments

Comments
 (0)