Simple "hello world" style program for X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensor Nucleo Expansion Board

Dependencies:   X_NUCLEO_IKS01A1 mbed

Dependents:   ese_project_copy ese_project_share

X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensor Nucleo Expansion Board Firmware Package

Introduction

This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Nucleo Expansion Board.

Example Application

First of all, the example application outputs information retrieved from the Expansion Board over UART. Launch a terminal application (e.g.: PuTTY on Windows, Minicom on Linux) and set the UART port to 9600 bps, 8 bit, No Parity, 1 stop bit.

This is just a simple "hello world" style program for the X-NUCLEO-IKS01A1 MEMS Inertial & Environmental Sensor Nucleo Expansion Board.

Committer:
Wolfgang Betz
Date:
Mon Aug 31 12:07:22 2015 +0200
Revision:
3:a2d2342526db
Parent:
0:c71c9af137dd
Child:
4:b1526d074d83
Rename 'pressure_sensor' in 'pt_sensor'

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 0:c71c9af137dd 1 /**
Wolfgang Betz 0:c71c9af137dd 2 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 3 * @file main.cpp
Wolfgang Betz 0:c71c9af137dd 4 * @author AST / EST
Wolfgang Betz 0:c71c9af137dd 5 * @version V0.0.1
Wolfgang Betz 0:c71c9af137dd 6 * @date 14-August-2015
Wolfgang Betz 0:c71c9af137dd 7 * @brief Simple Example application for using the X_NUCLEO_IKS01A1
Wolfgang Betz 0:c71c9af137dd 8 * MEMS Inertial & Environmental Sensor Nucleo expansion board.
Wolfgang Betz 0:c71c9af137dd 9 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 10 * @attention
Wolfgang Betz 0:c71c9af137dd 11 *
Wolfgang Betz 0:c71c9af137dd 12 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Wolfgang Betz 0:c71c9af137dd 13 *
Wolfgang Betz 0:c71c9af137dd 14 * Redistribution and use in source and binary forms, with or without modification,
Wolfgang Betz 0:c71c9af137dd 15 * are permitted provided that the following conditions are met:
Wolfgang Betz 0:c71c9af137dd 16 * 1. Redistributions of source code must retain the above copyright notice,
Wolfgang Betz 0:c71c9af137dd 17 * this list of conditions and the following disclaimer.
Wolfgang Betz 0:c71c9af137dd 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
Wolfgang Betz 0:c71c9af137dd 19 * this list of conditions and the following disclaimer in the documentation
Wolfgang Betz 0:c71c9af137dd 20 * and/or other materials provided with the distribution.
Wolfgang Betz 0:c71c9af137dd 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Wolfgang Betz 0:c71c9af137dd 22 * may be used to endorse or promote products derived from this software
Wolfgang Betz 0:c71c9af137dd 23 * without specific prior written permission.
Wolfgang Betz 0:c71c9af137dd 24 *
Wolfgang Betz 0:c71c9af137dd 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Wolfgang Betz 0:c71c9af137dd 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Wolfgang Betz 0:c71c9af137dd 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Wolfgang Betz 0:c71c9af137dd 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Wolfgang Betz 0:c71c9af137dd 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Wolfgang Betz 0:c71c9af137dd 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Wolfgang Betz 0:c71c9af137dd 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Wolfgang Betz 0:c71c9af137dd 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Wolfgang Betz 0:c71c9af137dd 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Wolfgang Betz 0:c71c9af137dd 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wolfgang Betz 0:c71c9af137dd 35 *
Wolfgang Betz 0:c71c9af137dd 36 ******************************************************************************
Wolfgang Betz 0:c71c9af137dd 37 */
Wolfgang Betz 0:c71c9af137dd 38
Wolfgang Betz 0:c71c9af137dd 39 /* Includes */
Wolfgang Betz 0:c71c9af137dd 40 #include "mbed.h"
Wolfgang Betz 0:c71c9af137dd 41 #include "x_nucleo_iks01a1.h"
Wolfgang Betz 0:c71c9af137dd 42
Wolfgang Betz 0:c71c9af137dd 43 /* Instantiate the expansion board */
Wolfgang Betz 0:c71c9af137dd 44 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance();
Wolfgang Betz 0:c71c9af137dd 45
Wolfgang Betz 0:c71c9af137dd 46 /* Retrieve the composing elements of the expansion board */
Wolfgang Betz 0:c71c9af137dd 47 static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
Wolfgang Betz 0:c71c9af137dd 48 static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
Wolfgang Betz 0:c71c9af137dd 49 static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
Wolfgang Betz 0:c71c9af137dd 50 static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 51 static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 52 static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
Wolfgang Betz 3:a2d2342526db 53 static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;
Wolfgang Betz 0:c71c9af137dd 54
Wolfgang Betz 0:c71c9af137dd 55 /* Helper function for printing floats & doubles */
Wolfgang Betz 0:c71c9af137dd 56 static char *printDouble(char* str, double v, int decimalDigits=2)
Wolfgang Betz 0:c71c9af137dd 57 {
Wolfgang Betz 0:c71c9af137dd 58 int i = 1;
Wolfgang Betz 0:c71c9af137dd 59 int intPart, fractPart;
Wolfgang Betz 0:c71c9af137dd 60 int len;
Wolfgang Betz 0:c71c9af137dd 61 char *ptr;
Wolfgang Betz 0:c71c9af137dd 62
Wolfgang Betz 0:c71c9af137dd 63 /* prepare decimal digits multiplicator */
Wolfgang Betz 0:c71c9af137dd 64 for (;decimalDigits!=0; i*=10, decimalDigits--);
Wolfgang Betz 0:c71c9af137dd 65
Wolfgang Betz 0:c71c9af137dd 66 /* calculate integer & fractinal parts */
Wolfgang Betz 0:c71c9af137dd 67 intPart = (int)v;
Wolfgang Betz 0:c71c9af137dd 68 fractPart = (int)((v-(double)(int)v)*i);
Wolfgang Betz 0:c71c9af137dd 69
Wolfgang Betz 0:c71c9af137dd 70 /* fill in integer part */
Wolfgang Betz 0:c71c9af137dd 71 sprintf(str, "%i.", intPart);
Wolfgang Betz 0:c71c9af137dd 72
Wolfgang Betz 0:c71c9af137dd 73 /* prepare fill in of fractional part */
Wolfgang Betz 0:c71c9af137dd 74 len = strlen(str);
Wolfgang Betz 0:c71c9af137dd 75 ptr = &str[len];
Wolfgang Betz 0:c71c9af137dd 76
Wolfgang Betz 0:c71c9af137dd 77 /* fill in leading fractional zeros */
Wolfgang Betz 0:c71c9af137dd 78 for (i/=10;i>1; i/=10, ptr++) {
Wolfgang Betz 0:c71c9af137dd 79 if(fractPart >= i) break;
Wolfgang Betz 0:c71c9af137dd 80 *ptr = '0';
Wolfgang Betz 0:c71c9af137dd 81 }
Wolfgang Betz 0:c71c9af137dd 82
Wolfgang Betz 0:c71c9af137dd 83 /* fill in (rest of) fractional part */
Wolfgang Betz 0:c71c9af137dd 84 sprintf(ptr, "%i", fractPart);
Wolfgang Betz 0:c71c9af137dd 85
Wolfgang Betz 0:c71c9af137dd 86 return str;
Wolfgang Betz 0:c71c9af137dd 87 }
Wolfgang Betz 0:c71c9af137dd 88
Wolfgang Betz 0:c71c9af137dd 89
Wolfgang Betz 0:c71c9af137dd 90 /* Simple main function */
Wolfgang Betz 0:c71c9af137dd 91 int main() {
Wolfgang Betz 0:c71c9af137dd 92 uint8_t id;
Wolfgang Betz 0:c71c9af137dd 93 float value1, value2;
Wolfgang Betz 0:c71c9af137dd 94 char buffer1[32], buffer2[32];
Wolfgang Betz 0:c71c9af137dd 95 int32_t axes[3];
Wolfgang Betz 0:c71c9af137dd 96
Wolfgang Betz 0:c71c9af137dd 97 printf("\r\n--- Starting new run ---\r\n");
Wolfgang Betz 0:c71c9af137dd 98
Wolfgang Betz 0:c71c9af137dd 99 humidity_sensor->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 100 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 101 pressure_sensor->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 102 printf("LPS25H pressure & temperature = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 103 magnetometer->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 104 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 105 gyroscope->ReadID(&id);
Wolfgang Betz 0:c71c9af137dd 106 printf("LSM6DS0 accelerometer & gyroscope = 0x%X\r\n", id);
Wolfgang Betz 0:c71c9af137dd 107
Wolfgang Betz 0:c71c9af137dd 108 wait(3);
Wolfgang Betz 0:c71c9af137dd 109
Wolfgang Betz 0:c71c9af137dd 110 while(1) {
Wolfgang Betz 0:c71c9af137dd 111 printf("\r\n");
Wolfgang Betz 0:c71c9af137dd 112
Wolfgang Betz 0:c71c9af137dd 113 temp_sensor1->GetTemperature(&value1);
Wolfgang Betz 0:c71c9af137dd 114 humidity_sensor->GetHumidity(&value2);
Wolfgang Betz 0:c71c9af137dd 115 printf("HTS221: [temp] %7s°C, [hum] %s%%\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 116
Wolfgang Betz 0:c71c9af137dd 117 temp_sensor2->GetFahrenheit(&value1);
Wolfgang Betz 0:c71c9af137dd 118 pressure_sensor->GetPressure(&value2);
Wolfgang Betz 0:c71c9af137dd 119 printf("LPS25H: [temp] %7s°F, [press] %smbar\r\n", printDouble(buffer1, value1), printDouble(buffer2, value2));
Wolfgang Betz 0:c71c9af137dd 120
Wolfgang Betz 0:c71c9af137dd 121 printf("---\r\n");
Wolfgang Betz 0:c71c9af137dd 122
Wolfgang Betz 0:c71c9af137dd 123 magnetometer->Get_M_Axes(axes);
Wolfgang Betz 0:c71c9af137dd 124 printf("LIS3MDL [mag/mgauss]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 125
Wolfgang Betz 0:c71c9af137dd 126 accelerometer->Get_X_Axes(axes);
Wolfgang Betz 0:c71c9af137dd 127 printf("LSM6DS0 [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 128
Wolfgang Betz 0:c71c9af137dd 129 gyroscope->Get_G_Axes(axes);
Wolfgang Betz 0:c71c9af137dd 130 printf("LSM6DS0 [gyro/mdps]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]);
Wolfgang Betz 0:c71c9af137dd 131
Wolfgang Betz 0:c71c9af137dd 132 wait(1.5);
Wolfgang Betz 0:c71c9af137dd 133 }
Wolfgang Betz 0:c71c9af137dd 134 }