33
33
34
34
**Hardware:**
35
35
36
- .. todo:: Update the PID for the below and add links to any specific hardware product page(s), or category page(s)
37
- * Adafruit's DPS310 Breakout: https://adafruit.com/product/44XX
36
+ * Adafruit's DPS310 Breakout: https://www.adafruit.com/product/4494
38
37
39
38
**Software and Dependencies:**
40
39
49
48
# Common imports; remove if unused or pylint will complain
50
49
from time import sleep
51
50
import adafruit_bus_device .i2c_device as i2c_device
52
- from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct , Struct
53
- from adafruit_register .i2c_struct_array import StructArray
51
+ from adafruit_register .i2c_struct import UnaryStruct , ROUnaryStruct
54
52
from adafruit_register .i2c_bit import RWBit , ROBit
55
53
from adafruit_register .i2c_bits import RWBits , ROBits
56
54
68
66
_DPS310_TMPCOEFSRCE = 0x28 # Temperature calibration src
69
67
70
68
#pylint: enable=bad-whitespace
69
+ #pylint: disable=no-member,unnecessary-pass
70
+
71
71
class CV :
72
72
"""struct helper"""
73
73
@@ -92,7 +92,6 @@ class Mode(CV):
92
92
"""Options for ``mode``"""
93
93
pass #pylint: disable=unnecessary-pass
94
94
95
- #pylint: disable=no-member
96
95
Mode .add_values ((
97
96
('IDLE' , 0 , "Idle" , None ),
98
97
('ONE_PRESSURE' , 1 , "One-Shot Pressure" , None ),
@@ -131,8 +130,9 @@ class Samples(CV):
131
130
('COUNT_64' , 6 , 64 , None ),
132
131
('COUNT_128' , 7 , 128 , None ),
133
132
))
134
-
133
+ #pylint: enable=unnecessary-pass
135
134
class DPS310 :
135
+ #pylint: disable=too-many-instance-attributes
136
136
"""Library for the DPS310 Precision Barometric Pressure Sensor.
137
137
138
138
:param ~busio.I2C i2c_bus: The I2C bus the DPS310 is connected to.
@@ -178,13 +178,27 @@ def __init__(self, i2c_bus, address=_DPS310_DEFAULT_ADDRESS):
178
178
179
179
if self ._device_id != _DPS310_DEVICE_ID :
180
180
raise RuntimeError ("Failed to find DPS310 - check your wiring!" )
181
-
181
+ self ._pressure_scale = None
182
+ self ._temp_scale = None
183
+ self ._c0 = None
184
+ self ._c1 = None
185
+ self ._c00 = None
186
+ self ._c00 = None
187
+ self ._c10 = None
188
+ self ._c10 = None
189
+ self ._c01 = None
190
+ self ._c11 = None
191
+ self ._c20 = None
192
+ self ._c21 = None
193
+ self ._c30 = None
194
+ self ._oversample_scalefactor = (524288 , 1572864 , 3670016 , 7864320 , 253952 ,
195
+ 516096 , 1040384 , 2088960 )
182
196
self .initialize ()
183
197
184
198
def initialize (self ):
185
199
"""Reset the sensor to the default state"""
186
- # print("called initialize")
187
- self . _init_values ()
200
+
201
+
188
202
self .reset ()
189
203
# wait for hardware reset to finish
190
204
sleep (0.010 )
@@ -194,51 +208,32 @@ def initialize(self):
194
208
self .mode = Mode .CONT_PRESTEMP
195
209
196
210
# wait until we have at least one good measurement
197
- tmprdy = self ._temp_ready
198
- prsrdy = self ._pressure_ready
199
- while (tmprdy is False ) or (prsrdy is False ):
211
+ while (self ._temp_ready is False ) or (self ._pressure_ready is False ):
200
212
sleep (0.001 )
201
- tmprdy = self ._temp_ready
202
- prsrdy = self ._pressure_ready
203
213
204
214
def reset (self ):
205
215
"""Perform a soft-reset on the sensor"""
206
216
self ._reset = 0x89
207
217
while not self ._sensor_ready :
208
218
sleep (0.001 )
209
219
210
- def _init_values (self ):
211
- self ._pressure_scale = None
212
- self ._temp_scale = None
213
- self ._c0 = None
214
- self ._c1 = None
215
- self ._c00 = None
216
- self ._c00 = None
217
- self ._c10 = None
218
- self ._c10 = None
219
- self ._c01 = None
220
- self ._c11 = None
221
- self ._c20 = None
222
- self ._c21 = None
223
- self ._c30 = None
224
- self ._oversample_scalefactor = (524288 , 1572864 , 3670016 , 7864320 , 253952 , 516096 , 1040384 , 2088960 )
225
-
226
220
@property
227
221
def pressure (self ):
228
222
"""Returns the current pressure reading in kPA"""
229
223
230
224
temp_reading = self ._raw_temperature
231
- raw_temperature = self .twosComplement (temp_reading , 24 )
225
+ raw_temperature = self .twos_complement (temp_reading , 24 )
232
226
pressure_reading = self ._raw_pressure
233
- raw_pressure = self .twosComplement (pressure_reading , 24 )
227
+ raw_pressure = self .twos_complement (pressure_reading , 24 )
234
228
_scaled_rawtemp = raw_temperature / self ._temp_scale
235
229
236
230
_temperature = _scaled_rawtemp * self ._c1 + self ._c0 / 2.0
237
231
238
232
p_red = raw_pressure / self ._pressure_scale
239
233
240
234
241
- pres_calc = self ._c00 + p_red * (self ._c10 + p_red * (self ._c20 + p_red * self ._c30 )) + _scaled_rawtemp * (self ._c01 + p_red * (self ._c11 + p_red * self ._c21 ))
235
+ pres_calc = (self ._c00 + p_red * (self ._c10 + p_red * (self ._c20 + p_red * self ._c30 )) +
236
+ _scaled_rawtemp * (self ._c01 + p_red * (self ._c11 + p_red * self ._c21 )))
242
237
243
238
final_pressure = pres_calc / 100
244
239
return final_pressure
@@ -262,6 +257,7 @@ def temperature_ready(self):
262
257
263
258
@property
264
259
def pressure_ready (self ):
260
+ """Returns true if pressure readings are ready"""
265
261
return self ._pressure_ready
266
262
267
263
@property
@@ -271,11 +267,11 @@ def mode(self):
271
267
272
268
@mode .setter
273
269
def mode (self , value ):
270
+ """Set the mode"""
274
271
if not Mode .is_valid (value ):
275
272
raise AttributeError ("mode must be an `Mode`" )
276
273
277
274
self ._mode_bits = value
278
- ################# PRESSURE CONFIG ########################################
279
275
280
276
def pressure_configuration (self , rate , oversample ):
281
277
"""Configure the pressure rate and oversample count"""
@@ -292,8 +288,9 @@ def temperature_configuration(self, rate, oversample):
292
288
self ._temp_shiftbit = (oversample > Samples .COUNT_8 )
293
289
self ._temp_measurement_src_bit = self ._calib_coeff_temp_src_bit
294
290
295
- def twosComplement (self , val , bits ):
296
- if (val & (1 << (bits - 1 ))):
291
+ @staticmethod
292
+ def _twos_complement (val , bits ):
293
+ if val & (1 << (bits - 1 )):
297
294
val -= (1 << bits )
298
295
299
296
return val
@@ -316,18 +313,18 @@ def _read_calibration(self):
316
313
coeffs [offset ] = buffer [1 ]
317
314
318
315
self ._c0 = (coeffs [0 ] << 4 ) | ((coeffs [1 ] >> 4 ) & 0x0F )
319
- self ._c0 = self .twosComplement (self ._c0 , 12 )
316
+ self ._c0 = self .twos_complement (self ._c0 , 12 )
320
317
321
- self ._c1 = self .twosComplement (((coeffs [1 ] & 0x0F ) << 8 ) | coeffs [2 ], 12 )
318
+ self ._c1 = self .twos_complement (((coeffs [1 ] & 0x0F ) << 8 ) | coeffs [2 ], 12 )
322
319
323
320
self ._c00 = (coeffs [3 ] << 12 ) | (coeffs [4 ] << 4 ) | ((coeffs [5 ] >> 4 ) & 0x0F )
324
- self ._c00 = self .twosComplement (self ._c00 , 20 )
321
+ self ._c00 = self .twos_complement (self ._c00 , 20 )
325
322
326
323
self ._c10 = ((coeffs [5 ] & 0x0F ) << 16 ) | (coeffs [6 ] << 8 ) | coeffs [7 ]
327
- self ._c10 = self .twosComplement (self ._c10 , 20 )
324
+ self ._c10 = self .twos_complement (self ._c10 , 20 )
328
325
329
- self ._c01 = self .twosComplement ((coeffs [8 ] << 8 ) | coeffs [9 ], 16 )
330
- self ._c11 = self .twosComplement ((coeffs [10 ] << 8 ) | coeffs [11 ], 16 )
331
- self ._c20 = self .twosComplement ((coeffs [12 ] << 8 ) | coeffs [13 ], 16 )
332
- self ._c21 = self .twosComplement ((coeffs [14 ] << 8 ) | coeffs [15 ], 16 )
333
- self ._c30 = self .twosComplement ((coeffs [16 ] << 8 ) | coeffs [17 ], 16 )
326
+ self ._c01 = self .twos_complement ((coeffs [8 ] << 8 ) | coeffs [9 ], 16 )
327
+ self ._c11 = self .twos_complement ((coeffs [10 ] << 8 ) | coeffs [11 ], 16 )
328
+ self ._c20 = self .twos_complement ((coeffs [12 ] << 8 ) | coeffs [13 ], 16 )
329
+ self ._c21 = self .twos_complement ((coeffs [14 ] << 8 ) | coeffs [15 ], 16 )
330
+ self ._c30 = self .twos_complement ((coeffs [16 ] << 8 ) | coeffs [17 ], 16 )
0 commit comments