IKS01A1 Demo

Dependencies:   X_NUCLEO_IKS01A1

main.cpp

Committer:
noutram
Date:
2019-09-11
Revision:
12:504236c45846
Parent:
11:c613d631c1e9

File content as of revision 12:504236c45846:

/**
 ******************************************************************************
 * @file    main.cpp
 * @author  AST / EST
 * @version V0.0.1
 * @date    14-August-2015
 * @brief   Simple Example application for using the X_NUCLEO_IKS01A1 
 *          MEMS Inertial & Environmental Sensor Nucleo expansion board.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of STMicroelectronics nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
*/ 

/* Includes */
#include "mbed.h"
#include "x_nucleo_iks01a1.h"
void doSample();
void samplerThread();

/* Instantiate the expansion board */
static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);

/* Retrieve the composing elements of the expansion board */
static GyroSensor *gyroscope = mems_expansion_board->GetGyroscope();
static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
static MagneticSensor *magnetometer = mems_expansion_board->magnetometer;
static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor;
static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor;
static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor;

Serial pc(USBTX, USBRX);
Thread sampler(osPriorityRealtime);
EventQueue queue;

DigitalIn sw1(USER_BUTTON);
int32_t m_axes[3];
int32_t a_axes[3];
int32_t g_axes[3];
uint8_t id;

const unsigned Fs = 100;
const double T = 1.0 / (double)Fs;
const unsigned Tms = (unsigned)(T*1000.0);
unsigned long tt = 0;
      
void samplerThread()
{
   queue.dispatch();    
}
void doSample()
{
    magnetometer->get_m_axes(m_axes);    
    accelerometer->get_x_axes(a_axes);
    gyroscope->get_g_axes(g_axes);
        
    pc.printf("%lu,",tt); tt+=Tms;
    pc.printf("%d,%d,%d,", m_axes[0], m_axes[1], m_axes[2]);
    pc.printf("%d,%d,%d,", a_axes[0], a_axes[1], a_axes[2]);
    pc.printf("%d,%d,%d\r\n", g_axes[0], g_axes[1], g_axes[2]);
}

/* Simple main function */
int main() {

  pc.baud(926100);
  
  humidity_sensor->read_id(&id);
  pressure_sensor->read_id(&id);
  magnetometer->read_id(&id);
  gyroscope->read_id(&id);
  
  pc.printf("Press the blue button to start\n\rThen press the black reset button to stop\n\r");

  while (sw1 == 0);
  wait_ms(250.0);
  while (sw1 == 1);
  wait_ms(250.0);
  
  pc.printf("time[ms],x[mag/mgauss],y[mag/mgauss],z[mag/mgauss],x[acc/mg],y[acc/mg],z[acc/mg],x[gyro/mdps],y[gyro/mdps],z[gyro/mdps]\n\r");   
  
  sampler.start(samplerThread);
  queue.call_every(Tms, doSample);
  sampler.join();
  
  while(true) {
      pc.printf("Should never get here!\n\r");
      wait(1.0);
  }
  
}