con timer

Dependencies:   X_NUCLEO_IKS01A2 mbed

Fork of HelloWorld_IKS01A2 by duckietownhsunina

Committer:
cparata
Date:
Fri Aug 19 12:13:37 2016 +0000
Revision:
2:f23b144da50a
Parent:
1:4c13def99152
Child:
8:8f495e604424
Add interfaces for all components

Who changed what in which revision?

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