ST / Mbed OS SingleDoubleTap_LSM6DSO_IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

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  SRA
00005  * @version V1.0.0
00006  * @date    5-March-2019
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A3
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2019 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 "rtos.h"
00042 #include "XNucleoIKS01A3.h"
00043 
00044 /* Instantiate the expansion board */
00045 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
00046 
00047 /* Retrieve the composing elements of the expansion board */
00048 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
00049 
00050 InterruptIn mybutton(BUTTON1);
00051 DigitalOut myled(LED1);
00052 
00053 volatile int mems_event = 0;
00054 volatile int change_mode = 0;
00055 static int mode = 1; /* 0 means No tap, 1 means Single Tap enabled, 2 means Double Tap enabled*/
00056 
00057 /* User button callback. */
00058 void pressed_cb()
00059 {
00060     change_mode = 1;
00061 }
00062 
00063 /* Interrupt 1 callback. */
00064 void int1_cb()
00065 {
00066     mems_event = 1;
00067 }
00068 
00069 /* Simple main function */
00070 int main()
00071 {
00072     /* Attach callback to User button press */
00073     mybutton.fall(&pressed_cb);
00074     /* Attach callback to LSM6DSO INT1 */
00075     acc_gyro->attach_int1_irq(&int1_cb);
00076 
00077     /* Enable LSM6DSO accelerometer */
00078     acc_gyro->enable_x();
00079     /* Enable Single Tap Detection. */
00080     acc_gyro->enable_single_tap_detection();
00081 
00082     printf("\r\n--- Starting new run ---\r\n");
00083 
00084     while (1) {
00085         if (change_mode) {
00086             change_mode = 0;
00087             mode = ((mode + 1) % 3);
00088             switch (mode) {
00089                 case 0:
00090                     /* Disable Double Tap */
00091                     acc_gyro->disable_double_tap_detection();
00092                     break;
00093                 case 1:
00094                     /* Enable Single Tap */
00095                     acc_gyro->enable_single_tap_detection();
00096                     break;
00097                 case 2:
00098                     /* Disable Single Tap and Enable Double Tap */
00099                     acc_gyro->disable_single_tap_detection();
00100                     acc_gyro->enable_double_tap_detection();
00101                     break;
00102             }
00103         }
00104 
00105         if (mems_event) {
00106             mems_event = 0;
00107             switch (mode) {
00108                 case 0:
00109                 default:
00110                     break;
00111                 case 1: {
00112                     LSM6DSO_Event_Status_t status;
00113                     acc_gyro->get_event_status(&status);
00114                     if (status.TapStatus) {
00115                         /* Led blinking. */
00116                         myled = 1;
00117                         ThisThread::sleep_for(100);
00118                         myled = 0;
00119 
00120                         /* Output data. */
00121                         printf("Single Tap Detected!\r\n");
00122                     }
00123                     break;
00124                 }
00125                 case 2: {
00126                     LSM6DSO_Event_Status_t status;
00127                     acc_gyro->get_event_status(&status);
00128                     if (status.DoubleTapStatus) {
00129                         /* Double Led blinking */
00130                         myled = 1;
00131                         ThisThread::sleep_for(100);
00132                         myled = 0;
00133                         ThisThread::sleep_for(100);
00134                         myled = 1;
00135                         ThisThread::sleep_for(100);
00136                         myled = 0;
00137 
00138                         /* Output data. */
00139                         printf("Double Tap Detected!\r\n");
00140                     }
00141                     break;
00142                 }
00143             }
00144         }
00145     }
00146 }