/*******************************************************************************
 * @file	app_raw.h
 * @author	CandyPops Co.
 * @version V1.0.0
 * @date	2022-09-05
 * @brief
 ******************************************************************************/

/*******************************************************************************
 * [Header overview] ICM42670P IMU driver application layer declarations
 *
 * Function prototypes and operating mode configuration macros for
 * IMU sensor initialization, configuration, and data reading.
 *
 * Key configuration macros:
 *   SERIF_TYPE         - Communication interface (UI_I2C)
 *   USE_LOW_NOISE_MODE - 1: low-noise (800Hz), 0: low-power (100Hz)
 *   USE_HIGH_RES_MODE  - 1: 20-bit high-res, 0: 16-bit standard
 *   USE_FIFO           - 1: use FIFO, 0: direct register read
 ******************************************************************************/

#ifndef _APP_RAW_H_
#define _APP_RAW_H_
#include <stdbool.h>
#include "sdk_config.h"

#include <stdint.h>
#include "inv_imu_transport.h"
#include "inv_imu_defs.h"
#include "inv_imu_driver.h"


/*** Configuration macros ***/

/*
 * MCU-IMU communication interface selection
 * UI_I2C: use I2C communication (default)
 */
#define SERIF_TYPE           UI_I2C

/*
 * Power mode selection
 * 1: Low-noise mode — 800Hz ODR, high precision, higher power consumption
 * 0: Low-power mode — 100Hz ODR, lower power consumption
 * Note: Low-noise mode cannot be used with ODR below 12.5Hz
 */
#define USE_LOW_NOISE_MODE   1

/*
 * FIFO resolution mode selection
 * 0: Low resolution — 16-bit data (default)
 * 1: High resolution — 20-bit data (FSR locked to 16g/2000dps)
 */
#define USE_HIGH_RES_MODE    0

/*
 * Data read method selection
 * 0: Direct register read (currently in use)
 * 1: Read from FIFO
 */
#define USE_FIFO             0


/**
 * \brief Resets and initializes the IMU device. Includes WHOAMI verification.
 *        Must complete successfully before calling any other IMU access functions.
 *
 * \return 0=success, negative=error
 */
int setup_imu_device(struct inv_imu_serif *icm_serif);

/**
 * \brief Configures the device for gyro and accel output.
 *        Applies FSR, ODR, power mode, and FIFO settings.
 * \return 0=success, negative=error
 */
int configure_imu_device(void);

/**
 * \brief Retrieves IMU data from FIFO or registers.
 *        Internally triggers imu_callback() for data processing.
 * \return 0=success, negative=error
 */
int get_imu_data(void);

/**
 * \brief Sensor data receive callback. Applies mounting matrix then
 *        outputs data according to info4/BLE/UART mode.
 * \param[in] event Structure containing one sensor data packet
 */
void imu_callback(inv_imu_sensor_event_t *event);

/**
 * \brief Direct I2C register read bypassing the driver API.
 *        Reads sensor data immediately without DRDY interrupt and sends via BLE.
 *        Switches IMU to sleep mode after reading to save power.
 * \return 0=success, negative=error
 */
int imu_read_direct(void);

/**
 * \brief Read IMU die temperature directly and return deg C x 100.
 * \param[out] temp_x100 Optional temperature output in Celsius x 100.
 * \param[out] temp_c Optional temperature output in Celsius.
 * \return 0=success, negative=error
 */
int imu_read_temperature_x100(uint16_t *temp_x100, float *temp_c);

/**
 * \brief Return true while IMU FIFO capture is active.
 */
bool imu_fifo_capture_is_active(void);

/**
 * \brief Start IMU internal FIFO capture for mtb? test flow.
 *        Configures accel/gyro 100 Hz and flushes FIFO before capture.
 */
int imu_fifo_capture_start(void);

/**
 * \brief Stop IMU FIFO capture, drain FIFO, and send raw FIFO bytes as rim: packets.
 */
int imu_fifo_capture_stop_and_send_rim(void);

/**
 * \brief Stop IMU FIFO capture without sending rim: packets.
 */
void imu_fifo_capture_disable(void);

/*
 * mtb? / rim: binary layout (every BLE fragment)
 *   [ 'r' 'i' 'm' ':' ] [ total_sample_count u16 BE ] [ 12 * total_sample_count bytes ... ]
 *   total_sample_count is the same in each fragment; per-fragment sample count = (payload_len - 6) / 12.
 *
 * IMU_FIFO_RIM_TARGET_SAMPLES: if > 0, cap at newest N samples (drop older when FIFO has more).
 *     When fewer than N valid samples remain, send the actual count in the rim header (no zero pad).
 *     Set to 0 to send all valid samples after filtering (up to the driver buffer limit).
 * IMU_FIFO_RIM_DROP_LEADING_SAMPLES: drop the oldest N FIFO-derived samples before MIN/TARGET
 *     (reduces startup + early-window transients; 0 = off).
 * Override before including this header or via -D from the toolchain.
 */
#ifndef IMU_FIFO_RIM_TARGET_SAMPLES
#define IMU_FIFO_RIM_TARGET_SAMPLES 15U
#endif
#ifndef IMU_FIFO_RIM_MIN_SAMPLES
#define IMU_FIFO_RIM_MIN_SAMPLES 0U
#endif
#ifndef IMU_FIFO_RIM_DROP_LEADING_SAMPLES
#define IMU_FIFO_RIM_DROP_LEADING_SAMPLES 0U
#endif

/*
 * mtb? FIFO path — ICM42670 UI low-noise filter bandwidth (inv_imu_set_*_ln_bw).
 * Enum suffix is approximate -3dB BW in Hz; smaller => smoother, more phase lag.
 * Match to imu_fifo_capture_start() ODR (50Hz in mtb? FIFO mode).
 */
#ifndef IMU_FIFO_MTB_ACCEL_LN_BW
#define IMU_FIFO_MTB_ACCEL_LN_BW ACCEL_CONFIG1_ACCEL_FILT_BW_16
#endif
#ifndef IMU_FIFO_MTB_GYRO_LN_BW
#define IMU_FIFO_MTB_GYRO_LN_BW GYRO_CONFIG1_GYRO_FILT_BW_16
#endif


#endif /* !_APP_RAW_H_ */
