Simple application to test ST motion and environmental MEMS sensors. Supports NUCLEO+X-NUCLEO-IKS01A2 , STEVAL-STLKT01V1 (a.k.a. SensorTile) and B-L475E-IOT01A boards.
Dependencies: HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X
Supports NUCLEO+X-NUCLEO-IKS01A2 , STEVAL-STLKT01V1 (a.k.a. SensorTile) and B-L475E-IOT01A boards.
Diff: main.cpp
- Revision:
- 8:39ecd15538f9
- Parent:
- 5:2fdca1624a33
--- a/main.cpp Tue Oct 03 16:35:18 2017 +0200 +++ b/main.cpp Wed Oct 04 13:29:18 2017 +0000 @@ -44,52 +44,52 @@ #include "LSM6DSLSensor.h" #ifdef TARGET_DISCO_L475VG_IOT01A + #include "lis3mdl_class.h" -#else // X-Nucleo-IKS01A2 +#include "VL53L0X.h" + +#else // Nucleo-XXX + X-Nucleo-IKS01A2 or SensorTile + +#ifdef TARGET_NUCLEO_L476RG +#define TARGET_SENSOR_TILE // comment out to use actual NUCLEO-L476RG instead of SensorTile +#endif + #include "LSM303AGRMagSensor.h" #include "LSM303AGRAccSensor.h" + #endif /* Retrieve the composing elements of the expansion board */ +/* Interface definition */ #ifdef TARGET_DISCO_L475VG_IOT01A static DevI2C devI2c(PB_11,PB_10); #else // X-Nucleo-IKS01A2 or SensorTile -#define TARGET_SENSOR_TILE // to be compiled as NUCLEO_L476RG until the target sensor tile will be created #ifdef TARGET_SENSOR_TILE -SPI devSPI(PB_15, NC, PB_13); // 3W on SensorTile + Arduino Cradle + Nucleo (compiled as Nucleo_L476RG) -static Serial ser(PC_12,PD_2); +#define SPI_TYPE_LPS22HB LPS22HBSensor::SPI3W +#define SPI_TYPE_LSM6DSL LSM6DSLSensor::SPI3W +SPI devSPI(PB_15, NC, PB_13); // 3-wires SPI on SensorTile +static Serial ser(PC_12,PD_2); // Serial with SensorTile Cradle Exp. Board + Nucleo #define printf(...) ser.printf(__VA_ARGS__) -#else -#ifdef SPI4W_TEST -SPI devSPI(PB_15, PB_14, PB_13); // 4W -#else // IKS01A2 + Nucleo +#else // Nucleo-XXX + X-Nucleo-IKS01A2 static DevI2C devI2c(D14,D15); -//static HTS221Sensor hum_temp(&devI2c); -#endif #endif #endif - -#if defined (TARGET_SENSOR_TILE) || defined (SPI4W_TEST) +/* Environmental sensors */ #ifdef TARGET_SENSOR_TILE -#define SPI_TYPE_LPS22HB LPS22HBSensor::SPI3W -#define SPI_TYPE_LSM6DSL LSM6DSLSensor::SPI3W -#else -#define SPI_TYPE LPS22HBSensor::SPI4W -#endif -//static LPS22HBSensor press_temp(&devSPI, PA_10, NC, SPI_TYPE); static LPS22HBSensor press_temp(&devSPI, PA_3, NC, SPI_TYPE_LPS22HB); -#else // target Nucleoxxx + IKS01A2 +#else // Nucleo-XXX + X-Nucleo-IKS01A2 or B-L475E-IOT01A2 static LPS22HBSensor press_temp(&devI2c); static HTS221Sensor hum_temp(&devI2c); #endif +/* Motion sensors */ #ifdef TARGET_DISCO_L475VG_IOT01A static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address static LIS3MDL magnetometer(&devI2c); -#else // X-NUCLEO_IKS01A2 or SensTile +#else // X-NUCLEO-IKS01A2 or SensorTile #if defined (TARGET_SENSOR_TILE) static LSM6DSLSensor acc_gyro(&devSPI,PB_12, NC, PA_2, SPI_TYPE_LSM6DSL); static LSM303AGRMagSensor magnetometer(&devSPI, PB_1); @@ -101,6 +101,12 @@ #endif #endif +/* Range sensor - B-L475E-IOT01A2 only */ +#ifdef TARGET_DISCO_L475VG_IOT01A +static DigitalOut shutdown_pin(PC_6); +static VL53L0X range(&devI2c, &shutdown_pin, PC_7); +#endif + /* Simple main function */ int main() { uint8_t id; @@ -112,12 +118,18 @@ #ifndef TARGET_SENSOR_TILE hum_temp.init(NULL); #endif + press_temp.init(NULL); -#ifndef TARGET_DISCO_L475VG_IOT01A magnetometer.init(NULL); + acc_gyro.init(NULL); + +#ifdef TARGET_DISCO_L475VG_IOT01A + range.init_sensor(VL53L0X_DEFAULT_ADDRESS); +#else // X-NUCLEO-IKS01A2 or SensorTile accelerometer.init(NULL); #endif - acc_gyro.init(NULL); + + /* Enable all sensors */ #ifndef TARGET_SENSOR_TILE @@ -143,7 +155,7 @@ magnetometer.read_id(&id); #ifdef TARGET_DISCO_L475VG_IOT01A printf("LIS3MDL magnetometer = 0x%X\r\n", id); -#else // X-NUCLEO-IKS01A2 +#else // X-NUCLEO-IKS01A2 or SensorTile printf("LSM303AGR magnetometer = 0x%X\r\n", id); accelerometer.read_id(&id); printf("LSM303AGR accelerometer = 0x%X\r\n", id); @@ -159,19 +171,19 @@ #ifndef TARGET_SENSOR_TILE hum_temp.get_temperature(&value1); hum_temp.get_humidity(&value2); - printf("HTS221: [temp] %7f C, [hum] %f%%\r\n", value1, value2); + printf("HTS221: [temp] %.2f C, [hum] %.2f%%\r\n", value1, value2); #endif value1=value2=0; press_temp.get_temperature(&value1); press_temp.get_pressure(&value2); - printf("LPS22HB: [temp] %7f C, [press] %f mbar\r\n", value1, value2); + printf("LPS22HB: [temp] %.2f C, [press] %.2f mbar\r\n", value1, value2); printf("---\r\n"); magnetometer.get_m_axes(axes); #ifdef TARGET_DISCO_L475VG_IOT01A printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); -#else // X-NUCLEO-IKS01A2 +#else // X-NUCLEO-IKS01A2 or SensorTile printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); accelerometer.get_x_axes(axes); printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); @@ -183,11 +195,21 @@ acc_gyro.get_g_axes(axes); printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); -#if defined (TARGET_SENSOR_TILE) || defined (TARGET_DISCO_L475VG_IOT01A) +#ifdef TARGET_DISCO_L475VG_IOT01A + uint32_t distance; + int status = range.get_distance(&distance); + if (status == VL53L0X_ERROR_NONE) { + printf("VL53L0X [mm]: %6ld\r\n", distance); + } else { + printf("VL53L0X [mm]: --\r\n"); + } +#endif + +#if defined (TARGET_SENSOR_TILE) printf("\033[7A"); #else printf("\033[8A"); #endif - wait(1); + wait(0.5); } }