![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
PMK2020 iks01a3 mbed
Dependencies: X_NUCLEO_IKS01A3
main.cpp@7:8895c4a4d17d, 2020-06-04 (annotated)
- Committer:
- cparata
- Date:
- Thu Jun 04 16:43:21 2020 +0000
- Revision:
- 7:8895c4a4d17d
- Parent:
- 5:ad46bcbf3320
Update the code to be compatible with mbed OS 6
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 | 7:8895c4a4d17d | 41 | #include "rtos.h" |
cparata | 0:cb5eb44cbfb5 | 42 | #include "XNucleoIKS01A3.h" |
cparata | 5:ad46bcbf3320 | 43 | |
cparata | 0:cb5eb44cbfb5 | 44 | /* Instantiate the expansion board */ |
cparata | 0:cb5eb44cbfb5 | 45 | static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4); |
cparata | 5:ad46bcbf3320 | 46 | |
cparata | 0:cb5eb44cbfb5 | 47 | /* Retrieve the composing elements of the expansion board */ |
cparata | 0:cb5eb44cbfb5 | 48 | static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer; |
cparata | 5:ad46bcbf3320 | 49 | |
cparata | 0:cb5eb44cbfb5 | 50 | InterruptIn mybutton(USER_BUTTON); |
cparata | 0:cb5eb44cbfb5 | 51 | DigitalOut myled(LED1); |
cparata | 5:ad46bcbf3320 | 52 | |
cparata | 0:cb5eb44cbfb5 | 53 | volatile int mems_event = 0; |
cparata | 0:cb5eb44cbfb5 | 54 | volatile int send_orientation_request = 0; |
cparata | 5:ad46bcbf3320 | 55 | |
cparata | 0:cb5eb44cbfb5 | 56 | /* User button callback. */ |
cparata | 5:ad46bcbf3320 | 57 | void pressed_cb() |
cparata | 5:ad46bcbf3320 | 58 | { |
cparata | 5:ad46bcbf3320 | 59 | send_orientation_request = 1; |
cparata | 0:cb5eb44cbfb5 | 60 | } |
cparata | 5:ad46bcbf3320 | 61 | |
cparata | 0:cb5eb44cbfb5 | 62 | /* Interrupt 1 callback. */ |
cparata | 5:ad46bcbf3320 | 63 | void int1_cb() |
cparata | 5:ad46bcbf3320 | 64 | { |
cparata | 5:ad46bcbf3320 | 65 | mems_event = 1; |
cparata | 0:cb5eb44cbfb5 | 66 | } |
cparata | 5:ad46bcbf3320 | 67 | |
cparata | 0:cb5eb44cbfb5 | 68 | /* Print the orientation. */ |
cparata | 5:ad46bcbf3320 | 69 | void send_orientation() |
cparata | 5:ad46bcbf3320 | 70 | { |
cparata | 5:ad46bcbf3320 | 71 | uint8_t xl = 0; |
cparata | 5:ad46bcbf3320 | 72 | uint8_t xh = 0; |
cparata | 5:ad46bcbf3320 | 73 | uint8_t yl = 0; |
cparata | 5:ad46bcbf3320 | 74 | uint8_t yh = 0; |
cparata | 5:ad46bcbf3320 | 75 | uint8_t zl = 0; |
cparata | 5:ad46bcbf3320 | 76 | uint8_t zh = 0; |
cparata | 5:ad46bcbf3320 | 77 | |
cparata | 5:ad46bcbf3320 | 78 | accelerometer->get_6d_orientation_xl(&xl); |
cparata | 5:ad46bcbf3320 | 79 | accelerometer->get_6d_orientation_xh(&xh); |
cparata | 5:ad46bcbf3320 | 80 | accelerometer->get_6d_orientation_yl(&yl); |
cparata | 5:ad46bcbf3320 | 81 | accelerometer->get_6d_orientation_yh(&yh); |
cparata | 5:ad46bcbf3320 | 82 | accelerometer->get_6d_orientation_zl(&zl); |
cparata | 5:ad46bcbf3320 | 83 | accelerometer->get_6d_orientation_zh(&zh); |
cparata | 5:ad46bcbf3320 | 84 | |
cparata | 5:ad46bcbf3320 | 85 | if (xl == 1 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 0) { |
cparata | 5:ad46bcbf3320 | 86 | printf("\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 | | " \ |
cparata | 5:ad46bcbf3320 | 93 | "\r\n |________________| \r\n"); |
cparata | 5:ad46bcbf3320 | 94 | } |
cparata | 5:ad46bcbf3320 | 95 | |
cparata | 5:ad46bcbf3320 | 96 | else if (xl == 0 && yl == 1 && zl == 0 && xh == 0 && yh == 0 && zh == 0) { |
cparata | 5:ad46bcbf3320 | 97 | printf("\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 | | " \ |
cparata | 5:ad46bcbf3320 | 104 | "\r\n |________________| \r\n"); |
cparata | 5:ad46bcbf3320 | 105 | } |
cparata | 5:ad46bcbf3320 | 106 | |
cparata | 5:ad46bcbf3320 | 107 | else if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 1 && zh == 0) { |
cparata | 5:ad46bcbf3320 | 108 | printf("\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 | * | " \ |
cparata | 5:ad46bcbf3320 | 115 | "\r\n |________________| \r\n"); |
cparata | 5:ad46bcbf3320 | 116 | } |
cparata | 5:ad46bcbf3320 | 117 | |
cparata | 5:ad46bcbf3320 | 118 | else if (xl == 0 && yl == 0 && zl == 0 && xh == 1 && yh == 0 && zh == 0) { |
cparata | 5:ad46bcbf3320 | 119 | printf("\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 | * | " \ |
cparata | 5:ad46bcbf3320 | 126 | "\r\n |________________| \r\n"); |
cparata | 5:ad46bcbf3320 | 127 | } |
cparata | 5:ad46bcbf3320 | 128 | |
cparata | 5:ad46bcbf3320 | 129 | else if (xl == 0 && yl == 0 && zl == 0 && xh == 0 && yh == 0 && zh == 1) { |
cparata | 5:ad46bcbf3320 | 130 | printf("\r\n __*_____________ " \ |
cparata | 5:ad46bcbf3320 | 131 | "\r\n |________________| \r\n"); |
cparata | 5:ad46bcbf3320 | 132 | } |
cparata | 5:ad46bcbf3320 | 133 | |
cparata | 5:ad46bcbf3320 | 134 | else if (xl == 0 && yl == 0 && zl == 1 && xh == 0 && yh == 0 && zh == 0) { |
cparata | 5:ad46bcbf3320 | 135 | printf("\r\n ________________ " \ |
cparata | 5:ad46bcbf3320 | 136 | "\r\n |________________| " \ |
cparata | 5:ad46bcbf3320 | 137 | "\r\n * \r\n"); |
cparata | 5:ad46bcbf3320 | 138 | } |
cparata | 5:ad46bcbf3320 | 139 | |
cparata | 5:ad46bcbf3320 | 140 | else { |
cparata | 5:ad46bcbf3320 | 141 | printf("None of the 6D orientation axes is set in LIS2DW12 - accelerometer.\r\n"); |
cparata | 5:ad46bcbf3320 | 142 | } |
cparata | 0:cb5eb44cbfb5 | 143 | } |
cparata | 5:ad46bcbf3320 | 144 | |
cparata | 0:cb5eb44cbfb5 | 145 | /* Simple main function */ |
cparata | 5:ad46bcbf3320 | 146 | int main() |
cparata | 5:ad46bcbf3320 | 147 | { |
cparata | 5:ad46bcbf3320 | 148 | /* Attach callback to User button press */ |
cparata | 5:ad46bcbf3320 | 149 | mybutton.fall(&pressed_cb); |
cparata | 5:ad46bcbf3320 | 150 | /* Attach callback to LIS2DW12 INT1 */ |
cparata | 5:ad46bcbf3320 | 151 | accelerometer->attach_int1_irq(&int1_cb); |
cparata | 5:ad46bcbf3320 | 152 | |
cparata | 5:ad46bcbf3320 | 153 | /* Enable LIS2DW12 accelerometer */ |
cparata | 5:ad46bcbf3320 | 154 | accelerometer->enable_x(); |
cparata | 5:ad46bcbf3320 | 155 | /* Enable 6D Orientation. */ |
cparata | 5:ad46bcbf3320 | 156 | accelerometer->enable_6d_orientation(); |
cparata | 5:ad46bcbf3320 | 157 | |
cparata | 5:ad46bcbf3320 | 158 | printf("\r\n--- Starting new run ---\r\n"); |
cparata | 5:ad46bcbf3320 | 159 | |
cparata | 5:ad46bcbf3320 | 160 | while (1) { |
cparata | 5:ad46bcbf3320 | 161 | if (mems_event) { |
cparata | 5:ad46bcbf3320 | 162 | mems_event = 0; |
cparata | 5:ad46bcbf3320 | 163 | LIS2DW12_Event_Status_t status; |
cparata | 5:ad46bcbf3320 | 164 | accelerometer->get_event_status(&status); |
cparata | 5:ad46bcbf3320 | 165 | if (status.D6DOrientationStatus) { |
cparata | 5:ad46bcbf3320 | 166 | /* Send 6D Orientation */ |
cparata | 5:ad46bcbf3320 | 167 | send_orientation(); |
cparata | 5:ad46bcbf3320 | 168 | |
cparata | 5:ad46bcbf3320 | 169 | /* Led blinking. */ |
cparata | 5:ad46bcbf3320 | 170 | myled = 1; |
cparata | 7:8895c4a4d17d | 171 | ThisThread::sleep_for(200); |
cparata | 5:ad46bcbf3320 | 172 | myled = 0; |
cparata | 5:ad46bcbf3320 | 173 | } |
cparata | 5:ad46bcbf3320 | 174 | } |
cparata | 5:ad46bcbf3320 | 175 | |
cparata | 5:ad46bcbf3320 | 176 | if (send_orientation_request) { |
cparata | 5:ad46bcbf3320 | 177 | send_orientation_request = 0; |
cparata | 5:ad46bcbf3320 | 178 | send_orientation(); |
cparata | 5:ad46bcbf3320 | 179 | } |
cparata | 0:cb5eb44cbfb5 | 180 | } |
cparata | 0:cb5eb44cbfb5 | 181 | } |