Skip to content

lis2mdl: Auto-trigger single conversion in idle mode.#73

Merged
nedseb merged 2 commits intomainfrom
fix/lis2mdl-auto-trigger
Mar 14, 2026
Merged

lis2mdl: Auto-trigger single conversion in idle mode.#73
nedseb merged 2 commits intomainfrom
fix/lis2mdl-auto-trigger

Conversation

@nedseb
Copy link
Copy Markdown
Contributor

@nedseb nedseb commented Mar 13, 2026

Closes #70

Summary

When the LIS2MDL is in idle mode (power_down() called), reading magnetic field or temperature data now automatically triggers a single conversion and waits for data ready, instead of returning stale values.

  • _ensure_data() — checks is_idle(), sets mode to "single", polls data_ready() (up to 100ms)
  • Called in read_magnet() and read_temperature_raw()
  • All higher-level methods (read_magnet_uT(), magnitude_uT(), read_temperature_c(), heading_flat_only(), etc.) benefit automatically

Follows the same convention as WSEN-PADS, WSEN-HIDS, HTS221, and ISM330DL (#44).

Test plan

Mock tests (no hardware)

python3 -m pytest tests/ -k "lis2mdl and mock" -v
tests/test_scenarios.py::test_scenario[lis2mdl/Verify WHO_AM_I register/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read WHO_AM_I via method/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read status register/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read magnetic field returns tuple/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read magnetic field in uT returns tuple/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read temperature returns float/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Temperature with offset/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Temperature with two-point calibration/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Magnetic field readable after power down/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Temperature readable after power down/mock] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/No auto-trigger when already active/mock] PASSED

11 passed

Hardware tests (STeaMi board connected)

python3 -m pytest tests/ --port /dev/ttyACM0 -k "lis2mdl and hardware" -s -v
tests/test_scenarios.py::test_scenario[lis2mdl/Verify WHO_AM_I register/hardware] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read WHO_AM_I via method/hardware] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Read status register/hardware] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Fresh magnitude after power down/hardware] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Fresh temperature after power down/hardware] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Magnitude in plausible range/hardware] PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Temperature in plausible range/hardware] PASSED

7 passed, 4 skipped

@nedseb nedseb changed the title lis2mdl: Auto-trigger single conversion in idle mode. [WIP] lis2mdl: Auto-trigger single conversion in idle mode. Mar 13, 2026
@nedseb nedseb added the enhancement New feature or request label Mar 13, 2026
@nedseb
Copy link
Copy Markdown
Contributor Author

nedseb commented Mar 13, 2026

Observed behavior confirming the need for _ensure_data()

Running the LIS2MDL hardware tests in sequence shows a magnitude inconsistency caused by stale data after soft_reset() / reboot():

tests/test_scenarios.py::test_scenario[lis2mdl/Magnitude in plausible range/hardware] 178.19 PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Temperature in plausible range/hardware] 5.75 PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Soft reset then WHO_AM_I/hardware] 0x40 PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Reboot then WHO_AM_I/hardware] 0x40 PASSED
tests/test_scenarios.py::test_scenario[lis2mdl/Magnetic field values feel correct/hardware]
  Magnitude: 0.21 µT    <-- stale/zero data after reboot put sensor in idle
  Temperature: 5.75 °C
  Heading: 236.45 °
  • First magnitude read (178.19 µT): sensor was in continuous mode from init, but the value is abnormally high (~25-65 µT expected). May indicate that the first read arrived before stable data was available.
  • Second magnitude read (0.21 µT): after soft_reset() and reboot() tests, the sensor is back in idle mode. magnitude_uT() reads stale/zero registers without triggering a new conversion.

This confirms the _ensure_data() pattern is needed: read_magnet() and read_temperature_raw() should detect idle mode and trigger a single conversion before reading.

Additional observation

The temperature reads 5.75°C at ~22°C ambient — this is a separate issue tracked in #97 (missing offset correction in read_temperature_c()).

@nedseb nedseb force-pushed the fix/lis2mdl-auto-trigger branch from 525b973 to 663c1c0 Compare March 14, 2026 21:06
@nedseb nedseb marked this pull request as ready for review March 14, 2026 21:07
@nedseb nedseb requested a review from Copilot March 14, 2026 21:07
@nedseb nedseb assigned nedseb and unassigned Charly-sketch Mar 14, 2026
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

This PR updates the LIS2MDL driver so that read methods automatically trigger a one-shot conversion when the device is in idle mode (after power_down()), aligning behavior with the repository’s “auto-trigger in power-down/idle” convention.

Changes:

  • Add LIS2MDL._ensure_data() to switch to single-conversion mode and wait for data_ready() when the sensor is idle.
  • Call _ensure_data() from read_magnet() and read_temperature_raw() so higher-level APIs inherit the behavior.
  • Add new scenario tests for “read after power down” behavior in both mock and hardware modes.

Reviewed changes

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

File Description
lib/lis2mdl/lis2mdl/device.py Adds _ensure_data() and hooks it into magnet/temperature reads to auto-trigger conversions in idle mode.
tests/scenarios/lis2mdl.yaml Adds mock + hardware scenarios intended to validate “fresh reads after power down” behavior.

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

Comment thread lib/lis2mdl/lis2mdl/device.py
Comment thread tests/scenarios/lis2mdl.yaml
@nedseb nedseb changed the title [WIP] lis2mdl: Auto-trigger single conversion in idle mode. lis2mdl: Auto-trigger single conversion in idle mode. Mar 14, 2026
@nedseb nedseb merged commit 466f4f9 into main Mar 14, 2026
3 checks passed
@nedseb nedseb deleted the fix/lis2mdl-auto-trigger branch March 14, 2026 21:52
@semantic-release-updater
Copy link
Copy Markdown

🎉 This PR is included in version 0.0.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

enhancement New feature or request released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lis2mdl: Auto-trigger single conversion in idle mode.

3 participants