28#include "../functors.h"
29#include "../i2c_device.h"
30#include "../i2c_device_utilities.h"
31#include "../utilities.h"
41 float theta = atan2(y, x);
53 FOUR_SAMPLES = 2 << 5,
54 EIGHT_SAMPLES = 3 << 5
124 return data_ & READY;
139 static constexpr uint8_t READY =
bits::BV8(0);
140 static constexpr uint8_t LOCK =
bits::BV8(1);
215 template<
typename MANAGER>
220 template<
typename OUT,
typename IN>
using FUTURE =
typename PARENT::template FUTURE<OUT, IN>;
223 template<u
int8_t REGISTER,
typename T = u
int8_t,
typename FUNCTOR = functor::Identity<T>>
225 template<u
int8_t REGISTER,
typename T = u
int8_t,
typename FUNCTOR = functor::Identity<T>>
227 template<
typename T, uint8_t... REGISTERS>
230 static constexpr const uint8_t DEVICE_ADDRESS = 0x1E << 1;
232 static constexpr const uint8_t CONFIG_REG_A = 0;
233 static constexpr const uint8_t CONFIG_REG_B = 1;
234 static constexpr const uint8_t MODE_REG = 2;
235 static constexpr const uint8_t OUTPUT_REG_1 = 3;
236 static constexpr const uint8_t STATUS_REG = 9;
237 static constexpr const uint8_t IDENT_REG_A = 10;
238 static constexpr const uint8_t IDENT_REG_B = 11;
239 static constexpr const uint8_t IDENT_REG_C = 12;
242 class Sensor3DSwitcher
251 int16_t temp = result.
y;
292 Gain gain = Gain::GAIN_1_3GA,
296 :
PARENT{
bits::OR8(uint8_t(measurement), uint8_t(rate), uint8_t(samples)),
297 uint8_t(gain), uint8_t(mode)} {}
301 return static_cast<Gain>(this->get_input().value(1));
327 gain_ = GAIN(
future.gain());
475 return this->
template sync_write<EndFuture>();
488 if (this->
template sync_read<StatusFuture>(
status))
510 return this->
template sync_read<MagneticFieldsFuture>(fields);
525 convert_field_to_mGa(fields.
x);
526 convert_field_to_mGa(fields.
y);
527 convert_field_to_mGa(fields.
z);
531 void convert_field_to_mGa(int16_t& value)
533 value = value * 1000L / gain_;
536 static constexpr uint16_t GAIN(
Gain gain)
538 if (gain == Gain::GAIN_0_88GA)
return 1370;
539 if (gain == Gain::GAIN_1_3GA)
return 1090;
540 if (gain == Gain::GAIN_1_9GA)
return 820;
541 if (gain == Gain::GAIN_2_5GA)
return 660;
542 if (gain == Gain::GAIN_4_0GA)
return 440;
543 if (gain == Gain::GAIN_4_7GA)
return 390;
544 if (gain == Gain::GAIN_5_6GA)
return 330;
Create a future to be used by asynchronous method begin(BeginFuture&).
I2C device driver for the HMC5883L compass chip.
bool begin(OperatingMode mode=OperatingMode::SINGLE, Gain gain=Gain::GAIN_1_3GA, DataOutput rate=DataOutput::RATE_15HZ, SamplesAveraged samples=SamplesAveraged::ONE_SAMPLE, MeasurementMode measurement=MeasurementMode::NORMAL)
Start operation of this compass chip.
Status status() INLINE
Get the curent chip status.
int status(StatusFuture &future) INLINE
Get the curent chip status.
bool end() INLINE
Stop operation of this compass chip.
void convert_fields_to_mGA(Sensor3D &fields)
Convert raw fields measured obtained with magnetic_fields() to actual physical values,...
int magnetic_fields(MagneticFieldsFuture &future)
Read the magnetic fields (as raw values) on 3 axes (datasheet p15-16).
int begin(BeginFuture &future)
Start operation of this compass chip.
bool magnetic_fields(Sensor3D &fields)
Read the magnetic fields (as raw values) on 3 axes (datasheet p15-16).
HMC5883L(MANAGER &manager)
Create a new device driver for a HMC5883L chip.
int end(EndFuture &future) INLINE
Stop operation of this compass chip.
The chip status, as defined in datasheet p16.
bool lock() const
Check datasheet p16 for further explanations.
bool ready() const
Readiness of device.
Composition functor: applies 2 functors one after each other.
Constant functor: always returns a constant value.
Base class for all I2C devices.
int async_write(F &future, bool stop=true)
Helper method that asynchronously launches I2C commands for a simple Future performing only one write...
int async_multi_write(F &future, bool stop=true)
Helper method that asynchronously launches I2C commands for a simple Future performing several regist...
MANAGER MANAGER
the type of I2C Manager that can handle this device.
int async_read(F &future, bool stop=true)
Helper method that asynchronously launches I2C commands for a simple Future performing one write foll...
Generic Future that can be used to read an I2C device register.
Generic Future that can be used to write to several I2C device registers.
Generic Future that can be used to write to an I2C device register.
Common types used by 3D sensors (e.g.
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
static constexpr uint8_t OR8(uint8_t val1, uint8_t val2)
Create a uint8_t bitwise OR boolean operation between uint8_t operands.
Defines API for magnetic sensors for direction, speed and acceleration properties.
float magnetic_heading(int16_t x, int16_t y)
Calculate the magnetic heading (heading measured clockwise from magnetic north) from X and Y magnetic...
MeasurementMode
The measurement mode as defined in datasheet p12, table6.
Gain
The gain to set for the chip, as defined in datasheet p13, table9.
SamplesAveraged
The number of samples to average every time a measurement is required from the HMC5883L chip (datashe...
OperatingMode
The operating mode of the chip as defined in datasheet p10, p14 table 12.
DataOutput
The output rate when used in continuous mode (datasheet p12).
Contains the API around Future implementation.
@ READY
The status of a Future once its output value has been fully set by a provider.
Define API to define and manage I2C devices.
Structure to store 3 axis data for one sensor (gyroscope or accelerometer).
int16_t y
Sensor value on Y axis.
int16_t z
Sensor value on Z axis.
int16_t x
Sensor value on X axis.