44
44
"""
45
45
__version__ = "0.0.0-auto.0"
46
46
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LPS2X.git"
47
+ from time import sleep
47
48
from micropython import const
48
49
import adafruit_bus_device .i2c_device as i2cdevice
49
50
from adafruit_register .i2c_struct import ROUnaryStruct
50
51
from adafruit_register .i2c_bits import RWBits , ROBits
51
52
from adafruit_register .i2c_bit import RWBit
52
53
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
-
62
54
# _LPS2X_I2CADDR_DEFAULT = 0x5D # LPS2X default i2c address
63
55
# _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
74
68
# _LPS25_CTRL_REG3 = 0x22 # Third control register. Includes interrupt polarity
75
69
# _LPS25_CTRL_REG4 = 0x23 # Fourth control register. Includes DRDY INT control
76
70
# _LPS25_INTERRUPT_CFG = 0x24 # Interrupt control register
77
71
# _LPS25_THS_P_L_REG = 0xB0 # Pressure threshold value for int
78
72
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
81
82
82
83
83
84
class CV :
@@ -124,29 +125,6 @@ class Rate(CV):
124
125
pass # pylint: disable=unnecessary-pass
125
126
126
127
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
-
150
128
class LPS2X : # pylint: disable=too-many-instance-attributes
151
129
"""Base class ST LPS2x family of pressure sensors
152
130
@@ -156,11 +134,9 @@ class LPS2X: # pylint: disable=too-many-instance-attributes
156
134
157
135
"""
158
136
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 )
164
140
165
141
def __init__ (self , i2c_bus , address = _LPS2X_DEFAULT_ADDRESS , chip_id = None ):
166
142
self .i2c_device = i2cdevice .I2CDevice (i2c_bus , address )
@@ -171,6 +147,7 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS, chip_id=None):
171
147
172
148
self .reset ()
173
149
self .initialize ()
150
+ sleep (0.010 ) # delay 10ms for first reading
174
151
175
152
def initialize (self ): # pylint: disable=no-self-use
176
153
"""Configure the sensor with the default settings. For use after calling `reset()`"""
@@ -198,7 +175,7 @@ def pressure(self):
198
175
def temperature (self ):
199
176
"""The current temperature measurement in degrees C"""
200
177
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
202
179
203
180
@property
204
181
def data_rate (self ):
@@ -216,6 +193,9 @@ def data_rate(self, value):
216
193
217
194
self ._data_rate = value
218
195
196
+ # void setPresThreshold(uint16_t hPa_delta);
197
+ # bool getEvent(sensors_event_t *pressure, sensors_event_t *temp);
198
+
219
199
220
200
class LPS25 (LPS2X ):
221
201
"""Library for the ST LPS25 pressure sensors
@@ -226,14 +206,15 @@ class LPS25(LPS2X):
226
206
227
207
"""
228
208
229
- enabled = RWBit (_CTRL_REG1 , 7 )
209
+ enabled = RWBit (_LPS25_CTRL_REG1 , 7 )
230
210
"""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 )
231
213
232
214
def __init__ (self , i2c_bus , address = _LPS2X_DEFAULT_ADDRESS ):
233
215
234
216
Rate .add_values (
235
217
(
236
- # leave these for backwards compatibility? nah
237
218
("LPS25_RATE_ONE_SHOT" , 0 , 0 , None ),
238
219
("LPS25_RATE_1_HZ" , 1 , 1 , None ),
239
220
("LPS25_RATE_7_HZ" , 2 , 7 , None ),
@@ -243,13 +224,18 @@ def __init__(self, i2c_bus, address=_LPS2X_DEFAULT_ADDRESS):
243
224
)
244
225
245
226
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
247
230
248
231
def initialize (self ):
249
232
"""Configure the sensor with the default settings. For use after calling `reset()`"""
250
233
self .enabled = True
251
234
self .data_rate = Rate .LPS25_RATE_25_HZ # pylint:disable=no-member
252
235
236
+ # void configureInterrupt(bool activelow, bool opendrain,
237
+ # bool pres_high = false, bool pres_low = false);
238
+
253
239
254
240
class LPS22 (LPS2X ):
255
241
"""Library for the ST LPS22 pressure sensors
@@ -260,16 +246,15 @@ class LPS22(LPS2X):
260
246
261
247
"""
262
248
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 )
265
251
266
252
def __init__ (
267
253
self , i2c_bus , address = _LPS2X_DEFAULT_ADDRESS , chip_id = _LPS22HB_CHIP_ID
268
254
):
269
-
255
+ # Only adding Class-appropriate rates
270
256
Rate .add_values (
271
257
(
272
- # leave these for backwards compatibility? nah
273
258
("LPS22_RATE_ONE_SHOT" , 0 , 0 , None ),
274
259
("LPS22_RATE_1_HZ" , 1 , 1 , None ),
275
260
("LPS22_RATE_10_HZ" , 2 , 10 , None ),
@@ -279,106 +264,15 @@ def __init__(
279
264
)
280
265
)
281
266
282
- # /**
283
- # * @brief
284
- # *
285
- # * Allowed values for `setDataRate`.
286
- # */
287
- # typedef enum {
288
- # LPS22_RATE_ONE_SHOT,
289
-
290
- # } lps22_rate_t;
291
-
292
267
super ().__init__ (i2c_bus , address )
293
- self .initialize ()
268
+ self ._temp_scaling = 100
294
269
295
270
def initialize (self ):
296
271
"""Configure the sensor with the default settings. For use after calling `reset()`"""
297
- self .enabled = True
272
+ # self.enabled = True
298
273
self .data_rate = Rate .LPS22_RATE_75_HZ # pylint:disable=no-member
299
274
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