cheng bao / HelloWorld_ST

Dependencies:   HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X

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    5-September-2017
00007  * @brief   Simple Example application for using  X_NUCLEO_IKS01A2  
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion and 
00009  *          B-L475E-IOT01A2 boards.
00010  ******************************************************************************
00011  * @attention
00012  *
00013  * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00014  *
00015  * Redistribution and use in source and binary forms, with or without modification,
00016  * are permitted provided that the following conditions are met:
00017  *   1. Redistributions of source code must retain the above copyright notice,
00018  *      this list of conditions and the following disclaimer.
00019  *   2. Redistributions in binary form must reproduce the above copyright notice,
00020  *      this list of conditions and the following disclaimer in the documentation
00021  *      and/or other materials provided with the distribution.
00022  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00023  *      may be used to endorse or promote products derived from this software
00024  *      without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00029  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00030  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00031  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00032  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00034  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  ******************************************************************************
00038 */ 
00039 
00040 /* Includes */
00041 #include "mbed.h"
00042 #include "HTS221Sensor.h"
00043 #include "LPS22HBSensor.h"
00044 #include "LSM6DSLSensor.h"
00045 
00046 #ifdef TARGET_DISCO_L475VG_IOT01A
00047 
00048 #include "lis3mdl_class.h"
00049 #include "VL53L0X.h"
00050 
00051 #else // Nucleo-XXX + X-Nucleo-IKS01A2 or SensorTile
00052 
00053 #ifdef TARGET_NUCLEO_L476RG
00054 #define TARGET_SENSOR_TILE   // comment out to use actual NUCLEO-L476RG instead of SensorTile
00055 #endif
00056 
00057 #include "LSM303AGRMagSensor.h"
00058 #include "LSM303AGRAccSensor.h"
00059 
00060 #endif
00061 
00062 
00063 /* Retrieve the composing elements of the expansion board */
00064 
00065 /* Interface definition */
00066 #ifdef TARGET_DISCO_L475VG_IOT01A
00067 static DevI2C devI2c(PB_11,PB_10);
00068 #else // X-Nucleo-IKS01A2 or SensorTile
00069 #ifdef TARGET_SENSOR_TILE
00070 #define SPI_TYPE_LPS22HB   LPS22HBSensor::SPI3W
00071 #define SPI_TYPE_LSM6DSL   LSM6DSLSensor::SPI3W
00072 SPI devSPI(PB_15, NC, PB_13);  // 3-wires SPI on SensorTile  
00073 static Serial ser(PC_12,PD_2); // Serial with SensorTile Cradle Exp. Board + Nucleo   
00074 #define printf(...) ser.printf(__VA_ARGS__)     
00075 #else  // Nucleo-XXX + X-Nucleo-IKS01A2 
00076 static DevI2C devI2c(D14,D15);
00077 #endif
00078 #endif
00079 
00080 /* Environmental sensors */
00081 #ifdef TARGET_SENSOR_TILE
00082 static LPS22HBSensor press_temp(&devSPI, PA_3, NC, SPI_TYPE_LPS22HB); 
00083 #else  // Nucleo-XXX + X-Nucleo-IKS01A2 or B-L475E-IOT01A2
00084 static LPS22HBSensor press_temp(&devI2c);
00085 static HTS221Sensor hum_temp(&devI2c);
00086 #endif
00087 
00088 /* Motion sensors */
00089 #ifdef TARGET_DISCO_L475VG_IOT01A
00090 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
00091 static LIS3MDL magnetometer(&devI2c);
00092 #else // X-NUCLEO-IKS01A2 or SensorTile
00093 #if defined (TARGET_SENSOR_TILE)
00094 static LSM6DSLSensor acc_gyro(&devSPI,PB_12, NC, PA_2, SPI_TYPE_LSM6DSL); 
00095 static LSM303AGRMagSensor magnetometer(&devSPI, PB_1);
00096 static LSM303AGRAccSensor accelerometer(&devSPI, PC_4);
00097 #else
00098 static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH,D4,D5); // high address
00099 static LSM303AGRMagSensor magnetometer(&devI2c);
00100 static LSM303AGRAccSensor accelerometer(&devI2c);
00101 #endif
00102 #endif
00103 
00104 /* Range sensor - B-L475E-IOT01A2 only */
00105 #ifdef TARGET_DISCO_L475VG_IOT01A
00106 static DigitalOut shutdown_pin(PC_6);
00107 static VL53L0X range(&devI2c, &shutdown_pin, PC_7);
00108 #endif
00109 
00110 /* Simple main function */
00111 int main() {
00112   uint8_t id;
00113   float value1, value2;
00114 //  char buffer1[32], buffer2[32];
00115   int32_t axes[3];
00116   
00117   /* Init all sensors with default params */
00118 #ifndef TARGET_SENSOR_TILE
00119   hum_temp.init(NULL);
00120 #endif  
00121 
00122   press_temp.init(NULL);
00123   magnetometer.init(NULL);
00124   acc_gyro.init(NULL);
00125   
00126 #ifdef TARGET_DISCO_L475VG_IOT01A
00127   range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
00128 #else // X-NUCLEO-IKS01A2 or SensorTile
00129   accelerometer.init(NULL);
00130 #endif
00131 
00132 
00133   
00134   /* Enable all sensors */
00135 #ifndef TARGET_SENSOR_TILE  
00136   hum_temp.enable();
00137 #endif  
00138   press_temp.enable();
00139 #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2
00140   magnetometer.enable();
00141   accelerometer.enable();
00142 #endif
00143   acc_gyro.enable_x();
00144   acc_gyro.enable_g();
00145   
00146   printf("\033[2J\033[20A");
00147   printf ("\r\n--- Starting new run ---\r\n\r\n");
00148 
00149 #ifndef TARGET_SENSOR_TILE
00150   hum_temp.read_id(&id);
00151   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00152 #endif  
00153   press_temp.read_id(&id);
00154   printf("LPS22HB pressure & temperature    = 0x%X\r\n", id);
00155   magnetometer.read_id(&id);
00156 #ifdef TARGET_DISCO_L475VG_IOT01A
00157   printf("LIS3MDL magnetometer              = 0x%X\r\n", id);
00158 #else // X-NUCLEO-IKS01A2 or SensorTile
00159   printf("LSM303AGR magnetometer            = 0x%X\r\n", id);
00160   accelerometer.read_id(&id);
00161   printf("LSM303AGR accelerometer           = 0x%X\r\n", id); 
00162 #endif
00163   acc_gyro.read_id(&id);
00164   printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
00165   
00166   printf("\n\r--- Reading sensor values ---\n\r"); ;
00167  
00168   while(1) {
00169     printf("\r\n");
00170 
00171 #ifndef TARGET_SENSOR_TILE    
00172     hum_temp.get_temperature(&value1);
00173     hum_temp.get_humidity(&value2);
00174     printf("HTS221:  [temp] %.2f C, [hum]   %.2f%%\r\n", value1, value2);
00175 #endif    
00176     value1=value2=0;    
00177     press_temp.get_temperature(&value1);
00178     press_temp.get_pressure(&value2);
00179     printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value1, value2);
00180 
00181     printf("---\r\n");
00182 
00183     magnetometer.get_m_axes(axes);
00184 #ifdef TARGET_DISCO_L475VG_IOT01A
00185     printf("LIS3MDL [mag/mgauss]:    %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00186 #else // X-NUCLEO-IKS01A2 or SensorTile
00187     printf("LSM303AGR [mag/mgauss]:  %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00188     accelerometer.get_x_axes(axes);
00189     printf("LSM303AGR [acc/mg]:      %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);    
00190 #endif    
00191 
00192     acc_gyro.get_x_axes(axes);
00193     printf("LSM6DSL [acc/mg]:        %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00194 
00195     acc_gyro.get_g_axes(axes);
00196     printf("LSM6DSL [gyro/mdps]:     %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
00197 
00198 #ifdef TARGET_DISCO_L475VG_IOT01A
00199     uint32_t distance;
00200     int status = range.get_distance(&distance);
00201     if (status == VL53L0X_ERROR_NONE) {
00202         printf("VL53L0X [mm]:            %6ld\r\n", distance);
00203     } else {
00204         printf("VL53L0X [mm]:                --\r\n");
00205     }
00206 #endif
00207 
00208 #if defined (TARGET_SENSOR_TILE)  
00209     printf("\033[7A");
00210 #else    
00211     printf("\033[8A");
00212 #endif
00213     wait(0.5);
00214   }
00215 }