/*
 * __________________________________________________________________
 *
 * Copyright (C) [2023] by InvenSense, Inc.
 * 
 * Permission to use, copy, modify, and/or distribute this software
 * for any purpose with or without fee is hereby granted.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY  AND FITNESS. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE  FOR ANY SPECIAL, DIRECT, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 * __________________________________________________________________
 */

#ifndef _EXAMPLE_EIS_H_
#define _EXAMPLE_EIS_H_

#include <stdint.h>
#include "Invn/Drivers/Ixm42xxx/Ixm42xxxTransport.h"
#include "Invn/Drivers/Ixm42xxx/Ixm42xxxDefs.h"
#include "Invn/Drivers/Ixm42xxx/Ixm42xxxDriver_HL.h"

/* InvenSense algorithm */
#include "invn_algo_agm.h"


/*
 * If set, FSYNC flag and counter are read from FIFO
 * else they are read from ICM registers 
*/
#define FSYNC_USE_FIFO 1

/*
 * Gyroscope fequency
 * Use type IXM42XXX_GYRO_CONFIG0_ODR_t to define gyro frequency.
 * This type is defined in Ixm42xxxDefs.h.
 */
#define GYRO_FREQ   IXM42XXX_GYRO_CONFIG0_ODR_200_HZ

/*
 * Gyroscope Full Scale Range
 * Use type IXM42XXX_GYRO_CONFIG0_FS_SEL_t defined in Ixm42xxxDefs.h
 */
#define GYRO_FSR    IXM42XXX_GYRO_CONFIG0_FS_SEL_2000dps



/**
 * \brief This function is in charge of reseting and initializing Ixm42xxx device. It should
 * be successfully executed before any access to Ixm42xxx device.
 */
int SetupInvDevice(struct inv_ixm42xxx_serif * icm_serif);

/**
 * \brief This function configures the device in order to output gyro and accelerometer.
 *
 * It initialyses clock calibration module (this will allow to extend the 16 bits 
 * timestamp produced by Ixm42xxx to a 64 bits timestamp).
 * Then function sets full scale range and frequency for gyro and it 
 * starts it in low-noise mode. (note that low-power mode is not available 
 * for gyroscope in Ixm42xxx).
 *
 * \return 0 on success, negative value on error.
 */
int ConfigureInvDevice();

/**
 * \brief This function initializes the AGM algorithm.
 *
 * \return 0 on success, negative value on error.
 */
int InitInvAGMAlgo(void);

/**
 * \brief This function extracts data from the Ixm42xxx FIFO.
 *
 * The function just calls Ixm42xxx driver function inv_ixm42xxx_get_data_from_fifo.
 * But note that for each packet extracted from FIFO, a user defined function is called to 
 * allow custom handling of each packet. In this example custom packet handling function
 * is HandleInvDeviceFifoPacket.
 *
 * \return 0 on success, negative value on error.
 */
int GetDataFromFIFO(void);

/**
 * \brief This function provides custom handling for FIFO data packets.
 *
 * It is passed in parameter at driver init time and it is called by 
 * inv_ixm42xxx_get_data_from_fifo function each time a new valid packet is extracted 
 * from FIFO.
 * In this implementation, function extends packet timestamp from 16 to 64 bits and then
 * process data from packet and print them on UART.
 *
 * \param[in] event structure containing sensor data from one packet
 */
void HandleInvDeviceFifoPacket(inv_ixm42xxx_sensor_event_t * event);


/**
 * \brief This function extracts data from the Ixm42xxx registers.
 *
 * The function just calls Ixm42xxx driver function inv_ixm42xxx_get_data_from_registers.
 * But note that for each packet extracted from FIFO, a user defined function is called to 
 * allow custom handling of each packet. In this example custom packet handling function
 * is HandleInvDeviceFifoPacket.
 *
 * \return 0 on success, negative value on error.
 */
int GetDataFromInvDevice(void);

/**
 * \brief This function provides custom handling for data registers.
 *
 * It is passed in parameter at driver init time and it is called by 
 * inv_ixm42xxx_get_data_from_registers function each time new data are read from registers.
 * In this implementation, function process data and print them on UART.
 *
 * \param[in] event structure containing sensor data from one packet
 */
void HandleInvDeviceDataRegisters(inv_ixm42xxx_sensor_event_t * event);

/*!
 * \brief Initialize algorithm to compute the calibrated gyroscope
 *
 * \param[in] odr_us  ixm42xxx output gyroscope data rate in microseconds
 */
void ConfigureAlgorithm(uint32_t gyro_odr_us);

#endif /* !_EXAMPLE_EIS_H_ */
