IKS01A3

Dependencies:   X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  SRA
00005  * @version V1.0.0
00006  * @date    5-March-2019
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A3
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */
00038 
00039 /* Includes */
00040 #include "mbed.h"
00041 #include "XNucleoIKS01A3.h"
00042 
00043 /* Instantiate the expansion board */
00044 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
00045 
00046 /* Retrieve the composing elements of the expansion board */
00047 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
00048 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00049 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
00050 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
00051 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
00052 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
00053 
00054 /* Helper function for printing floats & doubles */
00055 static char *print_double(char *str, double v, int decimalDigits = 2)
00056 {
00057     int i = 1;
00058     int intPart, fractPart;
00059     int len;
00060     char *ptr;
00061 
00062     /* prepare decimal digits multiplicator */
00063     for (; decimalDigits != 0; i *= 10, decimalDigits--);
00064 
00065     /* calculate integer & fractinal parts */
00066     intPart = (int)v;
00067     fractPart = (int)((v - (double)(int)v) * i);
00068 
00069     /* fill in integer part */
00070     sprintf(str, "%i.", intPart);
00071 
00072     /* prepare fill in of fractional part */
00073     len = strlen(str);
00074     ptr = &str[len];
00075 
00076     /* fill in leading fractional zeros */
00077     for (i /= 10; i > 1; i /= 10, ptr++) {
00078         if (fractPart >= i) {
00079             break;
00080         }
00081         *ptr = '0';
00082     }
00083 
00084     /* fill in (rest of) fractional part */
00085     sprintf(ptr, "%i", fractPart);
00086 
00087     return str;
00088 }
00089 
00090 /* Simple main function */
00091 int main()
00092 {
00093     uint8_t id;
00094     float value1, value2;
00095     char buffer1[32], buffer2[32];
00096     int32_t axes[3];
00097 
00098     /* Enable all sensors */
00099     hum_temp->enable();
00100     press_temp->enable();
00101     temp->enable();
00102     magnetometer->enable();
00103     accelerometer->enable_x();
00104     acc_gyro->enable_x();
00105     acc_gyro->enable_g();
00106 
00107     printf("\r\n--- Starting new run ---\r\n");
00108 
00109     hum_temp->read_id(&id);
00110     printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00111     press_temp->read_id(&id);
00112     printf("LPS22HH  pressure & temperature   = 0x%X\r\n", id);
00113     temp->read_id(&id);
00114     printf("STTS751 temperature               = 0x%X\r\n", id);
00115     magnetometer->read_id(&id);
00116     printf("LIS2MDL magnetometer              = 0x%X\r\n", id);
00117     accelerometer->read_id(&id);
00118     printf("LIS2DW12 accelerometer            = 0x%X\r\n", id);
00119     acc_gyro->read_id(&id);
00120     printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
00121 
00122     while (1) {
00123         printf("\r\n");
00124 
00125         hum_temp->get_temperature(&value1);
00126         hum_temp->get_humidity(&value2);
00127         printf("HTS221: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00128 
00129         press_temp->get_temperature(&value1);
00130         press_temp->get_pressure(&value2);
00131         printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00132 
00133         temp->get_temperature(&value1);
00134         printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1));
00135 
00136         printf("---\r\n");
00137 
00138         magnetometer->get_m_axes(axes);
00139         printf("LIS2MDL [mag/mgauss]:  %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00140 
00141         accelerometer->get_x_axes(axes);
00142         printf("LIS2DW12 [acc/mg]:  %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00143 
00144         acc_gyro->get_x_axes(axes);
00145         printf("LSM6DSO [acc/mg]:      %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00146 
00147         acc_gyro->get_g_axes(axes);
00148         printf("LSM6DSO [gyro/mdps]:   %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00149 
00150         wait(1.5);
00151     }
00152 }