From 943fa0ba9af092438204e59b041e93b8c4c2099e Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Fri, 17 Mar 2023 17:25:37 +0100
Subject: [PATCH] [LIS2MDL] Updated comments and removed interrupt signals
 generation functionality

---
 src/shared/sensors/LIS2MDL/LIS2MDL.h   |  73 +++--------------
 src/shared/sensors/LIS3MDL/LIS3MDL.cpp |  34 +-------
 src/shared/sensors/LIS3MDL/LIS3MDL.h   | 105 ++++++-------------------
 3 files changed, 39 insertions(+), 173 deletions(-)

diff --git a/src/shared/sensors/LIS2MDL/LIS2MDL.h b/src/shared/sensors/LIS2MDL/LIS2MDL.h
index 03110470f..15cc0136b 100644
--- a/src/shared/sensors/LIS2MDL/LIS2MDL.h
+++ b/src/shared/sensors/LIS2MDL/LIS2MDL.h
@@ -32,27 +32,19 @@ namespace Boardcore
 {
 
 /**
- * Driver for LIS2MDL, a three-axis magnetic sensor.
+ * @brief Driver for LIS2MDL, a three-axis magnetic sensor.
  */
 class LIS2MDL : public Sensor<LIS2MDLData>
 {
 public:
-    /**
-     * @brief Constants for Output Data Rate configuration.
-     *
-     */
     enum ODR : uint8_t
     {
-        ODR_10_HZ  = 0x00,  //!< 10 Hz
-        ODR_20_HZ  = 0x01,  //!< 20 Hz
-        ODR_50_HZ  = 0x02,  //!< 50 Hz
-        ODR_100_HZ = 0x03,  //!< 100 Hz
+        ODR_10_HZ  = 0x00,  ///< 10 Hz
+        ODR_20_HZ  = 0x01,  ///< 20 Hz
+        ODR_50_HZ  = 0x02,  ///< 50 Hz
+        ODR_100_HZ = 0x03,  ///< 100 Hz
     };
 
-    /**
-     * @brief Mode of operation constants.
-     *
-     */
     enum OperativeMode : uint8_t
     {
         MD_CONTINUOUS = 0x00,
@@ -62,65 +54,26 @@ public:
     };
 
     /**
-     * @brief Sensor configuration
+     * @brief Sensor configuration.
      *
-     * This struct contains all the settings the user
-     * is able to modify with the help of the driver.
-     * They are applied in the constructor of LIS2MDL class
+     * This struct contains all the settings the user is able to modify with the
+     * help of the driver. They are applied in the constructor of LIS2MDL class
      * and on each call of LIS2MDL::applyConfig()
      */
     struct Config
     {
         Config() {}
-        /**
-         * @brief Data rate configuration
-         *
-         * Default: 10 Hz
-         *
-         * @see LIS2MDL::ODR
-         */
-        ODR odr = ODR_10_HZ;
 
-        /**
-         * @brief Mode of operation of the device
-         * Default value: 11 - Idle mode 2
-         *
-         * @see LIS2MDL::OperativeMode
-         */
+        ODR odr                  = ODR_10_HZ;
         OperativeMode deviceMode = MD_IDLE1;
 
         /**
-         * Enables temperature sensor.
-         * Default: true
-         */
-        bool enableTemperature = true;
-
-        /**
-         * @brief Sets the value of tempDivider.
+         * @brief Divide the temperature sampling rate.
          *
-         * With the given value you can instruct the driver to update
-         * the temperature according to a different rate.
-         * The temperature will be updated only once in `tempDivider` calls
-         * to sampleImpl(), so for example:
-         * 2 -> updated half the times,
-         * 1 -> updated every time.
-         */
-        unsigned temperatureDivider = 1;
-
-        /**
-         * @brief Enables interrupts
-         *
-         * Whether are interrupts enabled respectively on the x, y and z axis.
-         * If it is set to true on at least one axis, the interrupts will be
-         * generated otherwise, they will be completely disabled by the driver.
-         */
-        bool enableInterrupt[3] = {false, false, false};
-
-        /**
-         * Absolute value of the threshold that triggers the interrupt
-         * (expressed in gauss).
+         * This is used to limit the sampling of the temperature, use 0 to
+         * disable it completely.
          */
-        float threshold = 0;
+        unsigned temperatureDivider = 0;
 
         /**
          * @brief BDU setting
diff --git a/src/shared/sensors/LIS3MDL/LIS3MDL.cpp b/src/shared/sensors/LIS3MDL/LIS3MDL.cpp
index 095a2fee3..229947811 100644
--- a/src/shared/sensors/LIS3MDL/LIS3MDL.cpp
+++ b/src/shared/sensors/LIS3MDL/LIS3MDL.cpp
@@ -151,7 +151,7 @@ bool LIS3MDL::applyConfig(Config config)
     currDiv = 0;
 
     // CTRL_REG1
-    if (config.enableTemperature)
+    if (config.temperatureDivider != 0)
     {
         reg = ENABLE_TEMPERATURE;
     }
@@ -187,36 +187,6 @@ bool LIS3MDL::applyConfig(Config config)
     spi.writeRegister(CTRL_REG5, reg);
     err |= spi.readRegister(CTRL_REG5) != reg;
 
-    // INT_CFG
-    if (config.enableInterrupt[0])
-        reg = ENABLE_INT_X;
-    else
-        reg = 0;
-
-    if (config.enableInterrupt[1])
-        reg |= ENABLE_INT_Y;
-    if (config.enableInterrupt[2])
-        reg |= ENABLE_INT_Z;
-
-    // The interrupt of at least one axis is enabled
-    if (reg)
-        reg |= ENABLE_INT_PIN;
-
-    reg |= 0x08;
-    spi.writeRegister(INT_CFG, reg);
-    err |= spi.readRegister(INT_CFG) != reg;
-
-    // INT_THS
-    uint16_t val = static_cast<uint16_t>(std::abs(config.threshold / mUnit));
-    reg          = static_cast<uint8_t>(0xff & val);
-    spi.writeRegister(INT_THS_L, reg);
-    err |= spi.readRegister(INT_THS_L) != reg;
-
-    reg = static_cast<uint8_t>(val >> 8);
-    reg &= 0x7f;  // Remove MSB (according to the datasheet, it must be zero)
-    spi.writeRegister(INT_THS_H, reg);
-    err |= spi.readRegister(INT_THS_H) != reg;
-
     // Set mUnit according to scale
     updateUnit(config.scale);
 
@@ -253,7 +223,7 @@ LIS3MDLData LIS3MDL::sampleImpl()
     int16_t val;
     LIS3MDLData newData{};
 
-    if (mConfig.enableTemperature)
+    if (mConfig.temperatureDivider != 0)
     {
         if (currDiv == 0)
         {
diff --git a/src/shared/sensors/LIS3MDL/LIS3MDL.h b/src/shared/sensors/LIS3MDL/LIS3MDL.h
index 698432e93..efc9d1e25 100644
--- a/src/shared/sensors/LIS3MDL/LIS3MDL.h
+++ b/src/shared/sensors/LIS3MDL/LIS3MDL.h
@@ -46,19 +46,19 @@ public:
      */
     enum ODR : uint8_t
     {
-        ODR_0_625_HZ = 0x00,  //!< 0.625 Hz
-        ODR_1_25_HZ  = 0x04,  //!<  1.25 Hz
-        ODR_2_5_HZ   = 0x08,  //!<   2.5 Hz
-        ODR_5_HZ     = 0x0c,  //!<     5 Hz
-        ODR_10_HZ    = 0x10,  //!<    10 Hz
-        ODR_20_HZ    = 0x14,  //!<    20 Hz
-        ODR_40_HZ    = 0x18,  //!<    40 Hz
-        ODR_80_HZ    = 0x1c,  //!<    80 Hz
-
-        ODR_155_HZ  = 0x62,  //!<    155 Hz
-        ODR_300_HZ  = 0x42,  //!<    300 Hz
-        ODR_560_HZ  = 0x22,  //!<    560 Hz
-        ODR_1000_HZ = 0x02,  //!<   1000 Hz
+        ODR_0_625_HZ = 0x00,  ///< 0.625 Hz
+        ODR_1_25_HZ  = 0x04,  ///<  1.25 Hz
+        ODR_2_5_HZ   = 0x08,  ///<   2.5 Hz
+        ODR_5_HZ     = 0x0c,  ///<     5 Hz
+        ODR_10_HZ    = 0x10,  ///<    10 Hz
+        ODR_20_HZ    = 0x14,  ///<    20 Hz
+        ODR_40_HZ    = 0x18,  ///<    40 Hz
+        ODR_80_HZ    = 0x1c,  ///<    80 Hz
+
+        ODR_155_HZ  = 0x62,  ///<    155 Hz
+        ODR_300_HZ  = 0x42,  ///<    300 Hz
+        ODR_560_HZ  = 0x22,  ///<    560 Hz
+        ODR_1000_HZ = 0x02,  ///<   1000 Hz
 
         /**
          * Constant used by the driver: this bit
@@ -90,85 +90,28 @@ public:
     };
 
     /**
-     * @brief Sensor configuration
+     * @brief Sensor configuration.
      *
-     * This struct contains all the settings the user
-     * is able to modify with the help of the driver.
-     * They are applied in the constructor of LIS3MDL class
+     * This struct contains all the settings the user is able to modify with the
+     * help of the driver. They are applied in the constructor of LIS3MDL class
      * and on each call of LIS3MDL::applyConfig()
      */
     struct Config
     {
         Config() {}
 
-        /**
-         * @brief Full scale setting.
-         *
-         * @see LIS3MDL::FullScale
-         */
-        FullScale scale = FS_8_GAUSS;
-
-        /**
-         * @brief Data rate configuration
-         *
-         * Default: 40 Hz
-         *
-         * Important: if ODR is set more than 80 Hz, operative mode of x and y
-         * axis will be set accordingly.
-         *
-         * @see LIS3MDL::ODR
-         */
-        ODR odr = ODR_40_HZ;
-
-        /**
-         * @brief Operative mode for x and y axis.
-         *
-         * Note: if ODR is greater than 80 Hz, this setting will be ignored and
-         * actual operative mode will be set depending of the chosen frequency.
-         *
-         * @see LIS3MDL::OperativeMode
-         */
+        FullScale scale      = FS_8_GAUSS;
+        ODR odr              = ODR_40_HZ;
         OperativeMode xyMode = OM_ULTRA_HIGH_POWER;
+        OperativeMode zMode  = OM_ULTRA_HIGH_POWER;
 
         /**
-         * Operative mode for z axis.
+         * @brief Divide the temperature sampling rate.
          *
-         * @see LIS3MDL::OM
-         */
-        OperativeMode zMode = OM_ULTRA_HIGH_POWER;
-
-        /**
-         * Enables temperature sensor.
-         * Default: true
-         */
-        bool enableTemperature = true;
-
-        /**
-         * @brief Sets the value of tempDivider.
-         *
-         * With the given value you can instruct the driver to update
-         * the temperature according to a different rate.
-         * The temperature will be updated only once in `tempDivider` calls
-         * to sampleImpl(), so for example:
-         * 2 -> updated half the times,
-         * 1 -> updated every time.
-         */
-        unsigned temperatureDivider = 1;
-
-        /**
-         * @brief Enables interrupts
-         *
-         * Whether are interrupts enabled respectively on the x, y and z axis.
-         * If it is set to true on at least one axis, the interrupts will be
-         * generated otherwise, they will be completely disabled by the driver.
-         */
-        bool enableInterrupt[3] = {false, false, false};
-
-        /**
-         * Absolute value of the threshold that triggers the interrupt
-         * (expressed in gauss).
+         * This is used to limit the sampling of the temperature, use 0 to
+         * disable it completely.
          */
-        float threshold = 0;
+        unsigned temperatureDivider = 0;
 
         /**
          * @brief BDU setting
@@ -187,7 +130,7 @@ public:
     bool selfTest() override;
 
     /**
-     * @brief Overwrites the sensor settings.
+     * @brief Overrides the sensor settings.
      *
      * Writes a certain config to the sensor registers. This method is
      * automatically called in LIS3MDL::init() using as parameter the
-- 
GitLab