/***********************************************************************************
 * @Copyright (c)	:(C)2017, Qindao ieslab Co., Whn
 * @FileName   	  :stm32_lcd_driver.c
 * @Author       	:Team
 * @Version      	:V1.0
 * @Date         	:2017-8-25
 * @Description	:the function of the entity of system processor
 ************************************************************************************/
 
/* Includes ------------------------------------------------------------------*/
#include "UWater_lcd_driver.h"
#include "UWater_timer1uS_driver.h"

/* Private variables ---------------------------------------------------------*/
//LCD_HandleTypeDef hlcd; /*xixiSTM32相关的结构体*/	
static u8 s_display_num_state; //显示数字状态机状态变量
//static u8 s_display_fun_state; //显示固化字符状态机状态变量

////////////////////////////////xixi固化字符相关的状态变量TOP////////////////////////////////////
static u16 s_seg_flag; //接口调用标志位 //gaishanxiugai 数字显示扩大到10个, u8改为u16
static u8 s_seg_data[10]; //10个数码管数据缓存区  //gaishanxiugai
static u8 s_valve_state; //阀门状态
static u8 s_val_state; //电池电压状态
static u8 s_low_flow_state; //超低流量报警状态
static u8 s_alarm_state; //报警
static u8 s_failure_state; //故障
static u8 s_active_RF_state=0; //正在无线通信
static u8 s_active_RF_state_old=0;
//static u8 s_yuan_state; //元
//static u8 s_pay_money_state; //请缴费
//static u8 s_price_state; //单价
//static u8 s_lost_state; //剩余
//static u8 s_buy_state; //购入


static u8 s_fun_flag; //接口调用标志位
static FUN_DATA s_fun_data; //固化字符按位定义是否显示


//static u8 s_lcd_display_state = 0;       //lcd显示状态机状态变量
//static u16 s_seg_flag = 0;                //数据接口调用标志位
//static u8 s_fun_flag = 0;                //固化字符接口调用标志位
//static FUN_DATA s_fun_data;              //固化字符按位定义是否显示
//static u8 s_seg_data[11] = {0};           //8个数码管数据缓存区0 1 2 3 4 5 6 7 x x 10
//static u8 s_valve_state = 2;             //阀门状态
//static u8 s_val_state = 0;               //电池电压状态
//static u8 s_alarm_state = 0;             //报警状态
////static u8 s_active_GPRS_state = 0;       //正在GPRS通信状态
//static u8 s_please_pay_state = 0;        //请缴费-欠费状态
////////////////////////////////xixi固化字符相关的状态变量BOTTOM////////////////////////////////////


static u32 s_lcd_us_num = 0;             //lcd微秒级定时器计数值


static const u32 SEG_A_BUF_D4[10]={A0_COM4, A1_COM4, A2_COM4, A3_COM4, A4_COM4, A5_COM4, A6_COM4, A7_COM4, A8_COM4, A9_COM4};
static const u32 SEG_B_BUF_D3[10]={B0_COM3, B1_COM3, B2_COM3, B3_COM3, B4_COM3, B5_COM3, B6_COM3, B7_COM3, B8_COM3, B9_COM3};
static const u32 SEG_C_BUF_D2[10]={C0_COM2, C1_COM2, C2_COM2, C3_COM2, C4_COM2, C5_COM2, C6_COM2, C7_COM2, C8_COM2, C9_COM2};
static const u32 SEG_D_BUF_D1[10]={D0_COM1, D1_COM1, D2_COM1, D3_COM1, D4_COM1, D5_COM1, D6_COM1, D7_COM1, D8_COM1, D9_COM1};
static const u32 SEG_E_BUF_D2[10]={E0_COM2, E1_COM2, E2_COM2, E3_COM2, E4_COM2, E5_COM2, E6_COM2, E7_COM2, E8_COM2, E9_COM2};
static const u32 SEG_F_BUF_D4[10]={F0_COM4, F1_COM4, F2_COM4, F3_COM4, F4_COM4, F5_COM4, F6_COM4, F7_COM4, F8_COM4, F9_COM4};
static const u32 SEG_G_BUF_D3[10]={G0_COM3, G1_COM3, G2_COM3, G3_COM3, G4_COM3, G5_COM3, G6_COM3, G7_COM3, G8_COM3, G9_COM3};
/* Private function prototypes -----------------------------------------------*/
//xixiGPIO 的初始化
//static void LcdClockConfigure(void);
//static void LcdInitConfigure(LCD_HandleTypeDef *hlcd);
//static void LcdMspInit(LCD_HandleTypeDef* hlcd);
//static void LcdGpioInit(GPIO_TypeDef *GPIOx, LCD_GPIO_InitTypeDef *GPIO_Init);

static void LcdGpioInit(void);//xixi华大的GPIO初始化

void LcdDataSet(u8 lcddata_data, u8 num);
void LcdWriteData(u8 num,u32 data1,u32 data2,u32 data3,u32 data4);
//static void LcdClearData(u8 num);
static void LcdTimeOut(void);

void LcdDisplayLowFlowAlarm(u8 flag);
void LcdDisplayFailure(u8 flag);

/**************************************************************
Function:static void LcdGpioInit(LCD_HandleTypeDef* hlcd)
Description: GPIO初始化配置
Input：无
Return:无
Others:处于MainSpace，内部接口
**************************************************************/
//xixi 根据io需要修改
static void LcdGpioInit(void)
{
		Gpio_SetAnalogMode(GpioPortA, GpioPin9);  //COM0
		Gpio_SetAnalogMode(GpioPortA, GpioPin10); //COM1
		Gpio_SetAnalogMode(GpioPortA, GpioPin11); //COM2
		Gpio_SetAnalogMode(GpioPortA, GpioPin12); //COM3   

		Gpio_SetAnalogMode(GpioPortC, GpioPin4);  //S1-SEG15
		Gpio_SetAnalogMode(GpioPortC, GpioPin5);  //S2-SEG14
		Gpio_SetAnalogMode(GpioPortB, GpioPin0);  //S3-SEG13
		Gpio_SetAnalogMode(GpioPortB, GpioPin1);  //S4-SEG12
		Gpio_SetAnalogMode(GpioPortB, GpioPin2);  //S5-SEG11
		Gpio_SetAnalogMode(GpioPortE, GpioPin12); //S6-SEG51
		Gpio_SetAnalogMode(GpioPortE, GpioPin13); //S7-SEG50
		Gpio_SetAnalogMode(GpioPortE, GpioPin14); //S8-SEG49
		Gpio_SetAnalogMode(GpioPortB, GpioPin10); //S9-SEG10
		Gpio_SetAnalogMode(GpioPortB, GpioPin11); //S10-SEG9
		Gpio_SetAnalogMode(GpioPortB, GpioPin12); //S11-SEG8
		Gpio_SetAnalogMode(GpioPortB, GpioPin13); //S12-SEG7
		Gpio_SetAnalogMode(GpioPortB, GpioPin14); //S13-SEG6
		Gpio_SetAnalogMode(GpioPortB, GpioPin15); //S14-SEG5
		Gpio_SetAnalogMode(GpioPortD, GpioPin8);  //S15-SEG47
		
		Gpio_SetAnalogMode(GpioPortD, GpioPin9);  //S16-SEG46
		Gpio_SetAnalogMode(GpioPortD, GpioPin10); //S17-SEG45
		Gpio_SetAnalogMode(GpioPortD, GpioPin11); //S18-SEG44
		Gpio_SetAnalogMode(GpioPortC, GpioPin6);  //S19-SEG4
		Gpio_SetAnalogMode(GpioPortC, GpioPin7);  //S20-SEG3
		Gpio_SetAnalogMode(GpioPortC, GpioPin8);  //S21-SEG2
		Gpio_SetAnalogMode(GpioPortC, GpioPin9);  //S22-SEG1
		Gpio_SetAnalogMode(GpioPortA, GpioPin8);  //S23-SEG0

		Gpio_SetAnalogMode(GpioPortB, GpioPin3);  //VLCDH
		Gpio_SetAnalogMode(GpioPortB, GpioPin4);  //VLCD3
		Gpio_SetAnalogMode(GpioPortB, GpioPin5);  //VLCD2
		Gpio_SetAnalogMode(GpioPortB, GpioPin6);  //VLCD1
		
}

/* Functions -------------------------------------------------------*/
/*************************************************
Function: void LcdInit(void)
Description: Lcd初始化
Input：无
Return:无
Others:处于MainSpace，第一类接口：上电初始化
*************************************************/
void LcdInit(void)
{	
	stc_lcd_cfg_t LcdInitStruct;
	stc_lcd_segcom_t LcdSegCom;

	DDL_ZERO_STRUCT(LcdInitStruct);
	DDL_ZERO_STRUCT(LcdSegCom);
	
	LcdGpioInit();        ///LCD seg和com IO引脚设置
	
	Sysctrl_SetPeripheralGate(SysctrlPeripheralLcd,TRUE);           ///< 开启LCD时钟
  
  	LcdSegCom.u32Seg0_31 = 0xffff0000;                              ///< 配置LCD_POEN0寄存器 开启SEG0~SEG15
	LcdSegCom.stc_seg32_51_com0_8_t.seg32_51_com0_8 = 0xffffffff;   ///< 初始化LCD_POEN1寄存器 全部关闭输出端口
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Com0_3 = 0;          ///< 使能COM0~COM3
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Mux = 0;             ///< Mux=0,Seg32_35=0,BSEL=LcdBias3表示:选择外部电容工作模式
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg32_35 = 0x00;		///< Seg32_35 引脚做外部电容的连接引脚
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg44 = 0;
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg45 = 0;
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg46 = 0;
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg47 = 0;
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg49 = 0;
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg50 = 0;
	LcdSegCom.stc_seg32_51_com0_8_t.segcom_bit.Seg51 = 0;
	Lcd_SetSegCom(&LcdSegCom);                                     ///< LCD COMSEG端口配置
	
	//M0P_LCD->CR0_f.CONTRAST = 0x0;							   ///< 对比度调节(内部电阻模式有效)
	
	LcdInitStruct.LcdBiasSrc = 	LcdExtCap;                          ///< 电容分压模式，需要外部电路配合
	LcdInitStruct.LcdDuty = 	LcdDuty4;                           ///< 1/4duty
	LcdInitStruct.LcdBias = 	LcdBias3;                           ///< 1/3 BIAS
	LcdInitStruct.LcdCpClk =	LcdClk2k;                           ///< 电压泵时钟频率选择2kHz ??
	LcdInitStruct.LcdScanClk = 	LcdClk512hz;                        ///< LCD扫描频率选择        Hz
	LcdInitStruct.LcdMode = 	LcdMode1;                           ///< 选择模式0
	LcdInitStruct.LcdClkSrc = 	LcdRCL;                             ///< LCD时钟选择RCL
	LcdInitStruct.LcdEn   = 	LcdEnable;                          ///< 使能LCD模块
	Lcd_Init(&LcdInitStruct);
	
	Lcd_ClearDisp();             ///< 清屏

//xixi状态初始化部分跟源程序一致,不变. top

	//调用的标志位与状态初始化
	s_display_num_state=0;  //显示数字状态机状态变量
	s_seg_flag=0; //接口调用标志位
	s_valve_state=0;//阀门状态 
	s_val_state=0; //电池电压状态
	s_low_flow_state=0; //超低流量报警状态
	s_alarm_state=0; //报警
	s_failure_state=0; //故障
	s_active_RF_state=0; //正在无线通信
	// s_yuan_state=0; //元
	// s_pay_money_state=0; //请缴费
	// s_price_state=0; //单价
	// s_lost_state=0; //剩余
	// s_buy_state=0; //购入
	s_fun_flag=0; //接口调用标志位
	s_fun_data.word =0;//固化字符按位定义是否显示

//xixi状态初始化部分跟源程序一致,不变. bottom

}


/*************************************************
Function: void LcdMachineDriver(void)
Description:LCD驱动任务机
Input:无
Return:无
Others:处于MainSpace，第二类接口：工作接口
*************************************************/
void LcdMachineDriver(void)//工作接口
{
	switch (s_display_num_state)
	{
		case LCD_DISPLAY_S0:
		{
			if ((s_fun_flag)||(s_seg_flag))
			{
				s_display_num_state = LCD_DISPLAY_S1;
				s_lcd_us_num = Timer1usGetTick();			
			}
			break;
		}
		case LCD_DISPLAY_S1:
		{
			//if ((((hlcd.Instance->SR) & LCD_FLAG_UDR)==LCD_FLAG_UDR) == RESET)//xixi这个判断条件后面再看一下
			if(1)//xixi这里原来是判断RAM是否被写保护,现在华大平台没有这个机制了,所以先用if(1)了
			{ 
				if (s_fun_flag)//固化字符显示
			  {				
					if (s_fun_data.seg.T1 != _FUN_T1)
					{					
						_FUN_T1 = s_fun_data.seg.T1;
					}
					if (s_fun_data.seg.T2 != _FUN_T2)
					{
						_FUN_T2 = s_fun_data.seg.T2;
					}
					if (s_fun_data.seg.T3 != _FUN_T3)
					{
						_FUN_T3 = s_fun_data.seg.T3;
					}
					if (s_fun_data.seg.T4 != _FUN_T4)
					{
						_FUN_T4 = s_fun_data.seg.T4;
					}
					if (s_fun_data.seg.T5 != _FUN_T5)
					{
						_FUN_T5 = s_fun_data.seg.T5;
					}
					if (s_fun_data.seg.T6 != _FUN_T6)
					{
						_FUN_T6 = s_fun_data.seg.T6;
					}
					if (s_fun_data.seg.T7 != _FUN_T7)
					{
						_FUN_T7 = s_fun_data.seg.T7;
					}
					if (s_fun_data.seg.T8 != _FUN_T8)
					{
						_FUN_T8 = s_fun_data.seg.T8;
					}
					if (s_fun_data.seg.T10 != _FUN_T10)
					{
						_FUN_T10 = s_fun_data.seg.T10;
					}
					if (s_fun_data.seg.T11 != _FUN_T11)
					{
						_FUN_T11 = s_fun_data.seg.T11;
					}
					if (s_fun_data.seg.T12 != _FUN_T12)
					{
						_FUN_T12 = s_fun_data.seg.T12;
					}
					 if (s_fun_data.seg.T18 != _FUN_T18)
					 {
						_FUN_T18 = s_fun_data.seg.T18;
					 }
					 if (s_fun_data.seg.T19 != _FUN_T19)
					 {
						_FUN_T19 = s_fun_data.seg.T19;
					 }
					 if (s_fun_data.seg.T20 != _FUN_T20)
					 {
						_FUN_T20 = s_fun_data.seg.T20;
					 }
					 if (s_fun_data.seg.T21 != _FUN_T21)
					 {
						_FUN_T21 = s_fun_data.seg.T21;
					 }
					 if (s_fun_data.seg.T22 != _FUN_T22)
					 {
						_FUN_T22 = s_fun_data.seg.T22;
					 }
					 if (s_fun_data.seg.T23 != _FUN_T23)
					 {
						_FUN_T23 = s_fun_data.seg.T23;
					 }
					 if (s_fun_data.seg.T24 != _FUN_T24)
					 {
						_FUN_T24 = s_fun_data.seg.T24;
					 }
					 if (s_fun_data.seg.T25 != _FUN_T25)
					 {
						_FUN_T25 = s_fun_data.seg.T25;
					 }
					 if (s_fun_data.seg.T26 != _FUN_T26)
					 {
						_FUN_T26 = s_fun_data.seg.T26;
					 }
					 if (s_fun_data.seg.T27 != _FUN_T27)
					 {
						_FUN_T27 = s_fun_data.seg.T27;
					 }
					 if (s_fun_data.seg.T28 != _FUN_T28)
					 {
						_FUN_T28 = s_fun_data.seg.T28;
					 }
					s_fun_flag = 0;
			  }		
				if (s_seg_flag)//数码管显示
				{	
					if (s_seg_flag & 0x0001) 
					{
						s_seg_flag &= ~0x0001;
						LcdClearData(0);
						LcdDataSet(s_seg_data[0], 0);
					}
					if (s_seg_flag & 0x0002)
					{
						s_seg_flag &= ~0x0002;
						LcdClearData(1);
						LcdDataSet(s_seg_data[1], 1);
					}
					if (s_seg_flag & 0x0004) 
					{
						s_seg_flag &= ~0x0004;
						LcdClearData(2);
						LcdDataSet(s_seg_data[2], 2);
					}
					if (s_seg_flag & 0x0008)
					{
						s_seg_flag &= ~0x0008;
						LcdClearData(3);
						LcdDataSet(s_seg_data[3], 3);
					}
					if (s_seg_flag & 0x0010) 
					{
						s_seg_flag &= ~0x0010;
						LcdClearData(4);
						LcdDataSet(s_seg_data[4], 4);
					}
					if (s_seg_flag & 0x0020) 
					{
						s_seg_flag &= ~0x0020;
						LcdClearData(5);
						LcdDataSet(s_seg_data[5], 5);
					}
					if (s_seg_flag & 0x0040)
					{
						s_seg_flag &= ~0x0040;
						LcdClearData(6);
						LcdDataSet(s_seg_data[6], 6);
					}
					if (s_seg_flag & 0x0080)
					{
						s_seg_flag &= ~0x0080;
						LcdClearData(7);
						LcdDataSet(s_seg_data[7], 7);
					}
					if (s_seg_flag & 0x0100)
					{
						s_seg_flag &= ~0x0100;
						LcdClearData(8);
						LcdDataSet(s_seg_data[8], 8);
					}
					if (s_seg_flag & 0x0200)
					{
						s_seg_flag &= ~0x0200;
						LcdClearData(9);
						LcdDataSet(s_seg_data[9], 9);
					}
					s_seg_flag = 0;
				}
			  //hlcd.Instance->CLR = LCD_FLAG_UDD;//xixi
			  //hlcd.Instance->SR |= LCD_SR_UDR;
				s_display_num_state = LCD_DISPLAY_S2;	
				s_lcd_us_num = Timer1usGetTick();				
			}
		 	else //xixi这个超时不会进入了
			{
				LcdTimeOut();
			}
			break;
		}
		case LCD_DISPLAY_S2:
		{
			//if((((hlcd.Instance->SR) & LCD_FLAG_UDD)==LCD_FLAG_UDD) != RESET)//xixi这个判断条件后面再看一下
			//if(Lcd_GetItStatus() != FALSE) //xixi华大周工写法:获取LCD中断标志位INTF,但没开中断,获取的标志位始终为TRUE,相当于if(1)
			if(1)
			{   
				s_display_num_state = LCD_DISPLAY_S3;
			}
			else
			{
				LcdTimeOut();
			}
			break;
		}
		case LCD_DISPLAY_S3:
		{
			if ((s_fun_data.seg.T1 != _FUN_T1) || (s_fun_data.seg.T2 != _FUN_T2) || (s_fun_data.seg.T3 != _FUN_T3)	
			 || (s_fun_data.seg.T4 != _FUN_T4) || (s_fun_data.seg.T5 != _FUN_T5) || (s_fun_data.seg.T6 != _FUN_T6)	
			 || (s_fun_data.seg.T7 != _FUN_T7) || (s_fun_data.seg.T8 != _FUN_T8) 
			 || (s_fun_data.seg.T10 != _FUN_T10) || (s_fun_data.seg.T11 != _FUN_T11) || (s_fun_data.seg.T12 != _FUN_T12)
			 || (s_fun_data.seg.T18 != _FUN_T18) || (s_fun_data.seg.T19 != _FUN_T19) || (s_fun_data.seg.T20 != _FUN_T20)
			 || (s_fun_data.seg.T21 != _FUN_T21) || (s_fun_data.seg.T22 != _FUN_T22)
			 || (s_fun_data.seg.T23 != _FUN_T23) || (s_fun_data.seg.T24 != _FUN_T24) || (s_fun_data.seg.T25 != _FUN_T25)
			 || (s_fun_data.seg.T26 != _FUN_T26) || (s_fun_data.seg.T27 != _FUN_T27) || (s_fun_data.seg.T28 != _FUN_T28))	
			{
				s_fun_flag = 1;
			}			
			s_display_num_state = LCD_DISPLAY_S0;			
			break;
		}
		default:
		{
			break;
		}
	}
}

/************************************************************************************
Function: static void LcdWriteData(u8 num,u32 data1,u32 data2,u32 data3,u32 data4)
Description:写LCD显示寄存器
Input:u8 num,u32 data1,u32 data2,u32 data3,u32 data4
      num取值范围0~9代表数字段10个位置。
Return:无
Others:内部接口
************************************************************************************/
//3ABCD,4ABCDEFG,8ABCDEFG,9ABCDEFG
void LcdWriteData(u8 num,const u32 data1,const u32 data2,const u32 data3, const u32 data4)
{    
	if(data1&0X0000FFFF)//判断是否有低位seg
	{
		M0P_LCD->RAM0 |= (data1&0x0000FFFF);
	}
	if(data2&0X0000FFFF)//判断是否有低位seg
	{
	    M0P_LCD->RAM1 |= (data2&0x0000FFFF);
	}
	if(data3&0X0000FFFF)//判断是否有低位seg
	{
		M0P_LCD->RAM2 |= (data3&0x0000FFFF);
	}
	if(data4&0X0000FFFF)//判断是否有低位seg
	{
		M0P_LCD->RAM3 |= (data4&0x0000FFFF);
	}
	
	if(data1&0XFFFF0000)//判断是否有高位seg
	{
		M0P_LCD->RAM8 |= (data1&0xFFFF0000)>>4;
	}
	if(data2&0XFFFF0000)//判断是否有高位seg
	{
		M0P_LCD->RAM9 |= (data2&0xFFFF0000)>>4;
	}
	if(data3&0XFFFF0000)//判断是否有高位seg
	{
		M0P_LCD->RAMA |= (data3&0xFFFF0000)>>4;
	}
	if(data4&0XFFFF0000)//判断是否有高位seg
	{
		M0P_LCD->RAMB |= (data4&0xFFFF0000)>>4;
	}
}

/**************************************************************
Function: static void LcdDataSet(u8 lcddata_data, u8 num)
Description:字模显示方法
Input:u8 lcddata_data, u8 num
Return:无
Others:内部接口
**************************************************************/

void LcdDataSet(u8 lcddata_data, u8 num)
{
	switch (lcddata_data)
	{
		case DIS_0:
		{	//0-ABCDEF
			LcdWriteData(num, SEG_D_BUF_D1[num], (SEG_C_BUF_D2[num]|SEG_E_BUF_D2[num]), SEG_B_BUF_D3[num], (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_1:
		{////1-BC
			LcdWriteData(num, 0, SEG_C_BUF_D2[num], SEG_B_BUF_D3[num], 0);
			break;
		}
		case DIS_2:
		{//2-ABDEG
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_E_BUF_D2[num], (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), SEG_A_BUF_D4[num]);
			break;
		}
		case DIS_3:
		{//3-ABCDG
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_C_BUF_D2[num], (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), SEG_A_BUF_D4[num]);
			break;
		}
		case DIS_4:
		{//4-BCFG
			LcdWriteData(num, 0, SEG_C_BUF_D2[num], (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), SEG_F_BUF_D4[num]);
			break;
		}
		case DIS_5:
		{//5-ACDFG
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_C_BUF_D2[num], SEG_G_BUF_D3[num], (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_6:
		{//6-ACDEFG
			LcdWriteData(num, SEG_D_BUF_D1[num], (SEG_C_BUF_D2[num]|SEG_E_BUF_D2[num]), SEG_G_BUF_D3[num], (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_7:
		{//7-ABC
			LcdWriteData(num, 0, SEG_C_BUF_D2[num], SEG_B_BUF_D3[num], SEG_A_BUF_D4[num]);
			break;
		}
		case DIS_8:
		{//8-ABCDEFG
			LcdWriteData(num, SEG_D_BUF_D1[num], (SEG_C_BUF_D2[num]|SEG_E_BUF_D2[num]), (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_9:
		{//9-ABCDFG
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_C_BUF_D2[num], (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
	
		case DIS_A:
		{//A-ABCDFG
			LcdWriteData(num, 0, (SEG_C_BUF_D2[num]|SEG_E_BUF_D2[num]), (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_B:
		{//B-CDEFG
			LcdWriteData(num, SEG_D_BUF_D1[num], (SEG_C_BUF_D2[num]|SEG_E_BUF_D2[num]), SEG_G_BUF_D3[num], SEG_F_BUF_D4[num]);
			break;
		}
		case DIS_C:
		{//C-ADEF
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_E_BUF_D2[num], 0, (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_D:
		{//D-BCDEG
			LcdWriteData(num, SEG_D_BUF_D1[num], (SEG_C_BUF_D2[num]|SEG_E_BUF_D2[num]), (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), 0);
			break;
		}
		case DIS_E:
		{//E-ADEFG
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_E_BUF_D2[num], SEG_G_BUF_D3[num], (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_F:
		{//F-AEFG
			LcdWriteData(num, 0, SEG_E_BUF_D2[num], SEG_G_BUF_D3[num], (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_P:
		{//P-ABEFG
			LcdWriteData(num, 0, SEG_E_BUF_D2[num], (SEG_B_BUF_D3[num]|SEG_G_BUF_D3[num]), (SEG_A_BUF_D4[num]|SEG_F_BUF_D4[num]));
			break;
		}
		case DIS_t:
		{//t-DEFG
			LcdWriteData(num, SEG_D_BUF_D1[num], SEG_E_BUF_D2[num], SEG_G_BUF_D3[num], SEG_F_BUF_D4[num]);
			break;
		}
		case DIS_HG:
		{//'-'-G
			LcdWriteData(num, 0, 0, SEG_G_BUF_D3[num], 0);
			break;
		}
		case DIS_BLANK:
		{
			LcdClearData(num);
			break;
		}
		default:
		{
			break;
		}
	}
}


/*************************************************
Function: static void LcdClearData(u8 num)
Description:清LCD显示寄存器
Input:u8 num  num取值范围0~9代表数字段10个位置。
Return:无
Others:内部接口
*************************************************/
//3ABCD,4ABCDEFG,8ABCDEFG,9ABCDEFG
 void LcdClearData(u8 num)
{
	if((num==3)||(num==7)||(num==8))//4\8\9都是高位SEG
	{
		M0P_LCD->RAM8 &= ~(SEG_D_BUF_D1[num]>>4);
		M0P_LCD->RAM9 &= ~((SEG_C_BUF_D2[num]>>4) | (SEG_E_BUF_D2[num]>>4));
		M0P_LCD->RAMA &= ~((SEG_B_BUF_D3[num]>>4) | (SEG_G_BUF_D3[num]>>4));
		M0P_LCD->RAMB &= ~((SEG_A_BUF_D4[num]>>4) | (SEG_F_BUF_D4[num]>>4));
	}
	else if((num==2))
	{
		M0P_LCD->RAMB &= ~(SEG_A_BUF_D4[num]>>4);
		M0P_LCD->RAMA &= ~(SEG_B_BUF_D3[num]>>4);
		M0P_LCD->RAM9 &= ~(SEG_C_BUF_D2[num]>>4);
		M0P_LCD->RAM8 &= ~(SEG_D_BUF_D1[num]>>4);		
		M0P_LCD->RAM1 &= ~(SEG_E_BUF_D2[num]);
		M0P_LCD->RAM3 &= ~(SEG_F_BUF_D4[num]);
		M0P_LCD->RAM2 &= ~(SEG_G_BUF_D3[num]);	
	}
	else
	{
		M0P_LCD->RAM0 &= ~(SEG_D_BUF_D1[num]);
		M0P_LCD->RAM1 &= ~(SEG_C_BUF_D2[num] | SEG_E_BUF_D2[num]);
		M0P_LCD->RAM2 &= ~(SEG_B_BUF_D3[num] | SEG_G_BUF_D3[num]);
		M0P_LCD->RAM3 &= ~(SEG_A_BUF_D4[num] | SEG_F_BUF_D4[num]);
	}  
}
/*******************************************************/
/*******************************************************/
/********************后面的代码都是逻辑性的*************/
/*******************************************************/
/*******************************************************/


/****************************************************
Function: void LcdDisplayData(u8 position, u8 data)
Description:LCD显示数字
Input:u8 position：数码管位置  u8 data：要显示的数据
Return:无
Others:处于MainSpace，第二类接口：工作接口
****************************************************/
void LcdDisplayData(u8 position, u8 data)
{
	 if (position < 10)//gaishanxiugai
    {
        s_seg_flag |= (1 << position);
        if ( data < DIS_ALL_NUM)
        {
            s_seg_data[position] = data;
        }
        else
        {
            s_seg_data[position] = 19;
        }
    }
}

/*************************************************
Function: u8 LcdGetDisplayData(u8 position)
Description:获取LCD当前显示的数字
Input:u8 position：获取数码管显示数字的位置
Return:当前显示的数字
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
u8 LcdGetDisplayData(u8 position)
{
	if (position < 10)//gaishanxiugai
	{
		return s_seg_data[position];
	}
	else
	{
		return 0xEE;
	}
}

/*************************************************
Function: void LcdDisplayFun(u32 fun)
Description:lcd显示固化字符
Input:u32 fun：按位定义的固化字符
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayFun(u32 fun)
{
    s_fun_flag = 1;
    if (s_valve_state == 0)
    {
        s_fun_data.word = (fun &(~FUN_VALVE_CLOSE)); //阀门开（默认阀门开）
       
    }
    else if (s_valve_state == 1)
    {
        
      s_fun_data.word =fun | FUN_VALVE_CLOSE;//阀门关

    }
//    else
//    {
//        s_fun_data.word = fun & (~FUN_VALVEOPEN);
//        s_fun_data.word &= (~FUN_VALVECLOSE);
//    }
    if (s_val_state)
    {
        s_fun_data.word |= FUN_BATTERY_LOW ; //欠压标志位
    }
    else
    {
        s_fun_data.word &= (~FUN_BATTERY_LOW );
    }
    if (s_low_flow_state)
    {
        s_fun_data.word |= FUN_FLOW_LOW ; //超低流量标志位
    }
    else
    {
        s_fun_data.word &= (~FUN_FLOW_LOW );
    }
    if (s_alarm_state)
    {
        s_fun_data.word |= FUN_ALARM; //报警
    }
    else
    {
        s_fun_data.word &= (~FUN_ALARM);
    }
    if (s_failure_state)
    {
        s_fun_data.word |= FUN_MALFUNCTION; //故障
    }
    else
    {
        s_fun_data.word &= (~FUN_MALFUNCTION);
    }
    if (s_active_RF_state)
    {
        s_fun_data.word |= FUN_GPRSING ; //正在无线活跃期
    }
    else
    {
        s_fun_data.word &= (~FUN_GPRSING );
    }
   /* if (s_yuan_state)
    {
        s_fun_data.word |= FUN_YUAN; //元
    }
    else
    {
        s_fun_data.word &= (~FUN_YUAN);
    }
    if (s_pay_money_state)
    {
        s_fun_data.word |= FUN_JF; //请缴费
    }
    else
    {
        s_fun_data.word &= (~FUN_JF);
    }
    if (s_price_state)
    {
        s_fun_data.word |= FUN_DJ ; //单价
    }
    else
    {
        s_fun_data.word &= (~FUN_DJ );
    }
    if (s_lost_state)
    {
        s_fun_data.word |= FUN_SY; //剩余
    }
    else
    {
        s_fun_data.word &= (~FUN_SY);
    }
    if (s_buy_state)
    {
        s_fun_data.word |= FUN_GR; //购入
    }
    else
    {
        s_fun_data.word &= (~FUN_GR);
    }*/
}

/*************************************************
Function: u32 LcdGetDisplayFun(void)
Description:获取lcd当前显示的固化字符
Input:无
Return:当前显示的字符
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
u32 LcdGetDisplayFun(void)
{
	return s_fun_data.word;
}

/*************************************************
Function: void LcdDisplayAll(void)
Description:LCD全显
Input:无
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayAll(void)
{
	s_fun_flag = 1;
	s_fun_data.word = 0x0FFFFFFF;
	LcdDisplayData(0, DIS_8); 
	LcdDisplayData(1, DIS_8); 
	LcdDisplayData(2, DIS_8); 
	LcdDisplayData(3, DIS_8); 
	LcdDisplayData(4, DIS_8); 
	LcdDisplayData(5, DIS_8); 
	LcdDisplayData(6, DIS_8); 
	LcdDisplayData(7, DIS_8); 
	LcdDisplayData(8, DIS_8); //gaishanxiugai
	LcdDisplayData(9, DIS_8); 
}


/*************************************************
Function: void LcdClearAll(void)
Description:LCD清屏
Input:无
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdClearAll(void)
{
	s_fun_flag = 1;
	s_fun_data.word = 0;
	LcdDisplayData(0, DIS_BLANK); 
	LcdDisplayData(1, DIS_BLANK); 
	LcdDisplayData(2, DIS_BLANK); 
	LcdDisplayData(3, DIS_BLANK); 
	LcdDisplayData(4, DIS_BLANK); 
	LcdDisplayData(5, DIS_BLANK); 
	LcdDisplayData(6, DIS_BLANK); 
	LcdDisplayData(7, DIS_BLANK); 	
	LcdDisplayData(8, DIS_BLANK); 	//gaishanxiugai
	LcdDisplayData(9, DIS_BLANK); 	
}

/*************************************************
Function: u8 LcdIfSleep(void)
Description:LCD是否允许休眠
Input:无
Return:TRUE允许；FALSE不允许
Others:处于Mainspace第三类接口：休眠前接口
*************************************************/
u8 LcdIfSleep(void)//休眠前接口
{
	if ( s_display_num_state == 0 && s_fun_flag == 0 && s_seg_flag == 0)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

/*************************************************
Function: void LcdPreSleep(void)
Description:LCD休眠前处理
Input:无
Return:无
Others:处于Mainspace第三类接口：休眠前接口
*************************************************/
void LcdPreSleep(void)
{
    //nothing
}

/*************************************************
Function: void LcdWakeSleep(void)
Description:LCD唤醒后处理
Input:无
Return:无
Others:处于Mainspace，第四类接口：唤醒接口
*************************************************/
void LcdWakeSleep(void)//唤醒后接口
{
    //nothing
}

/*************************************************
Function: void LcdDisplayValve(u8 flag)
Description:lcd显示阀门状态
Input:u8 flag：1关阀；0开阀 ；其它默认不显示
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayValve(u8 flag)
{
	s_fun_flag = 1;
	if (flag == 1)
	{
		s_valve_state = 1; //T7:阀门关
		s_fun_data.seg.T7 = 1;
	}
	else 
	{
		s_valve_state = 0; //T7:阀门开
		s_fun_data.seg.T7 = 0;
	}
	
}

/*************************************************
Function: void LcdDisplayVol(u8 flag)
Description:lcd显示换电池
Input:u8 flag：1欠压；0正常
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayVol(u8 flag)
{
	s_fun_flag = 1;
	if (flag)
	{
		s_val_state = 1;
		s_fun_data.seg.T5 = 1;
	}
	else
	{
		s_val_state = 0;
		s_fun_data.seg.T5 = 0;
	}
}

/*************************************************
Function: void LcdDisplayAlarm(u8 flag)
Description:lcd显示报警
Input:u8 flag：1报警；0正常
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayAlarm(u8 flag)
{
	s_fun_flag = 1;
	if (flag)
	{
		s_alarm_state = 1;
		s_fun_data.seg.T1 = 1;
	}
	else
	{
		s_alarm_state = 0;
		s_fun_data.seg.T1 = 0;
	}
}
/*************************************************
Function: void LcdDisplayLowFlowAlarm(u8 flag)
Description:lcd 低流量检测
Input:u8 flag：1报警；0正常
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayLowFlowAlarm(u8 flag)
 {
  s_fun_flag=1;
	 if(flag)
	 {
	  s_low_flow_state=1;
		s_fun_data.seg.T4 =1;
		 
	 }
	 else
	 {
	 s_low_flow_state=0;
		s_fun_data.seg .T4 =0;
	 }
 }
 /*************************************************
Function: void LcdDisplayFailure(u8 flag)
Description:lcd 故障检测
Input:u8 flag：1报警；0正常
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
 void LcdDisplayFailure(u8 flag)
 {
   s_fun_flag=1;
	 if(flag)
	 {
	   s_failure_state=1;
		 s_fun_data.seg .T2=1;
	 }
	 else
	 {
	  s_failure_state=0;
		 s_fun_data.seg.T2=0;
	 }
 }
 

/*************************************************
Function: void LcdDisplayActiveGPRS(u8 flag)
Description:lcd显示正在GPRS通信
Input:u8 flag：1正在通信；0正常
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
void LcdDisplayActiveGPRS(u8 flag)
{
	s_active_RF_state = flag;
	if(s_active_RF_state_old != s_active_RF_state)
	{
		s_active_RF_state_old = s_active_RF_state;
		s_fun_flag = 1;
		if (flag)
		{
			s_active_RF_state = 1;
			s_fun_data.seg.T3 = 1;
		}
		else
		{
			s_active_RF_state = 0;
			s_fun_data.seg.T3 = 0;
		}
	}
}
/*************************************************
Function: void LcdDisplayYuan(u8 flag)
Description:lcd显示汉字“元”
Input:u8 flag：None
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
/*void LcdDisplayYuan(u8 flag)
{
    s_fun_flag = 1;
    if (flag)
    {
        s_yuan_state = 1;
        s_fun_data.seg.T13 = 1;
    }
    else
    {
        s_yuan_state = 0;
        s_fun_data.seg.T13 = 0;
    }
}
*/
/*************************************************
Function: void LcdDisplayPrice(u8 flag)
Description:lcd显示汉字“单价”
Input:u8 flag：None
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
/*void LcdDisplayPrice(u8 flag)
{
	s_fun_flag = 1;
    if (flag)
    {
        s_price_state = 1;
        s_fun_data.seg .T15 = 1;
    }
    else
    {
        s_price_state = 0;
        s_fun_data.seg .T15= 0;
    }
	}
*/
/*************************************************
Function: void LcdDisplayPleasePay(u8 flag)
Description:lcd显示请缴费
Input:u8 flag：1请缴费；0正常
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
/*void LcdDisplayPleasePay(u8 flag)
{
	s_fun_flag = 1;
	if (flag)
	{
		s_pay_money_state= 1;
		s_fun_data.seg.T17 = 1;
	}
	else
	{
		s_pay_money_state = 0;
		s_fun_data.seg.T17 = 0;
	}
}
*/
/*************************************************
Function:   void LcdDisplaySurplus(u8 flag)
Description:lcd显示汉字“剩余”
Input:u8 flag：None
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
/*void LcdDisplaySurplus(u8 flag)
{
 s_fun_flag = 1;
    if (flag)
    {
        s_lost_state = 1;
        s_fun_data.seg.T14 = 1;
    }
    else
    {
        s_lost_state = 0;
        s_fun_data.seg.T14 = 0;
    }
}*/
/*************************************************
Function:   void LcdDisplayBuy(u8 flag)
Description:lcd显示汉字“购入”
Input:u8 flag：None
Return:无
Others:处于Mainspace，第二类接口：工作接口
*************************************************/
/*void LcdDisplayBuy(u8 flag)
{
    s_fun_flag = 1;
    if (flag)
    {
        s_buy_state = 1;
        s_fun_data.seg.T16 = 1;
    }
    else
    {
        s_buy_state = 0;
        s_fun_data.seg.T16 = 0;
    }
}
*/
/*************************************************
Function: void LcdDisplayEnable(void)
Description:使能LCD显示功能
Input:无
Return:无
Others:处于MainSpace，第二类接口：工作接口
*************************************************/
//xixi按新平台方法
void LcdDisplayEnable(void)
{
  M0P_LCD->CR0_f.EN = LcdEnable;//打开LCD
}

/*************************************************
Function: void LcdDisplayDisable(void)
Description:关闭LCD显示功能
Input:无
Return:无
Others:处于MainSpace，第二类接口：工作接口
*************************************************/
//xixi按新平台方法
void LcdDisplayDisable(void)
{
  M0P_LCD->CR0_f.EN = LcdDisable;//关闭LCD
}


/*******************************************************
Function: static void LcdTimeOut(void)
Description:LCD超时处理函数
Input:void
Return:无
Others:内部接口
*******************************************************/
static void LcdTimeOut(void)
{   
	if ((Timer1usGetTick() - s_lcd_us_num) > LCD_TIMEOUT_VALUE)
	{
		s_fun_flag = 0;	
		s_seg_flag = 0;	
		s_display_num_state = LCD_DISPLAY_S0;					
	}
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

