A1
Dependencies: LPS22HB LSM6DSL LSM303AGR HTS221 LIS3MDL VL53L0X
main.cpp@5:2fdca1624a33, 2017-10-03 (annotated)
- Committer:
- mapellil
- Date:
- Tue Oct 03 14:05:43 2017 +0200
- Revision:
- 5:2fdca1624a33
- Parent:
- 4:a876fbe13688
- Child:
- 8:39ecd15538f9
Added support for targets SensorTile (through define; set target NUCLEO_L476RG), DISCO_L475VG_IOT01A, Nucleoxx+IKS01A2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:6a97487a0ab3 | 1 | /** |
nikapov | 0:6a97487a0ab3 | 2 | ****************************************************************************** |
nikapov | 0:6a97487a0ab3 | 3 | * @file main.cpp |
nikapov | 0:6a97487a0ab3 | 4 | * @author CLab |
nikapov | 0:6a97487a0ab3 | 5 | * @version V1.0.0 |
nikapov | 0:6a97487a0ab3 | 6 | * @date 5-September-2017 |
nikapov | 0:6a97487a0ab3 | 7 | * @brief Simple Example application for using X_NUCLEO_IKS01A2 |
nikapov | 0:6a97487a0ab3 | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion and |
nikapov | 0:6a97487a0ab3 | 9 | * B-L475E-IOT01A2 boards. |
nikapov | 0:6a97487a0ab3 | 10 | ****************************************************************************** |
nikapov | 0:6a97487a0ab3 | 11 | * @attention |
nikapov | 0:6a97487a0ab3 | 12 | * |
nikapov | 0:6a97487a0ab3 | 13 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
nikapov | 0:6a97487a0ab3 | 14 | * |
nikapov | 0:6a97487a0ab3 | 15 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:6a97487a0ab3 | 16 | * are permitted provided that the following conditions are met: |
nikapov | 0:6a97487a0ab3 | 17 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:6a97487a0ab3 | 18 | * this list of conditions and the following disclaimer. |
nikapov | 0:6a97487a0ab3 | 19 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:6a97487a0ab3 | 20 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:6a97487a0ab3 | 21 | * and/or other materials provided with the distribution. |
nikapov | 0:6a97487a0ab3 | 22 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:6a97487a0ab3 | 23 | * may be used to endorse or promote products derived from this software |
nikapov | 0:6a97487a0ab3 | 24 | * without specific prior written permission. |
nikapov | 0:6a97487a0ab3 | 25 | * |
nikapov | 0:6a97487a0ab3 | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:6a97487a0ab3 | 27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:6a97487a0ab3 | 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:6a97487a0ab3 | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:6a97487a0ab3 | 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:6a97487a0ab3 | 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:6a97487a0ab3 | 32 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:6a97487a0ab3 | 33 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:6a97487a0ab3 | 34 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:6a97487a0ab3 | 35 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:6a97487a0ab3 | 36 | * |
nikapov | 0:6a97487a0ab3 | 37 | ****************************************************************************** |
nikapov | 0:6a97487a0ab3 | 38 | */ |
nikapov | 0:6a97487a0ab3 | 39 | |
nikapov | 0:6a97487a0ab3 | 40 | /* Includes */ |
nikapov | 0:6a97487a0ab3 | 41 | #include "mbed.h" |
nikapov | 0:6a97487a0ab3 | 42 | #include "HTS221Sensor.h" |
nikapov | 0:6a97487a0ab3 | 43 | #include "LPS22HBSensor.h" |
nikapov | 0:6a97487a0ab3 | 44 | #include "LSM6DSLSensor.h" |
nikapov | 0:6a97487a0ab3 | 45 | |
nikapov | 0:6a97487a0ab3 | 46 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 0:6a97487a0ab3 | 47 | #include "lis3mdl_class.h" |
nikapov | 0:6a97487a0ab3 | 48 | #else // X-Nucleo-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 49 | #include "LSM303AGRMagSensor.h" |
mapellil | 5:2fdca1624a33 | 50 | #include "LSM303AGRAccSensor.h" |
nikapov | 0:6a97487a0ab3 | 51 | #endif |
nikapov | 0:6a97487a0ab3 | 52 | |
nikapov | 0:6a97487a0ab3 | 53 | |
nikapov | 0:6a97487a0ab3 | 54 | /* Retrieve the composing elements of the expansion board */ |
nikapov | 0:6a97487a0ab3 | 55 | |
nikapov | 0:6a97487a0ab3 | 56 | #ifdef TARGET_DISCO_L475VG_IOT01A |
mapellil | 4:a876fbe13688 | 57 | static DevI2C devI2c(PB_11,PB_10); |
mapellil | 5:2fdca1624a33 | 58 | #else // X-Nucleo-IKS01A2 or SensorTile |
mapellil | 5:2fdca1624a33 | 59 | #define TARGET_SENSOR_TILE // to be compiled as NUCLEO_L476RG until the target sensor tile will be created |
mapellil | 5:2fdca1624a33 | 60 | #ifdef TARGET_SENSOR_TILE |
mapellil | 5:2fdca1624a33 | 61 | SPI devSPI(PB_15, NC, PB_13); // 3W on SensorTile + Arduino Cradle + Nucleo (compiled as Nucleo_L476RG) |
mapellil | 5:2fdca1624a33 | 62 | static Serial ser(PC_12,PD_2); |
mapellil | 5:2fdca1624a33 | 63 | #define printf(...) ser.printf(__VA_ARGS__) |
mapellil | 5:2fdca1624a33 | 64 | #else |
mapellil | 5:2fdca1624a33 | 65 | #ifdef SPI4W_TEST |
mapellil | 4:a876fbe13688 | 66 | SPI devSPI(PB_15, PB_14, PB_13); // 4W |
mapellil | 5:2fdca1624a33 | 67 | #else // IKS01A2 + Nucleo |
mapellil | 2:4d4fa82dbc59 | 68 | static DevI2C devI2c(D14,D15); |
mapellil | 5:2fdca1624a33 | 69 | //static HTS221Sensor hum_temp(&devI2c); |
nikapov | 0:6a97487a0ab3 | 70 | #endif |
mapellil | 4:a876fbe13688 | 71 | #endif |
mapellil | 4:a876fbe13688 | 72 | #endif |
nikapov | 0:6a97487a0ab3 | 73 | |
mapellil | 4:a876fbe13688 | 74 | |
mapellil | 5:2fdca1624a33 | 75 | #if defined (TARGET_SENSOR_TILE) || defined (SPI4W_TEST) |
mapellil | 5:2fdca1624a33 | 76 | #ifdef TARGET_SENSOR_TILE |
mapellil | 5:2fdca1624a33 | 77 | #define SPI_TYPE_LPS22HB LPS22HBSensor::SPI3W |
mapellil | 5:2fdca1624a33 | 78 | #define SPI_TYPE_LSM6DSL LSM6DSLSensor::SPI3W |
mapellil | 4:a876fbe13688 | 79 | #else |
mapellil | 4:a876fbe13688 | 80 | #define SPI_TYPE LPS22HBSensor::SPI4W |
mapellil | 4:a876fbe13688 | 81 | #endif |
mapellil | 5:2fdca1624a33 | 82 | //static LPS22HBSensor press_temp(&devSPI, PA_10, NC, SPI_TYPE); |
mapellil | 5:2fdca1624a33 | 83 | static LPS22HBSensor press_temp(&devSPI, PA_3, NC, SPI_TYPE_LPS22HB); |
mapellil | 4:a876fbe13688 | 84 | #else // target Nucleoxxx + IKS01A2 |
mapellil | 2:4d4fa82dbc59 | 85 | static LPS22HBSensor press_temp(&devI2c); |
mapellil | 5:2fdca1624a33 | 86 | static HTS221Sensor hum_temp(&devI2c); |
mapellil | 4:a876fbe13688 | 87 | #endif |
nikapov | 0:6a97487a0ab3 | 88 | |
nikapov | 0:6a97487a0ab3 | 89 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 3:82e8968382d6 | 90 | static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address |
mapellil | 2:4d4fa82dbc59 | 91 | static LIS3MDL magnetometer(&devI2c); |
mapellil | 5:2fdca1624a33 | 92 | #else // X-NUCLEO_IKS01A2 or SensTile |
mapellil | 5:2fdca1624a33 | 93 | #if defined (TARGET_SENSOR_TILE) |
mapellil | 5:2fdca1624a33 | 94 | static LSM6DSLSensor acc_gyro(&devSPI,PB_12, NC, PA_2, SPI_TYPE_LSM6DSL); |
mapellil | 5:2fdca1624a33 | 95 | static LSM303AGRMagSensor magnetometer(&devSPI, PB_1); |
mapellil | 5:2fdca1624a33 | 96 | static LSM303AGRAccSensor accelerometer(&devSPI, PC_4); |
mapellil | 5:2fdca1624a33 | 97 | #else |
nikapov | 3:82e8968382d6 | 98 | static LSM6DSLSensor acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_HIGH,D4,D5); // high address |
mapellil | 2:4d4fa82dbc59 | 99 | static LSM303AGRMagSensor magnetometer(&devI2c); |
mapellil | 5:2fdca1624a33 | 100 | static LSM303AGRAccSensor accelerometer(&devI2c); |
nikapov | 0:6a97487a0ab3 | 101 | #endif |
mapellil | 5:2fdca1624a33 | 102 | #endif |
nikapov | 0:6a97487a0ab3 | 103 | |
nikapov | 0:6a97487a0ab3 | 104 | /* Simple main function */ |
nikapov | 0:6a97487a0ab3 | 105 | int main() { |
nikapov | 0:6a97487a0ab3 | 106 | uint8_t id; |
nikapov | 0:6a97487a0ab3 | 107 | float value1, value2; |
mapellil | 5:2fdca1624a33 | 108 | // char buffer1[32], buffer2[32]; |
nikapov | 0:6a97487a0ab3 | 109 | int32_t axes[3]; |
nikapov | 0:6a97487a0ab3 | 110 | |
nikapov | 0:6a97487a0ab3 | 111 | /* Init all sensors with default params */ |
mapellil | 5:2fdca1624a33 | 112 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 113 | hum_temp.init(NULL); |
mapellil | 5:2fdca1624a33 | 114 | #endif |
nikapov | 0:6a97487a0ab3 | 115 | press_temp.init(NULL); |
mapellil | 5:2fdca1624a33 | 116 | #ifndef TARGET_DISCO_L475VG_IOT01A |
nikapov | 0:6a97487a0ab3 | 117 | magnetometer.init(NULL); |
mapellil | 5:2fdca1624a33 | 118 | accelerometer.init(NULL); |
mapellil | 5:2fdca1624a33 | 119 | #endif |
nikapov | 0:6a97487a0ab3 | 120 | acc_gyro.init(NULL); |
nikapov | 0:6a97487a0ab3 | 121 | |
nikapov | 0:6a97487a0ab3 | 122 | /* Enable all sensors */ |
mapellil | 5:2fdca1624a33 | 123 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 124 | hum_temp.enable(); |
mapellil | 5:2fdca1624a33 | 125 | #endif |
nikapov | 0:6a97487a0ab3 | 126 | press_temp.enable(); |
nikapov | 0:6a97487a0ab3 | 127 | #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 128 | magnetometer.enable(); |
mapellil | 5:2fdca1624a33 | 129 | accelerometer.enable(); |
nikapov | 0:6a97487a0ab3 | 130 | #endif |
nikapov | 0:6a97487a0ab3 | 131 | acc_gyro.enable_x(); |
nikapov | 0:6a97487a0ab3 | 132 | acc_gyro.enable_g(); |
nikapov | 0:6a97487a0ab3 | 133 | |
nikapov | 1:0f7838527fef | 134 | printf("\033[2J\033[20A"); |
nikapov | 1:0f7838527fef | 135 | printf ("\r\n--- Starting new run ---\r\n\r\n"); |
nikapov | 0:6a97487a0ab3 | 136 | |
mapellil | 5:2fdca1624a33 | 137 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 138 | hum_temp.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 139 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
mapellil | 5:2fdca1624a33 | 140 | #endif |
nikapov | 0:6a97487a0ab3 | 141 | press_temp.read_id(&id); |
mapellil | 5:2fdca1624a33 | 142 | printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 143 | magnetometer.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 144 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 1:0f7838527fef | 145 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 146 | #else // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 147 | printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
mapellil | 5:2fdca1624a33 | 148 | accelerometer.read_id(&id); |
mapellil | 5:2fdca1624a33 | 149 | printf("LSM303AGR accelerometer = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 150 | #endif |
nikapov | 0:6a97487a0ab3 | 151 | acc_gyro.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 152 | printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
nikapov | 1:0f7838527fef | 153 | |
nikapov | 1:0f7838527fef | 154 | printf("\n\r--- Reading sensor values ---\n\r"); ; |
nikapov | 0:6a97487a0ab3 | 155 | |
nikapov | 0:6a97487a0ab3 | 156 | while(1) { |
nikapov | 0:6a97487a0ab3 | 157 | printf("\r\n"); |
nikapov | 0:6a97487a0ab3 | 158 | |
mapellil | 5:2fdca1624a33 | 159 | #ifndef TARGET_SENSOR_TILE |
nikapov | 0:6a97487a0ab3 | 160 | hum_temp.get_temperature(&value1); |
nikapov | 0:6a97487a0ab3 | 161 | hum_temp.get_humidity(&value2); |
mapellil | 4:a876fbe13688 | 162 | printf("HTS221: [temp] %7f C, [hum] %f%%\r\n", value1, value2); |
mapellil | 5:2fdca1624a33 | 163 | #endif |
mapellil | 4:a876fbe13688 | 164 | value1=value2=0; |
nikapov | 0:6a97487a0ab3 | 165 | press_temp.get_temperature(&value1); |
nikapov | 0:6a97487a0ab3 | 166 | press_temp.get_pressure(&value2); |
mapellil | 4:a876fbe13688 | 167 | printf("LPS22HB: [temp] %7f C, [press] %f mbar\r\n", value1, value2); |
nikapov | 0:6a97487a0ab3 | 168 | |
nikapov | 0:6a97487a0ab3 | 169 | printf("---\r\n"); |
nikapov | 0:6a97487a0ab3 | 170 | |
nikapov | 0:6a97487a0ab3 | 171 | magnetometer.get_m_axes(axes); |
nikapov | 0:6a97487a0ab3 | 172 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 1:0f7838527fef | 173 | printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 174 | #else // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 175 | printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
mapellil | 5:2fdca1624a33 | 176 | accelerometer.get_x_axes(axes); |
mapellil | 5:2fdca1624a33 | 177 | printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 178 | #endif |
nikapov | 0:6a97487a0ab3 | 179 | |
nikapov | 0:6a97487a0ab3 | 180 | acc_gyro.get_x_axes(axes); |
nikapov | 1:0f7838527fef | 181 | printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 182 | |
nikapov | 0:6a97487a0ab3 | 183 | acc_gyro.get_g_axes(axes); |
nikapov | 1:0f7838527fef | 184 | printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 185 | |
mapellil | 5:2fdca1624a33 | 186 | #if defined (TARGET_SENSOR_TILE) || defined (TARGET_DISCO_L475VG_IOT01A) |
nikapov | 1:0f7838527fef | 187 | printf("\033[7A"); |
mapellil | 5:2fdca1624a33 | 188 | #else |
mapellil | 5:2fdca1624a33 | 189 | printf("\033[8A"); |
mapellil | 5:2fdca1624a33 | 190 | #endif |
nikapov | 1:0f7838527fef | 191 | wait(1); |
nikapov | 0:6a97487a0ab3 | 192 | } |
nikapov | 0:6a97487a0ab3 | 193 | } |