23
23
`adafruit_bmp280`
24
24
====================================================
25
25
26
- CircuitPython driver from BMP280 sensor
26
+ CircuitPython driver from BMP280 Temperature and Barometic Pressure sensor
27
27
28
28
* Author(s): ladyada
29
29
"""
40
40
41
41
BMP280_REGISTER_CHIPID = const (0xD0 )
42
42
BMP280_REGISTER_DIG_T1 = const (0x88 )
43
+ """
43
44
BMP280_REGISTER_DIG_T2 = const(0x8A)
44
45
BMP280_REGISTER_DIG_T3 = const(0x8C)
45
46
BMP280_REGISTER_DIG_P1 = const(0x8E)
51
52
BMP280_REGISTER_DIG_P7 = const(0x9A)
52
53
BMP280_REGISTER_DIG_P8 = const(0x9C)
53
54
BMP280_REGISTER_DIG_P9 = const(0x9E)
54
- BMP280_REGISTER_VERSION = const ( 0xD1 )
55
+ """
55
56
BMP280_REGISTER_SOFTRESET = const (0xE0 )
56
- BMP280_REGISTER_CAL26 = const (0xE1 ) # R calibration stored in 0xE1-0xF0
57
57
BMP280_REGISTER_STATUS = const (0xF3 )
58
58
BMP280_REGISTER_CONTROL = const (0xF4 )
59
59
BMP280_REGISTER_CONFIG = const (0xF5 )
62
62
63
63
class Adafruit_BMP280 :
64
64
def __init__ (self ):
65
+ """Check the BMP280 was found, read the coefficients and enable the sensor for continuous reads"""
65
66
# Check device ID.
66
- if BMP280_CHIPID != self ._read_byte (BMP280_REGISTER_CHIPID ):
67
- raise RuntimeError ('Failed to find BMP280!' )
67
+ id = self ._read_byte (BMP280_REGISTER_CHIPID )
68
+ if BMP280_CHIPID != id :
69
+ raise RuntimeError ('Failed to find BMP280! Chip ID 0x%x' % id )
68
70
self ._read_coefficients ()
69
- self ._write_byte (BMP280_REGISTER_CONTROL , 0x3F );
70
71
self .seaLevelhPa = 1013.25
71
72
72
73
@property
73
74
def temperature (self ):
74
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
78
+
75
79
# Wait for conversion to complete
76
80
while (self ._read_byte (BMP280_REGISTER_STATUS ) & 0x08 ):
77
81
time .sleep (0.002 )
@@ -89,7 +93,7 @@ def temperature(self):
89
93
90
94
@property
91
95
def pressure (self ):
92
- """Gets the compensated pressure in Pascals ."""
96
+ """Gets the compensated pressure in hectoPascals ."""
93
97
self .temperature # force read
94
98
95
99
adc = self ._read24 (BMP280_REGISTER_PRESSUREDATA ) / 16 # lowest 4 bits get dropped
@@ -106,10 +110,11 @@ def pressure(self):
106
110
var1 = self .dig_P9 * p * p / 2147483648.0
107
111
var2 = p * self .dig_P8 / 32768.0
108
112
p = p + (var1 + var2 + self .dig_P7 ) / 16.0
109
- return p
113
+ return p / 100
110
114
111
115
@property
112
116
def altitude (self ):
117
+ """calculate the altitude based on the sea level pressure (seaLevelPa) - which you must enter ahead of time)"""
113
118
p = self .pressure / 100.0 # in Si units for Pascal
114
119
return 44330 * (1.0 - math .pow (p / self .seaLevelhPa , 0.1903 ));
115
120
@@ -123,17 +128,13 @@ def _read_coefficients(self):
123
128
#print("%d %d %d" % (self.dig_P1, self.dig_P2, self.dig_P3))
124
129
#print("%d %d %d" % (self.dig_P4, self.dig_P5, self.dig_P6))
125
130
#print("%d %d %d" % (self.dig_P7, self.dig_P8, self.dig_P9))
126
-
127
- def _write_byte (self , register , value ):
128
- # Write a byte register value
129
- return self ._write_register_byte (register , value )
130
131
131
132
def _read_byte (self , register ):
132
- # Read a byte register value and return it.
133
+ """ Read a byte register value and return it"""
133
134
return self ._read_register (register , 1 )[0 ]
134
135
135
136
def _read24 (self , register ):
136
- # Read an unsigned 24-bit value as a floating point and return it.
137
+ """ Read an unsigned 24-bit value as a floating point and return it."""
137
138
ret = 0.0
138
139
for b in self ._read_register (register , 3 ):
139
140
ret *= 256.0
@@ -144,7 +145,6 @@ class Adafruit_BMP280_I2C(Adafruit_BMP280):
144
145
def __init__ (self , i2c , address = BMP280_ADDRESS ):
145
146
import adafruit_bus_device .i2c_device as i2c_device
146
147
self ._i2c = i2c_device .I2CDevice (i2c , address )
147
- self ._buffer = bytearray (3 )
148
148
super ().__init__ ()
149
149
150
150
def _read_register (self , register , length ):
@@ -159,3 +159,23 @@ def _write_register_byte(self, register, value):
159
159
with self ._i2c as i2c :
160
160
i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
161
161
#print("$%02X <= 0x%02X" % (register, value))
162
+
163
+ class Adafruit_BMP280_SPI (Adafruit_BMP280 ):
164
+ def __init__ (self , spi , cs , baudrate = 100000 ):
165
+ import adafruit_bus_device .spi_device as spi_device
166
+ self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
167
+ super ().__init__ ()
168
+
169
+ def _read_register (self , register , length ):
170
+ register = (register | 0x80 ) & 0xFF # Read single, bit 7 high.
171
+ with self ._spi as spi :
172
+ spi .write (bytearray ([register ]))
173
+ result = bytearray (length )
174
+ spi .readinto (result )
175
+ #print("$%02X => %s" % (register, [hex(i) for i in result]))
176
+ return result
177
+
178
+ def _write_register_byte (self , register , value ):
179
+ register &= 0x7F # Write, bit 7 low.
180
+ with self ._spi as spi :
181
+ spi .write (bytes ([register , value & 0xFF ]))
0 commit comments