#ifndef _TS_API_H
#define _TS_API_H

#include "py32_tk.h"

#define TK_APP_NONE 		0x00U
#define TK_APP_TOUCH_KEY 	0x01U
#define TK_APP_SLIDER 		0x02U
#define TK_APP_WHEEL 		0x04U
#define TK_APP_PANAL 		0x08U

#define TK_CH0 		0x00U
#define TK_CH1 		0x01U
#define TK_CH2 		0x02U
#define TK_CH3 		0x03U
#define TK_CH4 		0x04U
#define TK_CH5 		0x05U
#define TK_CH6 		0x06U
#define TK_CH7 		0x07U
#define TK_CH8 		0x08U
#define TK_CH9 		0x09U
#define TK_CH10 	0x0AU
#define TK_CH11 	0x0BU
#define TK_CH12 	0x0CU
#define TK_CH13 	0x0DU
#define TK_CH14 	0x0EU
#define TK_CH15 	0x0FU
#define TK_CH16 	0x10U
#define TK_CH17 	0x11U
#define TK_CH18 	0x12U
#define TK_CH19 	0x13U
#define TK_CH20 	0x14U
#define TK_CH21 	0x15U
#define TK_CH22 	0x16U
#define TK_CH23 	0x17U
#define TK_CH24 	0x18U
#define TK_CH25 	0x19U
#if LIB_09X
#define TK_CH26 	0x1AU
#define TK_CH27 	0x1BU
#define TK_CH28 	0x1CU
#define TK_CH29 	0x1DU
#define TK_CH30 	0x1EU
#define TK_CH31 	0x1FU
#define TK_CH32 	0x20U
#endif
#define TK_CH_NONE 	0xFFU

typedef enum
{
    TK_INIT,
    TK_SET_BASELINE,
    TK_GO,
    TK_ENTERSLEEP,
    TK_EXITSLEEP,
} TSState_TypeDef;

typedef enum
{
    STATE_IDLE,
    STATE_SINGLE_CH_ACQING,
    STATE_SINGLE_CH_ACQ_DONE,
    STATE_MULTI_CH_ACQING,
    STATE_MULTI_CH_ACQ_DONE,
	STATE_DETECT_CH_ACQING,
	STATE_SLIDER_CH_ACQING,
	STATE_REF_CH_ACQING,
} TK_RawAcqState_TypeDef;

#pragma pack(4)
typedef struct
{
    TSState_TypeDef TK_state;				// 触摸状态
    uint8_t TouchKeyChCnt; 					// 按键数量	
    uint8_t SleepEn;						// 休眠使能
    uint8_t KeyOutTimeLimitEn;				// 长按超时使能
    uint16_t SleepTime;						// 休眠计数值
    TK_Keyflag_Type KeyFlags;				// 用户按键标志
    TK_Keyflag_Type KeyFlagsPending;		// 触摸库按键标志
    TK_Keyflag_Type TK_KeyEnable;			// 使能触摸通道号
    uint16_t KeyOutTimeLimitCNT;			// 长按超时时间
	uint8_t RESERVED0;						
	uint8_t DetectOut;				
	uint8_t InsideOffset;					
	int16_t SliderOrWheelPosition[SLIDER_MAX]; // 滑条位置
	uint16_t InsideDif;
	uint32_t TK_FastScanBits;
	uint16_t SliderOutTimeLimitCNT;				// 长按超时时间
	TK_Keyflag_Type TK_KeyOutTimeLimitFlag;
} TKMainCtr_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
    uint8_t TouchAcqChCntDbg;  				// 扫描通道总数量，包含防水以及滑环
    uint8_t *TouchKeyChSeq; 				// 通道号
	uint8_t TouchAcqChCnt;  				// 扫描通道总数量，包含防水以及滑环
} TouchKeyChInfor_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
	uint8_t RESERVED0;							
    uint8_t WheelSliderTouch;					//滑条状态1
    uint8_t SliderWheelxStartChIdx[SLIDER_MAX]; //滑环启动坐标
    uint8_t SliderWheelxChCnt[SLIDER_MAX];  	//滑环通道数量
    uint8_t *SliderWheelType;					//滑条类型
    uint16_t *SliderWheelResloution;			//滑条分辨率
	uint16_t del_val[SLIDER_MAX];
	uint8_t TK_WheelConfirmTickCnt[SLIDER_MAX];
	uint8_t SliderWheelxStart;
	uint8_t SliderWheelxStop;
	uint8_t SliderWheelxCnt;
	uint16_t SliderWin;             // 窗口时间
	TK_Keyflag_Type SliderKenable; // 通道使能
} SliderChInfor_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
    uint16_t *TK_AreaConfirmTickCnt;
    uint16_t *TK_KeyOutTimeLimitCnt;
	uint8_t *TK_DebounceCnt;					// 消抖次数
} TK_TickCouter_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
    uint16_t *RawData;
	uint16_t *FilteredData;
    uint16_t *BaseLineData;
	uint16_t *FilteredDataMax;	
    int16_t *Differ;
    uint16_t *FingerTHD;
	uint16_t *FilteredDataDBG;
    uint16_t *BaseLineDataDBG; 
	uint16_t *FilteredDataMin;
	uint8_t *BaseRrimCnt;
	uint8_t *filterCount[2];            // filterCount：滤波计数器
	uint8_t *Noise_Def;
} TouchKeyDatas_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
	TK_Keyflag_Type CopaOutLowTrimStep;
	uint8_t CopaOutLowTrimIdac[MAX_CHS];
    uint16_t *SingleFilteredData;
	uint16_t *SingleBaseLineData;
    uint16_t *SingleAcqData;
	uint16_t *key_ratio[2];
	uint8_t *PendingTime;
} TouchKeyWaterProof_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
	uint16_t *SyncWindow;
	uint16_t *raw_dat;
	uint16_t *raw_dat2;
	uint32_t *raw_filter[2];
	uint16_t *ref_JumpAmp[2];	
} TouchKeySync_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct logQueue
{
    uint8_t *base;
    uint8_t front, rear;
    uint8_t sq_size;
	uint8_t app_connect;
} logQueue;
#pragma pack()

#pragma pack(4)
typedef struct
{
	uint8_t releaseRate;
	uint8_t Coeff;
	uint16_t BL_TraceUpTickCnt;
	uint16_t BL_TraceDnTickCnt;
	uint8_t RawCompensateEn;
	uint8_t BL_TraceMode;
	uint8_t FILTER_NOISE_DEF;
	uint8_t FAST_SCAN_CNT_DEF;
	uint16_t SLIDER_BL_TraceUpTickCnt;
	uint16_t SLIDER_BL_TraceDnTickCnt;
	uint8_t TkAcqAbandonCNT;
} TKCtrPara_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
    uint16_t Win;             
	uint8_t Coeff;
	uint8_t Enable;
} SyncSampling_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
	uint8_t ShieldStartChIdx;    	//屏蔽通道坐标
	uint8_t Noise;					//屏蔽通道稳定
	uint16_t Timeout;		
	int16_t RESERVED1[2];	
	uint8_t WaterProof_En;
	uint8_t WaterProof_Chs;			//Shield通道号
	uint8_t ShieldState;	
	uint16_t WaterProof_Time;		
	uint8_t WaterState;
	uint8_t DusterCloth_En;			//1 防湿抹布功能开启
	uint16_t DusterCloth_THD;		//湿抹布判断门限值
	uint8_t WaitTime;
} ShieldInfor_TypeDef;
#pragma pack()

#pragma pack(4)
typedef struct
{
	uint8_t ChsOffset;
	uint8_t ChsEnd;
	uint8_t ChsCnt;
	uint8_t TrimRatio;
	uint8_t Swdiv;
	uint16_t Win;
	uint16_t *DetectFilteredData;
	int16_t *Calibration;
} WaterDetect_TypeDef;
#pragma pack()

typedef struct
{
	uint8_t LpDac;           // 休眠电流
	uint8_t WakeDelay;       // 唤醒延时，唤醒后第一次采集数据不准确，所以丢掉
	uint16_t Baseline;       // 硬件比较器的值
	uint16_t NormalDelta;    // 唤醒差值
	uint16_t LpWin;          // 休眠窗口值
	TK_Keyflag_Type WakeChs; // 唤醒通道
	TK_Keyflag_Type LpKchs;  // 唤醒通道
} LP_InitTypeDef;
#pragma pack()
	
#pragma pack(4)
typedef struct
{
    uint32_t *RESERVED0;
    TouchKeyChInfor_TypeDef *pTouchKeyChInfor;		//R
    TouchKeyDatas_TypeDef *pTouchKeyDatas;			//R
    TKMainCtr_TypeDef *pTKCtr;						//R
    TK_TickCouter_TypeDef *pTK_TickCouter;			//R
    uint32_t *RESERVED1;
	uint32_t *RESERVED2;
    SliderChInfor_TypeDef *pSliderChInfor;			//R
    uint8_t *pDtRdy;								//R
    uint32_t *pVersion;								//R
	ShieldInfor_TypeDef *pShieldInfor;				//R
	logQueue *plogQueue;							//R
	uint32_t *pChip;								//R
} TK_Info_T;
#pragma pack()

extern uint32_t TK_FastScanBitsNow;
extern logQueue app_log;
extern uint32_t BaseDir;
extern TK_Info_T TK_Info;
extern TKMainCtr_TypeDef TKCtr;
extern TK_HandleTypeDef TK_Handle;
extern TK_RawAcqState_TypeDef RawDatasAcquisitionState; // 触摸扫描状态
extern TouchKeyDatas_TypeDef TouchKeyDatas;     // 触摸数据
extern TK_TickCouter_TypeDef TK_TickCouter;     // 滤波时间
extern TouchKeyChInfor_TypeDef TouchKeyChInfor; // 触摸数量以及通道
extern const uint8_t BL_TRACE_UP_STEP[16];
extern const uint8_t BL_TRACE_DN_STEP[16];
extern const TKCtrPara_TypeDef TKCtrPar; 
extern const uint8_t FingerPressDebounceCNT;
extern const uint8_t FingerReleaseDebounceCNT;
void TK_Init(void);
void TK_RegisterCfg(void);
void TK_UserParaInit(void);
void TK_MainFsm(void);
void TK_TimerHandler(uint8_t ms);
void TK_UpdatBaseLineData(uint8_t chs);
void DebugDataUpdate(void);
void TK_MultiChAcqStart(void);
void DetectCallback(void);
void PrintfTask(void);
void FilterExDeal(uint8_t chs);
void APP_TouchKeyDataProc(void);
uint8_t TK_ScanDone(void);
void TK_FastScanSet(void);
uint8_t TK_GetNextChidx(uint8_t now_idx);
uint16_t TK_RawDataCompensate(uint8_t chs, uint16_t raw);
uint16_t IIR_Filter(uint16_t PrvVal, uint16_t NewVal,uint8_t n,uint8_t bank);
TK_Keyflag_Type APP_TouchKeyFlagsMask(void);
void APP_TouchKeyDifferCalc(void);
void APP_TouchKeyFlagsGenerate(void);
void Key_Filtering(uint8_t chs);
void TK_Filtering(void);
void TK_SingleChAcqStart(uint8_t n, uint8_t type);
TK_Status TK_CsdIdacTrimChs(uint32_t chs, uint8_t TrimStep, uint8_t *idac, uint16_t win, uint16_t max_win);
uint8_t TK_GetNextChPartial(uint8_t idx);
void InnerDifCalc(void);
void SetChsBits(uint8_t i);
void delay_us(uint16_t nus);
/*
 * \brief  外部接口
 */
#if TK_SLEEP_ENABLE
extern LP_InitTypeDef LpHandle;
void TK_EnterStop(void);
#endif
#if TK_REF_ENABLE
void Inside_Init(uint16_t raw);
void Inside_In(uint16_t raw);
void InsideTrigger(uint16_t InsideDif,uint16_t Thd);
uint16_t Inside_Dif(uint16_t raw,uint16_t *base);
#endif
#if TK_LED_SACN
#define TK_LED_OFF	0
#define TK_LED_ON	1
#define TK_LED_COL	2
void TK_LedCallback(uint8_t State);
#endif
#if TK_SLIDER_ENABLE
extern SliderChInfor_TypeDef SliderChInfor;     // 滑条数据
extern const uint8_t SliderPressDebounceCNT;
extern const uint8_t SliderReleaseDebounceCNT;
void APP_TouchSliderOrWheelProc(void);
#endif
#if MIDAC_MODE
uint8_t TK_WinDetection(void);
#endif
#if TK_WATERPROOF_ENABLE
extern TouchKeyWaterProof_TypeDef *PTouchKeyWaterProof;
extern ShieldInfor_TypeDef ShieldInfor;
uint8_t WaterProof_Evt(uint8_t Evt,uint8_t chs);
uint8_t WaterProof_Isr(uint8_t Isr);
#endif
#if TK_SAMP_SYNC_ENABLE
extern const SyncSampling_TypeDef SyncSampling;
extern TouchKeySync_TypeDef *PTouchKeySync;
uint8_t SyncSamplingCallback(uint8_t *raw_cnt,uint16_t *raw_ref);
#endif
#if TK_WATERDETECT_ENABLE
extern WaterDetect_TypeDef WaterDetectInfor;
uint8_t WaterDetect_Evt(uint8_t Evt,uint8_t chs);
#endif
#if TK_TICK_USAGE
extern uint32_t user_isr_tick;
extern uint32_t user_fsm_tick;
#endif
#if 0
extern void log_printf(const char *fmt, ...);
#define lib_printf(...)      log_printf(__VA_ARGS__)                                 
#else
#define lib_printf(...)
#endif
#endif
