Referred coursework template 2019

Dependencies:   X_NUCLEO_IKS01A3 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    main.cpp
00004  * @author  SRA
00005  * @version V1.0.0
00006  * @date    5-March-2019
00007  * @brief   Simple Example application for using the X_NUCLEO_IKS01A3 
00008  *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037 */ 
00038 
00039 /* Includes */
00040 #include "mbed.h"
00041 #include "XNucleoIKS01A3.h"
00042 #include "USBMouse.h"
00043 
00044 /* Instantiate the expansion board */
00045 static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
00046 
00047 /* Retrieve the composing elements of the expansion board */
00048 static LIS2MDLSensor *magnetometer = mems_expansion_board->magnetometer;
00049 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00050 static LPS22HHSensor *press_temp = mems_expansion_board->pt_sensor;
00051 static LSM6DSOSensor *acc_gyro = mems_expansion_board->acc_gyro;
00052 static LIS2DW12Sensor *accelerometer = mems_expansion_board->accelerometer;
00053 static STTS751Sensor *temp = mems_expansion_board->t_sensor;
00054 
00055 /* Helper function for printing floats & doubles */
00056 static char *print_double(char* str, double v, int decimalDigits=2)
00057 {
00058   int i = 1;
00059   int intPart, fractPart;
00060   int len;
00061   char *ptr;
00062 
00063   /* prepare decimal digits multiplicator */
00064   for (;decimalDigits!=0; i*=10, decimalDigits--);
00065 
00066   /* calculate integer & fractinal parts */
00067   intPart = (int)v;
00068   fractPart = (int)((v-(double)(int)v)*i);
00069 
00070   /* fill in integer part */
00071   sprintf(str, "%i.", intPart);
00072 
00073   /* prepare fill in of fractional part */
00074   len = strlen(str);
00075   ptr = &str[len];
00076 
00077   /* fill in leading fractional zeros */
00078   for (i/=10;i>1; i/=10, ptr++) {
00079     if (fractPart >= i) {
00080       break;
00081     }
00082     *ptr = '0';
00083   }
00084 
00085   /* fill in (rest of) fractional part */
00086   sprintf(ptr, "%i", fractPart);
00087 
00088   return str;
00089 }
00090 
00091 USBMouse mouse;
00092 
00093 /* Simple main function */
00094 int main() {
00095   uint8_t id;
00096   float value1, value2;
00097   char buffer1[32], buffer2[32];
00098   int32_t axes[3];
00099   
00100     int16_t x = 0;
00101     int16_t y = 0;
00102     int32_t radius = 10;
00103     int32_t angle = 0;
00104   
00105   /* Enable all sensors */
00106   hum_temp->enable();
00107   press_temp->enable();
00108   temp->enable();
00109   magnetometer->enable();
00110   accelerometer->enable_x();
00111   acc_gyro->enable_x();
00112   acc_gyro->enable_g();
00113   
00114   printf("\r\n--- Starting new run ---\r\n");
00115 
00116   hum_temp->read_id(&id);
00117   printf("HTS221  humidity & temperature    = 0x%X\r\n", id);
00118   press_temp->read_id(&id);
00119   printf("LPS22HH  pressure & temperature   = 0x%X\r\n", id);
00120   temp->read_id(&id);
00121   printf("STTS751 temperature               = 0x%X\r\n", id);
00122   magnetometer->read_id(&id);
00123   printf("LIS2MDL magnetometer              = 0x%X\r\n", id);
00124   accelerometer->read_id(&id);
00125   printf("LIS2DW12 accelerometer            = 0x%X\r\n", id);
00126   acc_gyro->read_id(&id);
00127   printf("LSM6DSO accelerometer & gyroscope = 0x%X\r\n", id);
00128  
00129   while(1) {
00130     printf("\r\n");
00131 
00132     hum_temp->get_temperature(&value1);
00133     hum_temp->get_humidity(&value2);
00134     printf("HTS221: [temp] %7s C,   [hum] %s%%\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00135     
00136     press_temp->get_temperature(&value1);
00137     press_temp->get_pressure(&value2);
00138     printf("LPS22HH: [temp] %7s C, [press] %s mbar\r\n", print_double(buffer1, value1), print_double(buffer2, value2));
00139 
00140     temp->get_temperature(&value1);
00141     printf("STTS751: [temp] %7s C\r\n", print_double(buffer1, value1));
00142 
00143     printf("---\r\n");
00144 
00145     magnetometer->get_m_axes(axes);
00146     printf("LIS2MDL [mag/mgauss]:  %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00147     
00148     accelerometer->get_x_axes(axes);
00149     printf("LIS2DW12 [acc/mg]:  %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00150 
00151     acc_gyro->get_x_axes(axes);
00152     printf("LSM6DSO [acc/mg]:      %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00153 
00154     acc_gyro->get_g_axes(axes);
00155     printf("LSM6DSO [gyro/mdps]:   %6d, %6d, %6d\r\n", axes[0], axes[1], axes[2]);
00156 
00157     x = cos((double)angle*3.14/180.0)*radius;
00158     y = sin((double)angle*3.14/180.0)*radius;
00159 
00160     //will move mouse x, y away from its previous position on the screen
00161     mouse.move(x, y);
00162     angle += 3;
00163     wait(0.001);
00164   }
00165 }