@@ -115,11 +115,11 @@ class Rate(CV):
115
115
('RATE_128_HZ' , 7 , 128 , None )
116
116
))
117
117
118
- class Samples (CV ):
118
+ class SampleCount (CV ):
119
119
"""Options for oversample_count"""
120
120
pass
121
121
122
- Samples .add_values ((
122
+ SampleCount .add_values ((
123
123
('COUNT_1' , 0 , 1 , None ),
124
124
('COUNT_2' , 1 , 2 , None ),
125
125
('COUNT_4' , 2 , 4 , None ),
@@ -138,27 +138,20 @@ class DPS310:
138
138
:param address: The I2C slave address of the sensor
139
139
140
140
"""
141
-
142
-
143
141
# Register definitions
144
142
_device_id = ROUnaryStruct (_DPS310_PRODREVID , ">B" )
145
- _reset = UnaryStruct (_DPS310_RESET , ">B" )
143
+ _reset_register = UnaryStruct (_DPS310_RESET , ">B" )
146
144
_mode_bits = RWBits (3 , _DPS310_MEASCFG , 0 )
147
145
148
146
_pressure_ratebits = RWBits (3 , _DPS310_PRSCFG , 4 )
149
147
_pressure_osbits = RWBits (4 , _DPS310_PRSCFG , 0 )
150
- _pressure_config = RWBits (8 , _DPS310_PRSCFG , 0 )
151
-
152
148
153
149
_temp_ratebits = RWBits (3 , _DPS310_TMPCFG , 4 )
154
150
_temp_osbits = RWBits (4 , _DPS310_TMPCFG , 0 )
155
151
156
152
_temp_measurement_src_bit = RWBit (_DPS310_TMPCFG , 7 )
157
153
158
- _temp_config = RWBits (8 , _DPS310_TMPCFG , 0 )
159
-
160
154
_pressure_shiftbit = RWBit (_DPS310_CFGREG , 2 )
161
-
162
155
_temp_shiftbit = RWBit (_DPS310_CFGREG , 3 )
163
156
164
157
_coefficients_ready = RWBit (_DPS310_MEASCFG , 7 )
@@ -171,7 +164,6 @@ class DPS310:
171
164
172
165
_calib_coeff_temp_src_bit = ROBit (_DPS310_TMPCOEFSRCE , 7 )
173
166
174
-
175
167
def __init__ (self , i2c_bus , address = _DPS310_DEFAULT_ADDRESS ):
176
168
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
177
169
@@ -197,22 +189,28 @@ def __init__(self, i2c_bus, address=_DPS310_DEFAULT_ADDRESS):
197
189
def initialize (self ):
198
190
"""Reset the sensor to the default state"""
199
191
200
-
201
- self .reset ()
202
- # wait for hardware reset to finish
203
- sleep (0.010 )
192
+ self ._reset ()
204
193
self ._read_calibration ()
205
- self .pressure_configuration (Rate .RATE_64_HZ , Samples .COUNT_64 )
206
- self .temperature_configuration (Rate .RATE_64_HZ , Samples .COUNT_64 )
194
+
195
+ # make sure we're using the temperature source used for calibration
196
+ self ._temp_measurement_src_bit = self ._calib_coeff_temp_src_bit
197
+
198
+ self .pressure_rate = Rate .RATE_64_HZ
199
+ self .pressure_oversample_count = SampleCount .COUNT_64
200
+ self .temperature_rate = Rate .RATE_64_HZ
201
+ self .temperature_oversample_count = SampleCount .COUNT_64
207
202
self .mode = Mode .CONT_PRESTEMP
208
203
209
204
# wait until we have at least one good measurement
205
+
210
206
while (self ._temp_ready is False ) or (self ._pressure_ready is False ):
211
207
sleep (0.001 )
212
208
213
- def reset (self ):
209
+ def _reset (self ):
214
210
"""Perform a soft-reset on the sensor"""
215
- self ._reset = 0x89
211
+ self ._reset_register = 0x89
212
+ # wait for hardware reset to finish
213
+ sleep (0.010 )
216
214
while not self ._sensor_ready :
217
215
sleep (0.001 )
218
216
@@ -221,9 +219,9 @@ def pressure(self):
221
219
"""Returns the current pressure reading in kPA"""
222
220
223
221
temp_reading = self ._raw_temperature
224
- raw_temperature = self .twos_complement (temp_reading , 24 )
222
+ raw_temperature = self ._twos_complement (temp_reading , 24 )
225
223
pressure_reading = self ._raw_pressure
226
- raw_pressure = self .twos_complement (pressure_reading , 24 )
224
+ raw_pressure = self ._twos_complement (pressure_reading , 24 )
227
225
_scaled_rawtemp = raw_temperature / self ._temp_scale
228
226
229
227
_temperature = _scaled_rawtemp * self ._c1 + self ._c0 / 2.0
@@ -244,11 +242,6 @@ def temperature(self):
244
242
_temperature = _scaled_rawtemp * self ._c1 + self ._c0 / 2.0
245
243
return _temperature
246
244
247
- @property
248
- def sensor_ready (self ):
249
- """Identifies the sensorhas measurements ready"""
250
- return self ._sensor_ready
251
-
252
245
@property
253
246
def temperature_ready (self ):
254
247
"""Returns true if there is a temperature reading ready"""
@@ -272,20 +265,55 @@ def mode(self, value):
272
265
273
266
self ._mode_bits = value
274
267
275
- def pressure_configuration (self , rate , oversample ):
276
- """Configure the pressure rate and oversample count"""
277
- self ._pressure_ratebits = rate
278
- self ._pressure_osbits = oversample
279
- self ._pressure_shiftbit = (oversample > Samples .COUNT_8 )
280
- self ._pressure_scale = self ._oversample_scalefactor [oversample ]
281
-
282
- def temperature_configuration (self , rate , oversample ):
283
- """Configure the temperature rate and oversample count"""
284
- self ._temp_ratebits = rate
285
- self ._temp_osbits = oversample
286
- self ._temp_scale = self ._oversample_scalefactor [oversample ]
287
- self ._temp_shiftbit = (oversample > Samples .COUNT_8 )
288
- self ._temp_measurement_src_bit = self ._calib_coeff_temp_src_bit
268
+ @property
269
+ def pressure_rate (self ):
270
+ """Configure the pressure measurement rate. Must be a Rate"""
271
+ return self ._pressure_ratebits
272
+
273
+ @pressure_rate .setter
274
+ def pressure_rate (self , value ):
275
+ if not Rate .is_valid (value ):
276
+ raise AttributeError ("pressure_rate must be a Rate" )
277
+ self ._pressure_ratebits = value
278
+
279
+ @property
280
+ def pressure_oversample_count (self ):
281
+ """The number of samples taken per pressure measurement. Must be a SampleCount"""
282
+ return self ._pressure_osbits
283
+
284
+ @pressure_oversample_count .setter
285
+ def pressure_oversample_count (self , value ):
286
+ if not SampleCount .is_valid (value ):
287
+ raise AttributeError ("pressure_oversample_count must be a SampleCount" )
288
+
289
+ self ._pressure_osbits = value
290
+ self ._pressure_shiftbit = (value > SampleCount .COUNT_8 )
291
+ self ._pressure_scale = self ._oversample_scalefactor [value ]
292
+
293
+ @property
294
+ def temperature_rate (self ):
295
+ """Configure the temperature measurement rate. Must be a Rate"""
296
+ return self ._temp_ratebits
297
+
298
+ @temperature_rate .setter
299
+ def temperature_rate (self , value ):
300
+ if not Rate .is_valid (value ):
301
+ raise AttributeError ("temperature_rate must be a Rate" )
302
+ self ._temp_ratebits = value
303
+
304
+ @property
305
+ def temperature_oversample_count (self ):
306
+ """The number of samples taken per temperature measurement. Must be a SampleCount"""
307
+ return self ._temp_osbits
308
+
309
+ @temperature_oversample_count .setter
310
+ def temperature_oversample_count (self , value ):
311
+ if not SampleCount .is_valid (value ):
312
+ raise AttributeError ("temperature_oversample_count must be a SampleCount" )
313
+
314
+ self ._temp_osbits = value
315
+ self ._temp_scale = self ._oversample_scalefactor [value ]
316
+ self ._temp_shiftbit = (value > SampleCount .COUNT_8 )
289
317
290
318
@staticmethod
291
319
def _twos_complement (val , bits ):
@@ -312,18 +340,18 @@ def _read_calibration(self):
312
340
coeffs [offset ] = buffer [1 ]
313
341
314
342
self ._c0 = (coeffs [0 ] << 4 ) | ((coeffs [1 ] >> 4 ) & 0x0F )
315
- self ._c0 = self .twos_complement (self ._c0 , 12 )
343
+ self ._c0 = self ._twos_complement (self ._c0 , 12 )
316
344
317
- self ._c1 = self .twos_complement (((coeffs [1 ] & 0x0F ) << 8 ) | coeffs [2 ], 12 )
345
+ self ._c1 = self ._twos_complement (((coeffs [1 ] & 0x0F ) << 8 ) | coeffs [2 ], 12 )
318
346
319
347
self ._c00 = (coeffs [3 ] << 12 ) | (coeffs [4 ] << 4 ) | ((coeffs [5 ] >> 4 ) & 0x0F )
320
- self ._c00 = self .twos_complement (self ._c00 , 20 )
348
+ self ._c00 = self ._twos_complement (self ._c00 , 20 )
321
349
322
350
self ._c10 = ((coeffs [5 ] & 0x0F ) << 16 ) | (coeffs [6 ] << 8 ) | coeffs [7 ]
323
- self ._c10 = self .twos_complement (self ._c10 , 20 )
351
+ self ._c10 = self ._twos_complement (self ._c10 , 20 )
324
352
325
- self ._c01 = self .twos_complement ((coeffs [8 ] << 8 ) | coeffs [9 ], 16 )
326
- self ._c11 = self .twos_complement ((coeffs [10 ] << 8 ) | coeffs [11 ], 16 )
327
- self ._c20 = self .twos_complement ((coeffs [12 ] << 8 ) | coeffs [13 ], 16 )
328
- self ._c21 = self .twos_complement ((coeffs [14 ] << 8 ) | coeffs [15 ], 16 )
329
- self ._c30 = self .twos_complement ((coeffs [16 ] << 8 ) | coeffs [17 ], 16 )
353
+ self ._c01 = self ._twos_complement ((coeffs [8 ] << 8 ) | coeffs [9 ], 16 )
354
+ self ._c11 = self ._twos_complement ((coeffs [10 ] << 8 ) | coeffs [11 ], 16 )
355
+ self ._c20 = self ._twos_complement ((coeffs [12 ] << 8 ) | coeffs [13 ], 16 )
356
+ self ._c21 = self ._twos_complement ((coeffs [14 ] << 8 ) | coeffs [15 ], 16 )
357
+ self ._c30 = self ._twos_complement ((coeffs [16 ] << 8 ) | coeffs [17 ], 16 )
0 commit comments