tiz

Dependencies:   TextLCD X_NUCLEO_IKS01A1 func mbed-src mbed

Fork of mas by esproj

Committer:
herenvarno
Date:
Mon Dec 07 21:06:25 2015 +0000
Revision:
0:99767aa66bce
Child:
1:1df80fe13928
init

Who changed what in which revision?

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