32 : slave(bus, pin, spiConfig), configuration(config)
42 LOG_ERR(logger,
"Attempted to initialized sensor twice but failed");
51 if (res != WHO_AM_I_VALUE)
54 "WHO_AM_I value differs from expectation: read 0x{:x} "
55 "but expected 0x{:x}",
70 LOG_ERR(logger,
"Invoked selfTest() but sensor was uninitialized");
75 constexpr int NUM_SAMPLES = 5;
76 constexpr int SLEEP_TIME = 50;
79 constexpr float t = 0.1f;
83 constexpr float deltaRange[3][2] = {{1.f, 3.f}, {1.f, 3.f}, {0.1f, 1.f}};
85 float avgX = 0.f, avgY = 0.f, avgZ = 0.f;
93 for (
int i = 0; i < NUM_SAMPLES; ++i)
95 miosix::Thread::sleep(SLEEP_TIME);
98 avgX += lastData.magneticFieldX;
99 avgY += lastData.magneticFieldY;
100 avgZ += lastData.magneticFieldZ;
120 miosix::Thread::sleep(SLEEP_TIME);
123 deltas[0] = std::abs(lastData.magneticFieldX - avgX);
124 deltas[1] = std::abs(lastData.magneticFieldY - avgY);
125 deltas[2] = std::abs(lastData.magneticFieldZ - avgZ);
128 for (
int j = 0; j < 3; ++j)
130 if (deltas[j] < (deltaRange[j][0] - t) ||
131 deltas[j] > (deltaRange[j][1] + t))
152 uint8_t reg = 0, err = 0;
154 configuration = config;
158 reg = ENABLE_TEMPERATURE;
163 reg |= config.
xyMode << 4;
173 reg = CONTINUOS_CONVERSION;
178 reg = config.
zMode << 2;
192 updateUnit(config.
scale);
208 LOG_ERR(logger,
"Invoked sampleImpl() but sensor was uninitialized");
219 int16_t outTemp = spi.
readRegister16(TEMP_OUT_L | INCREMENT_REG_FLAG);
221 newData.temperature = DEG_PER_LSB * outTemp;
222 newData.temperature += REFERENCE_TEMPERATURE;
231 reinterpret_cast<uint8_t*
>(values),
sizeof(values));
233 newData.magneticFieldX = currentUnit * values[0];
234 newData.magneticFieldY = currentUnit * values[1];
235 newData.magneticFieldZ = currentUnit * values[2];
240void LIS3MDL::updateUnit(FullScale fs)
245 currentUnit = GAUSS_PER_LSB_FS_4;
249 currentUnit = GAUSS_PER_LSB_FS_8;
253 currentUnit = GAUSS_PER_LSB_FS_12;
257 currentUnit = GAUSS_PER_LSB_FS_16;
#define LOG_ERR(logger,...)
bool selfTest() override
Check if the sensor is working.
LIS3MDL(SPIBusInterface &bus, miosix::GpioPin pin, SPIBusConfig spiConfig={}, Config config={})
LIS3MDLData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
bool init() override
Initialize the sensor.
@ FS_16_GAUSS
+/- 16 gauss
@ FS_12_GAUSS
+/- 12 gauss
bool applyConfig(Config config)
Overrides the sensor settings.
Interface for low level access of a SPI bus as a master.
Provides high-level access to the SPI Bus for a single transaction.
uint8_t readRegister(uint8_t reg)
Reads an 8 bit register.
void readRegisters(uint8_t reg, uint8_t *data, size_t size)
Reads multiple bytes starting from the specified register.
void writeRegister(uint8_t reg, uint8_t data)
Writes an 8 bit register.
uint16_t readRegister16(uint8_t reg)
Reads a 16 bit register.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
bool doBlockDataUpdate
BDU setting.
unsigned temperatureDivider
Divide the temperature sampling rate.
SPI Bus configuration for a specific slave.
SPI::Order byteOrder
MSByte or LSByte first.
SPI::Mode mode
MSBit or LSBit first.