#ifndef __LCDSETUP_H__
#define __LCDSETUP_H__
/*--------------------------------------------------------------------------------------
*  @file     LCDSetup.h
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.09.11
*  @brief    LCD驱动
---------------------------------------------------------------------------------------*/
#include "stdint.h"

#define H_VIEW		1							//横屏定义

#define RED				0xf800				//红色
#define GREEN			0x07e0				//绿色
#define BLUE			0x001f				//蓝色
#define WHITE			0xffff				//白色
#define BLACK			0x0000				//黑色
#define YELLOW		0xFFE0				//黄色
#define GRAY0			0xEF7D				//灰色0 3165 00110 001011 00101
#define GRAY1			0x8410      	//灰色1      00000 000000 00000
#define GRAY2			0x4208      	//灰色2  1111111111011111

#ifdef OLD_LCD
	#define LCD_CTRLA				GPIOA				//定义TFT数据端口为PA组
	#define LCD_CTRLB				GPIOB				//定义TFT数据端口为PB组
	#define LCD_CTRLC				GPIOC				//定义TFT数据端口为PC组
	#define LCD_CTRLD				GPIOD				//定义TFT数据端口为PD组
	#define LCD_LED        	GPIO_Pin_11 //MCU_PD11   		对应接液晶屏(或者模块)TFT --PIN_LED背光正极（背光可以由IO口提供电流，或者外接3.3V电压）
	#define LCD_CS        	GPIO_Pin_8	//MCU_PD8				对应接液晶屏(或者模块)TFT --CS
	#define LCD_SCL        	GPIO_Pin_13	//MCU_PD13			对应接液晶屏(或者模块)TFT --SCL
	#define LCD_SDA        	GPIO_Pin_12	//MCU_PD12 MOSI	对应接液晶屏(或者模块)TFT --SDA 
	#define LCD_SDO        	GPIO_Pin_14	//MCU_PD14 MISO	对应接液晶屏(或者模块)TFT --SDO 
	#define LCD_RS         	GPIO_Pin_11	//MCU_PD11			对应接液晶屏(或者模块)TFT --RS/DC
	#define LCD_RST         GPIO_Pin_9	//MCU_PD9				对应接液晶屏(或者模块)TFT --RST
#else
	#define LCD_CTRLD				GPIOD				//定义TFT数据端口为PD组
	#define LCD_ERD					GPIO_Pin_0	//MCU_PD0				对应接液晶屏(或者模块)TFT -ERD串行通信接地，并行通信使能或“读”
	#define LCD_RWR					GPIO_Pin_1	//MCU_PD1				对应接液晶屏(或者模块)TFT -RWR串行通信接地，并行通信使能或读/写或写
	#define LCD_A0         	GPIO_Pin_2	//MCU_PD2				对应接液晶屏(或者模块)TFT --A0=H:显示数据 A0=L:指令
	#define LCD_RSTB        GPIO_Pin_3	//MCU_PD3				对应接液晶屏(或者模块)TFT --RSTB低复位
	#define LCD_CSB        	GPIO_Pin_4	//MCU_PD4				对应接液晶屏(或者模块)TFT --CSB低片选
	#define LCD_SDA        	GPIO_Pin_8	//MCU_PD8 MOSI	对应接液晶屏(或者模块)TFT --SDA 
	#define LCD_SCL        	GPIO_Pin_9	//MCU_PD9 			对应接液晶屏(或者模块)TFT --SCL	
#endif

//#define LCD_CS_SET(x) LCD_CTRL->ODR=(LCD_CTRL->ODR&~LCD_CS)|(x ? LCD_CS:0)

#ifdef OLD_LCD
	//液晶控制口置1操作语句宏定义
	#define	LCD_CS_SET  	LCD_CTRLD->BSRR=LCD_CS    
	#define	LCD_RS_SET  	LCD_CTRLD->BSRR=LCD_RS    
	#define	LCD_SDA_SET  	LCD_CTRLD->BSRR=LCD_SDA    
	#define	LCD_SCL_SET  	LCD_CTRLD->BSRR=LCD_SCL    
	#define	LCD_RST_SET  	LCD_CTRLD->BSRR=LCD_RST    
	#define	LCD_LED_SET  	LCD_CTRLC->BSRR=LCD_LED    

	//液晶控制口置0操作语句宏定义
	#define	LCD_CS_CLR  	LCD_CTRLD->BRR=LCD_CS    
	#define	LCD_RS_CLR  	LCD_CTRLD->BRR=LCD_RS    
	#define	LCD_SDA_CLR  	LCD_CTRLD->BRR=LCD_SDA    
	#define	LCD_SCL_CLR  	LCD_CTRLD->BRR=LCD_SCL    
	#define	LCD_RST_CLR  	LCD_CTRLD->BRR=LCD_RST    
	#define	LCD_LED_CLR  	LCD_CTRLC->BRR=LCD_LED  
#else
	//液晶控制口置1操作语句宏定义
	#define	LCD_CS_SET  	LCD_CTRLD->BSRR=LCD_CSB    
	#define	LCD_RS_SET  	LCD_CTRLD->BSRR=LCD_A0    
	#define	LCD_SDA_SET  	LCD_CTRLD->BSRR=LCD_SDA    
	#define	LCD_SCL_SET  	LCD_CTRLD->BSRR=LCD_SCL    
	#define	LCD_RST_SET  	LCD_CTRLD->BSRR=LCD_RSTB   
	#define LCD_ERD_SET		LCD_CTRLD->BSRR=LCD_ERD
	#define LCD_RWR_SET		LCD_CTRLD->BSRR=LCD_RWR

	//液晶控制口置0操作语句宏定义
	#define	LCD_CS_CLR  	LCD_CTRLD->BRR=LCD_CSB    
	#define	LCD_RS_CLR  	LCD_CTRLD->BRR=LCD_A0    
	#define	LCD_SDA_CLR  	LCD_CTRLD->BRR=LCD_SDA    
	#define	LCD_SCL_CLR  	LCD_CTRLD->BRR=LCD_SCL    
	#define	LCD_RST_CLR  	LCD_CTRLD->BRR=LCD_RSTB   
	#define	LCD_ERD_CLR  	LCD_CTRLD->BRR=LCD_ERD
	#define LCD_RWR_CLR		LCD_CTRLD->BRR=LCD_RWR
#endif


#define LCD_DATAOUT(x) LCD_DATA->ODR=x; //数据输出
#define LCD_DATAIN     LCD_DATA->IDR;   //数据输入

#define LCD_8BIT_MODE   0	//定义液晶驱动为8位模式   1为使能 数据口使用高8位DP_H
#define LCD_16BIT_MODE  1	//定义液晶驱动为16位模式  1为使能

#ifdef H_VIEW	//定义屏的尺寸
				#define X_MAX_PIXEL	        320
				#define Y_MAX_PIXEL	        240
#else
				#define X_MAX_PIXEL	        240
				#define Y_MAX_PIXEL	        320 
#endif

#define LCD_WR_DATA(data){\
LCD_RS_SET;\
LCD_CS_CLR;\
LCD_DATAOUT(data);\
LCD_WR_CLR;\
LCD_WR_SET;\
LCD_CS_SET;\
} 

void LCDInit( void );
void LCDClear( uint16_t Color );  
void LCDPartClear( uint8_t startX , uint8_t endX , uint8_t y , uint8_t yCounts );
void LCDWriteIndex( uint8_t index );
void LCDWriteData( uint8_t Data );
void LCDWriteData16Bit( uint8_t DataH,uint8_t DataL );
void LCDSetRegion( uint16_t x_start,uint16_t y_start,uint16_t x_end,uint16_t y_end );
void LCDSetXY( uint16_t x,uint16_t y );

void DisplayHeader( uint8_t DataH,uint8_t DataL,uint16_t x );
void DisplayGraphicsGBK24( uint16_t x,uint8_t y,uint8_t *gph );
void DisplayGraphicsGBK16( uint16_t x,uint8_t y,uint8_t *gph );
void DisplayGraphicsGBK12( uint16_t x,uint8_t y,uint8_t *gph );
void DisplayGraphicsGBK8_16( uint16_t x,uint8_t y,uint8_t *gph );
void DisplayGraphicsGBK8_12( uint16_t x,uint8_t y,uint8_t *gph );
void DisplayGraphicsGBK8_8( uint16_t x,uint8_t y,uint8_t *gph );

#endif
