![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
Example of multiple event detection for LSM6DSL in X-NUCLEO-IKS01A2
Dependencies: X_NUCLEO_IKS01A2 mbed
Fork of MultiEvent_IKS01A2 by
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.
main.cpp@9:10df66a84de2, 2017-09-07 (annotated)
- Committer:
- nikapov
- Date:
- Thu Sep 07 15:22:12 2017 +0000
- Revision:
- 9:10df66a84de2
- Parent:
- 6:80b6ffe56b29
Update libs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:763b80993db4 | 1 | /** |
cparata | 0:763b80993db4 | 2 | ****************************************************************************** |
cparata | 0:763b80993db4 | 3 | * @file main.cpp |
cparata | 2:fc97ec716eba | 4 | * @author CLab |
cparata | 2:fc97ec716eba | 5 | * @version V1.0.0 |
cparata | 2:fc97ec716eba | 6 | * @date 2-December-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>© 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" |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 41 | #include "XNucleoIKS01A2.h" |
cparata | 0:763b80993db4 | 42 | |
cparata | 0:763b80993db4 | 43 | /* Instantiate the expansion board */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 44 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
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 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 56 | /* User button callback. */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 57 | void pressed_cb() { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 58 | toggle_hw_event_enable = 1; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 59 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 60 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 61 | /* Interrupt 1 callback. */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 62 | void int1_cb() { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 63 | mems_event = 1; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 64 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 65 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 66 | /* Interrupt 2 callback. */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 67 | void int2_cb() { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 68 | mems_event = 1; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 69 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 70 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 71 | /* Print the orientation. */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 72 | void send_orientation() { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 73 | uint8_t xl = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 74 | uint8_t xh = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 75 | uint8_t yl = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 76 | uint8_t yh = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 77 | uint8_t zl = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 78 | uint8_t zh = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 79 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 80 | acc_gyro->get_6d_orientation_xl(&xl); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 81 | acc_gyro->get_6d_orientation_xh(&xh); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 82 | acc_gyro->get_6d_orientation_yl(&yl); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 83 | acc_gyro->get_6d_orientation_yh(&yh); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 84 | acc_gyro->get_6d_orientation_zl(&zl); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 85 | acc_gyro->get_6d_orientation_zh(&zh); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 86 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 87 | if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0 ) { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 88 | printf( "\r\n ________________ " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 89 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 90 | "\r\n | * | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 91 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 92 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 93 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 94 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 95 | "\r\n |________________| \r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 96 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 97 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 98 | else if ( xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 99 | printf( "\r\n ________________ " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 100 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 101 | "\r\n | * | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 102 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 103 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 104 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 105 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 106 | "\r\n |________________| \r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 107 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 108 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 109 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0 ) { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 110 | printf( "\r\n ________________ " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 111 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 112 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 113 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 114 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 115 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 116 | "\r\n | * | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 117 | "\r\n |________________| \r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 118 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 119 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 120 | else if ( xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0 ) { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 121 | printf( "\r\n ________________ " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 122 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 123 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 124 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 125 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 126 | "\r\n | | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 127 | "\r\n | * | " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 128 | "\r\n |________________| \r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 129 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 130 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 131 | else if ( xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1 ) { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 132 | printf( "\r\n __*_____________ " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 133 | "\r\n |________________| \r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 134 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 135 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 136 | else if ( xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0 ) { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 137 | printf( "\r\n ________________ " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 138 | "\r\n |________________| " \ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 139 | "\r\n * \r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 140 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 141 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 142 | else { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 143 | printf( "None of the 6D orientation axes is set in LSM6DSL - accelerometer.\r\n" ); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 144 | } |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 145 | } |
cparata | 0:763b80993db4 | 146 | |
cparata | 0:763b80993db4 | 147 | /* Simple main function */ |
cparata | 0:763b80993db4 | 148 | int main() { |
cparata | 0:763b80993db4 | 149 | /* Attach callback to User button press */ |
cparata | 1:4b4e6a89adce | 150 | mybutton.fall(&pressed_cb); |
cparata | 0:763b80993db4 | 151 | /* Attach callback to LSM6DSL INT1 */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 152 | acc_gyro->attach_int1_irq(&int1_cb); |
cparata | 0:763b80993db4 | 153 | /* Attach callback to LSM6DSL INT2 */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 154 | acc_gyro->attach_int2_irq(&int2_cb); |
cparata | 0:763b80993db4 | 155 | |
cparata | 0:763b80993db4 | 156 | /* Enable LSM6DSL accelerometer */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 157 | acc_gyro->enable_x(); |
cparata | 0:763b80993db4 | 158 | /* Enable HW events. */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 159 | acc_gyro->enable_pedometer(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 160 | acc_gyro->enable_tilt_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 161 | acc_gyro->enable_free_fall_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 162 | acc_gyro->enable_single_tap_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 163 | acc_gyro->enable_double_tap_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 164 | acc_gyro->enable_6d_orientation(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 165 | acc_gyro->enable_wake_up_detection(); |
cparata | 0:763b80993db4 | 166 | |
cparata | 0:763b80993db4 | 167 | printf("\r\n--- Starting new run ---\r\n"); |
cparata | 0:763b80993db4 | 168 | |
cparata | 0:763b80993db4 | 169 | while(1) { |
cparata | 0:763b80993db4 | 170 | if (mems_event) { |
cparata | 0:763b80993db4 | 171 | mems_event = 0; |
cparata | 0:763b80993db4 | 172 | LSM6DSL_Event_Status_t status; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 173 | acc_gyro->get_event_status(&status); |
cparata | 0:763b80993db4 | 174 | if (status.StepStatus) { |
cparata | 0:763b80993db4 | 175 | /* New step detected, so print the step counter */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 176 | acc_gyro->get_step_counter(&step_count); |
cparata | 0:763b80993db4 | 177 | printf("Step counter: %d\r\n", step_count); |
cparata | 0:763b80993db4 | 178 | } |
cparata | 0:763b80993db4 | 179 | |
cparata | 0:763b80993db4 | 180 | if (status.FreeFallStatus) { |
cparata | 0:763b80993db4 | 181 | /* Output data. */ |
cparata | 0:763b80993db4 | 182 | printf("Free Fall Detected!\r\n"); |
cparata | 0:763b80993db4 | 183 | } |
cparata | 0:763b80993db4 | 184 | |
cparata | 0:763b80993db4 | 185 | if (status.TapStatus) { |
cparata | 0:763b80993db4 | 186 | /* Output data. */ |
cparata | 0:763b80993db4 | 187 | printf("Single Tap Detected!\r\n"); |
cparata | 0:763b80993db4 | 188 | } |
cparata | 0:763b80993db4 | 189 | |
cparata | 0:763b80993db4 | 190 | if (status.DoubleTapStatus) { |
cparata | 0:763b80993db4 | 191 | /* Output data. */ |
cparata | 0:763b80993db4 | 192 | printf("Double Tap Detected!\r\n"); |
cparata | 0:763b80993db4 | 193 | } |
cparata | 0:763b80993db4 | 194 | |
cparata | 0:763b80993db4 | 195 | if (status.D6DOrientationStatus) { |
cparata | 0:763b80993db4 | 196 | /* Send 6D Orientation */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 197 | send_orientation(); |
cparata | 0:763b80993db4 | 198 | } |
cparata | 0:763b80993db4 | 199 | |
cparata | 0:763b80993db4 | 200 | if (status.TiltStatus) { |
cparata | 0:763b80993db4 | 201 | /* Output data. */ |
cparata | 0:763b80993db4 | 202 | printf("Tilt Detected!\r\n"); |
cparata | 0:763b80993db4 | 203 | } |
cparata | 0:763b80993db4 | 204 | |
cparata | 0:763b80993db4 | 205 | if (status.WakeUpStatus) { |
cparata | 0:763b80993db4 | 206 | /* Output data. */ |
cparata | 0:763b80993db4 | 207 | printf("Wake Up Detected!\r\n"); |
cparata | 0:763b80993db4 | 208 | } |
cparata | 0:763b80993db4 | 209 | } |
cparata | 0:763b80993db4 | 210 | |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 211 | if (toggle_hw_event_enable) { |
cparata | 0:763b80993db4 | 212 | toggle_hw_event_enable = 0; |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 213 | if (hw_event_is_enabled == 0) { |
cparata | 0:763b80993db4 | 214 | /* Enable HW events. */ |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 215 | acc_gyro->enable_pedometer(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 216 | acc_gyro->enable_tilt_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 217 | acc_gyro->enable_free_fall_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 218 | acc_gyro->enable_single_tap_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 219 | acc_gyro->enable_double_tap_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 220 | acc_gyro->enable_6d_orientation(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 221 | acc_gyro->enable_wake_up_detection(); |
cparata | 0:763b80993db4 | 222 | hw_event_is_enabled = 1; |
cparata | 0:763b80993db4 | 223 | } else { |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 224 | acc_gyro->disable_pedometer(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 225 | acc_gyro->disable_tilt_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 226 | acc_gyro->disable_free_fall_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 227 | acc_gyro->disable_single_tap_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 228 | acc_gyro->disable_double_tap_detection(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 229 | acc_gyro->disable_6d_orientation(); |
davide.aliprandi@st.com | 6:80b6ffe56b29 | 230 | acc_gyro->disable_wake_up_detection(); |
cparata | 0:763b80993db4 | 231 | hw_event_is_enabled = 0; |
cparata | 0:763b80993db4 | 232 | } |
cparata | 0:763b80993db4 | 233 | } |
cparata | 0:763b80993db4 | 234 | } |
cparata | 0:763b80993db4 | 235 | } |