Lin Team / Mbed OS WakeUp_LSM6DSO

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 "XNucleoIKS01A3.h"
00042 
00043 /* Instantiate the expansion board */
00044 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
00045 
00046 /* Retrieve the composing elements of the expansion board */
00047 static LSM6DSOSensor *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 toggle_wake_up_enable = 0;
00054 static int wake_up_is_enabled = 1;
00055 
00056 void pressed_cb();
00057 void INT1_cb();
00058 
00059 /* Simple main function */
00060 int main()
00061 {
00062     /* Attach callback to User button press */
00063     mybutton.fall(&pressed_cb);
00064     /* Attach callback to LSM6DSO INT1 */
00065     acc_gyro->attach_int1_irq(&INT1_cb);
00066 
00067     /* Enable LSM6DSO accelerometer */
00068     acc_gyro->enable_x();
00069     /* Enable Wake-Up Detection. */
00070     acc_gyro->enable_wake_up_detection();
00071 
00072 
00073  
00074 
00075     printf("\r\n--- Starting new run ---\r\n");
00076 
00077     while (1) {
00078         if (toggle_wake_up_enable) {
00079             toggle_wake_up_enable = 0;
00080             if (wake_up_is_enabled == 0) {
00081                 acc_gyro->enable_wake_up_detection();
00082                 wake_up_is_enabled = 1;
00083             } else {
00084                 acc_gyro->disable_wake_up_detection();
00085                 wake_up_is_enabled = 0;
00086             }
00087         }
00088 
00089         if (mems_event) {
00090             mems_event = 0;
00091             LSM6DSO_Event_Status_t status;
00092             acc_gyro->get_event_status(&status);
00093             if (status.WakeUpStatus) {
00094                 /* Led blinking. */
00095                 myled = 1;
00096                 wait(0.2);
00097                 myled = 0;
00098 
00099                 /* Output data. */
00100                 while(1)
00101                 {
00102                 printf("Wake Up Detected!\r\n");
00103                }
00104             }
00105         }
00106     }
00107 }
00108 
00109 void pressed_cb()
00110 {
00111     toggle_wake_up_enable = 1;
00112 }
00113 
00114 void INT1_cb()
00115 {
00116     mems_event = 1;
00117 }