43
43
"""
44
44
45
45
import time
46
+
46
47
try :
47
48
import struct
48
49
except ImportError :
55
56
56
57
_CHIP_ID = const (0x50 )
57
58
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 )
69
70
# pylint: enable=bad-whitespace
70
71
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
+
73
75
74
76
class BMP3XX :
75
77
"""Base class for BMP3XX sensor."""
76
78
77
79
def __init__ (self ):
78
80
chip_id = self ._read_byte (_REGISTER_CHIPID )
79
81
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 )
81
83
self ._read_coefficients ()
82
84
self .reset ()
83
85
self .sea_level_pressure = 1013.25
@@ -97,7 +99,7 @@ def temperature(self):
97
99
def altitude (self ):
98
100
"""The altitude in meters based on the currently set sea level pressure."""
99
101
# 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 )
101
103
102
104
@property
103
105
def pressure_oversampling (self ):
@@ -108,7 +110,9 @@ def pressure_oversampling(self):
108
110
def pressure_oversampling (self , oversample ):
109
111
if oversample not in _OSR_SETTINGS :
110
112
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
+ )
112
116
self ._write_register_byte (_REGISTER_OSR , new_setting )
113
117
114
118
@property
@@ -120,7 +124,9 @@ def temperature_oversampling(self):
120
124
def temperature_oversampling (self , oversample ):
121
125
if oversample not in _OSR_SETTINGS :
122
126
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
+ )
124
130
self ._write_register_byte (_REGISTER_OSR , new_setting )
125
131
126
132
@property
@@ -131,7 +137,9 @@ def filter_coefficient(self):
131
137
@filter_coefficient .setter
132
138
def filter_coefficient (self , coef ):
133
139
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
+ )
135
143
self ._write_register_byte (_REGISTER_CONFIG , _IIR_SETTINGS .index (coef ) << 1 )
136
144
137
145
def reset (self ):
@@ -169,19 +177,19 @@ def _read(self):
169
177
P1 , P2 , P3 , P4 , P5 , P6 , P7 , P8 , P9 , P10 , P11 = self ._pressure_calib
170
178
171
179
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
174
182
po1 = P5 + pd1 + pd2 + pd3
175
183
176
184
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
179
187
po2 = adc_p * (P1 + pd1 + pd2 + pd3 )
180
188
181
- pd1 = adc_p ** 2.
189
+ pd1 = adc_p ** 2.0
182
190
pd2 = P9 + P10 * temperature
183
191
pd3 = pd1 * pd2
184
- pd4 = pd3 + P11 * adc_p ** 3.
192
+ pd4 = pd3 + P11 * adc_p ** 3.0
185
193
186
194
pressure = po1 + po2 + pd4
187
195
@@ -197,20 +205,24 @@ def _read_coefficients(self):
197
205
# See datasheet, sec 9.1
198
206
# Note: forcing float math to prevent issues with boards that
199
207
# 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
214
226
215
227
def _read_byte (self , register ):
216
228
"""Read a byte register value and return it"""
@@ -224,11 +236,14 @@ def _write_register_byte(self, register, value):
224
236
"""Low level register writing, not implemented in base class"""
225
237
raise NotImplementedError ()
226
238
239
+
227
240
class BMP3XX_I2C (BMP3XX ):
228
241
"""Driver for I2C connected BMP3XX. Default address is 0x77 but another address can be passed
229
242
in as an argument"""
243
+
230
244
def __init__ (self , i2c , address = 0x77 ):
231
245
import adafruit_bus_device .i2c_device as i2c_device
246
+
232
247
self ._i2c = i2c_device .I2CDevice (i2c , address )
233
248
super ().__init__ ()
234
249
@@ -245,10 +260,13 @@ def _write_register_byte(self, register, value):
245
260
with self ._i2c as i2c :
246
261
i2c .write (bytes ((register & 0xFF , value & 0xFF )))
247
262
263
+
248
264
class BMP3XX_SPI (BMP3XX ):
249
265
"""Driver for SPI connected BMP3XX."""
266
+
250
267
def __init__ (self , spi , cs ):
251
268
import adafruit_bus_device .spi_device as spi_device
269
+
252
270
self ._spi = spi_device .SPIDevice (spi , cs )
253
271
# toggle CS low/high to put BMP3XX in SPI mode
254
272
with self ._spi :
0 commit comments