IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

Committer:
cparata
Date:
Wed Mar 06 13:21:38 2019 +0000
Revision:
0:535249dc4bf5
Child:
3:9f8fadc965fc
First version of HelloWorld_IKS01A3 application

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cparata 0:535249dc4bf5 1 /**
cparata 0:535249dc4bf5 2 ******************************************************************************
cparata 0:535249dc4bf5 3 * @file main.cpp
cparata 0:535249dc4bf5 4 * @author SRA
cparata 0:535249dc4bf5 5 * @version V1.0.0
cparata 0:535249dc4bf5 6 * @date 5-March-2019
cparata 0:535249dc4bf5 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A3
cparata 0:535249dc4bf5 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
cparata 0:535249dc4bf5 9 ******************************************************************************
cparata 0:535249dc4bf5 10 * @attention
cparata 0:535249dc4bf5 11 *
cparata 0:535249dc4bf5 12 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
cparata 0:535249dc4bf5 13 *
cparata 0:535249dc4bf5 14 * Redistribution and use in source and binary forms, with or without modification,
cparata 0:535249dc4bf5 15 * are permitted provided that the following conditions are met:
cparata 0:535249dc4bf5 16 * 1. Redistributions of source code must retain the above copyright notice,
cparata 0:535249dc4bf5 17 * this list of conditions and the following disclaimer.
cparata 0:535249dc4bf5 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
cparata 0:535249dc4bf5 19 * this list of conditions and the following disclaimer in the documentation
cparata 0:535249dc4bf5 20 * and/or other materials provided with the distribution.
cparata 0:535249dc4bf5 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
cparata 0:535249dc4bf5 22 * may be used to endorse or promote products derived from this software
cparata 0:535249dc4bf5 23 * without specific prior written permission.
cparata 0:535249dc4bf5 24 *
cparata 0:535249dc4bf5 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cparata 0:535249dc4bf5 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cparata 0:535249dc4bf5 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
cparata 0:535249dc4bf5 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
cparata 0:535249dc4bf5 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
cparata 0:535249dc4bf5 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
cparata 0:535249dc4bf5 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
cparata 0:535249dc4bf5 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
cparata 0:535249dc4bf5 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
cparata 0:535249dc4bf5 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cparata 0:535249dc4bf5 35 *
cparata 0:535249dc4bf5 36 ******************************************************************************
cparata 0:535249dc4bf5 37 */
cparata 0:535249dc4bf5 38
cparata 0:535249dc4bf5 39 /* Includes */
cparata 0:535249dc4bf5 40 #include "mbed.h"
cparata 0:535249dc4bf5 41 #include "XNucleoIKS01A3.h"
cparata 0:535249dc4bf5 42
cparata 0:535249dc4bf5 43 /* Instantiate the expansion board */
cparata 0:535249dc4bf5 44 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
cparata 0:535249dc4bf5 45
cparata 0:535249dc4bf5 46 /* Retrieve the composing elements of the expansion board */
cparata 0:535249dc4bf5 47 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
cparata 0:535249dc4bf5 48 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
cparata 0:535249dc4bf5 49 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
cparata 0:535249dc4bf5 50 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
cparata 0:535249dc4bf5 51 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
cparata 0:535249dc4bf5 52 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
cparata 0:535249dc4bf5 53
cparata 0:535249dc4bf5 54 /* Helper function for printing floats & doubles */
cparata 0:535249dc4bf5 55 static char *print_double(char* str, double v, int decimalDigits=2)
cparata 0:535249dc4bf5 56 {
cparata 0:535249dc4bf5 57 int i = 1;
cparata 0:535249dc4bf5 58 int intPart, fractPart;
cparata 0:535249dc4bf5 59 int len;
cparata 0:535249dc4bf5 60 char *ptr;
cparata 0:535249dc4bf5 61
cparata 0:535249dc4bf5 62 /* prepare decimal digits multiplicator */
cparata 0:535249dc4bf5 63 for (;decimalDigits!=0; i*=10, decimalDigits--);
cparata 0:535249dc4bf5 64
cparata 0:535249dc4bf5 65 /* calculate integer & fractinal parts */
cparata 0:535249dc4bf5 66 intPart = (int)v;
cparata 0:535249dc4bf5 67 fractPart = (int)((v-(double)(int)v)*i);
cparata 0:535249dc4bf5 68
cparata 0:535249dc4bf5 69 /* fill in integer part */
cparata 0:535249dc4bf5 70 sprintf(str, "%i.", intPart);
cparata 0:535249dc4bf5 71
cparata 0:535249dc4bf5 72 /* prepare fill in of fractional part */
cparata 0:535249dc4bf5 73 len = strlen(str);
cparata 0:535249dc4bf5 74 ptr = &str[len];
cparata 0:535249dc4bf5 75
cparata 0:535249dc4bf5 76 /* fill in leading fractional zeros */
cparata 0:535249dc4bf5 77 for (i/=10;i>1; i/=10, ptr++) {
cparata 0:535249dc4bf5 78 if (fractPart >= i) {
cparata 0:535249dc4bf5 79 break;
cparata 0:535249dc4bf5 80 }
cparata 0:535249dc4bf5 81 *ptr = '0';
cparata 0:535249dc4bf5 82 }
cparata 0:535249dc4bf5 83
cparata 0:535249dc4bf5 84 /* fill in (rest of) fractional part */
cparata 0:535249dc4bf5 85 sprintf(ptr, "%i", fractPart);
cparata 0:535249dc4bf5 86
cparata 0:535249dc4bf5 87 return str;
cparata 0:535249dc4bf5 88 }
cparata 0:535249dc4bf5 89
cparata 0:535249dc4bf5 90 /* Simple main function */
cparata 0:535249dc4bf5 91 int main() {
cparata 0:535249dc4bf5 92 uint8_t id;
cparata 0:535249dc4bf5 93 float value1, value2;
cparata 0:535249dc4bf5 94 char buffer1[32], buffer2[32];
cparata 0:535249dc4bf5 95 int32_t axes[3];
cparata 0:535249dc4bf5 96
cparata 0:535249dc4bf5 97 /* Enable all sensors */
cparata 0:535249dc4bf5 98 hum_temp->enable();
cparata 0:535249dc4bf5 99 press_temp->enable();
cparata 0:535249dc4bf5 100 temp->enable();
cparata 0:535249dc4bf5 101 magnetometer->enable();
cparata 0:535249dc4bf5 102 accelerometer->enable_x();
cparata 0:535249dc4bf5 103 acc_gyro->enable_x();
cparata 0:535249dc4bf5 104 acc_gyro->enable_g();
cparata 0:535249dc4bf5 105
cparata 0:535249dc4bf5 106 printf("\r\n--- Starting new run ---\r\n");
cparata 0:535249dc4bf5 107
cparata 0:535249dc4bf5 108 hum_temp->read_id(&id);
cparata 0:535249dc4bf5 109 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
cparata 0:535249dc4bf5 110 press_temp->read_id(&id);
cparata 0:535249dc4bf5 111 printf("LPS22HH pressure & temperature = 0x%X\r\n", id);
cparata 0:535249dc4bf5 112 temp->read_id(&id);
cparata 0:535249dc4bf5 113 printf("STTS751 temperature = 0x%X\r\n", id);
cparata 0:535249dc4bf5 114 magnetometer->read_id(&id);
cparata 0:535249dc4bf5 115 printf("LIS2MDL magnetometer = 0x%X\r\n", id);
cparata 0:535249dc4bf5 116 accelerometer->read_id(&id);
cparata 0:535249dc4bf5 117 printf("LIS2DW12 accelerometer = 0x%X\r\n", id);
cparata 0:535249dc4bf5 118 acc_gyro->read_id(&id);
cparata 0:535249dc4bf5 119 printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
cparata 0:535249dc4bf5 120
cparata 0:535249dc4bf5 121 while(1) {
cparata 0:535249dc4bf5 122 printf("\r\n");
cparata 0:535249dc4bf5 123
cparata 0:535249dc4bf5 124 hum_temp->get_temperature(&value1);
cparata 0:535249dc4bf5 125 hum_temp->get_humidity(&value2);
cparata 0:535249dc4bf5 126 printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
cparata 0:535249dc4bf5 127
cparata 0:535249dc4bf5 128 press_temp->get_temperature(&value1);
cparata 0:535249dc4bf5 129 press_temp->get_pressure(&value2);
cparata 0:535249dc4bf5 130 printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
cparata 0:535249dc4bf5 131
cparata 0:535249dc4bf5 132 temp->get_temperature(&value1);
cparata 0:535249dc4bf5 133 printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1));
cparata 0:535249dc4bf5 134
cparata 0:535249dc4bf5 135 printf("---\r\n");
cparata 0:535249dc4bf5 136
cparata 0:535249dc4bf5 137 magnetometer->get_m_axes(axes);
cparata 0:535249dc4bf5 138 printf("LIS2MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 139
cparata 0:535249dc4bf5 140 accelerometer->get_x_axes(axes);
cparata 0:535249dc4bf5 141 printf("LIS2DW12 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 142
cparata 0:535249dc4bf5 143 acc_gyro->get_x_axes(axes);
cparata 0:535249dc4bf5 144 printf("LSM6DSO [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 145
cparata 0:535249dc4bf5 146 acc_gyro->get_g_axes(axes);
cparata 0:535249dc4bf5 147 printf("LSM6DSO [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 148
cparata 0:535249dc4bf5 149 wait(1.5);
cparata 0:535249dc4bf5 150 }
cparata 0:535249dc4bf5 151 }