Skip to content

feat(bme280): Add compensated temperature, pressure, humidity readings#312

Merged
nedseb merged 5 commits intomainfrom
feat/bme280-readings
Mar 29, 2026
Merged

feat(bme280): Add compensated temperature, pressure, humidity readings#312
nedseb merged 5 commits intomainfrom
feat/bme280-readings

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 29, 2026

Closes #305

Summary

  • Status methods: _status(), data_ready(), temperature_ready(), pressure_ready(), humidity_ready() — all based on STATUS_MEASURING bit
  • Forced measurement: trigger_one_shot() sets forced mode in ctrl_meas, _wait_measurement() polls until complete
  • Raw data: _read_raw() burst-reads 8 bytes from 0xF7–0xFE, returns (raw_temp, raw_press, raw_hum)
  • Compensation formulas: integer arithmetic from BME280 datasheet section 4.2.3
    • _compensate_temperature() — hundredths of °C, updates t_fine
    • _compensate_pressure() — Pa in Q24.8 fixed point
    • _compensate_humidity() — Q22.10 fixed point with clamping
  • Public API: temperature() (°C), pressure_hpa() (hPa), humidity() (%RH), read() (tuple), read_one_shot()
  • 9 new mock tests verifying status, compensated values against reference calculations, t_fine update, trigger behavior

Test plan

Copilot AI review requested due to automatic review settings March 29, 2026 10:18
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds full measurement support to the BME280 driver, including compensated temperature/pressure/humidity readings derived from factory calibration, plus scenario-based mock validation.

Changes:

  • Implemented status helpers and one-shot triggering with measurement wait/polling.
  • Added burst raw-data read and integer compensation formulas (temperature updates t_fine).
  • Extended the BME280 YAML scenario with additional mock registers and measurement/status tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
lib/bme280/bme280/device.py Adds status APIs, one-shot trigger/wait, raw burst read, and compensated measurement methods.
tests/scenarios/bme280.yaml Adds mock data registers and scenario tests for status, compensated outputs, and one-shot triggering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/bme280/bme280/device.py Outdated
Comment on lines +234 to +235
if h == 0:
return 0
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_compensate_humidity() returns 0 when self.t_fine - 76800 == 0, but the BME280 integer humidity compensation formula does not have a divide-by-zero risk at that point. This early-return will produce an incorrect 0%RH reading at the (valid) temperature where t_fine == 76800. Remove this check and let the formula run (clamping at the end already bounds the result).

Suggested change
if h == 0:
return 0

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 343dab9: removed the if h == 0: return 0 early return. The clamping at the end (0 to 419430400) already bounds the result, and this check would incorrectly return 0%RH at t_fine=76800.

Comment thread lib/bme280/bme280/device.py Outdated
Comment on lines +192 to +200
"""Read raw ADC values for pressure, temperature, humidity (burst read).

Returns (raw_temp, raw_press, raw_hum) as 20-bit/20-bit/16-bit integers.
"""
data = self._read_block(REG_DATA_START, DATA_BLOCK_SIZE)
raw_press = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
raw_temp = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
raw_hum = (data[6] << 8) | data[7]
return raw_temp, raw_press, raw_hum
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _read_raw() docstring says it reads/returns values in the order "pressure, temperature, humidity", but the function actually returns (raw_temp, raw_press, raw_hum). This mismatch makes it easy to misuse the tuple. Update the docstring (and/or return order) so the documented order matches the actual return value.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 343dab9: removed the misleading "pressure, temperature, humidity" ordering from the first line. The docstring now says "burst read (0xF7-0xFE, 8 bytes)" and the Returns line clearly states (raw_temp, raw_press, raw_hum).

Comment thread tests/scenarios/bme280.yaml Outdated
Comment on lines +174 to +178
- name: "temperature_ready delegates to data_ready"
action: script
script: |
result = dev.temperature_ready() and dev.pressure_ready() and dev.humidity_ready()
expect_true: true
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test name doesn't match what is asserted: the script checks temperature_ready(), pressure_ready(), and humidity_ready(), but the name only mentions temperature_ready. Renaming the test to reflect all three methods will make failures easier to interpret.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 343dab9: renamed test to "All ready methods delegate to data_ready" to reflect that it checks temperature_ready(), pressure_ready(), and humidity_ready().

@nedseb nedseb merged commit f18aeaf into main Mar 29, 2026
9 checks passed
@nedseb nedseb deleted the feat/bme280-readings branch March 29, 2026 10:35
semantic-release-updater Bot pushed a commit that referenced this pull request Mar 29, 2026
# [0.5.0](v0.4.0...v0.5.0) (2026-03-29)

### Features

* **bme280:** Add compensated temperature, pressure, humidity readings ([#312](#312)) ([f18aeaf](f18aeaf))
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bme280: Implement temperature, pressure, and humidity readings.

2 participants