at(lis2mdl): Implement Arduino driver. #4#190
Open
DumontALINE wants to merge 7 commits into
Open
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.
Comment on lines
+10
to
+12
| constexpr uint8_t ADDR = LIS2MDL_I2C_ADDR; | ||
| constexpr uint8_t WHO_AM_I_VAL = LIS2MDL_WHO_AM_I_VAL; | ||
|
|
Comment on lines
+414
to
+418
| 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); |
Comment on lines
+3
to
+7
| #include <Arduino.h> | ||
| #include <LIS2MDL.h> | ||
| #include <Wire.h> | ||
| #include <unity.h> | ||
|
|
Comment on lines
+46
to
+49
| 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"); |
Comment on lines
+10
to
+12
| constexpr uint8_t ADDR = LIS2MDL_I2C_ADDR; | ||
| constexpr uint8_t WHO_AM_I_VAL = LIS2MDL_WHO_AM_I_VAL; | ||
|
|
Comment on lines
+73
to
+84
| 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); | ||
| } |
Comment on lines
+692
to
+703
| 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); | ||
| } |
Comment on lines
+499
to
+506
| 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(); |
Comment on lines
+385
to
+394
| 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(); | ||
| } | ||
| } |
Comment on lines
+10
to
+15
| struct Vec3i { | ||
| int16_t x, y, z; | ||
| }; | ||
| struct Vec3f { | ||
| float x, y, z; | ||
| }; |
daf2cd9 to
aaed191
Compare
aaed191 to
8ba847c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)