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

Multi Event Demo Application based on sensor expansion board X-NUCLEO-IKS01A2

Main function is to show how to detect the free fall, tap, double tap, tilt, wake up, 6D Orientation and step 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 simulate all the events and then view the notification using an hyper terminal.
- the user button can be used to enable/disable all hardware features.

Committer:
cparata
Date:
Thu Nov 24 16:45:14 2016 +0000
Revision:
1:4b4e6a89adce
Parent:
0:763b80993db4
Child:
2:fc97ec716eba
Add possibility to choose the interrupt line for HW events

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:763b80993db4 1 /**
cparata 0:763b80993db4 2 ******************************************************************************
cparata 0:763b80993db4 3 * @file main.cpp
cparata 0:763b80993db4 4 * @author AST / EST
cparata 0:763b80993db4 5 * @version V0.0.1
cparata 0:763b80993db4 6 * @date 9-August-2016
cparata 0:763b80993db4 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A2
cparata 0:763b80993db4 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:763b80993db4 9 ******************************************************************************
cparata 0:763b80993db4 10 * @attention
cparata 0:763b80993db4 11 *
cparata 0:763b80993db4 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
cparata 0:763b80993db4 13 *
cparata 0:763b80993db4 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:763b80993db4 15 * are permitted provided that the following conditions are met:
cparata 0:763b80993db4 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:763b80993db4 17 * this list of conditions and the following disclaimer.
cparata 0:763b80993db4 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:763b80993db4 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:763b80993db4 20 * and/or other materials provided with the distribution.
cparata 0:763b80993db4 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:763b80993db4 22 * may be used to endorse or promote products derived from this software
cparata 0:763b80993db4 23 * without specific prior written permission.
cparata 0:763b80993db4 24 *
cparata 0:763b80993db4 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:763b80993db4 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:763b80993db4 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:763b80993db4 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:763b80993db4 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:763b80993db4 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:763b80993db4 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:763b80993db4 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:763b80993db4 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:763b80993db4 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:763b80993db4 35 *
cparata 0:763b80993db4 36 ******************************************************************************
cparata 0:763b80993db4 37 */
cparata 0:763b80993db4 38
cparata 0:763b80993db4 39 /* Includes */
cparata 0:763b80993db4 40 #include "mbed.h"
cparata 0:763b80993db4 41 #include "x_nucleo_iks01a2.h"
cparata 0:763b80993db4 42
cparata 0:763b80993db4 43 /* Instantiate the expansion board */
cparata 0:763b80993db4 44 static X_NUCLEO_IKS01A2 *mems_expansion_board = X_NUCLEO_IKS01A2::Instance(D14, D15);
cparata 0:763b80993db4 45
cparata 0:763b80993db4 46 /* Retrieve the composing elements of the expansion board */
cparata 0:763b80993db4 47 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 0:763b80993db4 48
cparata 0:763b80993db4 49 InterruptIn mybutton(USER_BUTTON);
cparata 0:763b80993db4 50
cparata 0:763b80993db4 51 volatile int mems_event = 0;
cparata 0:763b80993db4 52 volatile int toggle_hw_event_enable = 0;
cparata 0:763b80993db4 53 static int hw_event_is_enabled = 1;
cparata 0:763b80993db4 54 uint16_t step_count = 0;
cparata 0:763b80993db4 55
cparata 1:4b4e6a89adce 56 void pressed_cb();
cparata 0:763b80993db4 57 void INT1_cb();
cparata 0:763b80993db4 58 void INT2_cb();
cparata 0:763b80993db4 59 void sendOrientation();
cparata 0:763b80993db4 60
cparata 0:763b80993db4 61 /* Simple main function */
cparata 0:763b80993db4 62 int main() {
cparata 0:763b80993db4 63 /* Attach callback to User button press */
cparata 1:4b4e6a89adce 64 mybutton.fall(&pressed_cb);
cparata 0:763b80993db4 65 /* Attach callback to LSM6DSL INT1 */
cparata 0:763b80993db4 66 acc_gyro->AttachINT1IRQ(&INT1_cb);
cparata 0:763b80993db4 67 /* Attach callback to LSM6DSL INT2 */
cparata 0:763b80993db4 68 acc_gyro->AttachINT2IRQ(&INT2_cb);
cparata 0:763b80993db4 69
cparata 0:763b80993db4 70 /* Enable LSM6DSL accelerometer */
cparata 0:763b80993db4 71 acc_gyro->Enable_X();
cparata 0:763b80993db4 72 /* Enable HW events. */
cparata 0:763b80993db4 73 acc_gyro->Enable_Pedometer();
cparata 0:763b80993db4 74 acc_gyro->Enable_Tilt_Detection();
cparata 0:763b80993db4 75 acc_gyro->Enable_Free_Fall_Detection();
cparata 0:763b80993db4 76 acc_gyro->Enable_Single_Tap_Detection();
cparata 0:763b80993db4 77 acc_gyro->Enable_Double_Tap_Detection();
cparata 0:763b80993db4 78 acc_gyro->Enable_6D_Orientation();
cparata 0:763b80993db4 79 acc_gyro->Enable_Wake_Up_Detection();
cparata 0:763b80993db4 80
cparata 0:763b80993db4 81 printf("\r\n--- Starting new run ---\r\n");
cparata 0:763b80993db4 82
cparata 0:763b80993db4 83 while(1) {
cparata 0:763b80993db4 84 if (mems_event) {
cparata 0:763b80993db4 85 mems_event = 0;
cparata 0:763b80993db4 86 LSM6DSL_Event_Status_t status;
cparata 0:763b80993db4 87 acc_gyro->Get_Event_Status(&status);
cparata 0:763b80993db4 88 if (status.StepStatus) {
cparata 0:763b80993db4 89 /* New step detected, so print the step counter */
cparata 0:763b80993db4 90 acc_gyro->Get_Step_Counter(&step_count);
cparata 0:763b80993db4 91 printf("Step counter: %d\r\n", step_count);
cparata 0:763b80993db4 92 }
cparata 0:763b80993db4 93
cparata 0:763b80993db4 94 if (status.FreeFallStatus) {
cparata 0:763b80993db4 95 /* Output data. */
cparata 0:763b80993db4 96 printf("Free Fall Detected!\r\n");
cparata 0:763b80993db4 97 }
cparata 0:763b80993db4 98
cparata 0:763b80993db4 99 if (status.TapStatus) {
cparata 0:763b80993db4 100 /* Output data. */
cparata 0:763b80993db4 101 printf("Single Tap Detected!\r\n");
cparata 0:763b80993db4 102 }
cparata 0:763b80993db4 103
cparata 0:763b80993db4 104 if (status.DoubleTapStatus) {
cparata 0:763b80993db4 105 /* Output data. */
cparata 0:763b80993db4 106 printf("Double Tap Detected!\r\n");
cparata 0:763b80993db4 107 }
cparata 0:763b80993db4 108
cparata 0:763b80993db4 109 if (status.D6DOrientationStatus) {
cparata 0:763b80993db4 110 /* Send 6D Orientation */
cparata 0:763b80993db4 111 sendOrientation();
cparata 0:763b80993db4 112 }
cparata 0:763b80993db4 113
cparata 0:763b80993db4 114 if (status.TiltStatus) {
cparata 0:763b80993db4 115 /* Output data. */
cparata 0:763b80993db4 116 printf("Tilt Detected!\r\n");
cparata 0:763b80993db4 117 }
cparata 0:763b80993db4 118
cparata 0:763b80993db4 119 if (status.WakeUpStatus) {
cparata 0:763b80993db4 120 /* Output data. */
cparata 0:763b80993db4 121 printf("Wake Up Detected!\r\n");
cparata 0:763b80993db4 122 }
cparata 0:763b80993db4 123 }
cparata 0:763b80993db4 124
cparata 0:763b80993db4 125 if(toggle_hw_event_enable) {
cparata 0:763b80993db4 126 toggle_hw_event_enable = 0;
cparata 0:763b80993db4 127 if(hw_event_is_enabled == 0) {
cparata 0:763b80993db4 128 /* Enable HW events. */
cparata 0:763b80993db4 129 acc_gyro->Enable_Pedometer();
cparata 0:763b80993db4 130 acc_gyro->Enable_Tilt_Detection();
cparata 0:763b80993db4 131 acc_gyro->Enable_Free_Fall_Detection();
cparata 0:763b80993db4 132 acc_gyro->Enable_Single_Tap_Detection();
cparata 0:763b80993db4 133 acc_gyro->Enable_Double_Tap_Detection();
cparata 0:763b80993db4 134 acc_gyro->Enable_6D_Orientation();
cparata 0:763b80993db4 135 acc_gyro->Enable_Wake_Up_Detection();
cparata 0:763b80993db4 136 hw_event_is_enabled = 1;
cparata 0:763b80993db4 137 } else {
cparata 0:763b80993db4 138 acc_gyro->Disable_Pedometer();
cparata 0:763b80993db4 139 acc_gyro->Disable_Tilt_Detection();
cparata 0:763b80993db4 140 acc_gyro->Disable_Free_Fall_Detection();
cparata 0:763b80993db4 141 acc_gyro->Disable_Single_Tap_Detection();
cparata 0:763b80993db4 142 acc_gyro->Disable_Double_Tap_Detection();
cparata 0:763b80993db4 143 acc_gyro->Disable_6D_Orientation();
cparata 0:763b80993db4 144 acc_gyro->Disable_Wake_Up_Detection();
cparata 0:763b80993db4 145 hw_event_is_enabled = 0;
cparata 0:763b80993db4 146 }
cparata 0:763b80993db4 147 }
cparata 0:763b80993db4 148 }
cparata 0:763b80993db4 149 }
cparata 0:763b80993db4 150
cparata 1:4b4e6a89adce 151 void pressed_cb() {
cparata 0:763b80993db4 152 toggle_hw_event_enable = 1;
cparata 0:763b80993db4 153 }
cparata 0:763b80993db4 154
cparata 0:763b80993db4 155 void INT1_cb() {
cparata 0:763b80993db4 156 mems_event = 1;
cparata 0:763b80993db4 157 }
cparata 0:763b80993db4 158
cparata 0:763b80993db4 159 void INT2_cb() {
cparata 0:763b80993db4 160 mems_event = 1;
cparata 0:763b80993db4 161 }
cparata 0:763b80993db4 162
cparata 0:763b80993db4 163 void sendOrientation() {
cparata 0:763b80993db4 164 uint8_t xl = 0;
cparata 0:763b80993db4 165 uint8_t xh = 0;
cparata 0:763b80993db4 166 uint8_t yl = 0;
cparata 0:763b80993db4 167 uint8_t yh = 0;
cparata 0:763b80993db4 168 uint8_t zl = 0;
cparata 0:763b80993db4 169 uint8_t zh = 0;
cparata 0:763b80993db4 170
cparata 0:763b80993db4 171 acc_gyro->Get_6D_Orientation_XL(&xl);
cparata 0:763b80993db4 172 acc_gyro->Get_6D_Orientation_XH(&xh);
cparata 0:763b80993db4 173 acc_gyro->Get_6D_Orientation_YL(&yl);
cparata 0:763b80993db4 174 acc_gyro->Get_6D_Orientation_YH(&yh);
cparata 0:763b80993db4 175 acc_gyro->Get_6D_Orientation_ZL(&zl);
cparata 0:763b80993db4 176 acc_gyro->Get_6D_Orientation_ZH(&zh);
cparata 0:763b80993db4 177
cparata 0:763b80993db4 178 if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 )
cparata 0:763b80993db4 179 {
cparata 0:763b80993db4 180 printf( "\r\n ________________ " \
cparata 0:763b80993db4 181 "\r\n | | " \
cparata 0:763b80993db4 182 "\r\n | * | " \
cparata 0:763b80993db4 183 "\r\n | | " \
cparata 0:763b80993db4 184 "\r\n | | " \
cparata 0:763b80993db4 185 "\r\n | | " \
cparata 0:763b80993db4 186 "\r\n | | " \
cparata 0:763b80993db4 187 "\r\n |________________| \r\n" );
cparata 0:763b80993db4 188 }
cparata 0:763b80993db4 189
cparata 0:763b80993db4 190 else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
cparata 0:763b80993db4 191 {
cparata 0:763b80993db4 192 printf( "\r\n ________________ " \
cparata 0:763b80993db4 193 "\r\n | | " \
cparata 0:763b80993db4 194 "\r\n | * | " \
cparata 0:763b80993db4 195 "\r\n | | " \
cparata 0:763b80993db4 196 "\r\n | | " \
cparata 0:763b80993db4 197 "\r\n | | " \
cparata 0:763b80993db4 198 "\r\n | | " \
cparata 0:763b80993db4 199 "\r\n |________________| \r\n" );
cparata 0:763b80993db4 200 }
cparata 0:763b80993db4 201
cparata 0:763b80993db4 202 else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 )
cparata 0:763b80993db4 203 {
cparata 0:763b80993db4 204 printf( "\r\n ________________ " \
cparata 0:763b80993db4 205 "\r\n | | " \
cparata 0:763b80993db4 206 "\r\n | | " \
cparata 0:763b80993db4 207 "\r\n | | " \
cparata 0:763b80993db4 208 "\r\n | | " \
cparata 0:763b80993db4 209 "\r\n | | " \
cparata 0:763b80993db4 210 "\r\n | * | " \
cparata 0:763b80993db4 211 "\r\n |________________| \r\n" );
cparata 0:763b80993db4 212 }
cparata 0:763b80993db4 213
cparata 0:763b80993db4 214 else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 )
cparata 0:763b80993db4 215 {
cparata 0:763b80993db4 216 printf( "\r\n ________________ " \
cparata 0:763b80993db4 217 "\r\n | | " \
cparata 0:763b80993db4 218 "\r\n | | " \
cparata 0:763b80993db4 219 "\r\n | | " \
cparata 0:763b80993db4 220 "\r\n | | " \
cparata 0:763b80993db4 221 "\r\n | | " \
cparata 0:763b80993db4 222 "\r\n | * | " \
cparata 0:763b80993db4 223 "\r\n |________________| \r\n" );
cparata 0:763b80993db4 224 }
cparata 0:763b80993db4 225
cparata 0:763b80993db4 226 else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 )
cparata 0:763b80993db4 227 {
cparata 0:763b80993db4 228 printf( "\r\n __*_____________ " \
cparata 0:763b80993db4 229 "\r\n |________________| \r\n" );
cparata 0:763b80993db4 230 }
cparata 0:763b80993db4 231
cparata 0:763b80993db4 232 else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 )
cparata 0:763b80993db4 233 {
cparata 0:763b80993db4 234 printf( "\r\n ________________ " \
cparata 0:763b80993db4 235 "\r\n |________________| " \
cparata 0:763b80993db4 236 "\r\n * \r\n" );
cparata 0:763b80993db4 237 }
cparata 0:763b80993db4 238
cparata 0:763b80993db4 239 else
cparata 0:763b80993db4 240 {
cparata 0:763b80993db4 241 printf( "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" );
cparata 0:763b80993db4 242 }
cparata 0:763b80993db4 243 }