4343"""
4444
4545import time
46+
4647try :
4748 import struct
4849except ImportError :
5556
5657_CHIP_ID = const (0x50 )
5758
58- # pylint: disable=bad-whitespace
59- _REGISTER_CHIPID = const (0x00 )
60- _REGISTER_STATUS = const (0x03 )
61- _REGISTER_PRESSUREDATA = const (0x04 )
62- _REGISTER_TEMPDATA = const (0x07 )
63- _REGISTER_CONTROL = const (0x1B )
64- _REGISTER_OSR = const (0x1C )
65- _REGISTER_ODR = const (0x1D )
66- _REGISTER_CONFIG = const (0x1F )
67- _REGISTER_CAL_DATA = const (0x31 )
68- _REGISTER_CMD = const (0x7E )
59+ # pylint: disable=import-outside-toplevel
60+ _REGISTER_CHIPID = const (0x00 )
61+ _REGISTER_STATUS = const (0x03 )
62+ _REGISTER_PRESSUREDATA = const (0x04 )
63+ _REGISTER_TEMPDATA = const (0x07 )
64+ _REGISTER_CONTROL = const (0x1B )
65+ _REGISTER_OSR = const (0x1C )
66+ _REGISTER_ODR = const (0x1D )
67+ _REGISTER_CONFIG = const (0x1F )
68+ _REGISTER_CAL_DATA = const (0x31 )
69+ _REGISTER_CMD = const (0x7E )
6970# pylint: enable=bad-whitespace
7071
71- _OSR_SETTINGS = (1 , 2 , 4 , 8 , 16 , 32 ) # pressure and temperature oversampling settings
72- _IIR_SETTINGS = (0 , 2 , 4 , 8 , 16 , 32 , 64 , 128 ) # IIR filter coefficients
72+ _OSR_SETTINGS = (1 , 2 , 4 , 8 , 16 , 32 ) # pressure and temperature oversampling settings
73+ _IIR_SETTINGS = (0 , 2 , 4 , 8 , 16 , 32 , 64 , 128 ) # IIR filter coefficients
74+
7375
7476class BMP3XX :
7577 """Base class for BMP3XX sensor."""
7678
7779 def __init__ (self ):
7880 chip_id = self ._read_byte (_REGISTER_CHIPID )
7981 if _CHIP_ID != chip_id :
80- raise RuntimeError (' Failed to find BMP3XX! Chip ID 0x%x' % chip_id )
82+ raise RuntimeError (" Failed to find BMP3XX! Chip ID 0x%x" % chip_id )
8183 self ._read_coefficients ()
8284 self .reset ()
8385 self .sea_level_pressure = 1013.25
@@ -97,7 +99,7 @@ def temperature(self):
9799 def altitude (self ):
98100 """The altitude in meters based on the currently set sea level pressure."""
99101 # see https://www.weather.gov/media/epz/wxcalc/pressureAltitude.pdf
100- return 44307.7 * (1 - (self .pressure / self .sea_level_pressure )** 0.190284 )
102+ return 44307.7 * (1 - (self .pressure / self .sea_level_pressure ) ** 0.190284 )
101103
102104 @property
103105 def pressure_oversampling (self ):
@@ -108,7 +110,9 @@ def pressure_oversampling(self):
108110 def pressure_oversampling (self , oversample ):
109111 if oversample not in _OSR_SETTINGS :
110112 raise ValueError ("Oversampling must be one of: {}" .format (_OSR_SETTINGS ))
111- new_setting = self ._read_byte (_REGISTER_OSR ) & 0xF8 | _OSR_SETTINGS .index (oversample )
113+ new_setting = self ._read_byte (_REGISTER_OSR ) & 0xF8 | _OSR_SETTINGS .index (
114+ oversample
115+ )
112116 self ._write_register_byte (_REGISTER_OSR , new_setting )
113117
114118 @property
@@ -120,7 +124,9 @@ def temperature_oversampling(self):
120124 def temperature_oversampling (self , oversample ):
121125 if oversample not in _OSR_SETTINGS :
122126 raise ValueError ("Oversampling must be one of: {}" .format (_OSR_SETTINGS ))
123- new_setting = self ._read_byte (_REGISTER_OSR ) & 0xC7 | _OSR_SETTINGS .index (oversample ) << 3
127+ new_setting = (
128+ self ._read_byte (_REGISTER_OSR ) & 0xC7 | _OSR_SETTINGS .index (oversample ) << 3
129+ )
124130 self ._write_register_byte (_REGISTER_OSR , new_setting )
125131
126132 @property
@@ -131,7 +137,9 @@ def filter_coefficient(self):
131137 @filter_coefficient .setter
132138 def filter_coefficient (self , coef ):
133139 if coef not in _IIR_SETTINGS :
134- raise ValueError ("Filter coefficient must be one of: {}" .format (_IIR_SETTINGS ))
140+ raise ValueError (
141+ "Filter coefficient must be one of: {}" .format (_IIR_SETTINGS )
142+ )
135143 self ._write_register_byte (_REGISTER_CONFIG , _IIR_SETTINGS .index (coef ) << 1 )
136144
137145 def reset (self ):
@@ -169,19 +177,19 @@ def _read(self):
169177 P1 , P2 , P3 , P4 , P5 , P6 , P7 , P8 , P9 , P10 , P11 = self ._pressure_calib
170178
171179 pd1 = P6 * temperature
172- pd2 = P7 * temperature ** 2.
173- pd3 = P8 * temperature ** 3.
180+ pd2 = P7 * temperature ** 2.0
181+ pd3 = P8 * temperature ** 3.0
174182 po1 = P5 + pd1 + pd2 + pd3
175183
176184 pd1 = P2 * temperature
177- pd2 = P3 * temperature ** 2.
178- pd3 = P4 * temperature ** 3.
185+ pd2 = P3 * temperature ** 2.0
186+ pd3 = P4 * temperature ** 3.0
179187 po2 = adc_p * (P1 + pd1 + pd2 + pd3 )
180188
181- pd1 = adc_p ** 2.
189+ pd1 = adc_p ** 2.0
182190 pd2 = P9 + P10 * temperature
183191 pd3 = pd1 * pd2
184- pd4 = pd3 + P11 * adc_p ** 3.
192+ pd4 = pd3 + P11 * adc_p ** 3.0
185193
186194 pressure = po1 + po2 + pd4
187195
@@ -197,20 +205,24 @@ def _read_coefficients(self):
197205 # See datasheet, sec 9.1
198206 # Note: forcing float math to prevent issues with boards that
199207 # do not support long ints for 2**<large int>
200- self ._temp_calib = ( coeff [0 ] / 2 ** - 8. , # T1
201- coeff [1 ] / 2 ** 30. , # T2
202- coeff [2 ] / 2 ** 48. ) # T3
203- self ._pressure_calib = ( (coeff [3 ]- 2 ** 14. ) / 2 ** 20. , # P1
204- (coeff [4 ]- 2 ** 14. ) / 2 ** 29. , # P2
205- coeff [5 ] / 2 ** 32. , # P3
206- coeff [6 ] / 2 ** 37. , # P4
207- coeff [7 ] / 2 ** - 3. , # P5
208- coeff [8 ] / 2 ** 6. , # P6
209- coeff [9 ] / 2 ** 8. , # P7
210- coeff [10 ] / 2 ** 15. , # P8
211- coeff [11 ] / 2 ** 48. , # P9
212- coeff [12 ] / 2 ** 48. , # P10
213- coeff [13 ] / 2 ** 65. ) # P11
208+ self ._temp_calib = (
209+ coeff [0 ] / 2 ** - 8.0 , # T1
210+ coeff [1 ] / 2 ** 30.0 , # T2
211+ coeff [2 ] / 2 ** 48.0 ,
212+ ) # T3
213+ self ._pressure_calib = (
214+ (coeff [3 ] - 2 ** 14.0 ) / 2 ** 20.0 , # P1
215+ (coeff [4 ] - 2 ** 14.0 ) / 2 ** 29.0 , # P2
216+ coeff [5 ] / 2 ** 32.0 , # P3
217+ coeff [6 ] / 2 ** 37.0 , # P4
218+ coeff [7 ] / 2 ** - 3.0 , # P5
219+ coeff [8 ] / 2 ** 6.0 , # P6
220+ coeff [9 ] / 2 ** 8.0 , # P7
221+ coeff [10 ] / 2 ** 15.0 , # P8
222+ coeff [11 ] / 2 ** 48.0 , # P9
223+ coeff [12 ] / 2 ** 48.0 , # P10
224+ coeff [13 ] / 2 ** 65.0 ,
225+ ) # P11
214226
215227 def _read_byte (self , register ):
216228 """Read a byte register value and return it"""
@@ -224,11 +236,14 @@ def _write_register_byte(self, register, value):
224236 """Low level register writing, not implemented in base class"""
225237 raise NotImplementedError ()
226238
239+
227240class BMP3XX_I2C (BMP3XX ):
228241 """Driver for I2C connected BMP3XX. Default address is 0x77 but another address can be passed
229242 in as an argument"""
243+
230244 def __init__ (self , i2c , address = 0x77 ):
231245 import adafruit_bus_device .i2c_device as i2c_device
246+
232247 self ._i2c = i2c_device .I2CDevice (i2c , address )
233248 super ().__init__ ()
234249
@@ -245,10 +260,13 @@ def _write_register_byte(self, register, value):
245260 with self ._i2c as i2c :
246261 i2c .write (bytes ((register & 0xFF , value & 0xFF )))
247262
263+
248264class BMP3XX_SPI (BMP3XX ):
249265 """Driver for SPI connected BMP3XX."""
266+
250267 def __init__ (self , spi , cs ):
251268 import adafruit_bus_device .spi_device as spi_device
269+
252270 self ._spi = spi_device .SPIDevice (spi , cs )
253271 # toggle CS low/high to put BMP3XX in SPI mode
254272 with self ._spi :
0 commit comments