32#include "../../../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_QMC5883L
37static constexpr uint8_t QMC5883L_SLAVE_ADDRESS = 0x0D;
69 uint8_t buffer[6] = {0};
70 int16_t
x = 0,
y = 0, z = 0;
72 int status =
readReg(REG_0x06_STATUS);
105 x = (int16_t)(buffer[1] << 8) | (buffer[0]);
106 y = (int16_t)(buffer[3] << 8) | (buffer[2]);
107 z = (int16_t)(buffer[5] << 8) | (buffer[4]);
195 float full_scale = 0;
196 float sensitivity = 0.0f;
197 uint8_t range_value = 0;
200 sensitivity = 0.00033333f;
201 range_value = 0x01 << 4;
205 sensitivity = 0.00008333f;
206 range_value = 0x00 << 4;
213 if (
updateBits(REG_0x09_CMD1, 0x30, range_value) > 0) {
231 int rangeInt =
static_cast<int>(data_rate_hz * 100 + 0.5);
233 uint8_t regValue = 0;
236 regValue = 0x00 << 2;
239 regValue = 0x01 << 2;
242 regValue = 0x02 << 2;
245 regValue = 0x03 << 2;
251 if (
updateBits(REG_0x09_CMD1, 0x0C, regValue) < 0) {
268 uint8_t mode_val = 0;
281 if (
updateBits(REG_0x09_CMD1, 0x03, mode_val) < 0) {
324 return updateBits(REG_0x09_CMD1, 0xC0, osr_val) == 0;
348 uint8_t buffer[2] = {0};
350 if (
readRegBuff(REG_0x07_TOUT_LOW, buffer, 2) < 0) {
354 raw_temp = (int16_t)((buffer[1] << 8) | buffer[0]);
406 static constexpr uint8_t REG_0x00_LSB_DX = 0x00;
407 static constexpr uint8_t REG_0x01_MSB_DX = 0x01;
408 static constexpr uint8_t REG_0x02_LSB_DY = 0x02;
409 static constexpr uint8_t REG_0x03_MSB_DY = 0x03;
410 static constexpr uint8_t REG_0x04_LSB_DZ = 0x04;
411 static constexpr uint8_t REG_0x05_MSB_DZ = 0x05;
412 static constexpr uint8_t REG_0x06_STATUS = 0x06;
413 static constexpr uint8_t REG_0x07_TOUT_LOW = 0x07;
414 static constexpr uint8_t REG_0x08_TOUT_HIGH = 0x08;
415 static constexpr uint8_t REG_0x09_CMD1 = 0x09;
416 static constexpr uint8_t REG_0x0A_CMD2 = 0x0A;
417 static constexpr uint8_t REG_0x0B_CMD3 = 0x0B;
418 static constexpr uint8_t REG_0x0D_CHIP_ID = 0x0D;
419 static constexpr uint8_t QMC5883L_CHIP_ID = 0xFF;
421 bool initImpl(uint8_t param)
override
429 uint8_t set_reset_period = 0x01;
430 if (
writeReg(REG_0x0B_CMD3, set_reset_period) < 0) {
454 return _info.
uid == QMC5883L_CHIP_ID;
@ MAGNETOMETER
Magnetometer sensor.
OperationMode
Enumeration of sensor operation modes.
MagFullScaleRange
Enumeration defining full-scale range settings for the sensor.
MagDownSampleRatio
Enumeration defining down-sample ratios for the sensor.
@ DSR_1
1x down-sample ratio
MagOverSampleRatio
Enumeration defining over-sample ratios for the sensor.
@ OSR_8
8x over-sample ratio
@ OSR_4
4x over-sample ratio
@ OSR_2
2x over-sample ratio
@ OSR_1
1x over-sample ratio
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
SensorTemperature temperature(bhy)
std::unique_ptr< SensorHal > hal
uint16_t _oversampling_rate
SensorConfig _config
Current configuration.
SensorInfo _info
Sensor information.
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool getTemperature(float &temperature, int16_t &raw_temp)
Gets the temperature reading from the sensor.
~SensorQMC5883L()=default
Destructor.
uint8_t getChipID() const
Gets the chip ID of the magnetometer.
bool setDownsamplingRate(MagDownSampleRatio dsr) override
QM5883L does not support downsampling.
bool selfTest() override
QMC5883L does not support self-test checks.
bool setOutputDataRate(float data_rate_hz) override
Sets the bandwidth of the magnetometer.
SensorQMC5883L()=default
Default constructor.
bool isDataSkipped()
Checks if data has been skipped.
bool isDataReady() override
Checks if new data is available from the sensor.
bool isDataOverflow()
Checks if data overflow has occurred.
bool configMagnetometer(OperationMode mode, MagFullScaleRange range, float data_rate_hz, MagOverSampleRatio osr, MagDownSampleRatio dsr=MagDownSampleRatio::DSR_1) override
Configures the magnetometer with multiple parameters.
bool reset() override
Resets the sensor to its default state.
bool setFullScaleRange(MagFullScaleRange range) override
Sets the full-scale range of the magnetometer.
bool setOversamplingRate(MagOverSampleRatio osr) override
Sets the oversampling rate of the magnetometer.
bool readData(MagnetometerData &data) override
Reads the magnetic field data from the sensor.
bool setOperationMode(OperationMode mode) override
Sets the operation mode of the magnetometer.
float calculateHeading(const MagnetometerData &data, float declination=0.0f)
Calculate heading (yaw) angle from magnetometer data in radians.
constexpr bool _isBitSet(T value, uint8_t bit)
Structure representing magnetometer data.
float heading_degrees
heading angle (degrees)
float heading
heading angle (radians)
bool skip_data
skip data flag
SensorVector magnetic_field
magnetic field strength (Gauss)
bool overflow
data overflow flag
int16_t x
X-axis raw value.
int16_t y
Y-axis raw value.
int16_t z
Z-axis raw value.
uint32_t latency
Reporting latency in milliseconds.
float sample_rate
Sample rate in Hz.
float range
Measurement range.
SensorType type
Type of the sensor.
OperationMode mode
Operation mode.
uint8_t version
Hardware/firmware version.
uint8_t i2c_address
Default I2C address.
uint32_t uid
Unique identifier.
SensorType type
Sensor type.
const char * manufacturer
Manufacturer name.
const char * model
Model name/identifier.