Skip to content

feat(bme280): Add measurement modes and sensor configuration#313

Merged
nedseb merged 2 commits intomainfrom
feat/bme280-modes
Mar 29, 2026
Merged

feat(bme280): Add measurement modes and sensor configuration#313
nedseb merged 2 commits intomainfrom
feat/bme280-modes

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 29, 2026

Closes #306

Summary

  • Power control: power_off() (sleep mode), power_on() (normal mode)
  • Continuous mode: set_continuous(standby=None) — enters normal mode with optional standby time
  • Oversampling: set_oversampling(temperature=, pressure=, humidity=) — per-channel config via OSRS_SKIP..OSRS_X16
  • IIR filter: set_iir_filter(coefficient)FILTER_OFF..FILTER_16
  • Standby time: set_standby(standby)STANDBY_0_5_MS..STANDBY_1000_MS
  • All configuration methods use read-modify-write to preserve unrelated register bits
  • 10 new mock tests covering mode transitions, oversampling config, filter/standby config, and bit preservation

Note: trigger_one_shot() and read_one_shot() were already implemented in #312 (issue #305).

Test plan

Copilot AI review requested due to automatic review settings March 29, 2026 10:41
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 configuration/mode-control APIs to the BME280 driver to support sleep/normal operation and tuning measurement behavior (oversampling, IIR filter, standby), with accompanying mock scenario coverage.

Changes:

  • Add power/mode helpers: power_off(), power_on(), and set_continuous(standby=None)
  • Add configuration helpers: set_oversampling(...), set_iir_filter(...), set_standby(...)
  • Extend tests/scenarios/bme280.yaml with mock scenarios for mode/config register writes and bit preservation

Reviewed changes

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

File Description
lib/bme280/bme280/device.py Introduces new public APIs for power/mode control and sensor configuration via register RMW.
tests/scenarios/bme280.yaml Adds mock scenarios asserting expected register writes for the new APIs.

💡 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 +183 to +185
if humidity is not None:
self._write_reg(REG_CTRL_HUM, humidity)
if temperature is not None or pressure is not None:
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.

set_oversampling() writes REG_CTRL_HUM when humidity is provided, but if only humidity is changed (temperature/pressure are None) it never writes REG_CTRL_MEAS. On BME280, ctrl_hum updates take effect only after a subsequent write to ctrl_meas (your own _configure_default() comment notes this), so humidity-only changes won't actually apply. Fix by performing a (read-modify-)write to REG_CTRL_MEAS after writing REG_CTRL_HUM even when temperature/pressure are unchanged (preserving the existing mode/oversampling bits).

Suggested change
if humidity is not None:
self._write_reg(REG_CTRL_HUM, humidity)
if temperature is not None or pressure is not None:
update_ctrl_meas = False
if humidity is not None:
# On BME280, changes to ctrl_hum take effect only after
# a subsequent write to ctrl_meas.
self._write_reg(REG_CTRL_HUM, humidity)
update_ctrl_meas = True
if temperature is not None or pressure is not None or update_ctrl_meas:

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.

Already fixed in f63099a: set_oversampling() now always rewrites ctrl_meas after ctrl_hum, even when only humidity is changed. The comment in the code references datasheet section 5.4.3.

Comment thread tests/scenarios/bme280.yaml Outdated
Comment on lines +366 to +367
hum_writes = [data[0] for reg, data in log if reg == 0xF2]
result = len(hum_writes) == 1 and hum_writes[0] == OSRS_X8
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.

This test asserts set_oversampling(humidity=...) only writes REG_CTRL_HUM (0xF2). However, BME280 latches ctrl_hum changes only after a write to REG_CTRL_MEAS (0xF4), so the correct behavior should include a REG_CTRL_MEAS write as well. Update the expectation to also require (at least) one write to 0xF4 following the 0xF2 write.

Suggested change
hum_writes = [data[0] for reg, data in log if reg == 0xF2]
result = len(hum_writes) == 1 and hum_writes[0] == OSRS_X8
hum_index = None
for idx, (reg, data) in enumerate(log):
if reg == 0xF2 and data[0] == OSRS_X8:
hum_index = idx
break
if hum_index is None:
result = False
else:
wrote_ctrl_after = any(reg == 0xF4 for reg, data in log[hum_index + 1:])
result = wrote_ctrl_after

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.

Already fixed in f63099a: the test now verifies that both ctrl_hum (0xF2) and ctrl_meas (0xF4) are written, and that 0xF2 comes before 0xF4 in the write log.

@nedseb nedseb merged commit 2af3fd9 into main Mar 29, 2026
9 checks passed
@nedseb nedseb deleted the feat/bme280-modes branch March 29, 2026 10:52
semantic-release-updater Bot pushed a commit that referenced this pull request Mar 29, 2026
# [0.6.0](v0.5.0...v0.6.0) (2026-03-29)

### Features

* **bme280:** Add measurement modes and sensor configuration ([#313](#313)) ([2af3fd9](2af3fd9))
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.6.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 measurement modes and configuration.

2 participants