Skip to content

Commit 060cb39

Browse files
committed
https://github.com/adafruit/Adafruit_CircuitPython_BMP280/issues/1
1 parent 15c1188 commit 060cb39

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

adafruit_bmp280.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@
2828
* Author(s): ladyada
2929
"""
3030
import time, math
31+
from micropython import const
3132
try:
3233
import struct
3334
except ImportError:
3435
import ustruct as struct
3536

3637
# I2C ADDRESS/BITS/SETTINGS
3738
# -----------------------------------------------------------------------
38-
BMP280_ADDRESS = const(0x77)
39-
BMP280_CHIPID = const(0x58)
39+
_BMP280_ADDRESS = const(0x77)
40+
_BMP280_CHIPID = const(0x58)
4041

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)
4344
"""
4445
BMP280_REGISTER_DIG_T2 = const(0x8A)
4546
BMP280_REGISTER_DIG_T3 = const(0x8C)
@@ -53,33 +54,34 @@
5354
BMP280_REGISTER_DIG_P8 = const(0x9C)
5455
BMP280_REGISTER_DIG_P9 = const(0x9E)
5556
"""
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)
6263

6364
class Adafruit_BMP280:
6465
def __init__(self):
6566
"""Check the BMP280 was found, read the coefficients and enable the sensor for continuous reads"""
6667
# Check device ID.
6768
id = self._read_byte(BMP280_REGISTER_CHIPID)
68-
if BMP280_CHIPID != id:
69+
if _BMP280_CHIPID != id:
6970
raise RuntimeError('Failed to find BMP280! Chip ID 0x%x' % id)
7071
self._read_coefficients()
7172
self.seaLevelhPa = 1013.25
7273

7374
@property
7475
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)
7879

7980
# Wait for conversion to complete
80-
while (self._read_byte(BMP280_REGISTER_STATUS) & 0x08):
81+
while (self._read_byte(_BMP280_REGISTER_STATUS) & 0x08):
8182
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
8385
#print("raw temp: ", UT)
8486

8587
var1 = (UT / 16384.0 - self.dig_T1 / 1024.0) * self.dig_T2
@@ -93,10 +95,11 @@ def temperature(self):
9395

9496
@property
9597
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
100103
var1 = float(self.t_fine) / 2.0 - 64000.0
101104
var2 = var1 * var1 * self.dig_P6 / 32768.0
102105
var2 = var2 + var1 * self.dig_P5 * 2.0
@@ -114,10 +117,11 @@ def pressure(self):
114117

115118
@property
116119
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)"""
118121
p = self.pressure # in Si units for hPascal
119122
return 44330 * (1.0 - math.pow(p / self.seaLevelhPa, 0.1903));
120123

124+
####################### Internal helpers ################################
121125
def _read_coefficients(self):
122126
"""Read & save the calibration coefficients"""
123127
coeff = self._read_register(BMP280_REGISTER_DIG_T1, 24)
@@ -143,11 +147,13 @@ def _read24(self, register):
143147

144148
class Adafruit_BMP280_I2C(Adafruit_BMP280):
145149
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"""
146151
import adafruit_bus_device.i2c_device as i2c_device
147152
self._i2c = i2c_device.I2CDevice(i2c, address)
148153
super().__init__()
149154

150155
def _read_register(self, register, length):
156+
"""Low level register reading over I2C, returns a list of values"""
151157
with self._i2c as i2c:
152158
i2c.write(bytes([register & 0xFF]))
153159
result = bytearray(length)
@@ -156,17 +162,20 @@ def _read_register(self, register, length):
156162
return result
157163

158164
def _write_register_byte(self, register, value):
165+
"""Low level register writing over I2C, writes one 8-bit value"""
159166
with self._i2c as i2c:
160167
i2c.write(bytes([register & 0xFF, value & 0xFF]))
161168
#print("$%02X <= 0x%02X" % (register, value))
162169

163170
class Adafruit_BMP280_SPI(Adafruit_BMP280):
164171
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'"""
165173
import adafruit_bus_device.spi_device as spi_device
166174
self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)
167175
super().__init__()
168176

169177
def _read_register(self, register, length):
178+
"""Low level register reading over SPI, returns a list of values"""
170179
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
171180
with self._spi as spi:
172181
spi.write(bytearray([register]))
@@ -176,6 +185,7 @@ def _read_register(self, register, length):
176185
return result
177186

178187
def _write_register_byte(self, register, value):
188+
"""Low level register writing over SPI, writes one 8-bit value"""
179189
register &= 0x7F # Write, bit 7 low.
180190
with self._spi as spi:
181191
spi.write(bytes([register, value & 0xFF]))

conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,6 @@
140140
author, 'AdafruitBMP280Library', 'One line description of project.',
141141
'Miscellaneous'),
142142
]
143+
144+
# API docs fix
145+
autodoc_mock_imports = ['micropython']

0 commit comments

Comments
 (0)