-
Notifications
You must be signed in to change notification settings - Fork 1
feat(bme280): Add measurement modes and sensor configuration #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -274,3 +274,135 @@ tests: | |||||||||||||||||||||||||
| result = triggered and abs(t - 25.08) < 0.1 | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # ----- Power and mode control ----- | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "power_off sets sleep mode" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.power_off() | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| wrote_sleep = any(reg == 0xF4 and (data[0] & 0x03) == 0x00 for reg, data in log) | ||||||||||||||||||||||||||
| result = wrote_sleep | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "power_on sets normal mode" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.power_on() | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| wrote_normal = any(reg == 0xF4 and (data[0] & 0x03) == 0x03 for reg, data in log) | ||||||||||||||||||||||||||
| result = wrote_normal | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "power_off preserves oversampling bits" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| from bme280.const import OSRS_X4, OSRS_T_SHIFT, OSRS_P_SHIFT | ||||||||||||||||||||||||||
| dev.set_oversampling(temperature=OSRS_X4, pressure=OSRS_X4) | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.power_off() | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| ctrl = [data[0] for reg, data in log if reg == 0xF4][-1] | ||||||||||||||||||||||||||
| osrs_t = (ctrl >> 5) & 0x07 | ||||||||||||||||||||||||||
| osrs_p = (ctrl >> 2) & 0x07 | ||||||||||||||||||||||||||
| result = osrs_t == OSRS_X4 and osrs_p == OSRS_X4 and (ctrl & 0x03) == 0x00 | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "set_continuous enters normal mode" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| dev.power_off() | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.set_continuous() | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| wrote_normal = any(reg == 0xF4 and (data[0] & 0x03) == 0x03 for reg, data in log) | ||||||||||||||||||||||||||
| result = wrote_normal | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "set_continuous with standby configures both" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| from bme280.const import STANDBY_500_MS, STANDBY_SHIFT | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.set_continuous(standby=STANDBY_500_MS) | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| wrote_config = any(reg == 0xF5 for reg, data in log) | ||||||||||||||||||||||||||
| wrote_normal = any(reg == 0xF4 and (data[0] & 0x03) == 0x03 for reg, data in log) | ||||||||||||||||||||||||||
| result = wrote_config and wrote_normal | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # ----- Configuration ----- | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "set_oversampling configures temperature and pressure" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| from bme280.const import OSRS_X2, OSRS_X4, OSRS_T_SHIFT, OSRS_P_SHIFT | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.set_oversampling(temperature=OSRS_X2, pressure=OSRS_X4) | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| ctrl_writes = [data[0] for reg, data in log if reg == 0xF4] | ||||||||||||||||||||||||||
| ctrl = ctrl_writes[-1] | ||||||||||||||||||||||||||
| osrs_t = (ctrl >> OSRS_T_SHIFT) & 0x07 | ||||||||||||||||||||||||||
| osrs_p = (ctrl >> OSRS_P_SHIFT) & 0x07 | ||||||||||||||||||||||||||
| result = osrs_t == OSRS_X2 and osrs_p == OSRS_X4 | ||||||||||||||||||||||||||
| expect_true: true | ||||||||||||||||||||||||||
| mode: [mock] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| - name: "set_oversampling configures humidity" | ||||||||||||||||||||||||||
| action: script | ||||||||||||||||||||||||||
| script: | | ||||||||||||||||||||||||||
| from bme280.const import OSRS_X8 | ||||||||||||||||||||||||||
| i2c.clear_write_log() | ||||||||||||||||||||||||||
| dev.set_oversampling(humidity=OSRS_X8) | ||||||||||||||||||||||||||
| log = i2c.get_write_log() | ||||||||||||||||||||||||||
| hum_writes = [data[0] for reg, data in log if reg == 0xF2] | ||||||||||||||||||||||||||
| result = len(hum_writes) == 1 and hum_writes[0] == OSRS_X8 | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set_oversampling()writesREG_CTRL_HUMwhenhumidityis provided, but if only humidity is changed (temperature/pressure are None) it never writesREG_CTRL_MEAS. On BME280,ctrl_humupdates take effect only after a subsequent write toctrl_meas(your own_configure_default()comment notes this), so humidity-only changes won't actually apply. Fix by performing a (read-modify-)write toREG_CTRL_MEASafter writingREG_CTRL_HUMeven when temperature/pressure are unchanged (preserving the existing mode/oversampling bits).There was a problem hiding this comment.
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 rewritesctrl_measafterctrl_hum, even when only humidity is changed. The comment in the code references datasheet section 5.4.3.