/** ##########################################################################
**     Filename  : key_encoder_driver.c
**     Project   :
**     Module    :
**     Processor :
**     Version   : 1.0
**     Compiler  :
**     Date/Time :
**     Abstract  :
**     Contents  :
**     Note      : 滚轮，
**
**     (c) Copyright dmdz Co.,Ltd
**  --------------------------------------------------------------------------
**               R E V I S I O N   H I S T O R Y
**  --------------------------------------------------------------------------
**  Date       Ver   Author             Description

**  -20230602-  --V1.0--  --mingyea---  --修改--

** #########################################################################*/

/*---------------------------------------------------------------------------
- I N C L U D E   F I L E S
----------------------------------------------------------------------------*/
#include "common_memory.h"
#include "common_cfg.h"
#include "key_encoder_driver.h"

/*---------------------------------------------------------------------------
- D E F I N E S / M A C R O S
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
- T Y P E   D E F I N I T I O N S
----------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
- S T A T I C   V A R I A B L E S
----------------------------------------------------------------------------*/
static key_encoder_s g_key_encoder = {0};

/*---------------------------------------------------------------------------
- G L O B A L   V A R I A B L E S
----------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
- C O N S T A N T S
----------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------
- F U N C T I O N   P R O T O T Y P E
----------------------------------------------------------------------------*/
static void key_encoder_driver_timeout(void);
static void key_encoder_set_event(u8 event);
static void key_encoder_logic_operate(void);

/*---------------------------------------------------------------------------
|Prototype		: void key_encoder_driver_init(void)
|Called by		: None
|Preconditions		: None
|Input parameters	: None
|Output parameters	: None
|Return value		: None
|Description		:  初始化
----------------------------------------------------------------------------*/
void key_encoder_driver_init(void)
{
	key_encoder_driver_cfg_init();
	common_memory_clear((u8*)&g_key_encoder,sizeof(g_key_encoder));
	g_key_encoder.state = KEY_ENCODER_STATE_ERROR;
	g_key_encoder.input0 = KEY_ENCODER_INPUT_0_PIN;
	g_key_encoder.input1 = KEY_ENCODER_INPUT_1_PIN;
	g_key_encoder.pre_input0 = KEY_ENCODER_INPUT_0_PIN;
	g_key_encoder.pre_input1 = KEY_ENCODER_INPUT_1_PIN;
	key_encoder_driver_goto_wakeup();	/*PRQA S 2987*/  /*内容被宏屏蔽*/
}

/*---------------------------------------------------------------------------
|Prototype		: void key_encoder_driver_timeout(void)
|Called by		: None
|Preconditions		: None
|Input parameters	: None
|Output parameters	: None
|Return value		: None
|Description		: cycle task  在1ms中断里调用
----------------------------------------------------------------------------*/
static void key_encoder_driver_timeout(void)
{

	if((g_key_encoder.input0 == g_key_encoder.pre_input0)
	&& (g_key_encoder.input1 == g_key_encoder.pre_input1))
	{
		g_key_encoder.time_count ++;
		if(g_key_encoder.time_count >= KEY_ENCODER_TIME_OUT)
		{
			g_key_encoder.time_count = 0;
			if(g_key_encoder.state != KEY_ENCODER_STATE_IDLE)
			{
				g_key_encoder.state = KEY_ENCODER_STATE_ERROR;
			}
			
		}
	}
	else
	{
		g_key_encoder.time_count = 0u;
	}


}

/*---------------------------------------------------------------------------
|Prototype		: void key_encoder_driver_task(void)
|Called by		: None
|Preconditions		: None
|Input parameters	: None
|Output parameters	: None
|Return value		: None
|Description		: cycle task  在1ms中断里调用
----------------------------------------------------------------------------*/
void key_encoder_driver_task(void)
{
	g_key_encoder.input0 = KEY_ENCODER_INPUT_0_PIN;
	g_key_encoder.input1 = KEY_ENCODER_INPUT_1_PIN;
	(void)key_encoder_logic_operate();
	(void)key_encoder_driver_timeout();
	g_key_encoder.pre_input0 = g_key_encoder.input0;
	g_key_encoder.pre_input1 = g_key_encoder.input1;
}

/*---------------------------------------------------------------------------
|Prototype		: void key_encoder_logic_operate(void)
|Called by		: None
|Preconditions		: None
|Input parameters	: None
|Output parameters	: None
|Return value		: None
|Description		: cycle task  在1ms中断里调用
----------------------------------------------------------------------------*/
#ifndef KEY_ENCODER_FULL_CYCLE_TRIG
void key_encoder_logic_operate(void)
{

	g_key_encoder.input0_changed = false;
	g_key_encoder.input1_changed = false;

	if(g_key_encoder.pre_input0 != g_key_encoder.input0)
	{
		g_key_encoder.input0_changed = true;
	}

	if(g_key_encoder.pre_input1 != g_key_encoder.input1)
	{
		g_key_encoder.input1_changed = true;
	}
	switch(g_key_encoder.state)
	{
		case KEY_ENCODER_STATE_IDLE:
			if( (g_key_encoder.input0_changed == true) && (g_key_encoder.input1_changed == false) )
			{
				g_key_encoder.state = KEY_ENCODER_STATE_1;
			}
			else if( (g_key_encoder.input0_changed == false) && (g_key_encoder.input1_changed == true) )
			{
				g_key_encoder.state = KEY_ENCODER_STATE_2;
			}
			else
			{
				g_key_encoder.state = KEY_ENCODER_STATE_IDLE;
			}
			break;
		case KEY_ENCODER_STATE_1:
			if( (g_key_encoder.input0_changed == false) && (g_key_encoder.input1_changed == true) )
			{
				(void)key_encoder_set_event(KEY_ENCODER_EVENT_1);	
				g_key_encoder.state = KEY_ENCODER_STATE_WAIT_RELEASE;
				
			}
			else if( (g_key_encoder.input0_changed == true))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_ERROR;
			}
			else
			{
				g_key_encoder.state = KEY_ENCODER_STATE_1;
			}
			break;
		case KEY_ENCODER_STATE_2:
			if( (g_key_encoder.input0_changed == true) && (g_key_encoder.input1_changed == false))
			{
				(void)key_encoder_set_event(KEY_ENCODER_EVENT_2);
				g_key_encoder.state = KEY_ENCODER_STATE_WAIT_RELEASE;

			}
			else if((g_key_encoder.input1_changed == true))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_ERROR;
			}
			else
			{
				g_key_encoder.state = KEY_ENCODER_STATE_2;
			}
			break;
		case KEY_ENCODER_STATE_WAIT_RELEASE:
		case KEY_ENCODER_STATE_ERROR:
			if((g_key_encoder.input1 == g_key_encoder.input0))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_IDLE;
			}
			break;
		default:
			while(1);
			break;
	}

}
#else
void key_encoder_logic_operate(void)
{
	switch(g_key_encoder.state)
	{
		case KEY_ENCODER_STATE_IDLE:
			g_key_encoder.result = KEY_ENCODER_EVENT_NULL;
			if((g_key_encoder.input0 == KEY_ENCODER_VALID_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_IDLE_LEVEL))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_1;
			}
			else if((g_key_encoder.input0 == KEY_ENCODER_IDLE_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_VALID_LEVEL))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_2;
			}
			else
			{
				g_key_encoder.state = KEY_ENCODER_STATE_IDLE;
			}
			break;
		case KEY_ENCODER_STATE_1:
			if((g_key_encoder.input0 == KEY_ENCODER_VALID_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_VALID_LEVEL))
			{
				g_key_encoder.result = KEY_ENCODER_EVENT_1;
				g_key_encoder.state = KEY_ENCODER_STATE_WAIT_RELEASE;
			}
			else if((g_key_encoder.input0 == KEY_ENCODER_VALID_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_IDLE_LEVEL))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_1;
			}
			else
			{
				g_key_encoder.state = KEY_ENCODER_STATE_ERROR;
			}		
			break;
		case KEY_ENCODER_STATE_2:
			if((g_key_encoder.input0 == KEY_ENCODER_VALID_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_VALID_LEVEL))
			{
				g_key_encoder.result = KEY_ENCODER_EVENT_2;
				g_key_encoder.state = KEY_ENCODER_STATE_WAIT_RELEASE;
			}
			else if((g_key_encoder.input0 == KEY_ENCODER_IDLE_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_VALID_LEVEL))
			{
				g_key_encoder.state = KEY_ENCODER_STATE_2;
			}
			else
			{
				g_key_encoder.state = KEY_ENCODER_STATE_ERROR;
			}		
			break;	
		case KEY_ENCODER_STATE_WAIT_RELEASE:
		case KEY_ENCODER_STATE_ERROR:
			if((g_key_encoder.input0 == KEY_ENCODER_IDLE_LEVEL) && (g_key_encoder.input1 == KEY_ENCODER_IDLE_LEVEL))
			{
				(void)key_encoder_set_event(g_key_encoder.result);	
				g_key_encoder.result = KEY_ENCODER_EVENT_NULL;
				g_key_encoder.state = KEY_ENCODER_STATE_IDLE;
			}
			break;
		default:
			while(1);
			break;
	}
}
#endif

/*---------------------------------------------------------------------------
|Prototype         :
|Called by         :
|Preconditions     : -
|Input parameters  : None  id 是邏輯id
|Output parameters : None
|Return value      : None
|Description       : 进入sleep前，要做的事情;---把数据
----------------------------------------------------------------------------*/
void key_encoder_driver_goto_sleep(void)
{
	common_memory_clear((u8*)&g_key_encoder,sizeof(g_key_encoder));
}


/*---------------------------------------------------------------------------
|Prototype         :
|Called by         :
|Preconditions     : -
|Input parameters  : None  id 是邏輯id
|Output parameters : None
|Return value      : None
|Description       : 进入sleep前，要做的事情;---把数据
----------------------------------------------------------------------------*/
void key_encoder_driver_goto_wakeup(void)
{
	//
}


/*---------------------------------------------------------------------------
|Prototype         :
|Called by         :
|Preconditions     :
|Output parameters : None
|Return value      : None
|Description       :
----------------------------------------------------------------------------*/
static void key_encoder_set_event(u8 event)
{
	if(g_key_encoder.event_num < NUM_OF_KEY_ENCODER_EVENT)
	{
		g_key_encoder.event_num ++;
		g_key_encoder.write_index ++;
		if(g_key_encoder.write_index >= NUM_OF_KEY_ENCODER_EVENT)
		{
			g_key_encoder.write_index = 0;
		}
		g_key_encoder.event[g_key_encoder.write_index] = event;
	}
}

/*---------------------------------------------------------------------------
|Prototype         :
|Called by         :
|Preconditions     :
|Output parameters : None
|Return value      : None
|Description       :
----------------------------------------------------------------------------*/
u8 key_encoder_get_event(void)
{
	u8 l_event;

	if(g_key_encoder.event_num)
	{
		g_key_encoder.event_num --;
		g_key_encoder.read_index ++;
		if(g_key_encoder.read_index >= NUM_OF_KEY_ENCODER_EVENT)
		{
			g_key_encoder.read_index = 0;
		}
		l_event = g_key_encoder.event[g_key_encoder.read_index];
	}
	else
	{
		l_event = KEY_ENCODER_EVENT_NULL;
	}

	return (l_event);
}




/* [] END OF FILE */
