Example of multiple event detection for LSM6DSL in X-NUCLEO-IKS01A2

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of MultiEvent_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 
00051 volatile int mems_event = 0;
00052 volatile int toggle_hw_event_enable = 0;
00053 static int hw_event_is_enabled = 1;
00054 uint16_t step_count = 0;
00055 
00056 /* User button callback. */
00057 void pressed_cb() {
00058   toggle_hw_event_enable = 1;
00059 }
00060 
00061 /* Interrupt 1 callback. */
00062 void int1_cb() {
00063   mems_event = 1;
00064 }
00065 
00066 /* Interrupt 2 callback. */
00067 void int2_cb() {
00068   mems_event = 1;
00069 }
00070 
00071 /* Print the orientation. */
00072 void send_orientation() {
00073   uint8_t xl = 0;
00074   uint8_t xh = 0;
00075   uint8_t yl = 0;
00076   uint8_t yh = 0;
00077   uint8_t zl = 0;
00078   uint8_t zh = 0;
00079   
00080   acc_gyro->get_6d_orientation_xl(&xl);
00081   acc_gyro->get_6d_orientation_xh(&xh);
00082   acc_gyro->get_6d_orientation_yl(&yl);
00083   acc_gyro->get_6d_orientation_yh(&yh);
00084   acc_gyro->get_6d_orientation_zl(&zl);
00085   acc_gyro->get_6d_orientation_zh(&zh);
00086   
00087   if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 ) {
00088     printf( "\r\n  ________________  " \
00089             "\r\n |                | " \
00090             "\r\n |  *             | " \
00091             "\r\n |                | " \
00092             "\r\n |                | " \
00093             "\r\n |                | " \
00094             "\r\n |                | " \
00095             "\r\n |________________| \r\n" );
00096   }
00097   
00098   else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) {
00099     printf( "\r\n  ________________  " \
00100             "\r\n |                | " \
00101             "\r\n |             *  | " \
00102             "\r\n |                | " \
00103             "\r\n |                | " \
00104             "\r\n |                | " \
00105             "\r\n |                | " \
00106             "\r\n |________________| \r\n" );
00107   }
00108   
00109   else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 ) {
00110     printf( "\r\n  ________________  " \
00111             "\r\n |                | " \
00112             "\r\n |                | " \
00113             "\r\n |                | " \
00114             "\r\n |                | " \
00115             "\r\n |                | " \
00116             "\r\n |  *             | " \
00117             "\r\n |________________| \r\n" );
00118   }
00119   
00120   else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) {
00121     printf( "\r\n  ________________  " \
00122             "\r\n |                | " \
00123             "\r\n |                | " \
00124             "\r\n |                | " \
00125             "\r\n |                | " \
00126             "\r\n |                | " \
00127             "\r\n |             *  | " \
00128             "\r\n |________________| \r\n" );
00129   }
00130   
00131   else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) {
00132     printf( "\r\n  __*_____________  " \
00133             "\r\n |________________| \r\n" );
00134   }
00135   
00136   else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 ) {
00137     printf( "\r\n  ________________  " \
00138             "\r\n |________________| " \
00139             "\r\n    *               \r\n" );
00140   }
00141   
00142   else {
00143     printf( "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
00144   }
00145 }
00146 
00147 /* Simple main function */
00148 int main() {
00149   /* Attach callback to User button press */
00150   mybutton.fall(&pressed_cb);
00151   /* Attach callback to LSM6DSL INT1 */
00152   acc_gyro->attach_int1_irq(&int1_cb);
00153   /* Attach callback to LSM6DSL INT2 */
00154   acc_gyro->attach_int2_irq(&int2_cb);
00155   
00156   /* Enable LSM6DSL accelerometer */
00157   acc_gyro->enable_x();
00158   /* Enable HW events. */
00159   acc_gyro->enable_pedometer();
00160   acc_gyro->enable_tilt_detection();
00161   acc_gyro->enable_free_fall_detection();
00162   acc_gyro->enable_single_tap_detection();
00163   acc_gyro->enable_double_tap_detection();
00164   acc_gyro->enable_6d_orientation();
00165   acc_gyro->enable_wake_up_detection();
00166   
00167   printf("\r\n--- Starting new run ---\r\n");
00168  
00169   while(1) {
00170     if (mems_event) {
00171       mems_event = 0;
00172       LSM6DSL_Event_Status_t status;
00173       acc_gyro->get_event_status(&status);
00174       if (status.StepStatus) {
00175         /* New step detected, so print the step counter */
00176         acc_gyro->get_step_counter(&step_count);
00177         printf("Step counter: %d\r\n", step_count);
00178       }
00179 
00180       if (status.FreeFallStatus) {
00181         /* Output data. */
00182         printf("Free Fall Detected!\r\n");
00183       }
00184 
00185       if (status.TapStatus) {
00186         /* Output data. */
00187         printf("Single Tap Detected!\r\n");
00188       }
00189 
00190       if (status.DoubleTapStatus) {
00191         /* Output data. */
00192         printf("Double Tap Detected!\r\n");
00193       }
00194 
00195       if (status.D6DOrientationStatus) {
00196         /* Send 6D Orientation */
00197         send_orientation();
00198       }
00199 
00200       if (status.TiltStatus) {
00201         /* Output data. */
00202         printf("Tilt Detected!\r\n");
00203       }
00204 
00205       if (status.WakeUpStatus) {
00206         /* Output data. */
00207         printf("Wake Up Detected!\r\n");
00208       }
00209     }
00210 
00211     if (toggle_hw_event_enable) {
00212       toggle_hw_event_enable = 0;
00213       if (hw_event_is_enabled == 0) {
00214         /* Enable HW events. */
00215         acc_gyro->enable_pedometer();
00216         acc_gyro->enable_tilt_detection();
00217         acc_gyro->enable_free_fall_detection();
00218         acc_gyro->enable_single_tap_detection();
00219         acc_gyro->enable_double_tap_detection();
00220         acc_gyro->enable_6d_orientation();
00221         acc_gyro->enable_wake_up_detection();
00222         hw_event_is_enabled = 1;
00223       } else {
00224         acc_gyro->disable_pedometer();
00225         acc_gyro->disable_tilt_detection();
00226         acc_gyro->disable_free_fall_detection();
00227         acc_gyro->disable_single_tap_detection();
00228         acc_gyro->disable_double_tap_detection();
00229         acc_gyro->disable_6d_orientation();
00230         acc_gyro->disable_wake_up_detection();
00231         hw_event_is_enabled = 0;
00232       }
00233     }
00234   }
00235 }