/*
 ******************************************************************************
 *
 * @file     led.h
 * @brief    led dispaly module
 *                 
 *
 * @version  1.0
 * @date     2023/05/12   09:59:40
 * @author   Alex Xu
 *
 * Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
 * All Rights Reserved
 *
 * History:
 * Revision          Date           Author            Desc
 * 1.0.0            2023/07/12      Alex          build this file
 ******************************************************************************
 */

#ifndef __LED_H__
#define __LED_H__

#include "define.h"

#ifdef LED_DISPLAY

#ifdef _DEBUG_LED
#define _DEBUG_LED_1
#define _DEBUG_LED_2
#endif

/******************************************************************************\
                          Macro definitions
\******************************************************************************/
sbit pin_LED_1 = P0^2;
sbit pin_LED_2 = P0^3;
sbit pin_LED_3 = P0^4;

#if 0
#define	LED_1_CONFIG()		do{ SFRADDR = P0_DRV; SFRDATA |= 0x03;SFRADDR = MFP_CTL0; SFRDATA &= 0xCF;}while(0)		//P02 
#define	LED_2_CONFIG()		do{ SFRADDR = P0_DRV; SFRDATA |= 0x08;SFRADDR = MFP_CTL0; SFRDATA &= 0x3F;}while(0)		//P03 
#define	LED_3_CONFIG()		do{ SFRADDR = P0_DRV; SFRDATA |= 0x20;SFRADDR = MFP_CTL1; SFRDATA &= 0xFC;}while(0)		//P04 
#endif

// LED端口定义
#define LEDR_ON()			pin_LED_2 = 1 			//LED_1_ON() p03
#define LEDR_OFF()			pin_LED_2 = 0 			//LED_1_OFF()
#define LEDR_TOG()			pin_LED_2 = !pin_LED_2 	//LED_1_RUN()

#define LEDG_ON()			pin_LED_1 = 1 			//LED_2_ON() p02
#define LEDG_OFF()			pin_LED_1 = 0 			//LED_2_OFF()
#define LEDG_TOG()			pin_LED_1 = !pin_LED_1 	//LED_2_RUN()

#define LEDY_ON()			pin_LED_3 = 1 			//LED_3_ON() p04
#define LEDY_OFF()			pin_LED_3 = 0 			//LED_3_OFF()
#define LEDY_TOG()			pin_LED_3 = !pin_LED_3 	//LED_3_RUN()

#define LED_PLUS_TIME 	1							//LED闪烁最小时间周期单位ms

/******************************************************************************\
                         Typedef definitions
\******************************************************************************/
//LED名称重定义，顺序和上方的宏定义保持一致
enum LED_ID
{
	LED_R,
	LED_G,
	LED_Y,
//	LED_B,
	LED_ID_MAX
};

//LED工作状态
enum LED_STATE
{
	LED_OFF,
	LED_ON,
	LED_FLASH,
//	LED_KEEP_FLASHING,
	LED_BREATHING_LIGHT
};

//LED控制结构体
typedef struct{

	uint8_t State[LED_ID_MAX];//LED	的工作状态
	uint8_t Flash_Duty[LED_ID_MAX];//LED占空比，即点亮时间
	uint8_t Flash_Period[LED_ID_MAX];//LED周期
	uint8_t Flash_Times[LED_ID_MAX];//闪烁次数
	uint8_t Timer[LED_ID_MAX];//时间计数 1ms计数一次
	
}TS_LED_INFO;

extern volatile uint8_t Display_RSta;		// 显示状态
extern volatile uint8_t Display_GSta;		// 显示状态
extern volatile uint8_t Display_YSta;		// 显示状态

/******************************************************************************\
                    Global variables and functions
\******************************************************************************/

/*
* 函数名称 : LED_Init
* 功能描述 : LED初始化
* 参    数 : NONE
* 返回值   : NONE
*/
/******************************************************************************/
//extern void LED_Init(void);
/******************************************************************************/

/*
* 函数名称 : LED_Clr
* 功能描述 : 熄灭全部LED
* 参    数 : NONE
* 返回值   : NONE
*/
/******************************************************************************/
extern void LED_Drv(void);
/******************************************************************************/
/*
* 函数名称 : LED_Set
* 功能描述 : LED工作方式设置
* 参    数 : State：工作方式       / Period:闪烁周期(单位ms) / Times:闪烁次数
* 返回值   : NONE
*/
/******************************************************************************/
extern void LED_Set(uint8_t LedId,uint8_t State,uint8_t Period,uint8_t Times);
/******************************************************************************/

// 外部操作接口;Fre:闪烁周期（ms）;n:闪烁次数
#define LED_R_ON()					LED_Set(LED_R, LED_ON, 0,0)
#define LED_R_OFF() 				LED_Set(LED_R, LED_OFF, 0,0)
#define LED_R_FLASH(Fre,n)			LED_Set(LED_R, LED_FLASH, Fre,n)
#define LED_R_KEEP_FLASHING(Fre)	LED_Set(LED_R, LED_FLASH, Fre,0xff)

#define LED_G_ON()					LED_Set(LED_G, LED_ON, 0,0)
#define LED_G_OFF() 				LED_Set(LED_G, LED_OFF, 0,0)
#define LED_G_FLASH(Fre,n)			LED_Set(LED_G, LED_FLASH, Fre,n)
#define LED_G_KEEP_FLASHING(Fre)	LED_Set(LED_G, LED_FLASH, Fre,0xff)

#define LED_Y_ON()					LED_Set(LED_Y, LED_ON, 0,0)
#define LED_Y_OFF() 				LED_Set(LED_Y, LED_OFF, 0,0)
#define LED_Y_FLASH(Fre,n)			LED_Set(LED_Y, LED_FLASH, Fre,n)
#define LED_Y_KEEP_FLASHING(Fre)	LED_Set(LED_Y, LED_FLASH, Fre,0xff)

#ifdef BREATHING_LIGHT

extern bit Breathing_Light_On;
/*
* 函数名称 : LED_Breathing_Light
* 功能描述 : LED呼吸灯驱动，需放在100us时间片中
* 参    数 : NONE
* 返回值   : NONE
*/
/******************************************************************************/
extern void LED_Breathing_Light(void);
/******************************************************************************/

#endif

#endif

#if DISPLAY_6PIN_188

extern uint8_t g_188_Num;

extern uint8_t Display_Ram_Tab[25];

extern void Display_Scan_6Pin_188(void);

extern void Display_Show_Num(uint8_t num);

extern void Display_Show_Vor(uint8_t earR_bat);

extern void Display_Show_Vol(uint8_t earL_bat);

//extern void Set_AllLed_Down(void);

extern void Set_NumLED_Down(void);
extern void Set_AllLed_Input(void);

#if 1
#define PIN0_IN()	do{SFRADDR = P0_OE;SFRDATA &= 0xDF;SFRADDR = P0_IE;SFRDATA |= 0x20;}while(0)				//Set P05 Input Enable.
#define PIN0_L()	do{SFRADDR = P0_OE;SFRDATA |= 0x20;P05 = 0;}while(0)				//Set P05 output low Enable.
#define PIN0_H()	do{SFRADDR = P0_OE;SFRDATA |= 0x20;P05 = 1;}while(0)				//Set P05 output high Enable.
#else
#define PIN0_IN()	do{SFRADDR = P1_OE;SFRDATA &= 0xFE;SFRADDR = P1_IE;SFRDATA |= 0x01;}while(0)				//Set P10 Input Enable.
#define PIN0_L()	do{SFRADDR = P1_OE;SFRDATA |= 0x01;P10 = 0;}while(0)				//Set P10 output low Enable.
#define PIN0_H()	do{SFRADDR = P1_OE;SFRDATA |= 0x01;P10 = 1;}while(0)				//Set P10 output high Enable.
#endif

#endif

#ifdef LED_188

#define PIN5_IN()	do{SFRADDR = P0_OE;SFRDATA &= 0xFE;SFRADDR = P0_IE;SFRDATA |= 0x01;}while(0)				//Set P00 Input Enable.设置输入功能前，需先关闭输出功能。
#define PIN4_IN()	do{SFRADDR = P0_OE;SFRDATA &= 0xFD;SFRADDR = P0_IE;SFRDATA |= 0x02;}while(0)				//Set P01 Input Enable.
#define PIN3_IN()	do{SFRADDR = P0_OE;SFRDATA &= 0xFB;SFRADDR = P0_IE;SFRDATA |= 0x04;}while(0)				//Set P02 Input Enable.
#define PIN2_IN()	do{SFRADDR = P0_OE;SFRDATA &= 0xF7;SFRADDR = P0_IE;SFRDATA |= 0x08;}while(0)				//Set P03 Input Enable.
#define PIN1_IN()	do{SFRADDR = P0_OE;SFRDATA &= 0xEF;SFRADDR = P0_IE;SFRDATA |= 0x10;}while(0)				//Set P04 Input Enable.
	
#define PIN5_L()	do{SFRADDR = P0_OE;SFRDATA |= 0x01;P00 = 0;}while(0)				//Set P00 output low Enable.
#define PIN4_L()	do{SFRADDR = P0_OE;SFRDATA |= 0x02;P01 = 0;}while(0)				//Set P01 output low Enable.
#define PIN3_L()	do{SFRADDR = P0_OE;SFRDATA |= 0x04;P02 = 0;}while(0)				//Set P02 output low Enable.
#define PIN2_L()	do{SFRADDR = P0_OE;SFRDATA |= 0x08;P03 = 0;}while(0)				//Set P03 output low Enable.
#define PIN1_L()	do{SFRADDR = P0_OE;SFRDATA |= 0x10;P04 = 0;}while(0)				//Set P04 output low Enable.
	
#define PIN5_H()	do{SFRADDR = P0_OE;SFRDATA |= 0x01;P00 = 1;}while(0)				//Set P00 output high Enable.
#define PIN4_H()	do{SFRADDR = P0_OE;SFRDATA |= 0x02;P01 = 1;}while(0)				//Set P01 output high Enable.
#define PIN3_H()	do{SFRADDR = P0_OE;SFRDATA |= 0x04;P02 = 1;}while(0)				//Set P02 output high Enable.
#define PIN2_H()	do{SFRADDR = P0_OE;SFRDATA |= 0x08;P03 = 1;}while(0)				//Set P03 output high Enable.
#define PIN1_H()	do{SFRADDR = P0_OE;SFRDATA |= 0x10;P04 = 1;}while(0)				//Set P04 output high Enable.


extern void Set_AllLed_Down(void);

#endif

#if DISPLAY_5PIN_188
extern uint8_t g_188_Num;
extern void LED_188_Init(void);

extern void LED_188_Drive(uint8_t Num);

#endif

#endif

