diff --git a/bme280.py b/bme280.py index 642468a..dc2374f 100644 --- a/bme280.py +++ b/bme280.py @@ -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) diff --git a/tests/test_bme280.py b/tests/test_bme280.py index 6ec2bc9..cc7ce23 100644 --- a/tests/test_bme280.py +++ b/tests/test_bme280.py @@ -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