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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    2-December-2016
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A2 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */ 
00038 
00039 /* Includes */
00040 #include "mbed.h"
00041 #include "XNucleoIKS01A2.h"
00042 
00043 /* Instantiate the expansion board */
00044 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00045 
00046 /* Retrieve the composing elements of the expansion board */
00047 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00048 
00049 InterruptIn mybutton(USER_BUTTON);
00050 DigitalOut myled(LED1);
00051 
00052 volatile int mems_event = 0;
00053 volatile int change_mode = 0;
00054 static int mode = 1; /* 0 means No tap, 1 means Single Tap enabled, 2 means Double Tap enabled*/
00055 
00056 /* User button callback. */
00057 void pressed_cb() {
00058   change_mode = 1;
00059 }
00060 
00061 /* Interrupt 1 callback. */
00062 void int1_cb() {
00063   mems_event = 1;
00064 }
00065 
00066 /* Simple main function */
00067 int main() {
00068   /* Attach callback to User button press */
00069   mybutton.fall(&pressed_cb);
00070   /* Attach callback to LSM6DSL INT1 */
00071   acc_gyro->attach_int1_irq(&int1_cb);
00072   
00073   /* Enable LSM6DSL accelerometer */
00074   acc_gyro->enable_x();
00075   /* Enable Single Tap Detection. */
00076   acc_gyro->enable_single_tap_detection();
00077   
00078   printf("\r\n--- Starting new run ---\r\n");
00079  
00080   while(1) {
00081     if(change_mode) {
00082       change_mode = 0;
00083       mode = ((mode + 1) % 3);
00084       switch(mode) {
00085         case 0:
00086           /* Disable Double Tap */
00087           acc_gyro->disable_double_tap_detection();
00088           break;
00089         case 1:
00090           /* Enable Single Tap */
00091           acc_gyro->enable_single_tap_detection();
00092           break;
00093         case 2:
00094           /* Disable Single Tap and Enable Double Tap */
00095           acc_gyro->disable_single_tap_detection();
00096           acc_gyro->enable_double_tap_detection();
00097           break;
00098       }
00099     }
00100  
00101     if (mems_event) {
00102       mems_event = 0;
00103       switch(mode) {
00104         case 0:
00105         default:
00106           break;
00107         case 1: {
00108           LSM6DSL_Event_Status_t status;
00109           acc_gyro->get_event_status(&status);
00110           if (status.TapStatus) {
00111             /* Led blinking. */
00112             myled = 1;
00113             wait(0.1);
00114             myled = 0;
00115 
00116             /* Output data. */
00117             printf("Single Tap Detected!\r\n");
00118           }
00119           break;
00120         }
00121         case 2: {
00122           LSM6DSL_Event_Status_t status;
00123           acc_gyro->get_event_status(&status);
00124           if (status.DoubleTapStatus) {
00125             /* Double Led blinking */
00126             myled = 1;
00127             wait(0.1);
00128             myled = 0;
00129             wait(0.1);
00130             myled = 1;
00131             wait(0.1);
00132             myled = 0;
00133 
00134             /* Output data. */
00135             printf("Double Tap Detected!\r\n");
00136           }
00137           break;
00138         }
00139       }
00140     }
00141   }
00142 }