LIS2DW12 accelerometer sensor library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Files at this revision

API Documentation at this revision

Comitter:
martlefebvre94
Date:
Tue Sep 17 08:40:17 2019 +0000
Parent:
4:94c5d5546161
Commit message:
Addition of functions for the configuration of the accelerometer sensor

Changed in this revision

LIS2DW12Sensor.cpp Show annotated file Show diff for this revision Revisions of this file
LIS2DW12Sensor.h Show annotated file Show diff for this revision Revisions of this file
--- a/LIS2DW12Sensor.cpp	Wed Jul 24 14:18:07 2019 +0000
+++ b/LIS2DW12Sensor.cpp	Tue Sep 17 08:40:17 2019 +0000
@@ -826,6 +826,235 @@
 }
 
 /**
+ * @brief  Read LIS2DW12 Accelerometer filter bandwidth
+ * @param  bw_filt the pointer to the filter bandwidth
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LIS2DW12Sensor::get_x_bw_filt(uint8_t *bw_filt)
+{
+    int ret = 0;
+    lis2dw12_bw_filt_t bw_filt_low_level;
+
+    /* Get current filter bandwidth. */
+    if (lis2dw12_filter_bandwidth_get(&_reg_ctx, &bw_filt_low_level) != 0) {
+        return 1;
+    }
+
+    switch (bw_filt_low_level) {
+        case LIS2DW12_ODR_DIV_2:
+            *bw_filt = 0;
+            break;
+
+        case LIS2DW12_ODR_DIV_4:
+            *bw_filt = 1;
+            break;
+
+        case LIS2DW12_ODR_DIV_10:
+            *bw_filt = 2;
+            break;
+
+        case LIS2DW12_ODR_DIV_20:
+            *bw_filt = 3;
+            break;
+
+        default:
+            ret = 1;
+            break;
+    }
+
+    return ret;
+}
+
+/**
+ * @brief  Set LIS2DW12 Accelerometer filter bandwidth
+ * @param  bw_filt the filter bandwidth to be set
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LIS2DW12Sensor::set_x_bw_filt(uint8_t bw_filt)
+{
+    lis2dw12_bw_filt_t new_bw_filt;
+
+    new_bw_filt = (bw_filt == 0)    ? LIS2DW12_ODR_DIV_2
+              : (bw_filt == 1)      ? LIS2DW12_ODR_DIV_4
+              : (bw_filt == 2)      ? LIS2DW12_ODR_DIV_10
+              :                     LIS2DW12_ODR_DIV_20;
+
+    if (lis2dw12_filter_bandwidth_set(&_reg_ctx, new_bw_filt) != 0) {
+        return 1;
+    }
+
+    return 0;
+}
+
+/**
+ * @brief  Read LIS2DW12 Accelerometer power mode
+ * @param  lp_mode the pointer to the low-power mode, mode the pointer to the mode, low_noise the pointer to the low-noise configuration
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LIS2DW12Sensor::get_x_power_mode(uint8_t *lp_mode, uint8_t *mode, uint8_t *low_noise)
+{
+    int ret = 0;
+    lis2dw12_mode_t mode_low_level;
+
+    /* Get current filter bandwidth. */
+    if (lis2dw12_power_mode_get(&_reg_ctx, &mode_low_level) != 0) {
+        return 1;
+    }
+
+    switch (mode_low_level) {
+        case LIS2DW12_HIGH_PERFORMANCE:
+            *lp_mode = 0;
+            *mode = 1;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_4:
+            *lp_mode = 3;
+            *mode = 0;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_3:
+            *lp_mode = 2;
+            *mode = 0;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_2:
+            *lp_mode = 1;
+            *mode = 0;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_12bit:
+            *lp_mode = 0;
+            *mode = 0;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_4:
+            *lp_mode = 3;
+            *mode = 2;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_3:
+            *lp_mode = 2;
+            *mode = 2;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_2:
+            *lp_mode = 1;
+            *mode = 2;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_12bit:
+            *lp_mode = 0;
+            *mode = 2;
+            *low_noise = 0;
+            break;
+        
+        case LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE:
+            *lp_mode = 0;
+            *mode = 1;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4:
+            *lp_mode = 3;
+            *mode = 0;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3:
+            *lp_mode = 2;
+            *mode = 0;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2:
+            *lp_mode = 1;
+            *mode = 0;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit:
+            *lp_mode = 0;
+            *mode = 0;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4:
+            *lp_mode = 3;
+            *mode = 2;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3:
+            *lp_mode = 2;
+            *mode = 2;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2:
+            *lp_mode = 1;
+            *mode = 2;
+            *low_noise = 1;
+            break;
+        
+        case LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit:
+            *lp_mode = 0;
+            *mode = 2;
+            *low_noise = 1;
+            break;
+
+        default:
+            ret = 1;
+            break;
+    }
+
+    return ret;
+}
+
+/**
+ * @brief  Set LIS2DW12 Accelerometer power mode
+ * @param  power_mode the power mode to be set
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LIS2DW12Sensor::set_x_power_mode(uint8_t power_mode)
+{
+    lis2dw12_mode_t new_mode;
+
+    new_mode = (power_mode == 0x04)     ? LIS2DW12_HIGH_PERFORMANCE
+              : (power_mode == 0x03)    ? LIS2DW12_CONT_LOW_PWR_4
+              : (power_mode == 0x02)    ? LIS2DW12_CONT_LOW_PWR_3
+              : (power_mode == 0x01)    ? LIS2DW12_CONT_LOW_PWR_2
+              : (power_mode == 0x00)    ? LIS2DW12_CONT_LOW_PWR_12bit
+              : (power_mode == 0x0B)    ? LIS2DW12_SINGLE_LOW_PWR_4
+              : (power_mode == 0x0A)    ? LIS2DW12_SINGLE_LOW_PWR_3
+              : (power_mode == 0x09)    ? LIS2DW12_SINGLE_LOW_PWR_2
+              : (power_mode == 0x08)    ? LIS2DW12_SINGLE_LOW_PWR_12bit
+              : (power_mode == 0x14)    ? LIS2DW12_HIGH_PERFORMANCE_LOW_NOISE
+              : (power_mode == 0x13)    ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_4
+              : (power_mode == 0x12)    ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_3
+              : (power_mode == 0x11)    ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_2
+              : (power_mode == 0x10)    ? LIS2DW12_CONT_LOW_PWR_LOW_NOISE_12bit
+              : (power_mode == 0x1B)    ? LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_4
+              : (power_mode == 0x1A)    ? LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_3
+              : (power_mode == 0x19)    ? LIS2DW12_SINGLE_LOW_PWR_LOW_NOISE_2
+              : (power_mode == 0x18)    ? LIS2DW12_SINGLE_LOW_LOW_NOISE_PWR_12bit
+              :                         LIS2DW12_HIGH_PERFORMANCE;
+
+    if (lis2dw12_power_mode_set(&_reg_ctx, new_mode) != 0) {
+        return 1;
+    }
+
+    return 0;
+}
+
+/**
  * @brief Enable the wake up detection for LIS2DW12 accelerometer sensor
  * @note  This function sets the LIS2DW12 accelerometer ODR to 200Hz and the LIS2DW12 accelerometer full scale to 2g
  * @retval 0 in case of success, an error code otherwise
--- a/LIS2DW12Sensor.h	Wed Jul 24 14:18:07 2019 +0000
+++ b/LIS2DW12Sensor.h	Tue Sep 17 08:40:17 2019 +0000
@@ -105,6 +105,10 @@
     virtual int set_x_odr(float odr);
     virtual int get_x_fs(float *fullScale);
     virtual int set_x_fs(float fullScale);
+    virtual int get_x_bw_filt(uint8_t* bw_filt);
+    virtual int set_x_bw_filt(uint8_t bw_filt);
+    virtual int get_x_power_mode(uint8_t* lp_mode, uint8_t* mode, uint8_t* low_noise);
+    virtual int set_x_power_mode(uint8_t power_mode);
     int set_x_odr_with_mode(float odr, LIS2DW12_Operating_Mode_t mode = LIS2DW12_HIGH_PERFORMANCE_MODE, LIS2DW12_Low_Noise_t noise = LIS2DW12_LOW_NOISE_DISABLE);
     int enable_x(void);
     int disable_x(void);