29
29
"""
30
30
import math
31
31
from time import sleep
32
+
32
33
try :
33
34
import struct
34
35
except ImportError :
62
63
IIR_FILTER_X8 = const (0x03 )
63
64
IIR_FILTER_X16 = const (0x04 )
64
65
65
- _BMP280_IIR_FILTERS = (IIR_FILTER_DISABLE , IIR_FILTER_X2 ,
66
- IIR_FILTER_X4 , IIR_FILTER_X8 , IIR_FILTER_X16 )
66
+ _BMP280_IIR_FILTERS = (
67
+ IIR_FILTER_DISABLE ,
68
+ IIR_FILTER_X2 ,
69
+ IIR_FILTER_X4 ,
70
+ IIR_FILTER_X8 ,
71
+ IIR_FILTER_X16 ,
72
+ )
67
73
68
74
"""overscan values for temperature, pressure, and humidity"""
69
75
OVERSCAN_DISABLE = const (0x00 )
73
79
OVERSCAN_X8 = const (0x04 )
74
80
OVERSCAN_X16 = const (0x05 )
75
81
76
- _BMP280_OVERSCANS = {OVERSCAN_DISABLE :0 , OVERSCAN_X1 :1 , OVERSCAN_X2 :2 ,
77
- OVERSCAN_X4 :4 , OVERSCAN_X8 :8 , OVERSCAN_X16 :16 }
82
+ _BMP280_OVERSCANS = {
83
+ OVERSCAN_DISABLE : 0 ,
84
+ OVERSCAN_X1 : 1 ,
85
+ OVERSCAN_X2 : 2 ,
86
+ OVERSCAN_X4 : 4 ,
87
+ OVERSCAN_X8 : 8 ,
88
+ OVERSCAN_X16 : 16 ,
89
+ }
78
90
79
91
"""mode values"""
80
92
MODE_SLEEP = const (0x00 )
86
98
standby timeconstant values
87
99
TC_X[_Y] where X=milliseconds and Y=tenths of a millisecond
88
100
"""
89
- STANDBY_TC_0_5 = const (0x00 ) #0.5ms
90
- STANDBY_TC_10 = const (0x06 ) #10ms
91
- STANDBY_TC_20 = const (0x07 ) #20ms
92
- STANDBY_TC_62_5 = const (0x01 ) #62.5ms
93
- STANDBY_TC_125 = const (0x02 ) #125ms
94
- STANDBY_TC_250 = const (0x03 ) #250ms
95
- STANDBY_TC_500 = const (0x04 ) #500ms
96
- STANDBY_TC_1000 = const (0x05 ) #1000ms
97
-
98
- _BMP280_STANDBY_TCS = (STANDBY_TC_0_5 , STANDBY_TC_10 , STANDBY_TC_20 ,
99
- STANDBY_TC_62_5 , STANDBY_TC_125 , STANDBY_TC_250 ,
100
- STANDBY_TC_500 , STANDBY_TC_1000 )
101
-
102
- class Adafruit_BMP280 : # pylint: disable=invalid-name
101
+ STANDBY_TC_0_5 = const (0x00 ) # 0.5ms
102
+ STANDBY_TC_10 = const (0x06 ) # 10ms
103
+ STANDBY_TC_20 = const (0x07 ) # 20ms
104
+ STANDBY_TC_62_5 = const (0x01 ) # 62.5ms
105
+ STANDBY_TC_125 = const (0x02 ) # 125ms
106
+ STANDBY_TC_250 = const (0x03 ) # 250ms
107
+ STANDBY_TC_500 = const (0x04 ) # 500ms
108
+ STANDBY_TC_1000 = const (0x05 ) # 1000ms
109
+
110
+ _BMP280_STANDBY_TCS = (
111
+ STANDBY_TC_0_5 ,
112
+ STANDBY_TC_10 ,
113
+ STANDBY_TC_20 ,
114
+ STANDBY_TC_62_5 ,
115
+ STANDBY_TC_125 ,
116
+ STANDBY_TC_250 ,
117
+ STANDBY_TC_500 ,
118
+ STANDBY_TC_1000 ,
119
+ )
120
+
121
+
122
+ class Adafruit_BMP280 : # pylint: disable=invalid-name
103
123
"""Base BMP280 object. Use `Adafruit_BMP280_I2C` or `Adafruit_BMP280_SPI` instead of this. This
104
124
checks the BMP280 was found, reads the coefficients and enables the sensor for continuous
105
125
reads"""
126
+
106
127
def __init__ (self ):
107
128
# Check device ID.
108
129
chip_id = self ._read_byte (_REGISTER_CHIPID )
109
130
if _CHIP_ID != chip_id :
110
- raise RuntimeError (' Failed to find BMP280! Chip ID 0x%x' % chip_id )
111
- #Set some reasonable defaults.
131
+ raise RuntimeError (" Failed to find BMP280! Chip ID 0x%x" % chip_id )
132
+ # Set some reasonable defaults.
112
133
self ._iir_filter = IIR_FILTER_DISABLE
113
134
self ._overscan_temperature = OVERSCAN_X2
114
135
self ._overscan_pressure = OVERSCAN_X16
@@ -129,21 +150,27 @@ def _read_temperature(self):
129
150
# Wait for conversion to complete
130
151
while self ._get_status () & 0x08 :
131
152
sleep (0.002 )
132
- raw_temperature = self ._read24 (_REGISTER_TEMPDATA ) / 16 # lowest 4 bits get dropped
133
- #print("raw temp: ", UT)
134
- var1 = (raw_temperature / 16384.0 - self ._temp_calib [0 ] / 1024.0 ) * self ._temp_calib [1 ]
135
- #print(var1)
136
- var2 = ((raw_temperature / 131072.0 - self ._temp_calib [0 ] / 8192.0 ) * (
137
- raw_temperature / 131072.0 - self ._temp_calib [0 ] / 8192.0 )) * self ._temp_calib [2 ]
138
- #print(var2)
153
+ raw_temperature = (
154
+ self ._read24 (_REGISTER_TEMPDATA ) / 16
155
+ ) # lowest 4 bits get dropped
156
+ # print("raw temp: ", UT)
157
+ var1 = (
158
+ raw_temperature / 16384.0 - self ._temp_calib [0 ] / 1024.0
159
+ ) * self ._temp_calib [1 ]
160
+ # print(var1)
161
+ var2 = (
162
+ (raw_temperature / 131072.0 - self ._temp_calib [0 ] / 8192.0 )
163
+ * (raw_temperature / 131072.0 - self ._temp_calib [0 ] / 8192.0 )
164
+ ) * self ._temp_calib [2 ]
165
+ # print(var2)
139
166
140
167
self ._t_fine = int (var1 + var2 )
141
- #print("t_fine: ", self.t_fine)
168
+ # print("t_fine: ", self.t_fine)
142
169
143
170
def _reset (self ):
144
171
"""Soft reset the sensor"""
145
172
self ._write_register_byte (_REGISTER_SOFTRESET , 0xB6 )
146
- sleep (0.004 ) #Datasheet says 2ms. Using 4ms just to be safe
173
+ sleep (0.004 ) # Datasheet says 2ms. Using 4ms just to be safe
147
174
148
175
def _write_ctrl_meas (self ):
149
176
"""
@@ -164,9 +191,9 @@ def _write_config(self):
164
191
"""Write the value to the config register in the device """
165
192
normal_flag = False
166
193
if self ._mode == MODE_NORMAL :
167
- #Writes to the config register may be ignored while in Normal mode
194
+ # Writes to the config register may be ignored while in Normal mode
168
195
normal_flag = True
169
- self .mode = MODE_SLEEP # So we switch to Sleep mode first
196
+ self .mode = MODE_SLEEP # So we switch to Sleep mode first
170
197
self ._write_register_byte (_REGISTER_CONFIG , self ._config )
171
198
if normal_flag :
172
199
self .mode = MODE_NORMAL
@@ -182,7 +209,7 @@ def mode(self):
182
209
@mode .setter
183
210
def mode (self , value ):
184
211
if not value in _BMP280_MODES :
185
- raise ValueError (' Mode \ ' %s\ ' not supported' % (value ))
212
+ raise ValueError (" Mode '%s' not supported" % (value ))
186
213
self ._mode = value
187
214
self ._write_ctrl_meas ()
188
215
@@ -197,7 +224,7 @@ def standby_period(self):
197
224
@standby_period .setter
198
225
def standby_period (self , value ):
199
226
if not value in _BMP280_STANDBY_TCS :
200
- raise ValueError (' Standby Period \ ' %s\ ' not supported' % (value ))
227
+ raise ValueError (" Standby Period '%s' not supported" % (value ))
201
228
if self ._t_standby == value :
202
229
return
203
230
self ._t_standby = value
@@ -214,7 +241,7 @@ def overscan_temperature(self):
214
241
@overscan_temperature .setter
215
242
def overscan_temperature (self , value ):
216
243
if not value in _BMP280_OVERSCANS :
217
- raise ValueError (' Overscan value \ ' %s\ ' not supported' % (value ))
244
+ raise ValueError (" Overscan value '%s' not supported" % (value ))
218
245
self ._overscan_temperature = value
219
246
self ._write_ctrl_meas ()
220
247
@@ -229,7 +256,7 @@ def overscan_pressure(self):
229
256
@overscan_pressure .setter
230
257
def overscan_pressure (self , value ):
231
258
if not value in _BMP280_OVERSCANS :
232
- raise ValueError (' Overscan value \ ' %s\ ' not supported' % (value ))
259
+ raise ValueError (" Overscan value '%s' not supported" % (value ))
233
260
self ._overscan_pressure = value
234
261
self ._write_ctrl_meas ()
235
262
@@ -244,7 +271,7 @@ def iir_filter(self):
244
271
@iir_filter .setter
245
272
def iir_filter (self , value ):
246
273
if not value in _BMP280_IIR_FILTERS :
247
- raise ValueError (' IIR Filter \ ' %s\ ' not supported' % (value ))
274
+ raise ValueError (" IIR Filter '%s' not supported" % (value ))
248
275
self ._iir_filter = value
249
276
self ._write_config ()
250
277
@@ -253,16 +280,16 @@ def _config(self):
253
280
"""Value to be written to the device's config register """
254
281
config = 0
255
282
if self .mode == MODE_NORMAL :
256
- config += ( self ._t_standby << 5 )
283
+ config += self ._t_standby << 5
257
284
if self ._iir_filter :
258
- config += ( self ._iir_filter << 2 )
285
+ config += self ._iir_filter << 2
259
286
return config
260
287
261
288
@property
262
289
def _ctrl_meas (self ):
263
290
"""Value to be written to the device's ctrl_meas register """
264
- ctrl_meas = ( self .overscan_temperature << 5 )
265
- ctrl_meas += ( self .overscan_pressure << 2 )
291
+ ctrl_meas = self .overscan_temperature << 5
292
+ ctrl_meas += self .overscan_pressure << 2
266
293
ctrl_meas += self .mode
267
294
return ctrl_meas
268
295
@@ -271,19 +298,19 @@ def measurement_time_typical(self):
271
298
"""Typical time in milliseconds required to complete a measurement in normal mode"""
272
299
meas_time_ms = 1
273
300
if self .overscan_temperature != OVERSCAN_DISABLE :
274
- meas_time_ms += ( 2 * _BMP280_OVERSCANS .get (self .overscan_temperature ) )
301
+ meas_time_ms += 2 * _BMP280_OVERSCANS .get (self .overscan_temperature )
275
302
if self .overscan_pressure != OVERSCAN_DISABLE :
276
- meas_time_ms += ( 2 * _BMP280_OVERSCANS .get (self .overscan_pressure ) + 0.5 )
303
+ meas_time_ms += 2 * _BMP280_OVERSCANS .get (self .overscan_pressure ) + 0.5
277
304
return meas_time_ms
278
305
279
306
@property
280
307
def measurement_time_max (self ):
281
308
"""Maximum time in milliseconds required to complete a measurement in normal mode"""
282
309
meas_time_ms = 1.25
283
310
if self .overscan_temperature != OVERSCAN_DISABLE :
284
- meas_time_ms += ( 2.3 * _BMP280_OVERSCANS .get (self .overscan_temperature ) )
311
+ meas_time_ms += 2.3 * _BMP280_OVERSCANS .get (self .overscan_temperature )
285
312
if self .overscan_pressure != OVERSCAN_DISABLE :
286
- meas_time_ms += ( 2.3 * _BMP280_OVERSCANS .get (self .overscan_pressure ) + 0.575 )
313
+ meas_time_ms += 2.3 * _BMP280_OVERSCANS .get (self .overscan_pressure ) + 0.575
287
314
return meas_time_ms
288
315
289
316
@property
@@ -328,14 +355,14 @@ def pressure(self):
328
355
def altitude (self ):
329
356
"""The altitude based on the sea level pressure (`sea_level_pressure`) - which you must
330
357
enter ahead of time)"""
331
- p = self .pressure # in Si units for hPascal
358
+ p = self .pressure # in Si units for hPascal
332
359
return 44330 * (1.0 - math .pow (p / self .sea_level_pressure , 0.1903 ))
333
360
334
361
####################### Internal helpers ################################
335
362
def _read_coefficients (self ):
336
363
"""Read & save the calibration coefficients"""
337
364
coeff = self ._read_register (_REGISTER_DIG_T1 , 24 )
338
- coeff = list (struct .unpack (' <HhhHhhhhhhhh' , bytes (coeff )))
365
+ coeff = list (struct .unpack (" <HhhHhhhhhhhh" , bytes (coeff )))
339
366
coeff = [float (i ) for i in coeff ]
340
367
# The temp_calib lines up with DIG_T# registers.
341
368
self ._temp_calib = coeff [:3 ]
@@ -368,11 +395,14 @@ def _write_register_byte(self, register, value):
368
395
"""Low level register writing, not implemented in base class"""
369
396
raise NotImplementedError ()
370
397
371
- class Adafruit_BMP280_I2C (Adafruit_BMP280 ): # pylint: disable=invalid-name
398
+
399
+ class Adafruit_BMP280_I2C (Adafruit_BMP280 ): # pylint: disable=invalid-name
372
400
"""Driver for I2C connected BMP280. Default address is 0x77 but another address can be passed
373
401
in as an argument"""
402
+
374
403
def __init__ (self , i2c , address = 0x77 ):
375
- import adafruit_bus_device .i2c_device as i2c_device
404
+ import adafruit_bus_device .i2c_device as i2c_device # pylint: disable=import-outside-toplevel
405
+
376
406
self ._i2c = i2c_device .I2CDevice (i2c , address )
377
407
super ().__init__ ()
378
408
@@ -382,20 +412,23 @@ def _read_register(self, register, length):
382
412
i2c .write (bytes ([register & 0xFF ]))
383
413
result = bytearray (length )
384
414
i2c .readinto (result )
385
- #print("$%02X => %s" % (register, [hex(i) for i in result]))
415
+ # print("$%02X => %s" % (register, [hex(i) for i in result]))
386
416
return result
387
417
388
418
def _write_register_byte (self , register , value ):
389
419
"""Low level register writing over I2C, writes one 8-bit value"""
390
420
with self ._i2c as i2c :
391
421
i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
392
- #print("$%02X <= 0x%02X" % (register, value))
422
+ # print("$%02X <= 0x%02X" % (register, value))
423
+
393
424
394
425
class Adafruit_BMP280_SPI (Adafruit_BMP280 ):
395
426
"""Driver for SPI connected BMP280. Default clock rate is 100000 but can be changed with
396
427
'baudrate'"""
428
+
397
429
def __init__ (self , spi , cs , baudrate = 100000 ):
398
- import adafruit_bus_device .spi_device as spi_device
430
+ import adafruit_bus_device .spi_device as spi_device # pylint: disable=import-outside-toplevel
431
+
399
432
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
400
433
super ().__init__ ()
401
434
@@ -407,7 +440,7 @@ def _read_register(self, register, length):
407
440
spi .write (bytearray ([register ]))
408
441
result = bytearray (length )
409
442
spi .readinto (result )
410
- #print("$%02X => %s" % (register, [hex(i) for i in result]))
443
+ # print("$%02X => %s" % (register, [hex(i) for i in result]))
411
444
return result
412
445
413
446
def _write_register_byte (self , register , value ):
0 commit comments