#include "drivers.h"
#if APP_LED_ENABLE
extern uint8_t led_data[LED_COUNT - 1];
const uint8_t num_tbl[] = {
    0X3F, 0X06, 0X5B, 0X4F, 0X66, 0X6D, 0X7D, 0X07, 0X7F, 0X6F,
};
#if 0
#define LED0_A_SET 		LED_OFFSET_CONFIG(1,1)
#define LED0_A_CLR 		LED_OFFSET_CONFIG(1,0)
#define LED0_B_SET 		LED_OFFSET_CONFIG(2,1)
#define LED0_B_CLR 		LED_OFFSET_CONFIG(2,0)
#define LED0_C_SET 		LED_OFFSET_CONFIG(16,1)
#define LED0_C_CLR 		LED_OFFSET_CONFIG(16,0)
#define LED0_D_SET 		LED_OFFSET_CONFIG(24,1)
#define LED0_D_CLR 		LED_OFFSET_CONFIG(24,0)
#define LED0_E_SET 		LED_OFFSET_CONFIG(3,1)
#define LED0_E_CLR 		LED_OFFSET_CONFIG(3,0)
#define LED0_F_SET 		LED_OFFSET_CONFIG(0,1)
#define LED0_F_CLR 		LED_OFFSET_CONFIG(0,0)
#define LED0_G_SET 		LED_OFFSET_CONFIG(8,1)
#define LED0_G_CLR 		LED_OFFSET_CONFIG(8,0)
#define LED0_DP_SET 	LED_OFFSET_CONFIG(5,1)
#define LED0_DP_CLR 	LED_OFFSET_CONFIG(5,0)

#define LED1_A_SET 		LED_OFFSET_CONFIG(10,1)
#define LED1_A_CLR 		LED_OFFSET_CONFIG(10,0)
#define LED1_B_SET 		LED_OFFSET_CONFIG(11,1)
#define LED1_B_CLR 		LED_OFFSET_CONFIG(11,0)
#define LED1_C_SET 		LED_OFFSET_CONFIG(25,1)
#define LED1_C_CLR 		LED_OFFSET_CONFIG(25,0)
#define LED1_D_SET 		LED_OFFSET_CONFIG(13,1)
#define LED1_D_CLR 		LED_OFFSET_CONFIG(13,0)
#define LED1_E_SET 		LED_OFFSET_CONFIG(12,1)
#define LED1_E_CLR 		LED_OFFSET_CONFIG(12,0)
#define LED1_F_SET 		LED_OFFSET_CONFIG(9,1)
#define LED1_F_CLR 		LED_OFFSET_CONFIG(9,0)
#define LED1_G_SET 		LED_OFFSET_CONFIG(17,1)
#define LED1_G_CLR 		LED_OFFSET_CONFIG(17,0)
#define LED1_DP_SET 	LED_OFFSET_CONFIG(21,1)
#define LED1_DP_CLR 	LED_OFFSET_CONFIG(21,0)

#define LED2_A_SET 		LED_OFFSET_CONFIG(27,1)
#define LED2_A_CLR 		LED_OFFSET_CONFIG(27,0)
#define LED2_B_SET 		LED_OFFSET_CONFIG(20,1)
#define LED2_B_CLR 		LED_OFFSET_CONFIG(20,0)
#define LED2_C_SET 		LED_OFFSET_CONFIG(28,1)
#define LED2_C_CLR 		LED_OFFSET_CONFIG(28,0)
#define LED2_D_SET 		LED_OFFSET_CONFIG(32,1)
#define LED2_D_CLR 		LED_OFFSET_CONFIG(32,0)
#define LED2_E_SET 		LED_OFFSET_CONFIG(34,1)
#define LED2_E_CLR 		LED_OFFSET_CONFIG(34,0)
#define LED2_F_SET 		LED_OFFSET_CONFIG(18,1)
#define LED2_F_CLR 		LED_OFFSET_CONFIG(18,0)
#define LED2_G_SET 		LED_OFFSET_CONFIG(26,1)
#define LED2_G_CLR 		LED_OFFSET_CONFIG(26,0)
#define LED2_DP_SET 	LED_OFFSET_CONFIG(29,1)
#define LED2_DP_CLR 	LED_OFFSET_CONFIG(29,0)
#else
#define LED0_A_SET 		LED_RAM_SET(1,0)
#define LED0_A_CLR 		LED_RAM_CLR(1,0)
#define LED0_B_SET 		LED_RAM_SET(2,0)
#define LED0_B_CLR 		LED_RAM_CLR(2,0)
#define LED0_C_SET 		LED_RAM_SET(0,3)
#define LED0_C_CLR 		LED_RAM_CLR(0,3)
#define LED0_D_SET 		LED_RAM_SET(0,4)
#define LED0_D_CLR 		LED_RAM_CLR(0,4)
#define LED0_E_SET 		LED_RAM_SET(3,0)
#define LED0_E_CLR 		LED_RAM_CLR(3,0)
#define LED0_F_SET 		LED_RAM_SET(0,1)
#define LED0_F_CLR 		LED_RAM_CLR(0,1)
#define LED0_G_SET 		LED_RAM_SET(0,2)
#define LED0_G_CLR 		LED_RAM_CLR(0,2)
#define LED0_DP_SET 	LED_RAM_SET(5,0)
#define LED0_DP_CLR 	LED_RAM_CLR(5,0)

#define LED1_A_SET 		LED_RAM_SET(2,1)
#define LED1_A_CLR 		LED_RAM_CLR(2,1)
#define LED1_B_SET 		LED_RAM_SET(3,1)
#define LED1_B_CLR 		LED_RAM_CLR(3,1)
#define LED1_C_SET 		LED_RAM_SET(1,4)
#define LED1_C_CLR 		LED_RAM_CLR(1,4)
#define LED1_D_SET 		LED_RAM_SET(5,1)
#define LED1_D_CLR 		LED_RAM_CLR(5,1)
#define LED1_E_SET 		LED_RAM_SET(4,1)
#define LED1_E_CLR 		LED_RAM_CLR(4,1)
#define LED1_F_SET 		LED_RAM_SET(1,2)
#define LED1_F_CLR 		LED_RAM_CLR(1,2)
#define LED1_G_SET 		LED_RAM_SET(1,3)
#define LED1_G_CLR 		LED_RAM_CLR(1,3)
#define LED1_DP_SET 	LED_RAM_SET(5,2)
#define LED1_DP_CLR 	LED_RAM_CLR(5,2)

#define LED2_A_SET 		LED_RAM_SET(3,4)
#define LED2_A_CLR 		LED_RAM_CLR(3,4)
#define LED2_B_SET 		LED_RAM_SET(4,2)
#define LED2_B_CLR 		LED_RAM_CLR(4,2)
#define LED2_C_SET 		LED_RAM_SET(4,3)
#define LED2_C_CLR 		LED_RAM_CLR(4,3)
#define LED2_D_SET 		LED_RAM_SET(0,5)
#define LED2_D_CLR 		LED_RAM_CLR(0,5)
#define LED2_E_SET 		LED_RAM_SET(2,5)
#define LED2_E_CLR 		LED_RAM_CLR(2,5)
#define LED2_F_SET 		LED_RAM_SET(2,3)
#define LED2_F_CLR 		LED_RAM_CLR(2,3)
#define LED2_G_SET 		LED_RAM_SET(2,4)
#define LED2_G_CLR 		LED_RAM_CLR(2,4)
#define LED2_DP_SET 	LED_RAM_SET(5,3)
#define LED2_DP_CLR 	LED_RAM_CLR(5,3)
#endif
/********************************************************
**	函数名	void LED_RAM_SET(uint8_t SEG,uint8_t COM)
**	描述	：设置COM口对应SEG
**	传入	：无
**	返回	：无
*********************************************************/
void LED_RAM_SET(uint8_t SEG,uint8_t COM)
{
	if(SEG <= COM)
		led_data[COM - 1] |= (1 << SEG);
	else 
		led_data[COM] |= (1 << SEG);
}
/********************************************************
**	函数名	void LED_RAM_CLR(uint8_t SEG,uint8_t COM)
**	描述	：清除COM口对应SEG
**	传入	：无
**	返回	：无
*********************************************************/
void LED_RAM_CLR(uint8_t SEG,uint8_t COM)
{
	if(SEG <= COM)
		led_data[COM - 1] &= ~(1 << SEG);
	else 
		led_data[COM] &= ~(1 << SEG);
}
/********************************************************
**	函数名	void LED_OFFSET_CONFIG(uint8_t offset,uint8_t State)
**	描述	：设置某一位置LED状态
**	传入	：无
**	返回	：无
*********************************************************/
void LED_OFFSET_CONFIG(uint8_t offset,uint8_t State)
{
	uint8_t seg,com;
	com = offset >> 3;
	seg = offset % 8;
	if(State)
		led_data[com] |= (1 << seg);
	else 
		led_data[com] &= ~(1 << seg);
}
/********************************************************
**	函数名	void LED_Default(void)
**	描述	：设置上电默认显示内容
**	传入	：无
**	返回	：无
*********************************************************/
void LED_Default(void)
{
    /*	默认显示		*/
	LED0_G_SET;
	LED1_G_SET;
	LED2_G_SET;
}
/********************************************************
**	函数名	void LED_Load(void)
**	描述	：设置显示数据
**	传入	：无
**	返回	：无
*********************************************************/
void LED_Load(uint16_t num)
{
	uint8_t smg_data[3];
	smg_data[0] = num_tbl[num / 100];
	smg_data[1] = num_tbl[num / 10 % 10];
	smg_data[2] = num_tbl[num % 10];
	/*	SMG0	*/
	if(smg_data[0] & 0X01)
		LED0_A_SET;
	else
		LED0_A_CLR;
	if(smg_data[0] & 0X02)
		LED0_B_SET;
	else
		LED0_B_CLR;
	if(smg_data[0] & 0X04)
		LED0_C_SET;
	else
		LED0_C_CLR;
	if(smg_data[0] & 0X08)
		LED0_D_SET;
	else
		LED0_D_CLR;
	if(smg_data[0] & 0X10)
		LED0_E_SET;
	else
		LED0_E_CLR;
	if(smg_data[0] & 0X20)
		LED0_F_SET;
	else
		LED0_F_CLR;
	if(smg_data[0] & 0X40)
		LED0_G_SET;
	else
		LED0_G_CLR;
	if(smg_data[0] & 0X80)
		LED0_DP_SET;
	else
		LED0_DP_CLR;
	/*	SMG1	*/
	if(smg_data[1] & 0X01)
		LED1_A_SET;
	else
		LED1_A_CLR;
	if(smg_data[1] & 0X02)
		LED1_B_SET;
	else
		LED1_B_CLR;
	if(smg_data[1] & 0X04)
		LED1_C_SET;
	else
		LED1_C_CLR;
	if(smg_data[1] & 0X08)
		LED1_D_SET;
	else
		LED1_D_CLR;
	if(smg_data[1] & 0X10)
		LED1_E_SET;
	else
		LED1_E_CLR;
	if(smg_data[1] & 0X20)
		LED1_F_SET;
	else
		LED1_F_CLR;
	if(smg_data[1] & 0X40)
		LED1_G_SET;
	else
		LED1_G_CLR;
	if(smg_data[1] & 0X80)
		LED1_DP_SET;
	else
		LED1_DP_CLR;
	/*	SMG2	*/
	if(smg_data[2] & 0X01)
		LED2_A_SET;
	else
		LED2_A_CLR;
	if(smg_data[2] & 0X02)
		LED2_B_SET;
	else
		LED2_B_CLR;
	if(smg_data[2] & 0X04)
		LED2_C_SET;
	else
		LED2_C_CLR;
	if(smg_data[2] & 0X08)
		LED2_D_SET;
	else
		LED2_D_CLR;
	if(smg_data[2] & 0X10)
		LED2_E_SET;
	else
		LED2_E_CLR;
	if(smg_data[2] & 0X20)
		LED2_F_SET;
	else
		LED2_F_CLR;
	if(smg_data[2] & 0X40)
		LED2_G_SET;
	else
		LED2_G_CLR;
	if(smg_data[2] & 0X80)
		LED2_DP_SET;
	else
		LED2_DP_CLR;
}

#endif
