|
| 1 | +/*! |
| 2 | + * @file drvLis2mdl.cpp |
| 3 | + * |
| 4 | + * Driver wrapper for the Adafruit LIS2MDL 3-axis magnetometer. |
| 5 | + */ |
| 6 | + |
| 7 | +#include "drvLis2mdl.h" |
| 8 | + |
| 9 | +#include <math.h> |
| 10 | + |
| 11 | +namespace { |
| 12 | +constexpr uint8_t kLis2mdlDefaultAddr = 0x1E; |
| 13 | +} |
| 14 | + |
| 15 | +/******************************************************************************/ |
| 16 | +/*! |
| 17 | + @brief Destructor for the LIS2MDL driver wrapper. |
| 18 | +*/ |
| 19 | +/******************************************************************************/ |
| 20 | +drvLis2mdl::~drvLis2mdl() { |
| 21 | + if (_mag) { |
| 22 | + delete _mag; |
| 23 | + _mag = nullptr; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/******************************************************************************/ |
| 28 | +/*! |
| 29 | + @brief Initializes the LIS2MDL sensor and begins I2C. |
| 30 | + @returns True if initialized successfully, False otherwise. |
| 31 | +*/ |
| 32 | +/******************************************************************************/ |
| 33 | +bool drvLis2mdl::begin() { |
| 34 | + WS_DEBUG_PRINTLN("[drvLis2mdl] Initialising sensor"); |
| 35 | + |
| 36 | + if (_mag) { |
| 37 | + delete _mag; |
| 38 | + _mag = nullptr; |
| 39 | + } |
| 40 | + |
| 41 | + _mag = new Adafruit_LIS2MDL(); |
| 42 | + if (!_mag) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + const uint8_t addr = |
| 47 | + _address == 0 ? kLis2mdlDefaultAddr : static_cast<uint8_t>(_address); |
| 48 | + if (!_mag->begin(addr, _i2c)) { |
| 49 | + WS_DEBUG_PRINTLN("[drvLis2mdl] Failed to initialise sensor"); |
| 50 | + delete _mag; |
| 51 | + _mag = nullptr; |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + _mag->setDataRate(LIS2MDL_RATE_50_HZ); |
| 56 | + _mag->enableInterrupts(false); |
| 57 | + _mag->interruptsActiveHigh(false); |
| 58 | + |
| 59 | + WS_DEBUG_PRINTLN("[drvLis2mdl] Sensor initialised successfully"); |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +/******************************************************************************/ |
| 64 | +/*! |
| 65 | + @brief Reads the LIS2MDL's magnetometer event. |
| 66 | + @param event Pointer to the magnetometer event to populate. |
| 67 | + @returns True if the event was obtained successfully. |
| 68 | +*/ |
| 69 | +/******************************************************************************/ |
| 70 | +bool drvLis2mdl::readMagEvent(sensors_event_t *event) { |
| 71 | + if (!_mag) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + return _mag->getEvent(event); |
| 75 | +} |
| 76 | + |
| 77 | +/******************************************************************************/ |
| 78 | +/*! |
| 79 | + @brief Computes the vector magnitude of a magnetometer reading. |
| 80 | + @param event Magnetometer event to evaluate. |
| 81 | + @param magnitude Reference to store the computed magnitude (micro Tesla). |
| 82 | + @returns True if the magnitude was computed successfully. |
| 83 | +*/ |
| 84 | +/******************************************************************************/ |
| 85 | +bool drvLis2mdl::computeMagnitude(const sensors_event_t &event, |
| 86 | + float &magnitude) { |
| 87 | + magnitude = |
| 88 | + sqrtf(event.magnetic.x * event.magnetic.x + |
| 89 | + event.magnetic.y * event.magnetic.y + |
| 90 | + event.magnetic.z * event.magnetic.z); |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +/******************************************************************************/ |
| 95 | +/*! |
| 96 | + @brief Gets the LIS2MDL's raw sensor event (magnitude stored in data[0]). |
| 97 | + @param rawEvent Pointer to the sensor event. |
| 98 | + @returns True if the sensor event was obtained successfully. |
| 99 | +*/ |
| 100 | +/******************************************************************************/ |
| 101 | +bool drvLis2mdl::getEventRaw(sensors_event_t *rawEvent) { |
| 102 | + sensors_event_t magEvent; |
| 103 | + if (!readMagEvent(&magEvent)) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + float magnitude = 0.0f; |
| 108 | + computeMagnitude(magEvent, magnitude); |
| 109 | + rawEvent->data[0] = magnitude; |
| 110 | + return true; |
| 111 | +} |
| 112 | + |
| 113 | +/******************************************************************************/ |
| 114 | +/*! |
| 115 | + @brief Gets the LIS2MDL's boolean sensor event. |
| 116 | + @param booleanEvent Pointer to the sensor event. |
| 117 | + @returns True once the placeholder value has been populated. |
| 118 | +*/ |
| 119 | +/******************************************************************************/ |
| 120 | +bool drvLis2mdl::getEventBoolean(sensors_event_t *booleanEvent) { |
| 121 | + // Magnetometers do not emit boolean events; reserve the field for future use. |
| 122 | + booleanEvent->data[0] = 0.0f; |
| 123 | + return true; |
| 124 | +} |
| 125 | + |
| 126 | +/******************************************************************************/ |
| 127 | +/*! |
| 128 | + @brief Gets the LIS2MDL's magnetometer sensor event (x,y,z in microTesla). |
| 129 | + @param magEvent Pointer to the magnetometer sensor event. |
| 130 | + @returns True if the sensor event was obtained successfully. |
| 131 | +*/ |
| 132 | +/******************************************************************************/ |
| 133 | +bool drvLis2mdl::getEventMagneticField(sensors_event_t *magEvent) { |
| 134 | + return readMagEvent(magEvent); |
| 135 | +} |
| 136 | + |
| 137 | +/******************************************************************************/ |
| 138 | +/*! |
| 139 | + @brief Registers the driver's default magnetometer sensor type. |
| 140 | +*/ |
| 141 | +/******************************************************************************/ |
| 142 | +void drvLis2mdl::ConfigureDefaultSensorTypes() { |
| 143 | + _default_sensor_types_count = 1; |
| 144 | + _default_sensor_types[0] = |
| 145 | + wippersnapper_sensor_SensorType_SENSOR_TYPE_MAGNETIC_FIELD; |
| 146 | +} |
0 commit comments