test
Dependencies: X_NUCLEO_IKS01A2 mbed
Fork of HelloWorld_IKS01A2 by
main.cpp@18:fde3070d9f93, 2018-07-24 (annotated)
- Committer:
- uqureshi
- Date:
- Tue Jul 24 17:23:44 2018 +0000
- Revision:
- 18:fde3070d9f93
- Parent:
- 13:fc873da5b445
save
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cparata | 0:69566eea0fba | 1 | /** |
cparata | 0:69566eea0fba | 2 | ****************************************************************************** |
cparata | 0:69566eea0fba | 3 | * @file main.cpp |
cparata | 8:8f495e604424 | 4 | * @author CLab |
cparata | 8:8f495e604424 | 5 | * @version V1.0.0 |
cparata | 8:8f495e604424 | 6 | * @date 2-December-2016 |
cparata | 0:69566eea0fba | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
cparata | 0:69566eea0fba | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
cparata | 0:69566eea0fba | 9 | ****************************************************************************** |
cparata | 0:69566eea0fba | 10 | * @attention |
cparata | 0:69566eea0fba | 11 | * |
cparata | 0:69566eea0fba | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
cparata | 0:69566eea0fba | 13 | * |
cparata | 0:69566eea0fba | 14 | * Redistribution and use in source and binary forms, with or without modification, |
cparata | 0:69566eea0fba | 15 | * are permitted provided that the following conditions are met: |
cparata | 0:69566eea0fba | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
cparata | 0:69566eea0fba | 17 | * this list of conditions and the following disclaimer. |
cparata | 0:69566eea0fba | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
cparata | 0:69566eea0fba | 19 | * this list of conditions and the following disclaimer in the documentation |
cparata | 0:69566eea0fba | 20 | * and/or other materials provided with the distribution. |
cparata | 0:69566eea0fba | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
cparata | 0:69566eea0fba | 22 | * may be used to endorse or promote products derived from this software |
cparata | 0:69566eea0fba | 23 | * without specific prior written permission. |
cparata | 0:69566eea0fba | 24 | * |
cparata | 0:69566eea0fba | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
cparata | 0:69566eea0fba | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
cparata | 0:69566eea0fba | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
cparata | 0:69566eea0fba | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
cparata | 0:69566eea0fba | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
cparata | 0:69566eea0fba | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
cparata | 0:69566eea0fba | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
cparata | 0:69566eea0fba | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
cparata | 0:69566eea0fba | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
cparata | 0:69566eea0fba | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
cparata | 0:69566eea0fba | 35 | * |
cparata | 0:69566eea0fba | 36 | ****************************************************************************** |
cparata | 0:69566eea0fba | 37 | */ |
cparata | 0:69566eea0fba | 38 | |
cparata | 0:69566eea0fba | 39 | /* Includes */ |
cparata | 0:69566eea0fba | 40 | #include "mbed.h" |
davide.aliprandi@st.com | 13:fc873da5b445 | 41 | #include "XNucleoIKS01A2.h" |
cparata | 0:69566eea0fba | 42 | |
cparata | 0:69566eea0fba | 43 | /* Instantiate the expansion board */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 44 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
cparata | 0:69566eea0fba | 45 | |
uqureshi | 18:fde3070d9f93 | 46 | /* Retrieve the LSM6DSL Object */ |
cparata | 0:69566eea0fba | 47 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
cparata | 0:69566eea0fba | 48 | |
uqureshi | 18:fde3070d9f93 | 49 | /*Initialize Serial port object*/ |
uqureshi | 18:fde3070d9f93 | 50 | Serial pc(USBTX, USBRX); |
uqureshi | 18:fde3070d9f93 | 51 | Timer t; |
uqureshi | 18:fde3070d9f93 | 52 | |
uqureshi | 18:fde3070d9f93 | 53 | /* Helper function for printing floats & doubles. Not using at the moment! */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 54 | static char *print_double(char* str, double v, int decimalDigits=2) |
cparata | 0:69566eea0fba | 55 | { |
cparata | 0:69566eea0fba | 56 | int i = 1; |
cparata | 0:69566eea0fba | 57 | int intPart, fractPart; |
cparata | 0:69566eea0fba | 58 | int len; |
cparata | 0:69566eea0fba | 59 | char *ptr; |
cparata | 0:69566eea0fba | 60 | |
cparata | 0:69566eea0fba | 61 | /* prepare decimal digits multiplicator */ |
cparata | 0:69566eea0fba | 62 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
cparata | 0:69566eea0fba | 63 | |
cparata | 0:69566eea0fba | 64 | /* calculate integer & fractinal parts */ |
cparata | 0:69566eea0fba | 65 | intPart = (int)v; |
cparata | 0:69566eea0fba | 66 | fractPart = (int)((v-(double)(int)v)*i); |
cparata | 0:69566eea0fba | 67 | |
cparata | 0:69566eea0fba | 68 | /* fill in integer part */ |
cparata | 0:69566eea0fba | 69 | sprintf(str, "%i.", intPart); |
cparata | 0:69566eea0fba | 70 | |
cparata | 0:69566eea0fba | 71 | /* prepare fill in of fractional part */ |
cparata | 0:69566eea0fba | 72 | len = strlen(str); |
cparata | 0:69566eea0fba | 73 | ptr = &str[len]; |
cparata | 0:69566eea0fba | 74 | |
cparata | 0:69566eea0fba | 75 | /* fill in leading fractional zeros */ |
cparata | 0:69566eea0fba | 76 | for (i/=10;i>1; i/=10, ptr++) { |
davide.aliprandi@st.com | 13:fc873da5b445 | 77 | if (fractPart >= i) { |
davide.aliprandi@st.com | 13:fc873da5b445 | 78 | break; |
davide.aliprandi@st.com | 13:fc873da5b445 | 79 | } |
cparata | 0:69566eea0fba | 80 | *ptr = '0'; |
cparata | 0:69566eea0fba | 81 | } |
cparata | 0:69566eea0fba | 82 | |
cparata | 0:69566eea0fba | 83 | /* fill in (rest of) fractional part */ |
cparata | 0:69566eea0fba | 84 | sprintf(ptr, "%i", fractPart); |
cparata | 0:69566eea0fba | 85 | |
cparata | 0:69566eea0fba | 86 | return str; |
cparata | 0:69566eea0fba | 87 | } |
cparata | 0:69566eea0fba | 88 | |
cparata | 0:69566eea0fba | 89 | /* Simple main function */ |
cparata | 0:69566eea0fba | 90 | int main() { |
cparata | 0:69566eea0fba | 91 | uint8_t id; |
uqureshi | 18:fde3070d9f93 | 92 | //float value1, value2; |
uqureshi | 18:fde3070d9f93 | 93 | //char buffer1[32], buffer2[32]; |
uqureshi | 18:fde3070d9f93 | 94 | int32_t a_axes[3]; |
uqureshi | 18:fde3070d9f93 | 95 | int32_t g_axes[3]; |
cparata | 0:69566eea0fba | 96 | |
uqureshi | 18:fde3070d9f93 | 97 | /* Set port speed*/ |
uqureshi | 18:fde3070d9f93 | 98 | pc.baud(115200); |
uqureshi | 18:fde3070d9f93 | 99 | |
uqureshi | 18:fde3070d9f93 | 100 | /* Enable gyro and accel sensors */ |
davide.aliprandi@st.com | 13:fc873da5b445 | 101 | acc_gyro->enable_x(); |
davide.aliprandi@st.com | 13:fc873da5b445 | 102 | acc_gyro->enable_g(); |
cparata | 0:69566eea0fba | 103 | |
uqureshi | 18:fde3070d9f93 | 104 | pc.printf("\r\n--- Starting new run ---\r\n"); |
cparata | 0:69566eea0fba | 105 | |
davide.aliprandi@st.com | 13:fc873da5b445 | 106 | acc_gyro->read_id(&id); |
uqureshi | 18:fde3070d9f93 | 107 | pc.printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);\ |
uqureshi | 18:fde3070d9f93 | 108 | |
uqureshi | 18:fde3070d9f93 | 109 | t.start(); |
cparata | 0:69566eea0fba | 110 | |
cparata | 0:69566eea0fba | 111 | while(1) { |
cparata | 0:69566eea0fba | 112 | |
uqureshi | 18:fde3070d9f93 | 113 | acc_gyro->get_x_axes(a_axes); |
uqureshi | 18:fde3070d9f93 | 114 | acc_gyro->get_g_axes(g_axes); |
uqureshi | 18:fde3070d9f93 | 115 | pc.printf("mg ,mdps: %d %6ld, %6ld, %6ld, %6ld, %6ld, %6ld,\r\n", t.read_ms(), a_axes[0], a_axes[1], a_axes[2],g_axes[0], g_axes[1], g_axes[2]); |
cparata | 0:69566eea0fba | 116 | |
uqureshi | 18:fde3070d9f93 | 117 | //wait_ms(10); |
cparata | 0:69566eea0fba | 118 | } |
cparata | 0:69566eea0fba | 119 | } |