#include "stm32f10x.h"
#include "epaper.h" 
#include "picture.h"
ErrorStatus HSEStartUpStatus;

#define DELAYTIME 1500
#define xDot 200
#define yDot 200
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);

/************************************************
************************************************/
////////Partial refresh schematic////////////////

/////Y/// （0,0）          /---/(x,y)
					//                 /---/
					//                /---/  
					//x
					//
					//
//Tips//
/*When the electronic paper is refreshed in full screen, the picture flicker is a normal phenomenon, and the main function is to clear the display afterimage in the previous picture.
  When the local refresh is performed, the screen does not flash.*/
/*When you need to transplant the driver, you only need to change the corresponding IO. The BUSY pin is the input mode and the others are the output mode. */
						

/////////////////////main//////////////////////////////////////
int main(void)
{
unsigned char fen_L,fen_H,miao_L,miao_H;
	#ifdef DEBUG
	  debug();
	#endif
	RCC_Configuration();
	//GPIO setting
	GPIO_Configuration();
	 
	  //Full screen refresh
		EPD_HW_Init(); //Electronic paper initialization
	 	EPD_WhiteScreen_ALL(gImage_1); //Refresh the picture in full screen
    driver_delay_xms(4000);
//////////////////////Partial refresh digital presentation//////////////////////////////////////
		EPD_SetRAMValue_BaseMap(gImage_logo);  //Partial refresh background color 
	  EPD_Part_Init();//Local refresh initialization (the top left corner of the screen is the origin)
	  EPD_Dis_Part(0,32,gImage_num1,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num2,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num3,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num4,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num5,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num6,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num7,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num8,32,32); //x,y,DATA,Resolution 32*32
		EPD_Dis_Part(0,32,gImage_num9,32,32); //x,y,DATA,Resolution 32*32
	  driver_delay_xms(1000);	
//////////////////////Partial refresh time demo/////////////////////////////////////
		EPD_SetRAMValue_BaseMap(gImage_basemap); //Partial refresh background color  		
		EPD_Part_Init();//Local refresh initialization (the top left corner of the screen is the origin)

		for(fen_H=0;fen_H<6;fen_H++)
		{
		for(fen_L=0;fen_L<10;fen_L++)
		{
		for(miao_H=0;miao_H<6;miao_H++) 	
		{
		for(miao_L=0;miao_L<10;miao_L++)
		{
       //Black colon display
				EPD_Dis_Part_myself(64,40,Num[miao_L],         //x-A,y-A,DATA-A
														64,72,Num[miao_H],         //x-B,y-B,DATA-B
														64,112,gImage_numdot,       //x-C,y-C,DATA-C
														64,154,Num[fen_L],         //x-D,y-D,DATA-D
														64,186,Num[fen_H],32,64);	 //x-E,y-E,DATA-E,Resolution 32*64
													
        //Black colon hiding
				EPD_Dis_Part_myself(64,40,Num[miao_L],         //x-A,y-A,DATA-A
														64,72,Num[miao_H],         //x-B,y-B,DATA-B
														64,112,gImage_white,       //x-C,y-C,DATA-C
														64,154,Num[fen_L],         //x-D,y-D,DATA-D
														64,186,Num[fen_H],32,64);	 //x-E,y-E,DATA-E,Resolution 32*64
															
														if((fen_L==0)&&(miao_H==1)&&(miao_L==8))
														goto Clear;
			
		}
		}
		
		}
	}
		
 driver_delay_xms(1000);		
////////////////////////////////////////////////////////////////////////	
			//Clear screen
		Clear:
		EPD_HW_Init(); //Electronic paper initialization
		EPD_WhiteScreen_White(); //Show all white
		EPD_DeepSleep(); //Enter deep sleep,Sleep instruction is necessary, please do not delete!!!
		while(1);		

}
///////////////////////////////////////////////////////////





/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
 
  // 复位RCC时钟配置(用于调试模式) 
  RCC_DeInit();
 
  // 使能外部晶振 
  RCC_HSEConfig(RCC_HSE_ON);
  
  // 等待外部晶振稳定 
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    // 设置锁相环频率PLLCLK = 8MHz * 9 = 72 MHz 
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  }
  else {
    // 使能内部晶振 
    RCC_HSICmd(ENABLE);
    // 等待内置振荡器稳定 
    while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

    // 设置锁相环频率PLLCLK = 8MHz/2 * 16 = 64 MHz 
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_16);
  }

    // 使能FLASH的预取缓冲 
  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  //设置代码延时值,FLASH_Latency_2为两个延时周期
  FLASH_SetLatency(FLASH_Latency_2);
	
  //设置系统总线时钟
  RCC_HCLKConfig(RCC_SYSCLK_Div1); 

  //设置高速设备总线时钟，RCC_HCLK_Div1为系统时钟除以1
  RCC_PCLK2Config(RCC_HCLK_Div1); 

  //设置低速设备总线时钟，RCC_HCLK_Div2为系统时钟除以2
  RCC_PCLK1Config(RCC_HCLK_Div2);
  
  //使能锁相环倍频
  RCC_PLLCmd(ENABLE);
  
  // 等待锁相环倍频后的频率稳定 
  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  
  // 选择锁相环时钟为系统时钟源 
  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  
  // 等待设置完成 
  while(RCC_GetSYSCLKSource() != 0x08);
    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
            RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO,
            ENABLE);

}

/*******************************************************************************
* Function Name  :  GPIO_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
 	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE, ENABLE);//使能PD、E端口时钟
	  				     	
	
	 //CS-->PD8   SCK-->PD9  SDO--->PD10 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;		//Port configuration
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;		 		
	GPIO_Init(GPIOD, &GPIO_InitStructure);	  	
	
	
	
	 // D/C--->PE15	   RES-->PE14
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;		//Port configuration
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;		 		
	GPIO_Init(GPIOE, &GPIO_InitStructure);	  				     		
	
	// BUSY--->PE13
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;	//Pull down input
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
 	GPIO_Init(GPIOE, &GPIO_InitStructure);				//Initialize GPIO
	
	 //LED
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;		//Port configuration
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;		 		
	GPIO_Init(GPIOE, &GPIO_InitStructure);
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{ 
  //NVIC_InitTypeDef NVIC_InitStructure;
  ;
}


#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif






