Example of hello world for X-NUCLEO-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 "rtos.h"
00042 #include "XNucleoIKS01A3.h"
00043 
00044 /* Instantiate the expansion board */
00045 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
00046 
00047 /* Retrieve the composing elements of the expansion board */
00048 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
00049 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00050 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
00051 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
00052 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
00053 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
00054 
00055 /* Helper function for printing floats & doubles */
00056 static char *print_double(char *str, double v, int decimalDigits = 2)
00057 {
00058     int i = 1;
00059     int intPart, fractPart;
00060     int len;
00061     char *ptr;
00062 
00063     /* prepare decimal digits multiplicator */
00064     for (; decimalDigits != 0; i *= 10, decimalDigits--);
00065 
00066     /* calculate integer & fractinal parts */
00067     intPart = (int)v;
00068     fractPart = (int)((v - (double)(int)v) * i);
00069 
00070     /* fill in integer part */
00071     sprintf(str, "%i.", intPart);
00072 
00073     /* prepare fill in of fractional part */
00074     len = strlen(str);
00075     ptr = &str[len];
00076 
00077     /* fill in leading fractional zeros */
00078     for (i /= 10; i > 1; i /= 10, ptr++) {
00079         if (fractPart >= i) {
00080             break;
00081         }
00082         *ptr = '0';
00083     }
00084 
00085     /* fill in (rest of) fractional part */
00086     sprintf(ptr, "%i", fractPart);
00087 
00088     return str;
00089 }
00090 
00091 /* Simple main function */
00092 int main()
00093 {
00094     uint8_t id;
00095     float value1, value2;
00096     char buffer1[32], buffer2[32];
00097     int32_t axes[3];
00098 
00099     /* Enable all sensors */
00100     hum_temp->enable();
00101     press_temp->enable();
00102     temp->enable();
00103     magnetometer->enable();
00104     accelerometer->enable_x();
00105     acc_gyro->enable_x();
00106     acc_gyro->enable_g();
00107 
00108     printf("\r\n--- Starting new run ---\r\n");
00109 
00110     hum_temp->read_id(&id);
00111     printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00112     press_temp->read_id(&id);
00113     printf("LPS22HH  pressure & temperature   = 0x%X\r\n", id);
00114     temp->read_id(&id);
00115     printf("STTS751 temperature               = 0x%X\r\n", id);
00116     magnetometer->read_id(&id);
00117     printf("LIS2MDL magnetometer              = 0x%X\r\n", id);
00118     accelerometer->read_id(&id);
00119     printf("LIS2DW12 accelerometer            = 0x%X\r\n", id);
00120     acc_gyro->read_id(&id);
00121     printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
00122 
00123     while (1) {
00124         printf("\r\n");
00125 
00126         hum_temp->get_temperature(&value1);
00127         hum_temp->get_humidity(&value2);
00128         printf("HTS221: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00129 
00130         press_temp->get_temperature(&value1);
00131         press_temp->get_pressure(&value2);
00132         printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00133 
00134         temp->get_temperature(&value1);
00135         printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1));
00136 
00137         printf("---\r\n");
00138 
00139         magnetometer->get_m_axes(axes);
00140         printf("LIS2MDL [mag/mgauss]:  %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00141 
00142         accelerometer->get_x_axes(axes);
00143         printf("LIS2DW12 [acc/mg]:  %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00144 
00145         acc_gyro->get_x_axes(axes);
00146         printf("LSM6DSO [acc/mg]:      %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00147 
00148         acc_gyro->get_g_axes(axes);
00149         printf("LSM6DSO [gyro/mdps]:   %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00150 
00151         ThisThread::sleep_for(1500);
00152     }
00153 }