/*
 * __________________________________________________________________
 *
 * 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_FREEFALL_DETECTION_H_
#define _EXAMPLE_FREEFALL_DETECTION_H_

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

/**
 * \brief This function is in charge of reseting and initializing Ixm42xxx device. It should
 * be succesfully executed before any access to Ixm42xxx device.
 *
 * \return 0 on success, negative value on error.
 */
int SetupInvDevice(struct inv_ixm42xxx_serif * icm_serif);

/**
 * \brief This function configures the device in order to output LowG and HighG event to detect a Free Fall.
 *
 * Then function sets frequency for accel, lowG, highG and optionnaly pedometer, and it 
 * starts them in the requested power mode.
 *
 * \param[in] freefall_freq : freefall frequency. See IXM42XXX_APEX_CONFIG0_DMP_ODR_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \param[in] ff_debounce_duration : Duration(us) during which LowG and HighG events are not taken into account after an HighG event.
 * \param[in] ff_max_duration_cm : Distance (cm) max crossed by the device after a LowG event, below which the detection of an HighG event triggers a freefall interrupt. 
 * \param[in] ff_min_duration_cm : Distance (cm) min crossed by the device after a LowG event, above which the detection of an HighG event triggers a freefall interrupt. 
 * \param[in] lowg_peak_th : absolute low peak threshold. See IXM42XXX_APEX_CONFIG5_LOWG_PEAK_TH_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \param[in] lowg_peak_hyst : low peak hysteresis threshold. See IXM42XXX_APEX_CONFIG4_LOWG_PEAK_TH_HYST_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \param[in] lowg_samples_th : time in sample number before trigerring lowG. See IXM42XXX_APEX_CONFIG5_LOWG_TIME_TH_SAMPLES_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \param[in] highg_peak_th : absolute high peak threshold. See IXM42XXX_APEX_CONFIG6_HIGHG_PEAK_TH_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \param[in] highg_peak_hyst : high peak hysteresis threshold. See IXM42XXX_APEX_CONFIG4_HIGHG_PEAK_TH_HYST_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \param[in] highg_samples_th : time threshold in sample number before trigerring lowG. See IXM42XXX_APEX_CONFIG6_HIGHG_TIME_TH_SAMPLES_t in Ixm42xxxDefs.h 
 *                         for possible values.
 * \return 0 on success, negative value on error.
 */
int ConfigureInvDevice(IXM42XXX_APEX_CONFIG0_DMP_ODR_t freefall_freq,
						IXM42XXX_APEX_CONFIG10_FF_DEBOUNCE_DURATION_t ff_debounce_duration,
						IXM42XXX_APEX_CONFIG10_FF_MAX_DURATION_t ff_max_duration_cm,
						IXM42XXX_APEX_CONFIG10_FF_MIN_DURATION_t ff_min_duration_cm,
						IXM42XXX_APEX_CONFIG5_LOWG_PEAK_TH_t lowg_peak_th,
						IXM42XXX_APEX_CONFIG4_LOWG_PEAK_TH_HYST_t lowg_peak_hyst,
						IXM42XXX_APEX_CONFIG5_LOWG_TIME_TH_SAMPLES_t lowg_samples_th,
						IXM42XXX_APEX_CONFIG6_HIGHG_PEAK_TH_t highg_peak_th,
						IXM42XXX_APEX_CONFIG4_HIGHG_PEAK_TH_HYST_t highg_peak_hyst,
						IXM42XXX_APEX_CONFIG6_HIGHG_TIME_TH_SAMPLES_t highg_samples_th);

/**
 * \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.
 *
 * \return 0 on success, negative value on error.
 */
int GetDataFromInvDevice(void);

/**
 * \brief This function converts the free fall duration from number of sample to cm.
 *
 * \return 0 on success, negative value on error.
 */
float convert_ff_duration_sample_to_cm(uint16_t ff_duration_samples);

#endif /* !_EXAMPLE_FREEFALL_DETECTION_H_ */
