28
28
* Author(s): ladyada
29
29
"""
30
30
import time , math
31
+ from micropython import const
31
32
try :
32
33
import struct
33
34
except ImportError :
34
35
import ustruct as struct
35
36
36
37
# I2C ADDRESS/BITS/SETTINGS
37
38
# -----------------------------------------------------------------------
38
- BMP280_ADDRESS = const (0x77 )
39
- BMP280_CHIPID = const (0x58 )
39
+ _BMP280_ADDRESS = const (0x77 )
40
+ _BMP280_CHIPID = const (0x58 )
40
41
41
- BMP280_REGISTER_CHIPID = const (0xD0 )
42
- BMP280_REGISTER_DIG_T1 = const (0x88 )
42
+ _BMP280_REGISTER_CHIPID = const (0xD0 )
43
+ _BMP280_REGISTER_DIG_T1 = const (0x88 )
43
44
"""
44
45
BMP280_REGISTER_DIG_T2 = const(0x8A)
45
46
BMP280_REGISTER_DIG_T3 = const(0x8C)
53
54
BMP280_REGISTER_DIG_P8 = const(0x9C)
54
55
BMP280_REGISTER_DIG_P9 = const(0x9E)
55
56
"""
56
- BMP280_REGISTER_SOFTRESET = const (0xE0 )
57
- BMP280_REGISTER_STATUS = const (0xF3 )
58
- BMP280_REGISTER_CONTROL = const (0xF4 )
59
- BMP280_REGISTER_CONFIG = const (0xF5 )
60
- BMP280_REGISTER_PRESSUREDATA = const (0xF7 )
61
- BMP280_REGISTER_TEMPDATA = const (0xFA )
57
+ _BMP280_REGISTER_SOFTRESET = const (0xE0 )
58
+ _BMP280_REGISTER_STATUS = const (0xF3 )
59
+ _BMP280_REGISTER_CONTROL = const (0xF4 )
60
+ _BMP280_REGISTER_CONFIG = const (0xF5 )
61
+ _BMP280_REGISTER_PRESSUREDATA = const (0xF7 )
62
+ _BMP280_REGISTER_TEMPDATA = const (0xFA )
62
63
63
64
class Adafruit_BMP280 :
64
65
def __init__ (self ):
65
66
"""Check the BMP280 was found, read the coefficients and enable the sensor for continuous reads"""
66
67
# Check device ID.
67
68
id = self ._read_byte (BMP280_REGISTER_CHIPID )
68
- if BMP280_CHIPID != id :
69
+ if _BMP280_CHIPID != id :
69
70
raise RuntimeError ('Failed to find BMP280! Chip ID 0x%x' % id )
70
71
self ._read_coefficients ()
71
72
self .seaLevelhPa = 1013.25
72
73
73
74
@property
74
75
def temperature (self ):
75
- """Gets the compensated temperature in degrees celsius."""
76
- # perform one measurement
77
- self ._write_register_byte (BMP280_REGISTER_CONTROL , 0xFE ); # high res, forced mode
76
+ """The compensated temperature in degrees celsius."""
77
+ # perform one measurement in high res, forced mode
78
+ self ._write_register_byte (_BMP280_REGISTER_CONTROL , 0xFE )
78
79
79
80
# Wait for conversion to complete
80
- while (self ._read_byte (BMP280_REGISTER_STATUS ) & 0x08 ):
81
+ while (self ._read_byte (_BMP280_REGISTER_STATUS ) & 0x08 ):
81
82
time .sleep (0.002 )
82
- UT = self ._read24 (BMP280_REGISTER_TEMPDATA ) / 16 # lowest 4 bits get dropped
83
+ # lowest 4 bits get dropped
84
+ UT = self ._read24 (_BMP280_REGISTER_TEMPDATA ) / 16
83
85
#print("raw temp: ", UT)
84
86
85
87
var1 = (UT / 16384.0 - self .dig_T1 / 1024.0 ) * self .dig_T2
@@ -93,10 +95,11 @@ def temperature(self):
93
95
94
96
@property
95
97
def pressure (self ):
96
- """Gets the compensated pressure in hectoPascals."""
97
- self .temperature # force read
98
-
99
- adc = self ._read24 (BMP280_REGISTER_PRESSUREDATA ) / 16 # lowest 4 bits get dropped
98
+ """The compensated pressure in hectoPascals."""
99
+ self .temperature # force read (gets temp & pressure)
100
+
101
+ # lowest 4 bits get dropped
102
+ adc = self ._read24 (_BMP280_REGISTER_PRESSUREDATA ) / 16
100
103
var1 = float (self .t_fine ) / 2.0 - 64000.0
101
104
var2 = var1 * var1 * self .dig_P6 / 32768.0
102
105
var2 = var2 + var1 * self .dig_P5 * 2.0
@@ -114,10 +117,11 @@ def pressure(self):
114
117
115
118
@property
116
119
def altitude (self ):
117
- """calculate the altitude based on the sea level pressure (seaLevelhPa) - which you must enter ahead of time)"""
120
+ """The altitude based on the sea level pressure (seaLevelhPa) - which you must enter ahead of time)"""
118
121
p = self .pressure # in Si units for hPascal
119
122
return 44330 * (1.0 - math .pow (p / self .seaLevelhPa , 0.1903 ));
120
123
124
+ ####################### Internal helpers ################################
121
125
def _read_coefficients (self ):
122
126
"""Read & save the calibration coefficients"""
123
127
coeff = self ._read_register (BMP280_REGISTER_DIG_T1 , 24 )
@@ -143,11 +147,13 @@ def _read24(self, register):
143
147
144
148
class Adafruit_BMP280_I2C (Adafruit_BMP280 ):
145
149
def __init__ (self , i2c , address = BMP280_ADDRESS ):
150
+ """Check the BMP280 was found, read the coefficients and enable the sensor for continuous reads. Default address is 0x77 but another address can be passed in as an argument"""
146
151
import adafruit_bus_device .i2c_device as i2c_device
147
152
self ._i2c = i2c_device .I2CDevice (i2c , address )
148
153
super ().__init__ ()
149
154
150
155
def _read_register (self , register , length ):
156
+ """Low level register reading over I2C, returns a list of values"""
151
157
with self ._i2c as i2c :
152
158
i2c .write (bytes ([register & 0xFF ]))
153
159
result = bytearray (length )
@@ -156,17 +162,20 @@ def _read_register(self, register, length):
156
162
return result
157
163
158
164
def _write_register_byte (self , register , value ):
165
+ """Low level register writing over I2C, writes one 8-bit value"""
159
166
with self ._i2c as i2c :
160
167
i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
161
168
#print("$%02X <= 0x%02X" % (register, value))
162
169
163
170
class Adafruit_BMP280_SPI (Adafruit_BMP280 ):
164
171
def __init__ (self , spi , cs , baudrate = 100000 ):
172
+ """Check the BMP280 was found, read the coefficients and enable the sensor for continuous reads. Default clock rate is 100000 but can be changed with 'baudrate'"""
165
173
import adafruit_bus_device .spi_device as spi_device
166
174
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
167
175
super ().__init__ ()
168
176
169
177
def _read_register (self , register , length ):
178
+ """Low level register reading over SPI, returns a list of values"""
170
179
register = (register | 0x80 ) & 0xFF # Read single, bit 7 high.
171
180
with self ._spi as spi :
172
181
spi .write (bytearray ([register ]))
@@ -176,6 +185,7 @@ def _read_register(self, register, length):
176
185
return result
177
186
178
187
def _write_register_byte (self , register , value ):
188
+ """Low level register writing over SPI, writes one 8-bit value"""
179
189
register &= 0x7F # Write, bit 7 low.
180
190
with self ._spi as spi :
181
191
spi .write (bytes ([register , value & 0xFF ]))
0 commit comments