/*
 * ________________________________________________________________________________________________________
 * Copyright (c) 2018-2019 InvenSense Inc. All rights reserved.
 *
 * This software, related documentation and any modifications thereto (collectively "Software") is subject
 * to InvenSense and its licensors' intellectual property rights under U.S. and international copyright
 * and other intellectual property rights laws.
 *
 * InvenSense and its licensors retain all intellectual property and proprietary rights in and to the Software
 * and any use, reproduction, disclosure or distribution of the Software without an express license agreement
 * from InvenSense is strictly prohibited.
 *
 * EXCEPT AS OTHERWISE PROVIDED IN A LICENSE AGREEMENT BETWEEN THE PARTIES, THE SOFTWARE IS
 * PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * EXCEPT AS OTHERWISE PROVIDED IN A LICENSE AGREEMENT BETWEEN THE PARTIES, IN NO EVENT SHALL
 * INVENSENSE BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL, 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 THE SOFTWARE.
 * ________________________________________________________________________________________________________
 */
#ifndef __EXAMPLE_AML_H__
#define __EXAMPLE_AML_H__

#include <stdint.h>
#include <stdbool.h>

#include "Icm406xxTransport.h"
#include "Icm406xxDefs.h"
#include "Icm406xxDriver_HL.h"
#include "Icm406xxExtFunc.h"
#include "invn_algo_aml.h"

/*
 * Accelerometer and gyroscope full scale range.
 */
#define ACCEL_FSR_G     ICM406XX_ACCEL_CONFIG0_FS_SEL_16g
#define GYRO_FSR_DPS    ICM406XX_GYRO_CONFIG0_FS_SEL_2000dps

/*
 * Accelerometer and gyroscope frequencies.
 */
#define ACCEL_FREQ      ICM406XX_ACCEL_CONFIG0_ODR_100_HZ
#define GYRO_FREQ       ICM406XX_GYRO_CONFIG0_ODR_100_HZ

/* 
 * Select communication link between SmartMotion and ICM406xx 
 */
// #define SERIF_TYPE      ICM406XX_UI_SPI4
 #define SERIF_TYPE      ICM406XX_UI_I2C

/*
 * Defines
 */

/* Mask to print data through UART */
#define MASK_PRINT_INPUT_ACC_DATA            0x01  /** algorithm inputs */
#define MASK_PRINT_INPUT_GYR_DATA            0x02  /** algorithm inputs */
#define MASK_PRINT_INPUT_CLICK               0x04  /** algorithm inputs */
#define MASK_PRINT_OUTPUT_STATUS             0x08  /** algorithm output status */
#define MASK_PRINT_OUTPUT_GYR_BIASES_DATA    0x10  /** algorithm output pointing delta data */
#define MASK_PRINT_OUTPUT_DELTA              0x20  /** algorithm output gyroscope biases data */
#define MASK_PRINT_OUTPUT_SWIPES             0x40  /** algorithm output swipes gesture detected */
#define MASK_PRINT_INPUT_TEST                0x80  /** For TEST purpose */
#define MASK_PRINT_OUTPUT_QUAT               0x100 /** algorithm output quaternion data */

/**
 * \brief This function is in charge of reseting and initializing Icm406xx device. It should
 * be succesfully executed before any access to Icm406xx device.
 * 
 * \param[in] icm_serif : Serial interface object.
 * \return 0 on success, negative value on error.
 */
int SetupInvDevice(struct inv_icm406xx_serif * icm_serif);

/**
 * \brief This function configures the device in order to output gyro and accelerometer.
 *
 * It initializes clock calibration module (this will allow to extend the 16 bits 
 * timestamp produced by Icm406xx to a 64 bits timestamp).
 * Then function sets full scale range and frequency for both accel and gyro and it 
 * starts.
 *
 * \return 0 on success, negative value on error.
 */
int ConfigureInvDevice(void);

/**
 * \brief This function initializes the AML algorithm.
 *
 * \return 0 on success, negative value on error.
 */
int InitInvAMLAlgo(void);

/**
 * \brief This function only reset the Swipe gesture recognition feature 
 * available with the AML algorithm.
 */
void ResetInvAMLAlgoSwipeRecognition(void);

/**
 * \brief Function called to update the button click status
 * \param[in] on_off Button status
 */
void ClickButton(bool on_off);

/**
 * \brief This function extracts data from the Icm406xx FIFO.
 *
 * The function just calls Icm406xx driver function inv_icm406xx_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 GetDataFromInvDevice(void);

/**
 * \brief This function is the custom handling packet function.
 *
 * It is passed in parameter at driver init time and it is called by 
 * inv_icm406xx_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_icm406xx_sensor_event_t * event);

#endif /* !__EXAMPLE_AML_H__ */