Skip to content

Commit 1ac62fd

Browse files
committed
ready for testing
1 parent e0b3f13 commit 1ac62fd

File tree

1 file changed

+48
-154
lines changed

1 file changed

+48
-154
lines changed

adafruit_lps2x.py

Lines changed: 48 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +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
58-
59-
_LPS25HB_CHIP_ID = 0xBD
60-
_LPS2X_DEFAULT_ADDRESS = 0x5D
61-
6254
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
6355
# _LPS2X_WHOAMI = 0x0F # Chip ID register
64-
65-
_LPS22HB_CHIP_ID = 0xB1 # LPS22 default device id from WHOAMI
66-
# _LPS22_THS_P_L_REG = 0x0C # Pressure threshold value for int
67-
# _LPS22_CTRL_REG1 = 0x10 # First control register. Includes BD & ODR
68-
# _LPS22_CTRL_REG2 = 0x11 # Second control register. Includes SW Reset
69-
# _LPS22_CTRL_REG3 = 0x12 # Third control register. Includes interrupt polarity
70-
71-
# _LPS25HB_CHIP_ID = 0xBD # LPS25HB default device id from WHOAMI
72-
# _LPS25_CTRL_REG1 = 0x20 # First control register. Includes BD & ODR
73-
# _LPS25_CTRL_REG2 = 0x21 # Second control register. Includes SW Reset
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
65+
66+
_LPS25_CTRL_REG1 = const(0x20) # First control register. Includes BD & ODR
67+
_LPS25_CTRL_REG2 = const(0x21) # Second control register. Includes SW Reset
7468
# _LPS25_CTRL_REG3 = 0x22 # Third control register. Includes interrupt polarity
7569
# _LPS25_CTRL_REG4 = 0x23 # Fourth control register. Includes DRDY INT control
7670
# _LPS25_INTERRUPT_CFG = 0x24 # Interrupt control register
7771
# _LPS25_THS_P_L_REG = 0xB0 # Pressure threshold value for int
7872

79-
# _LPS2X_PRESS_OUT_XL =(# | 0x80) ///< | 0x80 to set auto increment on multi-byte read
80-
# _LPS2X_TEMP_OUT_L = (0x2B # 0x80) ///< | 0x80 to set auto increment on
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
8182

8283

8384
class CV:
@@ -124,29 +125,6 @@ class Rate(CV):
124125
pass # pylint: disable=unnecessary-pass
125126

126127

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;
148-
149-
150128
class LPS2X: # pylint: disable=too-many-instance-attributes
151129
"""Base class ST LPS2x family of pressure sensors
152130
@@ -156,11 +134,9 @@ class LPS2X: # pylint: disable=too-many-instance-attributes
156134
157135
"""
158136

159-
_chip_id = ROUnaryStruct(_WHO_AM_I, "<B")
160-
_reset = RWBit(_CTRL_REG2, 2)
161-
_data_rate = RWBits(3, _CTRL_REG1, 4)
162-
_raw_temperature = ROUnaryStruct(_TEMP_OUT_L, "<h")
163-
_raw_pressure = ROBits(24, _PRESS_OUT_XL, 0, 3)
137+
_chip_id = ROUnaryStruct(_LPS2X_WHO_AM_I, "<B")
138+
_raw_temperature = ROUnaryStruct(_LPS2X_TEMP_OUT_L, "<h")
139+
_raw_pressure = ROBits(24, _LPS2X_PRESS_OUT_XL, 0, 3)
164140

165141
def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
166142
self.i2c_device = i2cdevice.I2CDevice(i2c_bus, address)
@@ -171,6 +147,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
171147

172148
self.reset()
173149
self.initialize()
150+
sleep(0.010) # delay 10ms for first reading
174151

175152
def initialize(self): # pylint: disable=no-self-use
176153
"""Configure the sensor with the default settings. For use after calling `reset()`"""
@@ -198,7 +175,7 @@ def pressure(self):
198175
def temperature(self):
199176
"""The current temperature measurement in degrees C"""
200177
raw_temperature = self._raw_temperature
201-
return (raw_temperature / 480) + 42.5
178+
return (raw_temperature / self._temp_scaling) + 42.5 # pylint:disable=no-member
202179

203180
@property
204181
def data_rate(self):
@@ -216,6 +193,9 @@ def data_rate(self, value):
216193

217194
self._data_rate = value
218195

196+
# void setPresThreshold(uint16_t hPa_delta);
197+
# bool getEvent(sensors_event_t *pressure, sensors_event_t *temp);
198+
219199

220200
class LPS25(LPS2X):
221201
"""Library for the ST LPS25 pressure sensors
@@ -226,14 +206,15 @@ class LPS25(LPS2X):
226206
227207
"""
228208

229-
enabled = RWBit(_CTRL_REG1, 7)
209+
enabled = RWBit(_LPS25_CTRL_REG1, 7)
230210
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
211+
_reset = RWBit(_LPS25_CTRL_REG2, 2)
212+
_data_rate = RWBits(3, _LPS25_CTRL_REG1, 4)
231213

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

234216
Rate.add_values(
235217
(
236-
# leave these for backwards compatibility? nah
237218
("LPS25_RATE_ONE_SHOT", 0, 0, None),
238219
("LPS25_RATE_1_HZ", 1, 1, None),
239220
("LPS25_RATE_7_HZ", 2, 7, None),
@@ -243,13 +224,18 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
243224
)
244225

245226
super().__init__(i2c_bus, address, chip_id=_LPS25HB_CHIP_ID)
246-
self.initialize()
227+
228+
self._temp_scaling = 480
229+
# self._inc_spi_flag = 0x40
247230

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

236+
# void configureInterrupt(bool activelow, bool opendrain,
237+
# bool pres_high = false, bool pres_low = false);
238+
253239

254240
class LPS22(LPS2X):
255241
"""Library for the ST LPS22 pressure sensors
@@ -260,16 +246,15 @@ class LPS22(LPS2X):
260246
261247
"""
262248

263-
enabled = RWBit(_CTRL_REG1, 7)
264-
"""Controls the power down state of the sensor. Setting to `False` will shut the sensor down"""
249+
_reset = RWBit(_LPS22_CTRL_REG2, 2)
250+
_data_rate = RWBits(3, _LPS22_CTRL_REG1, 4)
265251

266252
def __init__(
267253
self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=_LPS22HB_CHIP_ID
268254
):
269-
255+
# Only adding Class-appropriate rates
270256
Rate.add_values(
271257
(
272-
# leave these for backwards compatibility? nah
273258
("LPS22_RATE_ONE_SHOT", 0, 0, None),
274259
("LPS22_RATE_1_HZ", 1, 1, None),
275260
("LPS22_RATE_10_HZ", 2, 10, None),
@@ -279,106 +264,15 @@ def __init__(
279264
)
280265
)
281266

282-
# /**
283-
# * @brief
284-
# *
285-
# * Allowed values for `setDataRate`.
286-
# */
287-
# typedef enum {
288-
# LPS22_RATE_ONE_SHOT,
289-
290-
# } lps22_rate_t;
291-
292267
super().__init__(i2c_bus, address)
293-
self.initialize()
268+
self._temp_scaling = 100
294269

295270
def initialize(self):
296271
"""Configure the sensor with the default settings. For use after calling `reset()`"""
297-
self.enabled = True
272+
# self.enabled = True
298273
self.data_rate = Rate.LPS22_RATE_75_HZ # pylint:disable=no-member
299274

300-
301-
# """
302-
# class Adafruit_LPS2X {
303-
# public:
304-
# Adafruit_LPS2X();
305-
# ~Adafruit_LPS2X();
306-
307-
# bool begin_I2C(uint8_t i2c_addr = LPS2X_I2CADDR_DEFAULT,
308-
# TwoWire *wire = &Wire, int32_t sensor_id = 0);
309-
310-
# bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI,
311-
# int32_t sensor_id = 0);
312-
# bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
313-
# int8_t mosi_pin, int32_t sensor_id = 0);
314-
315-
# void setPresThreshold(uint16_t hPa_delta);
316-
# bool getEvent(sensors_event_t *pressure, sensors_event_t *temp);
317-
# void reset(void);
318-
319-
# Adafruit_Sensor *getTemperatureSensor(void);
320-
# Adafruit_Sensor *getPressureSensor(void);
321-
322-
# protected:
323-
# /**! @brief The subclasses' hardware initialization function
324-
# @param sensor_id The unique sensor id we want to assign it
325-
# @returns True on success, false if something went wrong! **/
326-
# virtual bool _init(int32_t sensor_id) = 0;
327-
328-
# void _read(void);
329-
330-
# float _temp, ///< Last reading's temperature (C)
331-
# _pressure; ///< Last reading's pressure (hPa)
332-
333-
# uint16_t _sensorid_pressure, ///< ID number for pressure
334-
# _sensorid_temp; ///< ID number for temperature
335-
# float temp_scaling = 1; ///< Different chips have different scalings
336-
# uint8_t inc_spi_flag =
337-
# 0; ///< If this chip has a bitflag for incrementing SPI registers
338-
339-
340-
# Adafruit_BusIO_Register *ctrl1_reg = NULL; ///< The first control register
341-
# Adafruit_BusIO_Register *ctrl2_reg = NULL; ///< The second control register
342-
# Adafruit_BusIO_Register *ctrl3_reg = NULL; ///< The third control register
343-
# Adafruit_BusIO_Register *threshp_reg = NULL; ///< Pressure threshold
344-
345-
# private:
346-
# friend class Adafruit_LPS2X_Temp; ///< Gives access to private members to
347-
# ///< Temp data object
348-
# friend class Adafruit_LPS2X_Pressure; ///< Gives access to private
349-
# ///< members to Pressure data
350-
# ///< object
351-
352-
353-
# };
354-
355-
# /** Specific subclass for LPS25 variant */
356-
# class Adafruit_LPS25 : public Adafruit_LPS2X {
357-
# public:
358-
# lps25_rate_t getDataRate(void);
359-
# void setDataRate(lps25_rate_t data_rate);
360-
# void powerDown(bool power_down);
361-
# void configureInterrupt(bool activelow, bool opendrain,
362-
# bool pres_high = false, bool pres_low = false);
363-
364-
# protected:
365-
# bool _init(int32_t sensor_id);
366-
# };
367-
368-
# /** Specific subclass for LPS22 variant */
369-
# class Adafruit_LPS22 : public Adafruit_LPS2X {
370-
# public:
371-
# lps22_rate_t getDataRate(void);
372-
# void setDataRate(lps22_rate_t data_rate);
373-
# void configureInterrupt(bool activelow, bool opendrain, bool data_ready,
374-
# bool pres_high = false, bool pres_low = false,
375-
# bool fifo_full = false, bool fifo_watermark = false,
376-
# bool fifo_overflow = false);
377-
378-
# protected:
379-
# bool _init(int32_t sensor_id);
380-
# };
381-
382-
# #endif
383-
384-
# """
275+
# void configureInterrupt(bool activelow, bool opendrain, bool data_ready,
276+
# bool pres_high = false, bool pres_low = false,
277+
# bool fifo_full = false, bool fifo_watermark = false,
278+
# bool fifo_overflow = false);

0 commit comments

Comments
 (0)