#include   "forceSnsr.h"
#include   "forceSnsr_Cfg.h"
#include   "forcedetect.h"
#include   <string.h>



/***************************************************************
copyright from private LiuXiao
if you have any question,you can contact me by email 461445092@qq.com
2022-10-05 17:56:24.020233
***************************************************************/

extern forcesnsr_RxReg_un_type forcesnsr_reg_state_un_bak;
static  volatile STATE_UINT8 forcedetect_btn_val;
detectforce_Reg_Cfg_Main_Str_typ detectforce_Reg_Str;

static uint8_t press_allowed_send_signal_flag = 0;
static uint16_t send_signal_flag_count = 0;
/***************************static function define end  ************************************/
/*combind  8bit and 4bit data to uint16
*/
static inline STATE_UINT16 forcedetect_combind_data(STATE_UINT8 H_byte_8bit,STATE_UINT8 L_byte_4bit)
{
	uint16_t temp1=0,temp2=0;
	temp1 = H_byte_8bit;
	temp2 = L_byte_4bit;
	temp1 <<= 4;
	temp1 += temp2;

	return temp1;
}


/***************************init function define begin************************************/
void  forcedetect_Init(void)
{
  forcesnsr_Init();
  detectforce_Reg_Str.btn_state = FALSE;
}
/***************************init function define end  ************************************/




/***************************main function define begin************************************/
void  forcedetect_MainTask(void)
{
	STATE_UINT8 adc_cnt=0;
     forcesnsr_MainTask();

	 adc_cnt = forcesnsr_reg_state_un_bak.forcesnsr_RxReg.SCOUNT;
     
	 if(detectforce_Reg_Str.Adc_cnt != adc_cnt)
	 {
		detectforce_Reg_Str.raw =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.ADCOUT_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.ADCOUT_L);
		detectforce_Reg_Str.baseline =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.BASELINE_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.BASELINE_L);
        detectforce_Reg_Str.force_snserr =  forcesnsr_reg_state_un_bak.forcesnsr_RxReg.SNSERR;
		detectforce_Reg_Str.autocal =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.AUTOCAL_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.AUTOCAL_L);
		detectforce_Reg_Str.int_theshold =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.INTRTHR_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.INTRTHR_L);
		detectforce_Reg_Str.adc_max =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.ADCOUT_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.ADCOUT_L);
		detectforce_Reg_Str.fallthr =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.FALLTHR_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.FALLTHR_L);
		detectforce_Reg_Str.temp =  forcedetect_combind_data(forcesnsr_reg_state_un_bak.forcesnsr_RxReg.TEMP_H,forcesnsr_reg_state_un_bak.forcesnsr_RxReg.TEMP_L);
		detectforce_Reg_Str.preload = forcesnsr_reg_state_un_bak.forcesnsr_RxReg.PRELDADJ;
        detectforce_Reg_Str.Adc_cnt = adc_cnt;
	 }
	 
	 if(TRUE == Get_force_btn_is_press_state())
	 {
		 detectforce_Reg_Str.btn_state = TRUE;
	 }
	 else
	 {
	 	 detectforce_Reg_Str.btn_state = FALSE;
	 }

	 if (detectforce_Reg_Str.raw > 50 && detectforce_Reg_Str.raw < 2048)
     {
        press_allowed_send_signal_flag = 1;
        send_signal_flag_count = 0;
     }
     else
     {
        if (send_signal_flag_count < 50)//2ms*50
        {
            send_signal_flag_count++;
        }
        else
        {
            press_allowed_send_signal_flag = 0;
        }
     }
	 
 }
/***************************main function define end  ************************************/


/*
return: ture is button pressed
*/
STATE_UINT8  Get_forcedetect_btn_is_press_state(void)
{
	return detectforce_Reg_Str.btn_state;
}

STATE_UINT16  Get_forcedetect_force_value(void)
{
	return detectforce_Reg_Str.raw;//F2-1N
}

STATE_UINT16 Get_forcedetect_basline_value(void)
{
    return detectforce_Reg_Str.baseline;
}

STATE_UINT8 Get_forcedetect_SNSERR_value(void)
{
    return detectforce_Reg_Str.force_snserr;//0 no error  1 sensor error
}

STATE_UINT8 Get_forcedetect_preload_value(void)
{
    return detectforce_Reg_Str.preload;
}

STATE_UINT8 Get_forcedetect_allowed_send_flag(void)
{
    return press_allowed_send_signal_flag;
}