Example of single tap and double tap detection for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of SingleDoubleTap_IKS01A2 by ST Expansion SW Team

Single and Double Tap Demo Application based on sensor expansion board X-NUCLEO-IKS01A2

Main function is to show how to detect the single and double tap events using the sensor expansion board and send a notification using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can try to tap the board and then view the notification using an hyper terminal. When the single tap is detected, the LED is switched on for a while.
- the user can press the user button to pass from the single tap detection to the double tap detection feature. The user can try to double tap the board and then view the notification using an hyper terminal. When the double tap is detected, the LED is switched on twice for a while.
- the user can press again the user button to disable the single and double tap detection feature.
- the user can press the user button to enable again the single tap detection feature and so on.

Files at this revision

API Documentation at this revision

Comitter:
cparata
Date:
Mon Nov 21 14:57:55 2016 +0000
Parent:
3:1676461f57b7
Child:
5:7da0dc121ec6
Commit message:
Add muti-event support

Changed in this revision

X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.cpp Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.cpp	Fri Aug 19 15:10:30 2016 +0000
+++ b/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.cpp	Mon Nov 21 14:57:55 2016 +0000
@@ -953,35 +953,6 @@
 }
 
 /**
- * @brief  Get status of free fall detection
- * @param  status the pointer where the status of free fall detection is stored; 0 means no detection, 1 means detection happened
- * @retval 0 in case of success, an error code otherwise
-*/
-int LSM6DSLSensor::Get_Status_Free_Fall_Detection(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_FF_EV_STATUS_t free_fall_status;
-  
-  if ( LSM6DSL_ACC_GYRO_R_FF_EV_STATUS( (void *)this, &free_fall_status ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( free_fall_status )
-  {
-    case LSM6DSL_ACC_GYRO_FF_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-    case LSM6DSL_ACC_GYRO_FF_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Set the free fall detection threshold for LSM6DSL accelerometer sensor
  * @param thr the threshold to be set
  * @retval 0 in case of success, an error code otherwise
@@ -1077,35 +1048,6 @@
 }
 
 /**
- * @brief Get the pedometer status for LSM6DSL accelerometer sensor
- * @param status the pointer to the pedometer status: 0 means no step detected, 1 means step detected
- * @retval 0 in case of success, an error code otherwise
- */
-int LSM6DSLSensor::Get_Status_Pedometer(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_PEDO_EV_STATUS_t pedometer_status;
-  
-  if ( LSM6DSL_ACC_GYRO_R_PEDO_EV_STATUS( (void *)this, &pedometer_status ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( pedometer_status )
-  {
-    case LSM6DSL_ACC_GYRO_PEDO_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-    case LSM6DSL_ACC_GYRO_PEDO_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Get the step counter for LSM6DSL accelerometer sensor
  * @param step_count the pointer to the step counter
  * @retval 0 in case of success, an error code otherwise
@@ -1224,35 +1166,6 @@
 }
 
 /**
- * @brief Get the tilt detection status for LSM6DSL accelerometer sensor
- * @param status the pointer to the tilt detection status: 0 means no tilt detected, 1 means tilt detected
- * @retval 0 in case of success, an error code otherwise
- */
-int LSM6DSLSensor::Get_Status_Tilt_Detection(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_TILT_EV_STATUS_t tilt_status;
-  
-  if ( LSM6DSL_ACC_GYRO_R_TILT_EV_STATUS( (void *)this, &tilt_status ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( tilt_status )
-  {
-    case LSM6DSL_ACC_GYRO_TILT_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-    case LSM6DSL_ACC_GYRO_TILT_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Enable the wake up detection for LSM6DSL accelerometer sensor
  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
  * @retval 0 in case of success, an error code otherwise
@@ -1332,35 +1245,6 @@
 }
 
 /**
- * @brief Get the status of the wake up detection for LSM6DSL accelerometer sensor
- * @param status the pointer to the status of the wake up detection: 0 means no detection, 1 means detection happened
- * @retval 0 in case of success, an error code otherwise
- */
-int LSM6DSLSensor::Get_Status_Wake_Up_Detection(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_WU_EV_STATUS_t wake_up_status;
-  
-  if ( LSM6DSL_ACC_GYRO_R_WU_EV_STATUS( (void *)this, &wake_up_status ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( wake_up_status )
-  {
-    case LSM6DSL_ACC_GYRO_WU_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-    case LSM6DSL_ACC_GYRO_WU_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Set the wake up threshold for LSM6DSL accelerometer sensor
  * @param thr the threshold to be set
  * @retval 0 in case of success, an error code otherwise
@@ -1511,37 +1395,6 @@
 }
 
 /**
- * @brief Get the single tap detection status for LSM6DSL accelerometer sensor
- * @param status the pointer to the single tap detection status: 0 means no single tap detected, 1 means single tap detected
- * @retval 0 in case of success, an error code otherwise
- */
-int LSM6DSLSensor::Get_Status_Single_Tap_Detection(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_SINGLE_TAP_EV_STATUS_t tap_status;
-  
-  if ( LSM6DSL_ACC_GYRO_R_SINGLE_TAP_EV_STATUS( (void *)this, &tap_status ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( tap_status )
-  {
-    case LSM6DSL_ACC_GYRO_SINGLE_TAP_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-     
-    case LSM6DSL_ACC_GYRO_SINGLE_TAP_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-      
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Enable the double tap detection for LSM6DSL accelerometer sensor
  * @note  This function sets the LSM6DSL accelerometer ODR to 416Hz and the LSM6DSL accelerometer full scale to 2g
  * @retval 0 in case of success, an error code otherwise
@@ -1693,37 +1546,6 @@
 }
 
 /**
- * @brief Get the double tap detection status for LSM6DSL accelerometer sensor
- * @param status the pointer to the double tap detection status: 0 means no double tap detected, 1 means double tap detected
- * @retval 0 in case of success, an error code otherwise
- */
-int LSM6DSLSensor::Get_Status_Double_Tap_Detection(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_DOUBLE_TAP_EV_STATUS_t tap_status;
-  
-  if ( LSM6DSL_ACC_GYRO_R_DOUBLE_TAP_EV_STATUS( (void *)this, &tap_status ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( tap_status )
-  {
-    case LSM6DSL_ACC_GYRO_DOUBLE_TAP_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-      
-    case LSM6DSL_ACC_GYRO_DOUBLE_TAP_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-      
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Set the tap threshold for LSM6DSL accelerometer sensor
  * @param thr the threshold to be set
  * @retval 0 in case of success, an error code otherwise
@@ -1851,35 +1673,6 @@
 }
 
 /**
- * @brief Get the status of the 6D orientation detection for LSM6DSL accelerometer sensor
- * @param status the pointer to the status of the 6D orientation detection: 0 means no detection, 1 means detection happened
- * @retval 0 in case of success, an error code otherwise
- */
-int LSM6DSLSensor::Get_Status_6D_Orientation(uint8_t *status)
-{
-  LSM6DSL_ACC_GYRO_D6D_EV_STATUS_t status_raw;
-  
-  if ( LSM6DSL_ACC_GYRO_R_D6D_EV_STATUS( (void *)this, &status_raw ) == MEMS_ERROR )
-  {
-    return 1;
-  }
-  
-  switch( status_raw )
-  {
-    case LSM6DSL_ACC_GYRO_D6D_EV_STATUS_DETECTED:
-      *status = 1;
-      break;
-    case LSM6DSL_ACC_GYRO_D6D_EV_STATUS_NOT_DETECTED:
-      *status = 0;
-      break;
-    default:
-      return 1;
-  }
-  
-  return 0;
-}
-
-/**
  * @brief Get the 6D orientation XL axis for LSM6DSL accelerometer sensor
  * @param xl the pointer to the 6D orientation XL axis
  * @retval 0 in case of success, an error code otherwise
@@ -2054,11 +1847,79 @@
 }
 
 /**
+ * @brief Get the status of all hardware events for LSM6DSL accelerometer sensor
+ * @param status the pointer to the status of all hardware events
+ * @retval 0 in case of success, an error code otherwise
+ */
+int LSM6DSLSensor::Get_Event_Status(LSM6DSL_Event_Status_t *status)
+{
+  uint8_t Wake_Up_Src = 0, Tap_Src = 0, D6D_Src = 0, Func_Src = 0;
+
+  memset((void *)status, 0x0, sizeof(LSM6DSL_Event_Status_t));
+
+  if(ReadReg(LSM6DSL_ACC_GYRO_WAKE_UP_SRC, &Wake_Up_Src) != 0)
+  {
+    return 1;
+  }
+
+  if(ReadReg(LSM6DSL_ACC_GYRO_TAP_SRC, &Tap_Src) != 0)
+  {
+    return 1;
+  }
+
+  if(ReadReg(LSM6DSL_ACC_GYRO_D6D_SRC, &D6D_Src) != 0)
+  {
+    return 1;
+  }
+
+  if(ReadReg(LSM6DSL_ACC_GYRO_FUNC_SRC, &Func_Src) != 0)
+  {
+    return 1;
+  }
+
+  if((Wake_Up_Src & LSM6DSL_ACC_GYRO_FF_EV_STATUS_MASK))
+  {
+    status->FreeFallStatus = 1;  
+  }
+
+  if((Wake_Up_Src & LSM6DSL_ACC_GYRO_WU_EV_STATUS_MASK))
+  {
+    status->WakeUpStatus = 1;  
+  }
+
+  if((Tap_Src & LSM6DSL_ACC_GYRO_SINGLE_TAP_EV_STATUS_MASK))
+  {
+    status->TapStatus = 1;  
+  }
+
+  if((Tap_Src & LSM6DSL_ACC_GYRO_DOUBLE_TAP_EV_STATUS_MASK))
+  {
+    status->DoubleTapStatus = 1;  
+  }
+
+  if((D6D_Src & LSM6DSL_ACC_GYRO_D6D_EV_STATUS_MASK))
+  {
+    status->D6DOrientationStatus = 1;  
+  }
+
+  if((Func_Src & LSM6DSL_ACC_GYRO_PEDO_EV_STATUS_MASK))
+  {
+    status->StepStatus = 1;  
+  }
+
+  if((Func_Src & LSM6DSL_ACC_GYRO_TILT_EV_STATUS_MASK))
+  {
+    status->TiltStatus = 1;  
+  }
+
+  return 0;
+}
+
+/**
  * @brief Read the data from register
  * @param reg register address
  * @param data register data
- * @retval 0 in case of success
- * @retval 1 in case of failure
+ * @retval 0 in case of success, an error code otherwise
  */
 int LSM6DSLSensor::ReadReg( uint8_t reg, uint8_t *data )
 {
@@ -2075,8 +1936,7 @@
  * @brief Write the data to register
  * @param reg register address
  * @param data register data
- * @retval 0 in case of success
- * @retval 1 in case of failure
+ * @retval 0 in case of success, an error code otherwise
  */
 int LSM6DSLSensor::WriteReg( uint8_t reg, uint8_t data )
 {
--- a/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.h	Fri Aug 19 15:10:30 2016 +0000
+++ b/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.h	Mon Nov 21 14:57:55 2016 +0000
@@ -97,6 +97,19 @@
 #define LSM6DSL_TAP_DURATION_TIME_MID_HIGH  0x0C
 #define LSM6DSL_TAP_DURATION_TIME_HIGH      0x0F  /**< Highest value of wake up threshold */
 
+/* Typedefs ------------------------------------------------------------------*/
+
+typedef struct
+{
+  unsigned int FreeFallStatus : 1;
+  unsigned int TapStatus : 1;
+  unsigned int DoubleTapStatus : 1;
+  unsigned int WakeUpStatus : 1;
+  unsigned int StepStatus : 1;
+  unsigned int TiltStatus : 1;
+  unsigned int D6DOrientationStatus : 1;
+} LSM6DSL_Event_Status_t;
+
 /* Class Declaration ---------------------------------------------------------*/
 
 /**
@@ -130,40 +143,34 @@
     int Disable_G(void);
     int Enable_Free_Fall_Detection(void);
     int Disable_Free_Fall_Detection(void);
-    int Get_Status_Free_Fall_Detection(uint8_t *status);
     int Set_Free_Fall_Threshold(uint8_t thr);
     int Enable_Pedometer(void);
     int Disable_Pedometer(void);
-    int Get_Status_Pedometer(uint8_t *status);
     int Get_Step_Counter(uint16_t *step_count);
     int Reset_Step_Counter(void);
     int Set_Pedometer_Threshold(uint8_t thr);
     int Enable_Tilt_Detection(void);
     int Disable_Tilt_Detection(void);
-    int Get_Status_Tilt_Detection(uint8_t *status);
     int Enable_Wake_Up_Detection(void);
     int Disable_Wake_Up_Detection(void);
-    int Get_Status_Wake_Up_Detection(uint8_t *status);
     int Set_Wake_Up_Threshold(uint8_t thr);
     int Enable_Single_Tap_Detection(void);
     int Disable_Single_Tap_Detection(void);
-    int Get_Status_Single_Tap_Detection(uint8_t *status);
     int Enable_Double_Tap_Detection(void);
     int Disable_Double_Tap_Detection(void);
-    int Get_Status_Double_Tap_Detection(uint8_t *status);
     int Set_Tap_Threshold(uint8_t thr);
     int Set_Tap_Shock_Time(uint8_t time);
     int Set_Tap_Quiet_Time(uint8_t time);
     int Set_Tap_Duration_Time(uint8_t time);
     int Enable_6D_Orientation(void);
     int Disable_6D_Orientation(void);
-    int Get_Status_6D_Orientation(uint8_t *status);
     int Get_6D_Orientation_XL(uint8_t *xl);
     int Get_6D_Orientation_XH(uint8_t *xh);
     int Get_6D_Orientation_YL(uint8_t *yl);
     int Get_6D_Orientation_YH(uint8_t *yh);
     int Get_6D_Orientation_ZL(uint8_t *zl);
     int Get_6D_Orientation_ZH(uint8_t *zh);
+    int Get_Event_Status(LSM6DSL_Event_Status_t *status);
     int ReadReg(uint8_t reg, uint8_t *data);
     int WriteReg(uint8_t reg, uint8_t data);
     
--- a/main.cpp	Fri Aug 19 15:10:30 2016 +0000
+++ b/main.cpp	Mon Nov 21 14:57:55 2016 +0000
@@ -99,9 +99,9 @@
         default:
           break;
         case 1: {
-          uint8_t status = 0;
-          acc_gyro->Get_Status_Single_Tap_Detection(&status);
-          if (status) {
+          LSM6DSL_Event_Status_t status;
+          acc_gyro->Get_Event_Status(&status);
+          if (status.TapStatus) {
             /* Led blinking. */
             myled = 1;
             wait(0.1);
@@ -112,9 +112,9 @@
           }
         }
         case 2: {
-          uint8_t status = 0;
-          acc_gyro->Get_Status_Double_Tap_Detection(&status);
-          if (status) {
+          LSM6DSL_Event_Status_t status;
+          acc_gyro->Get_Event_Status(&status);
+          if (status.DoubleTapStatus) {
             /* Double Led blinking */
             myled = 1;
             wait(0.1);