IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

Committer:
cparata
Date:
Wed Jul 24 14:22:13 2019 +0000
Revision:
5:7c883cce2bc4
Parent:
3:9f8fadc965fc
Format with Astyle

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 5:7c883cce2bc4 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 5:7c883cce2bc4 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 5:7c883cce2bc4 55 static char *print_double(char *str, double v, int decimalDigits = 2)
cparata 0:535249dc4bf5 56 {
cparata 5:7c883cce2bc4 57 int i = 1;
cparata 5:7c883cce2bc4 58 int intPart, fractPart;
cparata 5:7c883cce2bc4 59 int len;
cparata 5:7c883cce2bc4 60 char *ptr;
cparata 0:535249dc4bf5 61
cparata 5:7c883cce2bc4 62 /* prepare decimal digits multiplicator */
cparata 5:7c883cce2bc4 63 for (; decimalDigits != 0; i *= 10, decimalDigits--);
cparata 0:535249dc4bf5 64
cparata 5:7c883cce2bc4 65 /* calculate integer & fractinal parts */
cparata 5:7c883cce2bc4 66 intPart = (int)v;
cparata 5:7c883cce2bc4 67 fractPart = (int)((v - (double)(int)v) * i);
cparata 0:535249dc4bf5 68
cparata 5:7c883cce2bc4 69 /* fill in integer part */
cparata 5:7c883cce2bc4 70 sprintf(str, "%i.", intPart);
cparata 0:535249dc4bf5 71
cparata 5:7c883cce2bc4 72 /* prepare fill in of fractional part */
cparata 5:7c883cce2bc4 73 len = strlen(str);
cparata 5:7c883cce2bc4 74 ptr = &str[len];
cparata 0:535249dc4bf5 75
cparata 5:7c883cce2bc4 76 /* fill in leading fractional zeros */
cparata 5:7c883cce2bc4 77 for (i /= 10; i > 1; i /= 10, ptr++) {
cparata 5:7c883cce2bc4 78 if (fractPart >= i) {
cparata 5:7c883cce2bc4 79 break;
cparata 5:7c883cce2bc4 80 }
cparata 5:7c883cce2bc4 81 *ptr = '0';
cparata 0:535249dc4bf5 82 }
cparata 0:535249dc4bf5 83
cparata 5:7c883cce2bc4 84 /* fill in (rest of) fractional part */
cparata 5:7c883cce2bc4 85 sprintf(ptr, "%i", fractPart);
cparata 0:535249dc4bf5 86
cparata 5:7c883cce2bc4 87 return str;
cparata 0:535249dc4bf5 88 }
cparata 0:535249dc4bf5 89
cparata 0:535249dc4bf5 90 /* Simple main function */
cparata 5:7c883cce2bc4 91 int main()
cparata 5:7c883cce2bc4 92 {
cparata 5:7c883cce2bc4 93 uint8_t id;
cparata 5:7c883cce2bc4 94 float value1, value2;
cparata 5:7c883cce2bc4 95 char buffer1[32], buffer2[32];
cparata 5:7c883cce2bc4 96 int32_t axes[3];
cparata 5:7c883cce2bc4 97
cparata 5:7c883cce2bc4 98 /* Enable all sensors */
cparata 5:7c883cce2bc4 99 hum_temp->enable();
cparata 5:7c883cce2bc4 100 press_temp->enable();
cparata 5:7c883cce2bc4 101 temp->enable();
cparata 5:7c883cce2bc4 102 magnetometer->enable();
cparata 5:7c883cce2bc4 103 accelerometer->enable_x();
cparata 5:7c883cce2bc4 104 acc_gyro->enable_x();
cparata 5:7c883cce2bc4 105 acc_gyro->enable_g();
cparata 5:7c883cce2bc4 106
cparata 5:7c883cce2bc4 107 printf("\r\n--- Starting new run ---\r\n");
cparata 5:7c883cce2bc4 108
cparata 5:7c883cce2bc4 109 hum_temp->read_id(&id);
cparata 5:7c883cce2bc4 110 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 111 press_temp->read_id(&id);
cparata 5:7c883cce2bc4 112 printf("LPS22HH pressure & temperature = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 113 temp->read_id(&id);
cparata 5:7c883cce2bc4 114 printf("STTS751 temperature = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 115 magnetometer->read_id(&id);
cparata 5:7c883cce2bc4 116 printf("LIS2MDL magnetometer = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 117 accelerometer->read_id(&id);
cparata 5:7c883cce2bc4 118 printf("LIS2DW12 accelerometer = 0x%X\r\n", id);
cparata 5:7c883cce2bc4 119 acc_gyro->read_id(&id);
cparata 5:7c883cce2bc4 120 printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
cparata 0:535249dc4bf5 121
cparata 5:7c883cce2bc4 122 while (1) {
cparata 5:7c883cce2bc4 123 printf("\r\n");
cparata 5:7c883cce2bc4 124
cparata 5:7c883cce2bc4 125 hum_temp->get_temperature(&value1);
cparata 5:7c883cce2bc4 126 hum_temp->get_humidity(&value2);
cparata 5:7c883cce2bc4 127 printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
cparata 5:7c883cce2bc4 128
cparata 5:7c883cce2bc4 129 press_temp->get_temperature(&value1);
cparata 5:7c883cce2bc4 130 press_temp->get_pressure(&value2);
cparata 5:7c883cce2bc4 131 printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
cparata 5:7c883cce2bc4 132
cparata 5:7c883cce2bc4 133 temp->get_temperature(&value1);
cparata 5:7c883cce2bc4 134 printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1));
cparata 5:7c883cce2bc4 135
cparata 5:7c883cce2bc4 136 printf("---\r\n");
cparata 0:535249dc4bf5 137
cparata 5:7c883cce2bc4 138 magnetometer->get_m_axes(axes);
cparata 5:7c883cce2bc4 139 printf("LIS2MDL [mag/mgauss]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 140
cparata 5:7c883cce2bc4 141 accelerometer->get_x_axes(axes);
cparata 5:7c883cce2bc4 142 printf("LIS2DW12 [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 143
cparata 5:7c883cce2bc4 144 acc_gyro->get_x_axes(axes);
cparata 5:7c883cce2bc4 145 printf("LSM6DSO [acc/mg]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 146
cparata 5:7c883cce2bc4 147 acc_gyro->get_g_axes(axes);
cparata 5:7c883cce2bc4 148 printf("LSM6DSO [gyro/mdps]: %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
cparata 0:535249dc4bf5 149
cparata 5:7c883cce2bc4 150 wait(1.5);
cparata 5:7c883cce2bc4 151 }
cparata 0:535249dc4bf5 152 }