Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ def read_all(addr: int = DEVICE_ADDRESS) -> tuple[float, float, float]:
cal2 = bus.read_i2c_block_data(addr, 0xA1, 1)
cal3 = bus.read_i2c_block_data(addr, 0xE1, 7)

# Datasheet Appendix B: measurement time formula
# Datasheet Appendix B: minimum wait before first status check
wait_ms = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM) + 0.575)
time.sleep(wait_ms / 1000)

# Poll measuring bit (0xF3 bit 3) until measurement is complete
for _ in range(20):
if not (bus.read_byte_data(addr, 0xF3) & 0x08):
break
time.sleep(0.001)

data = bus.read_i2c_block_data(addr, 0xF7, 8)

dig_T1 = _get_ushort(cal1, 0)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_bme280.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,22 @@ def test_nvm_copy_timeout_raises_oserror():
import bme280
with pytest.raises(OSError, match="NVM copy"):
bme280.read_all()


def test_read_all_polls_measuring_bit():
bus = MagicMock()
# First call: NVM done (bit 0 = 0), then measuring active (bit 3 = 1), then done (0)
bus.read_byte_data.side_effect = [0x00, 0x08, 0x00]
bus.read_i2c_block_data.side_effect = [
[0] * 24,
[0],
[0] * 7,
[0] * 8,
]
with patch('bme280.smbus2.SMBus') as MockSMBus:
instance = MockSMBus.return_value
instance.__enter__ = MagicMock(return_value=bus)
instance.__exit__ = MagicMock(return_value=False)
import bme280
temperature, pressure, humidity = bme280.read_all()
assert bus.read_byte_data.call_count == 3
Loading