3
3
# SPDX-License-Identifier: MIT
4
4
5
5
"""
6
- `adafruit_dps310`
6
+ `adafruit_dps310.advanced `
7
7
================================================================================
8
8
9
- Library for the DPS310 Precision Barometric Pressure Sensor
9
+ Library for the DPS310 Precision Barometric Pressure Sensor. This is the advanced
10
+ version. Includes some features not present in `adafruit_dps310.basic`
10
11
11
- * Author(s): Bryan Siepert
12
+ * Author(s): Bryan Siepert, Jose David M.
12
13
13
14
Implementation Notes
14
15
--------------------
34
35
__version__ = "0.0.0-auto.0"
35
36
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DPS310.git"
36
37
37
- # Common imports; remove if unused or pylint will complain
38
- import math
39
38
from time import sleep
40
- from adafruit_bus_device import i2c_device
41
- from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct
42
- from adafruit_register .i2c_bit import RWBit , ROBit
43
- from adafruit_register .i2c_bits import RWBits , ROBits
44
-
45
- _DPS310_DEFAULT_ADDRESS = 0x77 # DPS310 default i2c address
46
- _DPS310_DEVICE_ID = 0x10 # DPS310 device identifier
47
-
48
- _DPS310_PRSB2 = 0x00 # Highest byte of pressure data
49
- _DPS310_TMPB2 = 0x03 # Highest byte of temperature data
50
- _DPS310_PRSCFG = 0x06 # Pressure configuration
51
- _DPS310_TMPCFG = 0x07 # Temperature configuration
52
- _DPS310_MEASCFG = 0x08 # Sensor configuration
53
- _DPS310_CFGREG = 0x09 # Interrupt/FIFO configuration
54
- _DPS310_RESET = 0x0C # Soft reset
55
- _DPS310_PRODREVID = 0x0D # Register that contains the part ID
56
- _DPS310_TMPCOEFSRCE = 0x28 # Temperature calibration src
39
+ from micropython import const
40
+ from adafruit_register .i2c_bits import RWBits
41
+ from adafruit_dps310 .basic import DPS310
57
42
58
43
# pylint: disable=no-member,unnecessary-pass
59
44
@@ -159,29 +144,45 @@ class SampleCount(CV):
159
144
)
160
145
)
161
146
# pylint: enable=unnecessary-pass
162
- class DPS310 :
147
+
148
+ _DPS310_DEFAULT_ADDRESS = const (0x77 ) # DPS310 default i2c address
149
+ # _DPS310_DEVICE_ID = const(0x10) # DPS310 device identifier
150
+
151
+ # _DPS310_PRSB2 = const(0x00) # Highest byte of pressure data
152
+ # _DPS310_TMPB2 = const(0x03) # Highest byte of temperature data
153
+ _DPS310_PRSCFG = const (0x06 ) # Pressure configuration
154
+ _DPS310_TMPCFG = const (0x07 ) # Temperature configuration
155
+ # _DPS310_MEASCFG = const(0x08) # Sensor configuration
156
+ # _DPS310_CFGREG = const(0x09) # Interrupt/FIFO configuration
157
+ # _DPS310_RESET = const(0x0C) # Soft reset
158
+ # _DPS310_PRODREVID = const(0x0D) # Register that contains the part ID
159
+ # _DPS310_TMPCOEFSRCE = const(0x28) # Temperature calibration src
160
+
161
+
162
+ class DPS310_Advanced (DPS310 ):
163
163
# pylint: disable=too-many-instance-attributes
164
164
"""Library for the DPS310 Precision Barometric Pressure Sensor.
165
+ This class contains some of other configurable features
165
166
166
167
:param ~busio.I2C i2c_bus: The I2C bus the DPS310 is connected to.
167
168
:param int address: The I2C device address. Defaults to :const:`0x77`
168
169
169
170
**Quickstart: Importing and using the DPS310**
170
171
171
- Here is an example of using the :class:`DPS310 ` class.
172
+ Here is an example of using the :class:`DPS310_Advanced ` class.
172
173
First you will need to import the libraries to use the sensor
173
174
174
175
.. code-block:: python
175
176
176
177
import board
177
- import adafruit_dps310
178
+ from adafruit_dps310.dps310_advanced import DPS310_Advanced as DPS310
178
179
179
180
Once this is done you can define your `board.I2C` object and define your sensor object
180
181
181
182
.. code-block:: python
182
183
183
184
i2c = board.I2C() # uses board.SCL and board.SDA
184
- dps310 = adafruit_dps310. DPS310(i2c)
185
+ dps310 = DPS310(i2c)
185
186
186
187
Now you have access to the :attr:`temperature` and :attr:`pressure` attributes.
187
188
@@ -192,66 +193,11 @@ class DPS310:
192
193
193
194
"""
194
195
# Register definitions
195
- _device_id = ROUnaryStruct (_DPS310_PRODREVID , ">B" )
196
- _reset_register = UnaryStruct (_DPS310_RESET , ">B" )
197
- _mode_bits = RWBits (3 , _DPS310_MEASCFG , 0 )
198
-
199
196
_pressure_ratebits = RWBits (3 , _DPS310_PRSCFG , 4 )
200
- _pressure_osbits = RWBits (4 , _DPS310_PRSCFG , 0 )
201
-
202
197
_temp_ratebits = RWBits (3 , _DPS310_TMPCFG , 4 )
203
- _temp_osbits = RWBits (4 , _DPS310_TMPCFG , 0 )
204
-
205
- _temp_measurement_src_bit = RWBit (_DPS310_TMPCFG , 7 )
206
-
207
- _pressure_shiftbit = RWBit (_DPS310_CFGREG , 2 )
208
- _temp_shiftbit = RWBit (_DPS310_CFGREG , 3 )
209
-
210
- _coefficients_ready = RWBit (_DPS310_MEASCFG , 7 )
211
- _sensor_ready = RWBit (_DPS310_MEASCFG , 6 )
212
- _temp_ready = RWBit (_DPS310_MEASCFG , 5 )
213
- _pressure_ready = RWBit (_DPS310_MEASCFG , 4 )
214
-
215
- _raw_pressure = ROBits (24 , _DPS310_PRSB2 , 0 , 3 , lsb_first = False )
216
- _raw_temperature = ROBits (24 , _DPS310_TMPB2 , 0 , 3 , lsb_first = False )
217
-
218
- _calib_coeff_temp_src_bit = ROBit (_DPS310_TMPCOEFSRCE , 7 )
219
-
220
- _reg0e = RWBits (8 , 0x0E , 0 )
221
- _reg0f = RWBits (8 , 0x0F , 0 )
222
- _reg62 = RWBits (8 , 0x62 , 0 )
223
198
224
199
def __init__ (self , i2c_bus , address = _DPS310_DEFAULT_ADDRESS ):
225
- self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
226
-
227
- if self ._device_id != _DPS310_DEVICE_ID :
228
- raise RuntimeError ("Failed to find DPS310 - check your wiring!" )
229
- self ._pressure_scale = None
230
- self ._temp_scale = None
231
- self ._c0 = None
232
- self ._c1 = None
233
- self ._c00 = None
234
- self ._c00 = None
235
- self ._c10 = None
236
- self ._c10 = None
237
- self ._c01 = None
238
- self ._c11 = None
239
- self ._c20 = None
240
- self ._c21 = None
241
- self ._c30 = None
242
- self ._oversample_scalefactor = (
243
- 524288 ,
244
- 1572864 ,
245
- 3670016 ,
246
- 7864320 ,
247
- 253952 ,
248
- 516096 ,
249
- 1040384 ,
250
- 2088960 ,
251
- )
252
- self .sea_level_pressure = 1013.25
253
- """Pressure in hectoPascals at sea level. Used to calibrate :attr:`altitude`."""
254
- self .initialize ()
200
+ super ().__init__ (i2c_bus , _DPS310_DEFAULT_ADDRESS )
255
201
256
202
def initialize (self ):
257
203
"""Initialize the sensor to continuous measurement"""
@@ -268,69 +214,6 @@ def initialize(self):
268
214
self .wait_temperature_ready ()
269
215
self .wait_pressure_ready ()
270
216
271
- # (https://github.com/Infineon/DPS310-Pressure-Sensor#temperature-measurement-issue)
272
- # similar to DpsClass::correctTemp(void) from infineon's c++ library
273
- def _correct_temp (self ):
274
- """Correct temperature readings on ICs with a fuse bit problem"""
275
- self ._reg0e = 0xA5
276
- self ._reg0f = 0x96
277
- self ._reg62 = 0x02
278
- self ._reg0e = 0
279
- self ._reg0f = 0
280
-
281
- # perform a temperature measurement
282
- # the most recent temperature will be saved internally
283
- # and used for compensation when calculating pressure
284
- _unused = self ._raw_temperature
285
-
286
- def reset (self ):
287
- """Reset the sensor"""
288
- self ._reset_register = 0x89
289
- # wait for hardware reset to finish
290
- sleep (0.010 )
291
- while not self ._sensor_ready :
292
- sleep (0.001 )
293
- self ._correct_temp ()
294
- self ._read_calibration ()
295
- # make sure we're using the temperature source used for calibration
296
- self ._temp_measurement_src_bit = self ._calib_coeff_temp_src_bit
297
-
298
- @property
299
- def pressure (self ):
300
- """Returns the current pressure reading in hPA"""
301
-
302
- temp_reading = self ._raw_temperature
303
- raw_temperature = self ._twos_complement (temp_reading , 24 )
304
- pressure_reading = self ._raw_pressure
305
- raw_pressure = self ._twos_complement (pressure_reading , 24 )
306
- _scaled_rawtemp = raw_temperature / self ._temp_scale
307
-
308
- _temperature = _scaled_rawtemp * self ._c1 + self ._c0 / 2.0
309
-
310
- p_red = raw_pressure / self ._pressure_scale
311
-
312
- pres_calc = (
313
- self ._c00
314
- + p_red * (self ._c10 + p_red * (self ._c20 + p_red * self ._c30 ))
315
- + _scaled_rawtemp * (self ._c01 + p_red * (self ._c11 + p_red * self ._c21 ))
316
- )
317
-
318
- final_pressure = pres_calc / 100
319
- return final_pressure
320
-
321
- @property
322
- def altitude (self ):
323
- """The altitude based on the sea level pressure (:attr:`sea_level_pressure`) -
324
- which you must enter ahead of time)"""
325
- return 44330 * (1.0 - math .pow (self .pressure / self .sea_level_pressure , 0.1903 ))
326
-
327
- @property
328
- def temperature (self ):
329
- """The current temperature reading in degrees Celsius"""
330
- _scaled_rawtemp = self ._raw_temperature / self ._temp_scale
331
- _temperature = _scaled_rawtemp * self ._c1 + self ._c0 / 2.0
332
- return _temperature
333
-
334
217
@property
335
218
def temperature_ready (self ):
336
219
"""Returns true if there is a temperature reading ready"""
@@ -434,44 +317,3 @@ def temperature_oversample_count(self, value):
434
317
self ._temp_osbits = value
435
318
self ._temp_scale = self ._oversample_scalefactor [value ]
436
319
self ._temp_shiftbit = value > SampleCount .COUNT_8
437
-
438
- @staticmethod
439
- def _twos_complement (val , bits ):
440
- if val & (1 << (bits - 1 )):
441
- val -= 1 << bits
442
-
443
- return val
444
-
445
- def _read_calibration (self ):
446
-
447
- while not self ._coefficients_ready :
448
- sleep (0.001 )
449
-
450
- buffer = bytearray (19 )
451
- coeffs = [None ] * 18
452
- for offset in range (18 ):
453
- buffer = bytearray (2 )
454
- buffer [0 ] = 0x10 + offset
455
-
456
- with self .i2c_device as i2c :
457
-
458
- i2c .write_then_readinto (buffer , buffer , out_end = 1 , in_start = 1 )
459
-
460
- coeffs [offset ] = buffer [1 ]
461
-
462
- self ._c0 = (coeffs [0 ] << 4 ) | ((coeffs [1 ] >> 4 ) & 0x0F )
463
- self ._c0 = self ._twos_complement (self ._c0 , 12 )
464
-
465
- self ._c1 = self ._twos_complement (((coeffs [1 ] & 0x0F ) << 8 ) | coeffs [2 ], 12 )
466
-
467
- self ._c00 = (coeffs [3 ] << 12 ) | (coeffs [4 ] << 4 ) | ((coeffs [5 ] >> 4 ) & 0x0F )
468
- self ._c00 = self ._twos_complement (self ._c00 , 20 )
469
-
470
- self ._c10 = ((coeffs [5 ] & 0x0F ) << 16 ) | (coeffs [6 ] << 8 ) | coeffs [7 ]
471
- self ._c10 = self ._twos_complement (self ._c10 , 20 )
472
-
473
- self ._c01 = self ._twos_complement ((coeffs [8 ] << 8 ) | coeffs [9 ], 16 )
474
- self ._c11 = self ._twos_complement ((coeffs [10 ] << 8 ) | coeffs [11 ], 16 )
475
- self ._c20 = self ._twos_complement ((coeffs [12 ] << 8 ) | coeffs [13 ], 16 )
476
- self ._c21 = self ._twos_complement ((coeffs [14 ] << 8 ) | coeffs [15 ], 16 )
477
- self ._c30 = self ._twos_complement ((coeffs [16 ] << 8 ) | coeffs [17 ], 16 )
0 commit comments