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:
Wed Nov 23 16:46:14 2016 +0000
Parent:
5:7da0dc121ec6
Child:
7:4f512b172555
Commit message:
Move interrupt settings inside component drivers

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
X_NUCLEO_IKS01A2/x_nucleo_iks01a2.cpp 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	Wed Nov 23 15:54:32 2016 +0000
+++ b/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.cpp	Wed Nov 23 16:46:14 2016 +0000
@@ -51,7 +51,7 @@
  * @param i2c object of an helper class which handles the I2C peripheral
  * @param address the address of the component's instance
  */
-LSM6DSLSensor::LSM6DSLSensor(DevI2C &i2c) : dev_i2c(i2c)
+LSM6DSLSensor::LSM6DSLSensor(DevI2C &i2c, PinName INT1_pin, PinName INT2_pin) : dev_i2c(i2c), INT1_irq(INT1_pin), INT2_irq(INT2_pin)
 {
   address = LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH; 
 };
@@ -60,7 +60,7 @@
  * @param i2c object of an helper class which handles the I2C peripheral
  * @param address the address of the component's instance
  */
-LSM6DSLSensor::LSM6DSLSensor(DevI2C &i2c, uint8_t address) : dev_i2c(i2c), address(address)
+LSM6DSLSensor::LSM6DSLSensor(DevI2C &i2c, PinName INT1_pin, PinName INT2_pin, uint8_t address) : dev_i2c(i2c), INT1_irq(INT1_pin), INT2_irq(INT2_pin), address(address)
 {
 
 };
--- a/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.h	Wed Nov 23 15:54:32 2016 +0000
+++ b/X_NUCLEO_IKS01A2/Components/LSM6DSLSensor/LSM6DSLSensor.h	Wed Nov 23 16:46:14 2016 +0000
@@ -119,8 +119,8 @@
 class LSM6DSLSensor : public MotionSensor, public GyroSensor
 {
   public:
-    LSM6DSLSensor(DevI2C &i2c);
-    LSM6DSLSensor(DevI2C &i2c, uint8_t address);
+    LSM6DSLSensor(DevI2C &i2c, PinName INT1_pin, PinName INT2_pin);
+    LSM6DSLSensor(DevI2C &i2c, PinName INT1_pin, PinName INT2_pin, uint8_t address);
     virtual int Init(void *init);
     virtual int ReadID(uint8_t *id);
     virtual int Get_X_Axes(int32_t *pData);
@@ -175,6 +175,66 @@
     int WriteReg(uint8_t reg, uint8_t data);
     
     /**
+     * @brief  Attaching an interrupt handler to the INT1 interrupt.
+     * @param  fptr An interrupt handler.
+     * @retval None.
+     */
+    void AttachINT1IRQ(void (*fptr)(void))
+    {
+        INT1_irq.rise(fptr);
+    }
+
+    /**
+     * @brief  Enabling the INT1 interrupt handling.
+     * @param  None.
+     * @retval None.
+     */
+    void EnableINT1IRQ(void)
+    {
+        INT1_irq.enable_irq();
+    }
+    
+    /**
+     * @brief  Disabling the INT1 interrupt handling.
+     * @param  None.
+     * @retval None.
+     */
+    void DisableINT1IRQ(void)
+    {
+        INT1_irq.disable_irq();
+    }
+    
+    /**
+     * @brief  Attaching an interrupt handler to the INT2 interrupt.
+     * @param  fptr An interrupt handler.
+     * @retval None.
+     */
+    void AttachINT2IRQ(void (*fptr)(void))
+    {
+        INT2_irq.rise(fptr);
+    }
+
+    /**
+     * @brief  Enabling the INT2 interrupt handling.
+     * @param  None.
+     * @retval None.
+     */
+    void EnableINT2IRQ(void)
+    {
+        INT2_irq.enable_irq();
+    }
+    
+    /**
+     * @brief  Disabling the INT2 interrupt handling.
+     * @param  None.
+     * @retval None.
+     */
+    void DisableINT2IRQ(void)
+    {
+        INT2_irq.disable_irq();
+    }
+    
+    /**
      * @brief Utility function to read data.
      * @param  pBuffer: pointer to data to be read.
      * @param  RegisterAddr: specifies internal address register to be read.
@@ -206,7 +266,10 @@
 
     /* Helper classes. */
     DevI2C &dev_i2c;
-    
+
+    InterruptIn INT1_irq;
+    InterruptIn INT2_irq;
+
     /* Configuration */
     uint8_t address;
     
--- a/X_NUCLEO_IKS01A2/x_nucleo_iks01a2.cpp	Wed Nov 23 15:54:32 2016 +0000
+++ b/X_NUCLEO_IKS01A2/x_nucleo_iks01a2.cpp	Wed Nov 23 16:46:14 2016 +0000
@@ -52,7 +52,7 @@
     magnetometer(new LSM303AGR_MAG_Sensor(*dev_i2c)),
     accelerometer(new LSM303AGR_ACC_Sensor(*dev_i2c)),
     pt_sensor(new LPS22HBSensor(*dev_i2c)),
-    acc_gyro(new LSM6DSLSensor(*dev_i2c))
+    acc_gyro(new LSM6DSLSensor(*dev_i2c, IKS01A2_PIN_LSM6DSL_INT1, IKS01A2_PIN_LSM6DSL_INT2))
 { 
   ht_sensor->Init(NULL);
   magnetometer->Init(NULL);
--- a/main.cpp	Wed Nov 23 15:54:32 2016 +0000
+++ b/main.cpp	Wed Nov 23 16:46:14 2016 +0000
@@ -46,7 +46,6 @@
 /* Retrieve the composing elements of the expansion board */
 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
 
-InterruptIn lsm6dsl_int1(IKS01A2_PIN_LSM6DSL_INT1);
 InterruptIn mybutton(USER_BUTTON);
 DigitalOut myled(LED1);
 
@@ -62,7 +61,7 @@
   /* Attach callback to User button press */
   mybutton.fall(&pressed);
   /* Attach callback to LSM6DSL INT1 */
-  lsm6dsl_int1.rise(&tap_cb);
+  acc_gyro->AttachINT1IRQ(&tap_cb);
   
   /* Enable LSM6DSL accelerometer */
   acc_gyro->Enable_X();