#include "HW_config.h"
#include "fatfs.h"
#include "ff.h"
#include "internal_flash.h"
#include "sdram_addr_map.h"
#include "file_cache.h"
#include "w25qxx.h"
#include "usb_host.h"
#include "usb_device.h"

//#define FLASH_SECTOR_SIZE 		 	0x00020000

#define FONT_24X24_PATH 	"HZK/HZK24S"

#define PIC_16X16_BMP_NUM		32
#define PIC_24X24_BMP_NUM		24
#define PIC_32X32_BMP_NUM		4

#define LCD_TOTAL_WIDTH 	800
#define LCD_LAYER1_ADDR 	0xC0000000

Bmp_Parameter_TypeDef pic_16x16_bmp_prm[PIC_16X16_BMP_NUM];
Bmp_Parameter_TypeDef pic_24x24_bmp_prm[PIC_24X24_BMP_NUM];
Bmp_Parameter_TypeDef pic_32x32_bmp_prm[PIC_32X32_BMP_NUM];
Bmp_Parameter_TypeDef pic_logo24_bmp_prm;
Bmp_Parameter_TypeDef pic_logo32_bmp_prm;

extern ApplicationTypeDef Appli_state;
//volatile BYTE ReadBuffer[256]={0};        /* 读缓冲区 */

void load_pic_logo_bmp_to_flash(void);

void pic_prm_init(void)
{
	unsigned int i = 0;
	
	for(i=0;i<PIC_16X16_BMP_NUM;i++){
		pic_16x16_bmp_prm[i].flash_address = FLASH_USER_PIC_16X16 + (FLASH_USER_PIC_16X16_UINT * i);
		pic_16x16_bmp_prm[i].pic_width = 16;
		pic_16x16_bmp_prm[i].pic_height = 16;
	}
	
	for(i=0;i<PIC_24X24_BMP_NUM;i++){
		pic_24x24_bmp_prm[i].flash_address = FLASH_USER_PIC_24X24 + (FLASH_USER_PIC_24X24_UINT * i);
		pic_24x24_bmp_prm[i].pic_width = 24;
		pic_24x24_bmp_prm[i].pic_height = 24;
	}
	
	for(i=0;i<PIC_32X32_BMP_NUM;i++){
		pic_32x32_bmp_prm[i].flash_address = FLASH_USER_PIC_32X32 + (FLASH_USER_PIC_32X32_UINT * i);
		pic_32x32_bmp_prm[i].pic_width = 32;
		pic_32x32_bmp_prm[i].pic_height = 32;
	}
	
	pic_logo24_bmp_prm.flash_address = FLASH_USER_PIC_LOGO24;
	pic_logo24_bmp_prm.pic_width = 90;
	pic_logo24_bmp_prm.pic_height = 24;
	
	pic_logo32_bmp_prm.flash_address = FLASH_USER_PIC_LOGO32;
	pic_logo32_bmp_prm.pic_width = 122;
	pic_logo32_bmp_prm.pic_height = 32;
}
/*
void erase_pic_16x16_lib_sector(void)
{
	unsigned int i = 0;
	
	for(i=0;i<FLASH_USER_PIC_16X16_SECTOR_NUM;i++)
		erase_internal_one_sector(FLASH_USER_PIC_16X16 + (FLASH_SECTOR_SIZE * i));
}

void erase_pic_24x24_lib_sector(void)
{
	unsigned int i;
	for(i=0;i<FLASH_USER_PIC_24X24_SECTOR_NUM;i++)
		erase_internal_one_sector(FLASH_USER_PIC_24X24 + (FLASH_SECTOR_SIZE * i));
}

void erase_pic_32x32_lib_sector(void)
{
	unsigned int i;
	for(i=0;i<FLASH_USER_PIC_32X32_SECTOR_NUM;i++)
		erase_internal_one_sector(FLASH_USER_PIC_32X32 + (FLASH_SECTOR_SIZE * i));
}
*/
void erase_pic_lib_sector(void)
{
	erase_internal_one_sector(FLASH_USER_PIC);
}

void erase_font_24x24_lib_sector(void)
{
	unsigned int i;
	for(i=0;i<FLASH_USER_FONT_24X24_SECTOR_NUM;i++)
		erase_internal_one_sector(FLASH_USER_FONT_24X24 + (FLASH_SECTOR_SIZE * i));
}

void mount_usb_file_sys(void)
{
	FRESULT f_res;                    /* 文件操做结果 */
	BYTE work[512];
	
	//在SD卡挂载文件系统，文件系统挂载时会对SD卡初始化
	f_res = f_mount(&USBHFatFS,(TCHAR const*)USBHPath,1);
	/*----------------------- 格式化测试 ---------------------------*/  
	/* 若是没有文件系统就格式化建立建立文件系统 */
	if(f_res == FR_NO_FILESYSTEM)
	{
	/* 格式化 */
		f_res=f_mkfs((TCHAR const*)USBHPath,FM_ANY,0,work,512);		//					
      
		if(f_res == FR_OK)
		{
			/* 格式化后，先取消挂载 */
			f_res = f_mount(NULL,(TCHAR const*)USBHPath,1);			
			/* 从新挂载	*/			
			f_res = f_mount(&USBHFatFS,(TCHAR const*)USBHPath,1);
		}
		else
		{
			// printf("《《格式化失败。》》\n");
			while(1);
		}
	}
	else if(f_res!=FR_OK)
	{
		// printf("！！SD卡挂载文件系统失败。(%d)\n",f_res);
		while(1);
	}
	else
	{
		//printf("》文件系统挂载成功，能够进行读写测试\n");
	}
}

int read_hzk_load_to_flash(void)
{
	unsigned int load_falsh_addr = 0;
	unsigned int load_sdram_addr = 0;
	int tSta = -3;
	
	uint32_t size;
	FIL file;													/* 文件对象 */
	
	//-------------------load flash---------------------------------------------------
	load_file_to_sdramcache(FONT_24X24_PATH,SDRAM_BUF_CACHE_ADDRESS, &tSta);
	f_open(&file, FONT_24X24_PATH, FA_OPEN_EXISTING | FA_READ);//get file.obj.objsize
	
	erase_font_24x24_lib_sector();
	load_falsh_addr = FLASH_USER_FONT_24X24;
	load_sdram_addr = SDRAM_BUF_CACHE_ADDRESS;
	HAL_FLASH_Unlock();
	for(size = 0;size < file.obj.objsize;(size+=32))
	{
		write_internal_flash(load_falsh_addr, load_sdram_addr);
		//W25QXX_Write(((unsigned char*)load_sdram_addr),load_falsh_addr,4096);
		load_falsh_addr+=32;
		load_sdram_addr+=32;
	}
	HAL_FLASH_Lock();
	
	/* 再也不读写，关闭文件 */
	f_close(&file);
	HAL_Delay(5);
	return tSta;
}

void load_pic_16x16_bmp(void)
{
	unsigned int x,y;
	
	char str_name[20] = "Bmp16/A000.bmp";
	
	for(x=0;x<PIC_16X16_BMP_NUM;x++)
	{
		LL_IWDG_ReloadCounter(IWDG1);
		for(y=0;y<14;y++)
		{
			pic_16x16_bmp_prm[x].name_str[y] = str_name[y];
		}
		
		pic_16x16_bmp_prm[x].start_address = SDRAM_PIC_16X16_ADDR_START + (x * SDRAM_PIC_16X16_MEM_UNIT);
		
		load_bmpfile_to_sdram(&pic_16x16_bmp_prm[x]);//load bmp file
		
		str_name[9] +=1;
		if(str_name[9] > '9')
		{
			str_name[9] = '0';
			str_name[8] +=1;
			if(str_name[8] > '9')
			{
				str_name[8] = '0';
				str_name[7] +=1;
			}
		}
	}
	
	LL_IWDG_ReloadCounter(IWDG1);
}

void load_pic_16x16_bmp_to_flash(void)
{
	unsigned int i = 0, size = 0;
	unsigned int load_falsh_addr = 0;
	unsigned int load_sdram_addr = 0;
	
	for(i=0;i<PIC_16X16_BMP_NUM;i++){
		pic_16x16_bmp_prm[i].flash_address = FLASH_USER_PIC_16X16 + (FLASH_USER_PIC_16X16_UINT * i);
	}
	
	//erase_pic_16x16_lib_sector();
	//HAL_FLASH_Unlock();
	for(i=0;i<PIC_16X16_BMP_NUM;i++){
		load_falsh_addr = pic_16x16_bmp_prm[i].flash_address;
		load_sdram_addr = pic_16x16_bmp_prm[i].start_address;
		
		for(size = 0;size < FLASH_USER_PIC_16X16_UINT;(size+=32))
		{
			write_internal_flash(load_falsh_addr, load_sdram_addr);
			//W25QXX_Write(((unsigned char*)load_sdram_addr),load_falsh_addr,4096);
			load_falsh_addr+=32;
			load_sdram_addr+=32;
		}
	}
	//load_pic_logo_bmp_to_flash();
	//HAL_FLASH_Lock();
}

void load_pic_24x24_bmp(void)
{
	unsigned int x,y;
	
	char str_name[20] = "Bmp24/B000.bmp";
	
	for(x=0;x<PIC_24X24_BMP_NUM;x++)
	{
		LL_IWDG_ReloadCounter(IWDG1);
		for(y=0;y<14;y++)
		{
			pic_24x24_bmp_prm[x].name_str[y] = str_name[y];
		}
		
		pic_24x24_bmp_prm[x].start_address = SDRAM_PIC_24X24_ADDR_START + (x * SDRAM_PIC_24X24_MEM_UNIT);
		
		load_bmpfile_to_sdram(&pic_24x24_bmp_prm[x]);//load bmp file
		
		str_name[9] +=1;
		if(str_name[9] > '9')
		{
			str_name[9] = '0';
			str_name[8] +=1;
			if(str_name[8] > '9')
			{
				str_name[8] = '0';
				str_name[7] +=1;
			}
		}
	}
	LL_IWDG_ReloadCounter(IWDG1);
}

void load_pic_24x24_bmp_to_flash(void)
{
	unsigned int i = 0, size = 0;
	unsigned int load_falsh_addr = 0;
	unsigned int load_sdram_addr = 0;
	
	for(i=0;i<PIC_24X24_BMP_NUM;i++){
		pic_24x24_bmp_prm[i].flash_address = FLASH_USER_PIC_24X24 + (FLASH_USER_PIC_24X24_UINT * i);
	}
	
	//erase_pic_24x24_lib_sector();
	//HAL_FLASH_Unlock();
	for(i=0;i<PIC_24X24_BMP_NUM;i++){
		load_falsh_addr = pic_24x24_bmp_prm[i].flash_address;
		load_sdram_addr = pic_24x24_bmp_prm[i].start_address;
		
		for(size = 0;size < FLASH_USER_PIC_24X24_UINT;(size+=32))
		{
			write_internal_flash(load_falsh_addr, load_sdram_addr);
			//W25QXX_Write(((unsigned char*)load_sdram_addr),load_falsh_addr,4096);
			load_falsh_addr+=32;
			load_sdram_addr+=32;
		}
	}
	//HAL_FLASH_Lock();
}

void load_pic_32x32_bmp(void)
{
	unsigned int x,y;
	
	char str_name[20] = "Bmp32/C000.bmp";
	
	for(x=0;x<PIC_32X32_BMP_NUM;x++)
	{
		LL_IWDG_ReloadCounter(IWDG1);
		for(y=0;y<14;y++)
		{
			pic_32x32_bmp_prm[x].name_str[y] = str_name[y];
		}
		
		pic_32x32_bmp_prm[x].start_address = SDRAM_PIC_32X32_ADDR_START + (x * SDRAM_PIC_32X32_MEM_UNIT);
		
		load_bmpfile_to_sdram(&pic_32x32_bmp_prm[x]);//load bmp file
		
		str_name[9] +=1;
		if(str_name[9] > '9')
		{
			str_name[9] = '0';
			str_name[8] +=1;
			if(str_name[8] > '9')
			{
				str_name[8] = '0';
				str_name[7] +=1;
			}
		}
	}
	LL_IWDG_ReloadCounter(IWDG1);
}

void load_pic_32x32_bmp_to_flash(void)
{
	unsigned int i = 0, size = 0;
	unsigned int load_falsh_addr = 0;
	unsigned int load_sdram_addr = 0;
	
	for(i=0;i<PIC_32X32_BMP_NUM;i++){
		pic_32x32_bmp_prm[i].flash_address = FLASH_USER_PIC_32X32 + (FLASH_USER_PIC_32X32_UINT * i);
	}
	
	//erase_pic_32x32_lib_sector();
	//HAL_FLASH_Unlock();
	for(i=0;i<PIC_32X32_BMP_NUM;i++){
		load_falsh_addr = pic_32x32_bmp_prm[i].flash_address;
		load_sdram_addr = pic_32x32_bmp_prm[i].start_address;
		
		for(size = 0;size < FLASH_USER_PIC_32X32_UINT;(size+=32))
		{
			write_internal_flash(load_falsh_addr, load_sdram_addr);
			//W25QXX_Write(((unsigned char*)load_sdram_addr),load_falsh_addr,4096);
			load_falsh_addr+=32;
			load_sdram_addr+=32;
		}
	}
	//HAL_FLASH_Lock();
}

void load_pic_logo_bmp(void)
{
	unsigned int y;
	
	char str_name[20] = "logo/logo24.bmp";
	
	LL_IWDG_ReloadCounter(IWDG1);
	for(y=0;y<15;y++)
	{
		pic_logo24_bmp_prm.name_str[y] = str_name[y];
	}
	
	pic_logo24_bmp_prm.start_address = SDRAM_PIC_LOGO24_ADDR_START;
	
	load_bmpfile_to_sdram(&pic_logo24_bmp_prm);//load bmp file
	
	str_name[9] = '0'+3;
	str_name[10]= '0'+2;
	
	for(y=0;y<15;y++)
	{
		pic_logo32_bmp_prm.name_str[y] = str_name[y];
	}
	
	pic_logo32_bmp_prm.start_address = SDRAM_PIC_LOGO32_ADDR_START;
	
	load_bmpfile_to_sdram(&pic_logo32_bmp_prm);//load bmp file

	LL_IWDG_ReloadCounter(IWDG1);
}

void load_pic_logo_bmp_to_flash(void)
{
	unsigned int i = 0, size = 0;
	unsigned int load_falsh_addr = 0;
	unsigned int load_sdram_addr = 0;
	
	//erase_pic_32x32_lib_sector();
	//HAL_FLASH_Unlock();
	load_falsh_addr = pic_logo24_bmp_prm.flash_address;
	load_sdram_addr = pic_logo24_bmp_prm.start_address;
	
	for(size = 0;size < FLASH_USER_PIC_LOGO24_UINT;(size+=32))
	{
		write_internal_flash(load_falsh_addr, load_sdram_addr);
		//W25QXX_Write(((unsigned char*)load_sdram_addr),load_falsh_addr,4096);
		load_falsh_addr+=32;
		load_sdram_addr+=32;
	}
	
	load_falsh_addr = pic_logo32_bmp_prm.flash_address;
	load_sdram_addr = pic_logo32_bmp_prm.start_address;
	
	for(size = 0;size < FLASH_USER_PIC_LOGO32_UINT;(size+=32))
	{
		write_internal_flash(load_falsh_addr, load_sdram_addr);
		//W25QXX_Write(((unsigned char*)load_sdram_addr),load_falsh_addr,4096);
		load_falsh_addr+=32;
		load_sdram_addr+=32;
	}
	//HAL_FLASH_Lock();
}

void load_pic_bmp_to_flash()
{
	erase_pic_lib_sector();
	HAL_FLASH_Unlock();
	load_pic_24x24_bmp_to_flash();
	load_pic_24x24_bmp_to_flash();
	load_pic_32x32_bmp_to_flash();
	load_pic_logo_bmp_to_flash();
	HAL_FLASH_Lock();
}

void load_pic_zone_on_full_screen(uint32_t pic_start_adress,uint32_t start_x,uint32_t start_y,Bmp_Parameter_TypeDef* zone_pic)
	//IIB on full pic
{
	uint32_t index_width = 0;
	uint32_t index_height= 0;
	
	uint32_t area_width	 = zone_pic->pic_width;
	uint32_t area_height = zone_pic->pic_height;
	
	uint32_t back_start_adress = pic_start_adress;
	//uint32_t surface_start_adress	= zone_pic->start_address;
	uint32_t surface_start_adress	= zone_pic->flash_address;
	
	back_start_adress = back_start_adress + (LCD_TOTAL_WIDTH * start_y * 4) + (start_x * 4);
	
	for(index_height = 0; index_height < area_height; index_height++)
	{
		for (index_width = 0; index_width < area_width; index_width++)
		{
			*(__IO uint32_t*) (back_start_adress) = *(__IO uint32_t *)surface_start_adress;
				
			back_start_adress+=4;  
			surface_start_adress+=4;
		}
		back_start_adress = back_start_adress + ((LCD_TOTAL_WIDTH - area_width) * 4);
	}
}

void load_pic_16x16_to_dispaly_layer(uint32_t start_x,uint32_t start_y,uint8_t pic_ord)
{
	if(pic_ord >= PIC_16X16_BMP_NUM)return;
	load_pic_zone_on_full_screen(LCD_LAYER1_ADDR,start_x,start_y,&pic_16x16_bmp_prm[pic_ord]);
}

void load_pic_24x24_to_dispaly_layer(uint32_t start_x,uint32_t start_y,uint8_t pic_ord)
{
	if(pic_ord >= PIC_24X24_BMP_NUM)return;
	load_pic_zone_on_full_screen(LCD_LAYER1_ADDR,start_x,start_y,&pic_24x24_bmp_prm[pic_ord]);
}

void load_pic_32x32_to_dispaly_layer(uint32_t start_x,uint32_t start_y,uint8_t pic_ord)
{
	if(pic_ord >= PIC_32X32_BMP_NUM)return;
	load_pic_zone_on_full_screen(LCD_LAYER1_ADDR,start_x,start_y,&pic_32x32_bmp_prm[pic_ord]);
}

void load_pic_logo24_to_dispaly_layer(uint32_t start_x,uint32_t start_y)
{
	load_pic_zone_on_full_screen(LCD_LAYER1_ADDR,start_x,start_y,&pic_logo24_bmp_prm);
}

void load_pic_logo32_to_dispaly_layer(uint32_t start_x,uint32_t start_y)
{
	load_pic_zone_on_full_screen(LCD_LAYER1_ADDR,start_x,start_y,&pic_logo32_bmp_prm);
}

void read_bmp_load_to_flash(void)
{
	load_pic_16x16_bmp();
	load_pic_24x24_bmp();
	load_pic_32x32_bmp();
	load_pic_logo_bmp();
	
	load_pic_bmp_to_flash();
}

void load_hzk_pic_task(void)//100ms
{
	static unsigned int time_count = 80;
	static unsigned int once_init = 1;
	
	if(time_count){
		time_count--;
		
		if(APPLICATION_READY == Appli_state){
			if(!(GPIOH->IDR & 0x0008)){//KEY
				gui_write_string24(20,0,0xff000000,0xffff0000,"LOADING_HZK_PIC");
				//mount_usb_file_sys();
				//read_hzk_load_to_flash();
				read_bmp_load_to_flash();
				gui_write_string24(20,0,0xff000000,0xffff0000,"LOAD_COMPLETE  ");
				time_count = 0;
			}else{
				pic_prm_init();
			}
		}
	}else{
		if(once_init){
			once_init = 0;
			
			usb_host_to_device();
		}
	}
}
