2929"""
3030import math
3131from time import sleep
32+
3233try :
3334 import struct
3435except ImportError :
6263IIR_FILTER_X8 = const (0x03 )
6364IIR_FILTER_X16 = const (0x04 )
6465
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+ )
6773
6874"""overscan values for temperature, pressure, and humidity"""
6975OVERSCAN_DISABLE = const (0x00 )
7379OVERSCAN_X8 = const (0x04 )
7480OVERSCAN_X16 = const (0x05 )
7581
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+ }
7890
7991"""mode values"""
8092MODE_SLEEP = const (0x00 )
8698standby timeconstant values
8799TC_X[_Y] where X=milliseconds and Y=tenths of a millisecond
88100"""
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
103123 """Base BMP280 object. Use `Adafruit_BMP280_I2C` or `Adafruit_BMP280_SPI` instead of this. This
104124 checks the BMP280 was found, reads the coefficients and enables the sensor for continuous
105125 reads"""
126+
106127 def __init__ (self ):
107128 # Check device ID.
108129 chip_id = self ._read_byte (_REGISTER_CHIPID )
109130 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.
112133 self ._iir_filter = IIR_FILTER_DISABLE
113134 self ._overscan_temperature = OVERSCAN_X2
114135 self ._overscan_pressure = OVERSCAN_X16
@@ -129,21 +150,27 @@ def _read_temperature(self):
129150 # Wait for conversion to complete
130151 while self ._get_status () & 0x08 :
131152 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)
139166
140167 self ._t_fine = int (var1 + var2 )
141- #print("t_fine: ", self.t_fine)
168+ # print("t_fine: ", self.t_fine)
142169
143170 def _reset (self ):
144171 """Soft reset the sensor"""
145172 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
147174
148175 def _write_ctrl_meas (self ):
149176 """
@@ -164,9 +191,9 @@ def _write_config(self):
164191 """Write the value to the config register in the device """
165192 normal_flag = False
166193 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
168195 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
170197 self ._write_register_byte (_REGISTER_CONFIG , self ._config )
171198 if normal_flag :
172199 self .mode = MODE_NORMAL
@@ -182,7 +209,7 @@ def mode(self):
182209 @mode .setter
183210 def mode (self , value ):
184211 if not value in _BMP280_MODES :
185- raise ValueError (' Mode \ ' %s\ ' not supported' % (value ))
212+ raise ValueError (" Mode '%s' not supported" % (value ))
186213 self ._mode = value
187214 self ._write_ctrl_meas ()
188215
@@ -197,7 +224,7 @@ def standby_period(self):
197224 @standby_period .setter
198225 def standby_period (self , value ):
199226 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 ))
201228 if self ._t_standby == value :
202229 return
203230 self ._t_standby = value
@@ -214,7 +241,7 @@ def overscan_temperature(self):
214241 @overscan_temperature .setter
215242 def overscan_temperature (self , value ):
216243 if not value in _BMP280_OVERSCANS :
217- raise ValueError (' Overscan value \ ' %s\ ' not supported' % (value ))
244+ raise ValueError (" Overscan value '%s' not supported" % (value ))
218245 self ._overscan_temperature = value
219246 self ._write_ctrl_meas ()
220247
@@ -229,7 +256,7 @@ def overscan_pressure(self):
229256 @overscan_pressure .setter
230257 def overscan_pressure (self , value ):
231258 if not value in _BMP280_OVERSCANS :
232- raise ValueError (' Overscan value \ ' %s\ ' not supported' % (value ))
259+ raise ValueError (" Overscan value '%s' not supported" % (value ))
233260 self ._overscan_pressure = value
234261 self ._write_ctrl_meas ()
235262
@@ -244,7 +271,7 @@ def iir_filter(self):
244271 @iir_filter .setter
245272 def iir_filter (self , value ):
246273 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 ))
248275 self ._iir_filter = value
249276 self ._write_config ()
250277
@@ -253,16 +280,16 @@ def _config(self):
253280 """Value to be written to the device's config register """
254281 config = 0
255282 if self .mode == MODE_NORMAL :
256- config += ( self ._t_standby << 5 )
283+ config += self ._t_standby << 5
257284 if self ._iir_filter :
258- config += ( self ._iir_filter << 2 )
285+ config += self ._iir_filter << 2
259286 return config
260287
261288 @property
262289 def _ctrl_meas (self ):
263290 """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
266293 ctrl_meas += self .mode
267294 return ctrl_meas
268295
@@ -271,19 +298,19 @@ def measurement_time_typical(self):
271298 """Typical time in milliseconds required to complete a measurement in normal mode"""
272299 meas_time_ms = 1
273300 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 )
275302 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
277304 return meas_time_ms
278305
279306 @property
280307 def measurement_time_max (self ):
281308 """Maximum time in milliseconds required to complete a measurement in normal mode"""
282309 meas_time_ms = 1.25
283310 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 )
285312 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
287314 return meas_time_ms
288315
289316 @property
@@ -328,14 +355,14 @@ def pressure(self):
328355 def altitude (self ):
329356 """The altitude based on the sea level pressure (`sea_level_pressure`) - which you must
330357 enter ahead of time)"""
331- p = self .pressure # in Si units for hPascal
358+ p = self .pressure # in Si units for hPascal
332359 return 44330 * (1.0 - math .pow (p / self .sea_level_pressure , 0.1903 ))
333360
334361 ####################### Internal helpers ################################
335362 def _read_coefficients (self ):
336363 """Read & save the calibration coefficients"""
337364 coeff = self ._read_register (_REGISTER_DIG_T1 , 24 )
338- coeff = list (struct .unpack (' <HhhHhhhhhhhh' , bytes (coeff )))
365+ coeff = list (struct .unpack (" <HhhHhhhhhhhh" , bytes (coeff )))
339366 coeff = [float (i ) for i in coeff ]
340367 # The temp_calib lines up with DIG_T# registers.
341368 self ._temp_calib = coeff [:3 ]
@@ -368,11 +395,14 @@ def _write_register_byte(self, register, value):
368395 """Low level register writing, not implemented in base class"""
369396 raise NotImplementedError ()
370397
371- class Adafruit_BMP280_I2C (Adafruit_BMP280 ): # pylint: disable=invalid-name
398+
399+ class Adafruit_BMP280_I2C (Adafruit_BMP280 ): # pylint: disable=invalid-name
372400 """Driver for I2C connected BMP280. Default address is 0x77 but another address can be passed
373401 in as an argument"""
402+
374403 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+
376406 self ._i2c = i2c_device .I2CDevice (i2c , address )
377407 super ().__init__ ()
378408
@@ -382,20 +412,23 @@ def _read_register(self, register, length):
382412 i2c .write (bytes ([register & 0xFF ]))
383413 result = bytearray (length )
384414 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]))
386416 return result
387417
388418 def _write_register_byte (self , register , value ):
389419 """Low level register writing over I2C, writes one 8-bit value"""
390420 with self ._i2c as i2c :
391421 i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
392- #print("$%02X <= 0x%02X" % (register, value))
422+ # print("$%02X <= 0x%02X" % (register, value))
423+
393424
394425class Adafruit_BMP280_SPI (Adafruit_BMP280 ):
395426 """Driver for SPI connected BMP280. Default clock rate is 100000 but can be changed with
396427 'baudrate'"""
428+
397429 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+
399432 self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
400433 super ().__init__ ()
401434
@@ -407,7 +440,7 @@ def _read_register(self, register, length):
407440 spi .write (bytearray ([register ]))
408441 result = bytearray (length )
409442 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]))
411444 return result
412445
413446 def _write_register_byte (self , register , value ):
0 commit comments