#include "u_global.h"
//#include "hw_lc_impl.h"
#include "user_driver.h"

Sys_t g_sys;
static AnalogTimer_t m_ag_timer_list[AG_TIMER_ID_MAX] = {0x00};

void clr_format_transition(uint8_t ts_type, uint8_t ts_num, uint8_t *clr_8, uint32_t *clr_32)
{
	int i = 0;
	if (ts_type == CLR_TS_TYPE_8_32)
	{
		
		for (i = 0; i < ts_num; i++)
		{
			
			clr_32[i] = clr_8[i * 3 + 2] << 16 | clr_8[i * 3] << 8 | clr_8[i * 3 + 1];
			//U_UART_PRINTF("rgb = %02X, %02X, %02X, clr_32 = %06X\n", clr_8[i * 3], clr_8[i * 3 + 1], clr_8[i * 3 + 2], clr_32[i]);
		}
	}
	else
	{
		for (i = 0; i < ts_num; i++)
		{
			clr_8[i * 3] 	 = clr_32[i] >> 8 & 0xFF;
			clr_8[i * 3 + 1] = clr_32[i] & 0xFF;
			clr_8[i * 3 + 2] = clr_32[i] >> 16 & 0xFF;
		}
	}
}

//uint32_t u_get_sys_time(void)
//{
//	return (HW_Get_Native_Clk_Avoid_Race() / 32);
//}
uint32_t u_get_sys_time(void)
{
	return (HW_Get_Native_Clk_Avoid_Race());//��ȡ����ms��
}
uint8_t u_ag_timer_start(AgTimerId timer_id, uint16_t timing, void (*callback)(void))
{
	if (timer_id >= AG_TIMER_ID_MAX || callback == NULL)
	{
		return GLO_ERR;
	}
//	
//	p_timer->en    = GLO_EN;
	if (timing)
	{
		m_ag_timer_list[timer_id].timing		 = timing ;
	}
	m_ag_timer_list[timer_id].timer_id		 = timer_id;
	m_ag_timer_list[timer_id].cb				 = callback;
	m_ag_timer_list[timer_id].record_time		 = u_get_sys_time() + m_ag_timer_list[timer_id].timing;
	
	return GLO_NO_ERR;
}

uint8_t u_ag_timer_stop(AgTimerId timer_id)
{
	if (timer_id >= AG_TIMER_ID_MAX)
	{
		return GLO_ERR;
	}
	
	m_ag_timer_list[timer_id].cb		 = NULL;
	return GLO_NO_ERR;
}

void ag_timer_sched(void)
{
	int i = 0;
	volatile uint32_t curr_sys_time = u_get_sys_time();
	
	for (i = 0; i < AG_TIMER_ID_MAX; i++)
	{
		if (m_ag_timer_list[i].cb && curr_sys_time >= m_ag_timer_list[i].record_time)
		{
			m_ag_timer_list[i].record_time = u_get_sys_time() + m_ag_timer_list[i].timing;
			m_ag_timer_list[i].cb();
		//	U_UART_PRINTF("sys_time = %d\r\n TaskID:%d", u_get_sys_time(),m_ag_timer_list[i]);
		}
	}
}

static SysEvent m_sys_event 		= {0x00};
static SysEvenInfo m_sys_event_info[SYS_EVENT_MAX_NUM] = {0x00};

void sys_event_init(void)
{
	memset(&m_sys_event, 0, sizeof(SysEvent));
	memset(&m_sys_event_info, 0, sizeof(m_sys_event_info));
}

uint8_t sys_event_new(uint16_t dely_time, void (*execute_cb)(void))
{
	uint8_t info_save_ind = 0x00, i = 0;
	
	for (i = 0; i < SYS_EVENT_MAX_NUM; i++)
	{
		if (((m_sys_event.enent_flag >> i) & 0x01) == 0)
		{
			info_save_ind = i;
			
			m_sys_event.enent_flag |= (0x01 << i);
			m_sys_event_info[info_save_ind].enent_flag_bit 	= (0x01 << i);
			m_sys_event_info[info_save_ind].execute_time   	= u_get_sys_time() + dely_time / SYS_TICK;
			m_sys_event_info[info_save_ind].execute_cb		= execute_cb;
			
			//U_UART_PRINTF("even_flag = %X\n", m_sys_event.enent_flag);
			
			return 1;
		}
	}
	return 0;
}

//void sys_event_free(uint16_t dely_time, void (*execute_cb)(void))
//{


//}

void sys_event_sched(void)
{
	int i = 0;
	volatile uint32_t curr_sys_time = 0x00;
	
	if (m_sys_event.enent_flag == 0x00)
	{
		return;
	}
	curr_sys_time = u_get_sys_time();
	
	for (i = 0; i < SYS_EVENT_MAX_NUM; i++)
	{
		if ((m_sys_event.enent_flag >> i) & 0x01)
		{
			if (curr_sys_time >= m_sys_event_info[i].execute_time)
			{
				m_sys_event.enent_flag &= ~(0x01 << i);
				
				m_sys_event_info[i].execute_cb();
//				U_UART_PRINTF("even_flag = %X\n", m_sys_event.enent_flag);
			}
		}
	}
}


void sys_param_init(void)
{
	memset(&g_sys, 0, sizeof(Sys_t));
	memset(m_ag_timer_list, 0, sizeof(m_ag_timer_list));
	//sys_event_init();
}

signed int u_bprintf_null(const char *fmt, ...)
{
	return 1;
}