Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions components/icm20948/src/icm20948.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,9 @@ Icm20948<I>::Value Icm20948<I>::read_magnetometer(std::error_code &ec) {
}
float sensitivity = get_magnetometer_sensitivity();
Value value = {
static_cast<float>(raw.x) / sensitivity,
static_cast<float>(raw.y) / sensitivity,
static_cast<float>(raw.z) / sensitivity,
static_cast<float>(raw.x) * sensitivity,
static_cast<float>(raw.y) * sensitivity,
static_cast<float>(raw.z) * sensitivity,
};
// update values
mag_values_ = value;
Expand Down Expand Up @@ -1021,7 +1021,17 @@ Icm20948<I>::RawValue Icm20948<I>::get_gyroscope_raw(std::error_code &ec) {

template <espp::icm20948::Interface I>
Icm20948<I>::RawValue Icm20948<I>::get_magnetometer_raw(std::error_code &ec) {
return get_raw(RegisterBank0::MAG_DATA, ec);
// the magnetometer data is read through the I2C master, but it has a
// different endianess than the accelerometer and gyroscope data.
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: “endianess” should be “endianness”.

Suggested change
// different endianess than the accelerometer and gyroscope data.
// different endianness than the accelerometer and gyroscope data.

Copilot uses AI. Check for mistakes.
auto raw_mag = get_raw(RegisterBank0::MAG_DATA, ec);
if (ec) {
return {0, 0, 0};
}
return {
static_cast<int16_t>(((raw_mag.x & 0xFF00) >> 8) | ((raw_mag.x & 0xFF) << 8)),
static_cast<int16_t>(((raw_mag.y & 0xFF00) >> 8) | ((raw_mag.y & 0xFF) << 8)),
static_cast<int16_t>(((raw_mag.z & 0xFF00) >> 8) | ((raw_mag.z & 0xFF) << 8)),
};
Comment on lines +1030 to +1034
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The manual byte-swap logic is duplicated for x/y/z and is a bit hard to read/maintain. Consider extracting a small helper (e.g., a local lambda) that swaps a 16-bit value (ideally operating on uint16_t) and reuse it for each axis to reduce repetition and avoid subtle signed/bitshift pitfalls.

Copilot uses AI. Check for mistakes.
}

template <espp::icm20948::Interface I>
Expand Down