a sensor hub for monitoring home environment
Dependencies: HTS221 LIS3MDL LPS22HB LSM303AGR LSM6DSL VL53L0X picojson
Fork of HelloWorld_ST_Sensors by
main.cpp
- Committer:
- mapellil
- Date:
- 2017-09-27
- Revision:
- 2:4d4fa82dbc59
- Parent:
- 1:0f7838527fef
- Child:
- 3:82e8968382d6
File content as of revision 2:4d4fa82dbc59:
/** ****************************************************************************** * @file main.cpp * @author CLab * @version V1.0.0 * @date 5-September-2017 * @brief Simple Example application for using X_NUCLEO_IKS01A2 * MEMS Inertial & Environmental Sensor Nucleo expansion and * B-L475E-IOT01A2 boards. ****************************************************************************** * @attention * * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes */ #include "mbed.h" #include "HTS221Sensor.h" #include "LPS22HBSensor.h" #include "LSM6DSLSensor.h" #ifdef TARGET_DISCO_L475VG_IOT01A #include "lis3mdl_class.h" #else // X-Nucleo-IKS01A2 #include "LSM303AGRMagSensor.h" #endif /* Retrieve the composing elements of the expansion board */ #ifdef TARGET_DISCO_L475VG_IOT01A static DevI2C devI2c = DevI2C(PB_11,PB_10); #else // X-Nucleo-IKS01A2 static DevI2C devI2c(D14,D15); #endif //static HTS221Sensor hum_temp = HTS221Sensor(&devI2c); static HTS221Sensor hum_temp(&devI2c); static LPS22HBSensor press_temp(&devI2c); #ifdef TARGET_DISCO_L475VG_IOT01A static LSM6DSLSensor acc_gyro(&devI2c,PD_11,NC,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW); // low address static LIS3MDL magnetometer(&devI2c); #else // X-NUCLEO_IKS01A2 static LSM6DSLSensor acc_gyro(&devI2c,D4,D5); // high address (default) static LSM303AGRMagSensor magnetometer(&devI2c); #endif /* Helper function for printing floats & doubles */ static char *print_double(char* str, double v, int decimalDigits=2) { int i = 1; int intPart, fractPart; int len; char *ptr; /* prepare decimal digits multiplicator */ for (;decimalDigits!=0; i*=10, decimalDigits--); /* calculate integer & fractinal parts */ intPart = (int)v; fractPart = (int)((v-(double)(int)v)*i); /* fill in integer part */ sprintf(str, "%i.", intPart); /* prepare fill in of fractional part */ len = strlen(str); ptr = &str[len]; /* fill in leading fractional zeros */ for (i/=10;i>1; i/=10, ptr++) { if (fractPart >= i) { break; } *ptr = '0'; } /* fill in (rest of) fractional part */ sprintf(ptr, "%i", fractPart); return str; } /* Simple main function */ int main() { uint8_t id; float value1, value2; char buffer1[32], buffer2[32]; int32_t axes[3]; /* Init all sensors with default params */ hum_temp.init(NULL); press_temp.init(NULL); magnetometer.init(NULL); acc_gyro.init(NULL); /* Enable all sensors */ hum_temp.enable(); press_temp.enable(); #ifndef TARGET_DISCO_L475VG_IOT01A // X-NUCLEO-IKS01A2 magnetometer.enable(); #endif acc_gyro.enable_x(); acc_gyro.enable_g(); printf("\033[2J\033[20A"); printf ("\r\n--- Starting new run ---\r\n\r\n"); hum_temp.read_id(&id); printf("HTS221 humidity & temperature = 0x%X\r\n", id); press_temp.read_id(&id); printf("LPS22HB pressure & temperature = 0x%X\r\n", id); magnetometer.read_id(&id); #ifdef TARGET_DISCO_L475VG_IOT01A printf("LIS3MDL magnetometer = 0x%X\r\n", id); #else // X-NUCLEO-IKS01A2 printf("LSM303AGR magnetometer = 0x%X\r\n", id); #endif acc_gyro.read_id(&id); printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); printf("\n\r--- Reading sensor values ---\n\r"); ; while(1) { printf("\r\n"); hum_temp.get_temperature(&value1); hum_temp.get_humidity(&value2); printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2)); press_temp.get_temperature(&value1); press_temp.get_pressure(&value2); printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, 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 printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); #endif acc_gyro.get_x_axes(axes); printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); acc_gyro.get_g_axes(axes); printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); printf("\033[7A"); wait(1); } }