a sensor hub for monitoring home environment
Dependencies: HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X picojson
Fork of HelloWorld_ST_Sensors by
main.cpp@2:4d4fa82dbc59, 2017-09-27 (annotated)
- Committer:
- mapellil
- Date:
- Wed Sep 27 16:51:31 2017 +0200
- Revision:
- 2:4d4fa82dbc59
- Parent:
- 1:0f7838527fef
- Child:
- 3:82e8968382d6
Fixed NonCopyable
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" |
nikapov | 0:6a97487a0ab3 | 50 | #endif |
nikapov | 0:6a97487a0ab3 | 51 | |
nikapov | 0:6a97487a0ab3 | 52 | |
nikapov | 0:6a97487a0ab3 | 53 | /* Retrieve the composing elements of the expansion board */ |
nikapov | 0:6a97487a0ab3 | 54 | |
nikapov | 0:6a97487a0ab3 | 55 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 0:6a97487a0ab3 | 56 | static DevI2C devI2c = DevI2C(PB_11,PB_10); |
nikapov | 0:6a97487a0ab3 | 57 | #else // X-Nucleo-IKS01A2 |
mapellil | 2:4d4fa82dbc59 | 58 | static DevI2C devI2c(D14,D15); |
nikapov | 0:6a97487a0ab3 | 59 | #endif |
nikapov | 0:6a97487a0ab3 | 60 | |
mapellil | 2:4d4fa82dbc59 | 61 | //static HTS221Sensor hum_temp = HTS221Sensor(&devI2c); |
mapellil | 2:4d4fa82dbc59 | 62 | static HTS221Sensor hum_temp(&devI2c); |
mapellil | 2:4d4fa82dbc59 | 63 | static LPS22HBSensor press_temp(&devI2c); |
nikapov | 0:6a97487a0ab3 | 64 | |
nikapov | 0:6a97487a0ab3 | 65 | #ifdef TARGET_DISCO_L475VG_IOT01A |
mapellil | 2:4d4fa82dbc59 | 66 | static LSM6DSLSensor acc_gyro(&devI2c,PD_11,NC,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW); // low address |
mapellil | 2:4d4fa82dbc59 | 67 | static LIS3MDL magnetometer(&devI2c); |
nikapov | 0:6a97487a0ab3 | 68 | #else // X-NUCLEO_IKS01A2 |
mapellil | 2:4d4fa82dbc59 | 69 | static LSM6DSLSensor acc_gyro(&devI2c,D4,D5); // high address (default) |
mapellil | 2:4d4fa82dbc59 | 70 | static LSM303AGRMagSensor magnetometer(&devI2c); |
nikapov | 0:6a97487a0ab3 | 71 | #endif |
nikapov | 0:6a97487a0ab3 | 72 | |
nikapov | 0:6a97487a0ab3 | 73 | /* Helper function for printing floats & doubles */ |
nikapov | 0:6a97487a0ab3 | 74 | static char *print_double(char* str, double v, int decimalDigits=2) |
nikapov | 0:6a97487a0ab3 | 75 | { |
nikapov | 0:6a97487a0ab3 | 76 | int i = 1; |
nikapov | 0:6a97487a0ab3 | 77 | int intPart, fractPart; |
nikapov | 0:6a97487a0ab3 | 78 | int len; |
nikapov | 0:6a97487a0ab3 | 79 | char *ptr; |
nikapov | 0:6a97487a0ab3 | 80 | |
nikapov | 0:6a97487a0ab3 | 81 | /* prepare decimal digits multiplicator */ |
nikapov | 0:6a97487a0ab3 | 82 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
nikapov | 0:6a97487a0ab3 | 83 | |
nikapov | 0:6a97487a0ab3 | 84 | /* calculate integer & fractinal parts */ |
nikapov | 0:6a97487a0ab3 | 85 | intPart = (int)v; |
nikapov | 0:6a97487a0ab3 | 86 | fractPart = (int)((v-(double)(int)v)*i); |
nikapov | 0:6a97487a0ab3 | 87 | |
nikapov | 0:6a97487a0ab3 | 88 | /* fill in integer part */ |
nikapov | 0:6a97487a0ab3 | 89 | sprintf(str, "%i.", intPart); |
nikapov | 0:6a97487a0ab3 | 90 | |
nikapov | 0:6a97487a0ab3 | 91 | /* prepare fill in of fractional part */ |
nikapov | 0:6a97487a0ab3 | 92 | len = strlen(str); |
nikapov | 0:6a97487a0ab3 | 93 | ptr = &str[len]; |
nikapov | 0:6a97487a0ab3 | 94 | |
nikapov | 0:6a97487a0ab3 | 95 | /* fill in leading fractional zeros */ |
nikapov | 0:6a97487a0ab3 | 96 | for (i/=10;i>1; i/=10, ptr++) { |
nikapov | 0:6a97487a0ab3 | 97 | if (fractPart >= i) { |
nikapov | 0:6a97487a0ab3 | 98 | break; |
nikapov | 0:6a97487a0ab3 | 99 | } |
nikapov | 0:6a97487a0ab3 | 100 | *ptr = '0'; |
nikapov | 0:6a97487a0ab3 | 101 | } |
nikapov | 0:6a97487a0ab3 | 102 | |
nikapov | 0:6a97487a0ab3 | 103 | /* fill in (rest of) fractional part */ |
nikapov | 0:6a97487a0ab3 | 104 | sprintf(ptr, "%i", fractPart); |
nikapov | 0:6a97487a0ab3 | 105 | |
nikapov | 0:6a97487a0ab3 | 106 | return str; |
nikapov | 0:6a97487a0ab3 | 107 | } |
nikapov | 0:6a97487a0ab3 | 108 | |
nikapov | 0:6a97487a0ab3 | 109 | /* Simple main function */ |
nikapov | 0:6a97487a0ab3 | 110 | int main() { |
nikapov | 0:6a97487a0ab3 | 111 | uint8_t id; |
nikapov | 0:6a97487a0ab3 | 112 | float value1, value2; |
nikapov | 0:6a97487a0ab3 | 113 | char buffer1[32], buffer2[32]; |
nikapov | 0:6a97487a0ab3 | 114 | int32_t axes[3]; |
nikapov | 0:6a97487a0ab3 | 115 | |
nikapov | 0:6a97487a0ab3 | 116 | /* Init all sensors with default params */ |
nikapov | 0:6a97487a0ab3 | 117 | hum_temp.init(NULL); |
nikapov | 0:6a97487a0ab3 | 118 | press_temp.init(NULL); |
nikapov | 0:6a97487a0ab3 | 119 | magnetometer.init(NULL); |
nikapov | 0:6a97487a0ab3 | 120 | acc_gyro.init(NULL); |
nikapov | 0:6a97487a0ab3 | 121 | |
nikapov | 0:6a97487a0ab3 | 122 | /* Enable all sensors */ |
nikapov | 0:6a97487a0ab3 | 123 | hum_temp.enable(); |
nikapov | 0:6a97487a0ab3 | 124 | press_temp.enable(); |
nikapov | 0:6a97487a0ab3 | 125 | #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 126 | magnetometer.enable(); |
nikapov | 0:6a97487a0ab3 | 127 | #endif |
nikapov | 0:6a97487a0ab3 | 128 | acc_gyro.enable_x(); |
nikapov | 0:6a97487a0ab3 | 129 | acc_gyro.enable_g(); |
nikapov | 0:6a97487a0ab3 | 130 | |
nikapov | 1:0f7838527fef | 131 | printf("\033[2J\033[20A"); |
nikapov | 1:0f7838527fef | 132 | printf ("\r\n--- Starting new run ---\r\n\r\n"); |
nikapov | 0:6a97487a0ab3 | 133 | |
nikapov | 0:6a97487a0ab3 | 134 | hum_temp.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 135 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 136 | press_temp.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 137 | printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 138 | magnetometer.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 139 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 1:0f7838527fef | 140 | printf("LIS3MDL magnetometer = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 141 | #else // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 142 | printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
nikapov | 0:6a97487a0ab3 | 143 | #endif |
nikapov | 0:6a97487a0ab3 | 144 | acc_gyro.read_id(&id); |
nikapov | 0:6a97487a0ab3 | 145 | printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
nikapov | 1:0f7838527fef | 146 | |
nikapov | 1:0f7838527fef | 147 | printf("\n\r--- Reading sensor values ---\n\r"); ; |
nikapov | 0:6a97487a0ab3 | 148 | |
nikapov | 0:6a97487a0ab3 | 149 | while(1) { |
nikapov | 0:6a97487a0ab3 | 150 | printf("\r\n"); |
nikapov | 0:6a97487a0ab3 | 151 | |
nikapov | 0:6a97487a0ab3 | 152 | hum_temp.get_temperature(&value1); |
nikapov | 0:6a97487a0ab3 | 153 | hum_temp.get_humidity(&value2); |
nikapov | 0:6a97487a0ab3 | 154 | printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
nikapov | 0:6a97487a0ab3 | 155 | |
nikapov | 0:6a97487a0ab3 | 156 | press_temp.get_temperature(&value1); |
nikapov | 0:6a97487a0ab3 | 157 | press_temp.get_pressure(&value2); |
nikapov | 0:6a97487a0ab3 | 158 | printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); |
nikapov | 0:6a97487a0ab3 | 159 | |
nikapov | 0:6a97487a0ab3 | 160 | printf("---\r\n"); |
nikapov | 0:6a97487a0ab3 | 161 | |
nikapov | 0:6a97487a0ab3 | 162 | magnetometer.get_m_axes(axes); |
nikapov | 0:6a97487a0ab3 | 163 | #ifdef TARGET_DISCO_L475VG_IOT01A |
nikapov | 1:0f7838527fef | 164 | printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 165 | #else // X-NUCLEO-IKS01A2 |
nikapov | 0:6a97487a0ab3 | 166 | printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 167 | #endif |
nikapov | 0:6a97487a0ab3 | 168 | |
nikapov | 0:6a97487a0ab3 | 169 | acc_gyro.get_x_axes(axes); |
nikapov | 1:0f7838527fef | 170 | printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 171 | |
nikapov | 0:6a97487a0ab3 | 172 | acc_gyro.get_g_axes(axes); |
nikapov | 1:0f7838527fef | 173 | printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
nikapov | 0:6a97487a0ab3 | 174 | |
nikapov | 1:0f7838527fef | 175 | printf("\033[7A"); |
nikapov | 1:0f7838527fef | 176 | wait(1); |
nikapov | 0:6a97487a0ab3 | 177 | } |
nikapov | 0:6a97487a0ab3 | 178 | } |