feat(lis2mdl): Implement Arduino driver. #4#190
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new Arduino driver library for the LIS2MDL 3‑axis magnetometer and introduces both native (MockWire) and on-device hardware validation tests to exercise the new API.
Changes:
- Added the
LIS2MDLdriver implementation and register constants. - Added a native unit test suite for register/config behavior and math helpers.
- Added a hardware test suite for basic plausibility/stability checks on real boards.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 25 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/native/test_lis2mdl/test_main.cpp | Native/mock unit tests for the new LIS2MDL driver API. |
| tests/hardware/test_lis2mdl/test_main.cpp | Hardware validation tests for LIS2MDL on STeaMi boards. |
| lib/lis2mdl/src/LIS2MDL.h | Public driver API, types, and calibration/heading interfaces. |
| lib/lis2mdl/src/LIS2MDL.cpp | Driver implementation (I2C register IO, modes, readings, calibration, heading, power/reset). |
| lib/lis2mdl/src/LIS2MDL_const.h | LIS2MDL register addresses and conversion constants. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| constexpr uint8_t ADDR = LIS2MDL_I2C_ADDR; | ||
| constexpr uint8_t WHO_AM_I_VAL = LIS2MDL_WHO_AM_I_VAL; | ||
|
|
| RUN_TEST(test_read_all_returns_all_channels); | ||
| RUN_TEST(test_read_one_shot_returns_magnetic_field); | ||
| RUN_TEST(test_read_one_shot_returns_zeros_on_timeout); | ||
| RUN_TEST(test_temperature_raw_is_signed); | ||
| RUN_TEST(test_set_temp_offset_shifts_reading); |
| #include <Arduino.h> | ||
| #include <LIS2MDL.h> | ||
| #include <Wire.h> | ||
| #include <unity.h> | ||
|
|
| TEST_ASSERT_TRUE_MESSAGE(avgMag > 5.0f, "magnitude trop faible"); | ||
| TEST_ASSERT_TRUE_MESSAGE(fabs(mag1 - avgMag) < tolerance, "lecture 1 instable"); | ||
| TEST_ASSERT_TRUE_MESSAGE(fabs(mag2 - avgMag) < tolerance, "lecture 2 instable"); | ||
| TEST_ASSERT_TRUE_MESSAGE(fabs(mag3 - avgMag) < tolerance, "lecture 3 instable"); |
| constexpr uint8_t ADDR = LIS2MDL_I2C_ADDR; | ||
| constexpr uint8_t WHO_AM_I_VAL = LIS2MDL_WHO_AM_I_VAL; | ||
|
|
| void LIS2MDL::setMode(const char* mode) { | ||
| uint8_t md = 0b00; | ||
| if (strcmp(mode, "single") == 0) { | ||
| md = 0b01; | ||
| } else if (strcmp(mode, "powerdown") == 0) { | ||
| md = 0b11; | ||
| } | ||
|
|
||
| uint8_t reg = readReg(LIS2MDL_CFG_REG_A); | ||
| reg = (reg & ~0b11) | md; | ||
| writeReg(LIS2MDL_CFG_REG_A, reg); | ||
| } |
| void LIS2MDL::powerOn(const char* mode) { | ||
| // Power on the sensor: 'continuous' (default) or 'single'. | ||
| uint8_t md; | ||
| if (mode == "single") { | ||
| md = 0b01; | ||
| } else { | ||
| md = 0b00; | ||
| } | ||
| uint8_t r = readReg(LIS2MDL_CFG_REG_A); | ||
| r = (r & ~0b11) | md; | ||
| writeReg(LIS2MDL_CFG_REG_A, r); | ||
| } |
| CalibrationQuality LIS2MDL::calibrateQuality(uint16_t samplesCheck, uint16_t delayMs) { | ||
| /* Evaluates the quality of the current calibration over a short sample. | ||
| Returns a dict with useful metrics: center (mean), anisotropy, XY radius dispersion. | ||
| (Move the board a bit while flat during the measurement.)*/ | ||
|
|
||
| float xs[samplesCheck], ys[samplesCheck], zs[samplesCheck]; | ||
| for (uint16_t i = 0; i < samplesCheck; i++) { | ||
| CalibratedField cal = calibratedField(); |
| void LIS2MDL::readRegisters(uint8_t startAddr, uint8_t length, uint8_t* buffer) { | ||
| // Dump of consecutive registers (useful for debugging). | ||
| _wire->beginTransmission(_address); | ||
| _wire->write(startAddr); | ||
| _wire->endTransmission(false); | ||
| _wire->requestFrom(_address, length); | ||
| for (uint8_t i = 0; i < length && _wire->available(); i++) { | ||
| buffer[i] = _wire->read(); | ||
| } | ||
| } |
| struct Vec3i { | ||
| int16_t x, y, z; | ||
| }; | ||
| struct Vec3f { | ||
| float x, y, z; | ||
| }; |
daf2cd9 to
aaed191
Compare
aaed191 to
8ba847c
Compare
|
Thanks for the implementation and the follow-up fixes. The driver, native tests, and hardware tests all seem to cover the requirements from #4, #24, and #36. Before merging, could you please reply to the remaining Copilot review comments (even if they are not going to be addressed)? It would make it clearer which suggestions were intentionally accepted, rejected, or considered out of scope. I don't have any blocking technical concerns at this stage. Once the review comments are acknowledged, this looks good to merge. |
|
🎉 This PR is included in version 0.20.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Summary
Adds a full driver for the LIS2MDL 3-axis magnetometer, including native mock tests and hardware validation tests. Closes #4
Close #36
Close #24
Changes
LIS2MDL.h,LIS2MDL.cpp, andLIS2MDL_const.himplementing the full driver APImagneticField,magneticFieldUt,calibratedField,magnitudeUt)temperature,setTempOffset,calibrateTemperature)setContinuous,triggerOneShot,readOneShot)calibrateMinmax2d,calibrateMinmax3d,calibrateApply,calibrateQuality)headingFlatOnly,headingWithTiltCompensation)readHwOffsets,setHwOffsets)powerOn,powerOff,softReset,reboot)Checklist
make lintpasses (clang-format)make buildpasses (PlatformIO)make test-nativepasses (if native tests exist)make test-hardwarepasses on a connected STeaMi (if hardware tests exist)