![](/media/cache/group/MAKE-NTU-Banner.png.50x50_q85.png)
"Sensors Reader" Sample Application for X-NUCLEO-IKS01A1 Expansion Board
Dependencies: X_NUCLEO_IKS01A2 mbed
Fork of Sensors_Reader by
main.cpp@68:61c4480769a1, 2017-02-25 (annotated)
- Committer:
- mbedAustin
- Date:
- Sat Feb 25 03:01:12 2017 +0000
- Revision:
- 68:61c4480769a1
- Parent:
- 62:67e2353604be
Updated sensor library from IKS0A1 to IKS0A2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 68:61c4480769a1 | 1 | /** |
mbedAustin | 68:61c4480769a1 | 2 | ****************************************************************************** |
mbedAustin | 68:61c4480769a1 | 3 | * @file main.cpp |
mbedAustin | 68:61c4480769a1 | 4 | * @author CLab |
mbedAustin | 68:61c4480769a1 | 5 | * @version V1.0.0 |
mbedAustin | 68:61c4480769a1 | 6 | * @date 2-December-2016 |
mbedAustin | 68:61c4480769a1 | 7 | * @brief Simple Example application for using the X_NUCLEO_IKS01A1 |
mbedAustin | 68:61c4480769a1 | 8 | * MEMS Inertial & Environmental Sensor Nucleo expansion board. |
mbedAustin | 68:61c4480769a1 | 9 | ****************************************************************************** |
mbedAustin | 68:61c4480769a1 | 10 | * @attention |
mbedAustin | 68:61c4480769a1 | 11 | * |
mbedAustin | 68:61c4480769a1 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
mbedAustin | 68:61c4480769a1 | 13 | * |
mbedAustin | 68:61c4480769a1 | 14 | * Redistribution and use in source and binary forms, with or without modification, |
mbedAustin | 68:61c4480769a1 | 15 | * are permitted provided that the following conditions are met: |
mbedAustin | 68:61c4480769a1 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
mbedAustin | 68:61c4480769a1 | 17 | * this list of conditions and the following disclaimer. |
mbedAustin | 68:61c4480769a1 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbedAustin | 68:61c4480769a1 | 19 | * this list of conditions and the following disclaimer in the documentation |
mbedAustin | 68:61c4480769a1 | 20 | * and/or other materials provided with the distribution. |
mbedAustin | 68:61c4480769a1 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbedAustin | 68:61c4480769a1 | 22 | * may be used to endorse or promote products derived from this software |
mbedAustin | 68:61c4480769a1 | 23 | * without specific prior written permission. |
mbedAustin | 68:61c4480769a1 | 24 | * |
mbedAustin | 68:61c4480769a1 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbedAustin | 68:61c4480769a1 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbedAustin | 68:61c4480769a1 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbedAustin | 68:61c4480769a1 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbedAustin | 68:61c4480769a1 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbedAustin | 68:61c4480769a1 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbedAustin | 68:61c4480769a1 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbedAustin | 68:61c4480769a1 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbedAustin | 68:61c4480769a1 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbedAustin | 68:61c4480769a1 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbedAustin | 68:61c4480769a1 | 35 | * |
mbedAustin | 68:61c4480769a1 | 36 | ****************************************************************************** |
mbedAustin | 68:61c4480769a1 | 37 | */ |
mbedAustin | 68:61c4480769a1 | 38 | |
mbedAustin | 68:61c4480769a1 | 39 | /* Includes */ |
mbedAustin | 68:61c4480769a1 | 40 | #include "mbed.h" |
mbedAustin | 68:61c4480769a1 | 41 | #include "x_nucleo_iks01a2.h" |
mbedAustin | 68:61c4480769a1 | 42 | |
mbedAustin | 68:61c4480769a1 | 43 | /* Instantiate the expansion board */ |
mbedAustin | 68:61c4480769a1 | 44 | static X_NUCLEO_IKS01A2 *mems_expansion_board = X_NUCLEO_IKS01A2::Instance(D14, D15, D4, D5); |
mbedAustin | 68:61c4480769a1 | 45 | |
mbedAustin | 68:61c4480769a1 | 46 | /* Retrieve the composing elements of the expansion board */ |
mbedAustin | 68:61c4480769a1 | 47 | static LSM303AGR_MAG_Sensor *magnetometer = mems_expansion_board->magnetometer; |
mbedAustin | 68:61c4480769a1 | 48 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
mbedAustin | 68:61c4480769a1 | 49 | static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor; |
mbedAustin | 68:61c4480769a1 | 50 | static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
mbedAustin | 68:61c4480769a1 | 51 | static LSM303AGR_ACC_Sensor *accelerometer = mems_expansion_board->accelerometer; |
mbedAustin | 68:61c4480769a1 | 52 | |
mbedAustin | 68:61c4480769a1 | 53 | /* Helper function for printing floats & doubles */ |
mbedAustin | 68:61c4480769a1 | 54 | static char *printDouble(char* str, double v, int decimalDigits=2) |
mbedAustin | 68:61c4480769a1 | 55 | { |
mbedAustin | 68:61c4480769a1 | 56 | int i = 1; |
mbedAustin | 68:61c4480769a1 | 57 | int intPart, fractPart; |
mbedAustin | 68:61c4480769a1 | 58 | int len; |
mbedAustin | 68:61c4480769a1 | 59 | char *ptr; |
mbedAustin | 68:61c4480769a1 | 60 | |
mbedAustin | 68:61c4480769a1 | 61 | /* prepare decimal digits multiplicator */ |
mbedAustin | 68:61c4480769a1 | 62 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
mbedAustin | 68:61c4480769a1 | 63 | |
mbedAustin | 68:61c4480769a1 | 64 | /* calculate integer & fractinal parts */ |
mbedAustin | 68:61c4480769a1 | 65 | intPart = (int)v; |
mbedAustin | 68:61c4480769a1 | 66 | fractPart = (int)((v-(double)(int)v)*i); |
mbedAustin | 68:61c4480769a1 | 67 | |
mbedAustin | 68:61c4480769a1 | 68 | /* fill in integer part */ |
mbedAustin | 68:61c4480769a1 | 69 | sprintf(str, "%i.", intPart); |
mbedAustin | 68:61c4480769a1 | 70 | |
mbedAustin | 68:61c4480769a1 | 71 | /* prepare fill in of fractional part */ |
mbedAustin | 68:61c4480769a1 | 72 | len = strlen(str); |
mbedAustin | 68:61c4480769a1 | 73 | ptr = &str[len]; |
mbedAustin | 68:61c4480769a1 | 74 | |
mbedAustin | 68:61c4480769a1 | 75 | /* fill in leading fractional zeros */ |
mbedAustin | 68:61c4480769a1 | 76 | for (i/=10;i>1; i/=10, ptr++) { |
mbedAustin | 68:61c4480769a1 | 77 | if(fractPart >= i) break; |
mbedAustin | 68:61c4480769a1 | 78 | *ptr = '0'; |
mbedAustin | 68:61c4480769a1 | 79 | } |
mbedAustin | 68:61c4480769a1 | 80 | |
mbedAustin | 68:61c4480769a1 | 81 | /* fill in (rest of) fractional part */ |
mbedAustin | 68:61c4480769a1 | 82 | sprintf(ptr, "%i", fractPart); |
mbedAustin | 68:61c4480769a1 | 83 | |
mbedAustin | 68:61c4480769a1 | 84 | return str; |
mbedAustin | 68:61c4480769a1 | 85 | } |
mbedAustin | 68:61c4480769a1 | 86 | |
mbedAustin | 68:61c4480769a1 | 87 | |
mbedAustin | 68:61c4480769a1 | 88 | /* Simple main function */ |
mbedAustin | 68:61c4480769a1 | 89 | int main() { |
mbedAustin | 68:61c4480769a1 | 90 | uint8_t id; |
mbedAustin | 68:61c4480769a1 | 91 | float value1, value2; |
mbedAustin | 68:61c4480769a1 | 92 | char buffer1[32], buffer2[32]; |
mbedAustin | 68:61c4480769a1 | 93 | int32_t axes[3]; |
mbedAustin | 68:61c4480769a1 | 94 | |
mbedAustin | 68:61c4480769a1 | 95 | /* Enable all sensors */ |
mbedAustin | 68:61c4480769a1 | 96 | hum_temp->Enable(); |
mbedAustin | 68:61c4480769a1 | 97 | press_temp->Enable(); |
mbedAustin | 68:61c4480769a1 | 98 | magnetometer->Enable(); |
mbedAustin | 68:61c4480769a1 | 99 | accelerometer->Enable(); |
mbedAustin | 68:61c4480769a1 | 100 | acc_gyro->Enable_X(); |
mbedAustin | 68:61c4480769a1 | 101 | acc_gyro->Enable_G(); |
mbedAustin | 68:61c4480769a1 | 102 | |
mbedAustin | 68:61c4480769a1 | 103 | printf("\r\n--- Starting new run ---\r\n"); |
mbedAustin | 68:61c4480769a1 | 104 | |
mbedAustin | 68:61c4480769a1 | 105 | hum_temp->ReadID(&id); |
mbedAustin | 68:61c4480769a1 | 106 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
mbedAustin | 68:61c4480769a1 | 107 | press_temp->ReadID(&id); |
mbedAustin | 68:61c4480769a1 | 108 | printf("LPS22HB pressure & temperature = 0x%X\r\n", id); |
mbedAustin | 68:61c4480769a1 | 109 | magnetometer->ReadID(&id); |
mbedAustin | 68:61c4480769a1 | 110 | printf("LSM303AGR magnetometer = 0x%X\r\n", id); |
mbedAustin | 68:61c4480769a1 | 111 | accelerometer->ReadID(&id); |
mbedAustin | 68:61c4480769a1 | 112 | printf("LSM303AGR accelerometer = 0x%X\r\n", id); |
mbedAustin | 68:61c4480769a1 | 113 | acc_gyro->ReadID(&id); |
mbedAustin | 68:61c4480769a1 | 114 | printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id); |
mbedAustin | 68:61c4480769a1 | 115 | |
mbedAustin | 68:61c4480769a1 | 116 | while(1) { |
mbedAustin | 68:61c4480769a1 | 117 | printf("\r\n"); |
mbedAustin | 68:61c4480769a1 | 118 | |
mbedAustin | 68:61c4480769a1 | 119 | hum_temp->GetTemperature(&value1); |
mbedAustin | 68:61c4480769a1 | 120 | hum_temp->GetHumidity(&value2); |
mbedAustin | 68:61c4480769a1 | 121 | printf("HTS221: [temp] %7s C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
mbedAustin | 68:61c4480769a1 | 122 | |
mbedAustin | 68:61c4480769a1 | 123 | press_temp->GetTemperature(&value1); |
mbedAustin | 68:61c4480769a1 | 124 | press_temp->GetPressure(&value2); |
mbedAustin | 68:61c4480769a1 | 125 | printf("LPS22HB: [temp] %7s C, [press] %s mbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2)); |
mbedAustin | 68:61c4480769a1 | 126 | |
mbedAustin | 68:61c4480769a1 | 127 | printf("---\r\n"); |
mbedAustin | 68:61c4480769a1 | 128 | |
mbedAustin | 68:61c4480769a1 | 129 | magnetometer->Get_M_Axes(axes); |
mbedAustin | 68:61c4480769a1 | 130 | printf("LSM303AGR [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
mbedAustin | 68:61c4480769a1 | 131 | |
mbedAustin | 68:61c4480769a1 | 132 | accelerometer->Get_X_Axes(axes); |
mbedAustin | 68:61c4480769a1 | 133 | printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
mbedAustin | 68:61c4480769a1 | 134 | |
mbedAustin | 68:61c4480769a1 | 135 | acc_gyro->Get_X_Axes(axes); |
mbedAustin | 68:61c4480769a1 | 136 | printf("LSM6DSL [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
mbedAustin | 68:61c4480769a1 | 137 | |
mbedAustin | 68:61c4480769a1 | 138 | acc_gyro->Get_G_Axes(axes); |
mbedAustin | 68:61c4480769a1 | 139 | printf("LSM6DSL [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
mbedAustin | 68:61c4480769a1 | 140 | |
mbedAustin | 68:61c4480769a1 | 141 | wait(1.5); |
mbedAustin | 68:61c4480769a1 | 142 | } |
mbedAustin | 68:61c4480769a1 | 143 | } |