Simple "hello world" style program for X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensor Nucleo Expansion Board

Dependencies:   X_NUCLEO_IKS01A1 mbed

Dependents:   ese_project_copy ese_project_share

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  AST / EST
00005  * @version V0.0.1
00006  * @date    14-August-2015
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A1 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2015 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 "x_nucleo_iks01a1.h"
00042 
00043 /* Instantiate the expansion board */
00044 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
00045 
00046 /* Retrieve the composing elements of the expansion board */
00047 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
00048 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
00049 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
00050 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
00051 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
00052 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
00053 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
00054 
00055 /* Helper function for printing floats & doubles */
00056 static char *printDouble(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) break;
00080     *ptr = '0';
00081   }
00082 
00083   /* fill in (rest of) fractional part */
00084   sprintf(ptr, "%i", fractPart);
00085 
00086   return str;
00087 }
00088 
00089 
00090 /* Simple main function */
00091 int main() {
00092   uint8_t id;
00093   float value1, value2;
00094   char buffer1[32], buffer2[32];
00095   int32_t axes[3];
00096   
00097   printf("\r\n--- Starting new run ---\r\n");
00098 
00099   humidity_sensor->read_id(&id);
00100   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00101   pressure_sensor->read_id(&id);
00102   printf("LPS25H  pressure & temperature    = 0x%X\r\n", id);
00103   magnetometer->read_id(&id);
00104   printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00105   gyroscope->read_id(&id);
00106   printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
00107   
00108   wait(3);
00109  
00110   while(1) {
00111     printf("\r\n");
00112 
00113     temp_sensor1->get_temperature(&value1);
00114     humidity_sensor->get_humidity(&value2);
00115     printf("HTS221: [temp] %7s°C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00116     
00117     temp_sensor2->get_fahrenheit(&value1);
00118     pressure_sensor->get_pressure(&value2);
00119     printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00120 
00121     printf("---\r\n");
00122 
00123     magnetometer->get_m_axes(axes);
00124     printf("LIS3MDL [mag/mgauss]:  %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
00125 
00126     accelerometer->get_x_axes(axes);
00127     printf("LSM6DS0 [acc/mg]:      %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
00128 
00129     gyroscope->get_g_axes(axes);
00130     printf("LSM6DS0 [gyro/mdps]:   %7ld, %7ld, %7ld\r\n", axes[0], axes[1], axes[2]);
00131 
00132     wait(1.5);
00133   }
00134 }