PMK2020 iks01a3 noos

Dependencies:   mbed X_NUCLEO_IKS01A3

Committer:
cparata
Date:
Wed Jul 24 14:29:33 2019 +0000
Revision:
5:ad46bcbf3320
Parent:
0:cb5eb44cbfb5
Format with Astyle

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:cb5eb44cbfb5 1 /**
cparata 0:cb5eb44cbfb5 2 ******************************************************************************
cparata 0:cb5eb44cbfb5 3 * @file main.cpp
cparata 0:cb5eb44cbfb5 4 * @author SRA
cparata 0:cb5eb44cbfb5 5 * @version V1.0.0
cparata 0:cb5eb44cbfb5 6 * @date 5-March-2019
cparata 5:ad46bcbf3320 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A3
cparata 0:cb5eb44cbfb5 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:cb5eb44cbfb5 9 ******************************************************************************
cparata 0:cb5eb44cbfb5 10 * @attention
cparata 0:cb5eb44cbfb5 11 *
cparata 0:cb5eb44cbfb5 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:cb5eb44cbfb5 13 *
cparata 0:cb5eb44cbfb5 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:cb5eb44cbfb5 15 * are permitted provided that the following conditions are met:
cparata 0:cb5eb44cbfb5 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:cb5eb44cbfb5 17 * this list of conditions and the following disclaimer.
cparata 0:cb5eb44cbfb5 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:cb5eb44cbfb5 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:cb5eb44cbfb5 20 * and/or other materials provided with the distribution.
cparata 0:cb5eb44cbfb5 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:cb5eb44cbfb5 22 * may be used to endorse or promote products derived from this software
cparata 0:cb5eb44cbfb5 23 * without specific prior written permission.
cparata 0:cb5eb44cbfb5 24 *
cparata 0:cb5eb44cbfb5 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:cb5eb44cbfb5 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:cb5eb44cbfb5 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:cb5eb44cbfb5 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:cb5eb44cbfb5 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:cb5eb44cbfb5 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:cb5eb44cbfb5 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:cb5eb44cbfb5 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:cb5eb44cbfb5 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:cb5eb44cbfb5 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:cb5eb44cbfb5 35 *
cparata 0:cb5eb44cbfb5 36 ******************************************************************************
cparata 5:ad46bcbf3320 37 */
cparata 5:ad46bcbf3320 38
cparata 0:cb5eb44cbfb5 39 /* Includes */
cparata 0:cb5eb44cbfb5 40 #include "mbed.h"
cparata 0:cb5eb44cbfb5 41 #include "XNucleoIKS01A3.h"
cparata 5:ad46bcbf3320 42
cparata 0:cb5eb44cbfb5 43 /* Instantiate the expansion board */
cparata 0:cb5eb44cbfb5 44 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 5:ad46bcbf3320 45
cparata 0:cb5eb44cbfb5 46 /* Retrieve the composing elements of the expansion board */
cparata 0:cb5eb44cbfb5 47 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
cparata 5:ad46bcbf3320 48
cparata 0:cb5eb44cbfb5 49 InterruptIn mybutton(USER_BUTTON);
cparata 0:cb5eb44cbfb5 50 DigitalOut myled(LED1);
cparata 5:ad46bcbf3320 51
cparata 0:cb5eb44cbfb5 52 volatile int mems_event = 0;
cparata 0:cb5eb44cbfb5 53 volatile int send_orientation_request = 0;
cparata 5:ad46bcbf3320 54
cparata 0:cb5eb44cbfb5 55 /* User button callback. */
cparata 5:ad46bcbf3320 56 void pressed_cb()
cparata 5:ad46bcbf3320 57 {
cparata 5:ad46bcbf3320 58 send_orientation_request = 1;
cparata 0:cb5eb44cbfb5 59 }
cparata 5:ad46bcbf3320 60
cparata 0:cb5eb44cbfb5 61 /* Interrupt 1 callback. */
cparata 5:ad46bcbf3320 62 void int1_cb()
cparata 5:ad46bcbf3320 63 {
cparata 5:ad46bcbf3320 64 mems_event = 1;
cparata 0:cb5eb44cbfb5 65 }
cparata 5:ad46bcbf3320 66
cparata 0:cb5eb44cbfb5 67 /* Print the orientation. */
cparata 5:ad46bcbf3320 68 void send_orientation()
cparata 5:ad46bcbf3320 69 {
cparata 5:ad46bcbf3320 70 uint8_t xl = 0;
cparata 5:ad46bcbf3320 71 uint8_t xh = 0;
cparata 5:ad46bcbf3320 72 uint8_t yl = 0;
cparata 5:ad46bcbf3320 73 uint8_t yh = 0;
cparata 5:ad46bcbf3320 74 uint8_t zl = 0;
cparata 5:ad46bcbf3320 75 uint8_t zh = 0;
cparata 5:ad46bcbf3320 76
cparata 5:ad46bcbf3320 77 accelerometer->get_6d_orientation_xl(&xl);
cparata 5:ad46bcbf3320 78 accelerometer->get_6d_orientation_xh(&xh);
cparata 5:ad46bcbf3320 79 accelerometer->get_6d_orientation_yl(&yl);
cparata 5:ad46bcbf3320 80 accelerometer->get_6d_orientation_yh(&yh);
cparata 5:ad46bcbf3320 81 accelerometer->get_6d_orientation_zl(&zl);
cparata 5:ad46bcbf3320 82 accelerometer->get_6d_orientation_zh(&zh);
cparata 5:ad46bcbf3320 83
cparata 5:ad46bcbf3320 84 if (xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:ad46bcbf3320 85 printf("\r\n ________________ " \
cparata 5:ad46bcbf3320 86 "\r\n | | " \
cparata 5:ad46bcbf3320 87 "\r\n | * | " \
cparata 5:ad46bcbf3320 88 "\r\n | | " \
cparata 5:ad46bcbf3320 89 "\r\n | | " \
cparata 5:ad46bcbf3320 90 "\r\n | | " \
cparata 5:ad46bcbf3320 91 "\r\n | | " \
cparata 5:ad46bcbf3320 92 "\r\n |________________| \r\n");
cparata 5:ad46bcbf3320 93 }
cparata 5:ad46bcbf3320 94
cparata 5:ad46bcbf3320 95 else if (xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:ad46bcbf3320 96 printf("\r\n ________________ " \
cparata 5:ad46bcbf3320 97 "\r\n | | " \
cparata 5:ad46bcbf3320 98 "\r\n | * | " \
cparata 5:ad46bcbf3320 99 "\r\n | | " \
cparata 5:ad46bcbf3320 100 "\r\n | | " \
cparata 5:ad46bcbf3320 101 "\r\n | | " \
cparata 5:ad46bcbf3320 102 "\r\n | | " \
cparata 5:ad46bcbf3320 103 "\r\n |________________| \r\n");
cparata 5:ad46bcbf3320 104 }
cparata 5:ad46bcbf3320 105
cparata 5:ad46bcbf3320 106 else if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0) {
cparata 5:ad46bcbf3320 107 printf("\r\n ________________ " \
cparata 5:ad46bcbf3320 108 "\r\n | | " \
cparata 5:ad46bcbf3320 109 "\r\n | | " \
cparata 5:ad46bcbf3320 110 "\r\n | | " \
cparata 5:ad46bcbf3320 111 "\r\n | | " \
cparata 5:ad46bcbf3320 112 "\r\n | | " \
cparata 5:ad46bcbf3320 113 "\r\n | * | " \
cparata 5:ad46bcbf3320 114 "\r\n |________________| \r\n");
cparata 5:ad46bcbf3320 115 }
cparata 5:ad46bcbf3320 116
cparata 5:ad46bcbf3320 117 else if (xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0) {
cparata 5:ad46bcbf3320 118 printf("\r\n ________________ " \
cparata 5:ad46bcbf3320 119 "\r\n | | " \
cparata 5:ad46bcbf3320 120 "\r\n | | " \
cparata 5:ad46bcbf3320 121 "\r\n | | " \
cparata 5:ad46bcbf3320 122 "\r\n | | " \
cparata 5:ad46bcbf3320 123 "\r\n | | " \
cparata 5:ad46bcbf3320 124 "\r\n | * | " \
cparata 5:ad46bcbf3320 125 "\r\n |________________| \r\n");
cparata 5:ad46bcbf3320 126 }
cparata 5:ad46bcbf3320 127
cparata 5:ad46bcbf3320 128 else if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1) {
cparata 5:ad46bcbf3320 129 printf("\r\n __*_____________ " \
cparata 5:ad46bcbf3320 130 "\r\n |________________| \r\n");
cparata 5:ad46bcbf3320 131 }
cparata 5:ad46bcbf3320 132
cparata 5:ad46bcbf3320 133 else if (xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0) {
cparata 5:ad46bcbf3320 134 printf("\r\n ________________ " \
cparata 5:ad46bcbf3320 135 "\r\n |________________| " \
cparata 5:ad46bcbf3320 136 "\r\n * \r\n");
cparata 5:ad46bcbf3320 137 }
cparata 5:ad46bcbf3320 138
cparata 5:ad46bcbf3320 139 else {
cparata 5:ad46bcbf3320 140 printf("None of the 6D orientation axes is set in LIS2DW12 - accelerometer.\r\n");
cparata 5:ad46bcbf3320 141 }
cparata 0:cb5eb44cbfb5 142 }
cparata 5:ad46bcbf3320 143
cparata 0:cb5eb44cbfb5 144 /* Simple main function */
cparata 5:ad46bcbf3320 145 int main()
cparata 5:ad46bcbf3320 146 {
cparata 5:ad46bcbf3320 147 /* Attach callback to User button press */
cparata 5:ad46bcbf3320 148 mybutton.fall(&pressed_cb);
cparata 5:ad46bcbf3320 149 /* Attach callback to LIS2DW12 INT1 */
cparata 5:ad46bcbf3320 150 accelerometer->attach_int1_irq(&int1_cb);
cparata 5:ad46bcbf3320 151
cparata 5:ad46bcbf3320 152 /* Enable LIS2DW12 accelerometer */
cparata 5:ad46bcbf3320 153 accelerometer->enable_x();
cparata 5:ad46bcbf3320 154 /* Enable 6D Orientation. */
cparata 5:ad46bcbf3320 155 accelerometer->enable_6d_orientation();
cparata 5:ad46bcbf3320 156
cparata 5:ad46bcbf3320 157 printf("\r\n--- Starting new run ---\r\n");
cparata 5:ad46bcbf3320 158
cparata 5:ad46bcbf3320 159 while (1) {
cparata 5:ad46bcbf3320 160 if (mems_event) {
cparata 5:ad46bcbf3320 161 mems_event = 0;
cparata 5:ad46bcbf3320 162 LIS2DW12_Event_Status_t status;
cparata 5:ad46bcbf3320 163 accelerometer->get_event_status(&status);
cparata 5:ad46bcbf3320 164 if (status.D6DOrientationStatus) {
cparata 5:ad46bcbf3320 165 /* Send 6D Orientation */
cparata 5:ad46bcbf3320 166 send_orientation();
cparata 5:ad46bcbf3320 167
cparata 5:ad46bcbf3320 168 /* Led blinking. */
cparata 5:ad46bcbf3320 169 myled = 1;
cparata 5:ad46bcbf3320 170 wait(0.2);
cparata 5:ad46bcbf3320 171 myled = 0;
cparata 5:ad46bcbf3320 172 }
cparata 5:ad46bcbf3320 173 }
cparata 5:ad46bcbf3320 174
cparata 5:ad46bcbf3320 175 if (send_orientation_request) {
cparata 5:ad46bcbf3320 176 send_orientation_request = 0;
cparata 5:ad46bcbf3320 177 send_orientation();
cparata 5:ad46bcbf3320 178 }
cparata 0:cb5eb44cbfb5 179 }
cparata 0:cb5eb44cbfb5 180 }