Make NTU Hackathon / Mbed 2 deprecated Program2_SensorReader

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of Sensors_Reader by ST

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  CLab
00005  * @version V1.0.0
00006  * @date    2-December-2016
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) 2016 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_iks01a2.h"
00042 
00043 /* Instantiate the expansion board */
00044 static X_NUCLEO_IKS01A2 *mems_expansion_board = X_NUCLEO_IKS01A2::Instance(D14, D15, D4, D5);
00045 
00046 /* Retrieve the composing elements of the expansion board */
00047 static LSM303AGR_MAG_Sensor *magnetometer = mems_expansion_board->magnetometer;
00048 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00049 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
00050 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00051 static LSM303AGR_ACC_Sensor *accelerometer = mems_expansion_board->accelerometer;
00052 
00053 /* Helper function for printing floats & doubles */
00054 static char *printDouble(char* str, double v, int decimalDigits=2)
00055 {
00056   int i = 1;
00057   int intPart, fractPart;
00058   int len;
00059   char *ptr;
00060 
00061   /* prepare decimal digits multiplicator */
00062   for (;decimalDigits!=0; i*=10, decimalDigits--);
00063 
00064   /* calculate integer & fractinal parts */
00065   intPart = (int)v;
00066   fractPart = (int)((v-(double)(int)v)*i);
00067 
00068   /* fill in integer part */
00069   sprintf(str, "%i.", intPart);
00070 
00071   /* prepare fill in of fractional part */
00072   len = strlen(str);
00073   ptr = &str[len];
00074 
00075   /* fill in leading fractional zeros */
00076   for (i/=10;i>1; i/=10, ptr++) {
00077     if(fractPart >= i) break;
00078     *ptr = '0';
00079   }
00080 
00081   /* fill in (rest of) fractional part */
00082   sprintf(ptr, "%i", fractPart);
00083 
00084   return str;
00085 }
00086 
00087 
00088 /* Simple main function */
00089 int main() {
00090   uint8_t id;
00091   float value1, value2;
00092   char buffer1[32], buffer2[32];
00093   int32_t axes[3];
00094   
00095   /* Enable all sensors */
00096   hum_temp->Enable();
00097   press_temp->Enable();
00098   magnetometer->Enable();
00099   accelerometer->Enable();
00100   acc_gyro->Enable_X();
00101   acc_gyro->Enable_G();
00102   
00103   printf("\r\n--- Starting new run ---\r\n");
00104 
00105   hum_temp->ReadID(&id);
00106   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00107   press_temp->ReadID(&id);
00108   printf("LPS22HB  pressure & temperature   = 0x%X\r\n", id);
00109   magnetometer->ReadID(&id);
00110   printf("LSM303AGR magnetometer            = 0x%X\r\n", id);
00111   accelerometer->ReadID(&id);
00112   printf("LSM303AGR accelerometer           = 0x%X\r\n", id);
00113   acc_gyro->ReadID(&id);
00114   printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
00115  
00116   while(1) {
00117     printf("\r\n");
00118 
00119     hum_temp->GetTemperature(&value1);
00120     hum_temp->GetHumidity(&value2);
00121     printf("HTS221: [temp] %7s C,   [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00122     
00123     press_temp->GetTemperature(&value1);
00124     press_temp->GetPressure(&value2);
00125     printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
00126 
00127     printf("---\r\n");
00128 
00129     magnetometer->Get_M_Axes(axes);
00130     printf("LSM303AGR [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00131     
00132     accelerometer->Get_X_Axes(axes);
00133     printf("LSM303AGR [acc/mg]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00134 
00135     acc_gyro->Get_X_Axes(axes);
00136     printf("LSM6DSL [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00137 
00138     acc_gyro->Get_G_Axes(axes);
00139     printf("LSM6DSL [gyro/mdps]:   %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00140 
00141     wait(1.5);
00142   }
00143 }