Skip to content

at(lis2mdl): Implement Arduino driver. #4#190

Open
DumontALINE wants to merge 7 commits into
mainfrom
feat/lis2mdl-driver
Open

at(lis2mdl): Implement Arduino driver. #4#190
DumontALINE wants to merge 7 commits into
mainfrom
feat/lis2mdl-driver

Conversation

@DumontALINE
Copy link
Copy Markdown
Contributor

@DumontALINE DumontALINE commented May 29, 2026

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

  • Add LIS2MDL.h, LIS2MDL.cpp, and LIS2MDL_const.h implementing the full driver API
  • Add magnetic field reading (magneticField, magneticFieldUt, calibratedField, magnitudeUt)
  • Add temperature reading with two-point calibration (temperature, setTempOffset, calibrateTemperature)
  • Add continuous and one-shot acquisition modes (setContinuous, triggerOneShot, readOneShot)
  • Add hard and soft calibration (calibrateMinmax2d, calibrateMinmax3d, calibrateApply, calibrateQuality)
  • Add tilt-compensated and flat-only compass heading (headingFlatOnly, headingWithTiltCompensation)
  • Add hardware offset registers read/write (readHwOffsets, setHwOffsets)
  • Add power management (powerOn, powerOff, softReset, reboot)
  • Add native mock test suite covering 34 test cases (register writes, signed values, calibration math, heading, timeout behaviour)
  • Add hardware test suite validating device detection, magnetic field plausibility, and sensor stability on real STeaMi silicon

Checklist

  • make lint passes (clang-format)
  • make build passes (PlatformIO)
  • make test-native passes (if native tests exist)
  • make test-hardware passes on a connected STeaMi (if hardware tests exist)
  • README updated (if adding/changing public API)
  • Examples added/updated (if applicable)
  • Commit messages follow conventional commits format

Copilot AI review requested due to automatic review settings May 29, 2026 12:19
@github-actions github-actions Bot added this to the Phase 3 — Drivers simples milestone May 29, 2026
@DumontALINE DumontALINE review requested due to automatic review settings May 29, 2026 12:21
Copilot AI review requested due to automatic review settings May 29, 2026 12:24
@DumontALINE DumontALINE requested a review from nedseb May 29, 2026 12:24
Copy link
Copy Markdown

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 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 LIS2MDL driver 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 thread lib/lis2mdl/src/LIS2MDL.h
Comment on lines +10 to +15
struct Vec3i {
int16_t x, y, z;
};
struct Vec3f {
float x, y, z;
};
@DumontALINE DumontALINE force-pushed the feat/lis2mdl-driver branch from daf2cd9 to aaed191 Compare May 29, 2026 13:26
@DumontALINE DumontALINE force-pushed the feat/lis2mdl-driver branch from aaed191 to 8ba847c Compare May 29, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(lis2mdl): Add hardware integration tests. test(lis2mdl): Add mock unit tests. feat(lis2mdl): Implement Arduino driver.

3 participants