#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "sram.h"
#include "malloc.h"
#include "ILI93xx.h"
#include "led.h"
#include "key.h"
#include "timer.h"
#include "touch.h"
#include "beep.h"
#include "GUI.h"
#include "DIALOG.h"
#include "mytext.h"
#include "ff.h"
#include "exfuns.h"
#include "sdio_sdcard.h"
#include "w25qxx.h"
#include "fontupd.h"
#include "includes.h"
#include "EmWinHZFont.h"
#include "jpegdisplay.h"
#include "bmpdisplay.h"
#include "WM.h"

//ALIENTEK 探索者STM32F407开发板 
//EMWIN5.22 JPEG图片显示实验
//技术支持：www.openedv.com
//广州市星翼电子科技有限公司

//START任务
//设置任务的优先级
#define START_TASK_PRIO				0
//任务堆栈大小 
#define START_STK_SIZE			  128
//任务堆栈
OS_STK	START_TASK_STK[START_STK_SIZE];
//start_task任务
void start_task(void *pdata);

//TOUCH任务
//设置任务优先级
#define TOUCH_TASK_PRIO				1
//任务堆栈大小
#define TOUCH_STK_SIZE				128
//任务堆栈
OS_STK TOUCH_TASK_STK[TOUCH_STK_SIZE];
//touch任务
void touch_task(void *pdata);

//按键任务
//设置任务优先级
#define KEY_TASK_PRIO				2
//任务堆栈大小
#define KEY_STK_SIZE				512
//任务堆栈
OS_STK	KEY_TASK_STK[KEY_STK_SIZE];
//key任务
void key_task(void *pdata);

//LED0任务
//设置任务优先级
#define LED0_TASK_PRIO 				3
//任务堆栈大小
#define LED0_STK_SIZE				64
//任务堆栈
OS_STK LED0_TASK_STK[LED0_STK_SIZE];
//led0任务
void led0_task(void *pdata);

//TOUCH任务
//设置任务优先级
#define FONTUPDATA_TASK_PRIO		4
//任务堆栈大小
#define FONTUPDATA_STK_SIZE			128
//任务堆栈
OS_STK FONTUPDATA_TASK_STK[FONTUPDATA_STK_SIZE];
//touch任务
void fontupdata_task(void *pdata);

//EMWINDEMO任务
//设置任务优先级
#define EMWINDEMO_TASK_PRIO			5
//任务堆栈大小
#define EMWINDEMO_STK_SIZE			2048
//任务堆栈
OS_STK EMWINDEMO_TASK_STK[EMWINDEMO_STK_SIZE];
//emwindemo_task任务
void emwin_maintask(void *pdata);

int main(void)
{
	delay_init(168);       	//延时初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);	 	//中断分组配置
	uart_init(115200);    	//串口波特率设置
	TFTLCD_Init();			//初始化LCD
	KEY_Init();				//初始化按键
	BEEP_Init();			//蜂鸣器初始化
	LED_Init();   			//LED初始化
	FSMC_SRAM_Init(); 		//SRAM初始化
	
	mem_init(SRAMIN); 		//内部RAM初始化
	mem_init(SRAMEX); 		//外部RAM初始化
	mem_init(SRAMCCM);		//CCM初始化
		
	exfuns_init();			//为fatfs文件系统分配内存
	f_mount(fs[0],"0:",1);	//挂载SD卡
	f_mount(fs[1],"1:",1);	//挂载FLASH
	
	while(SD_Init())	//检测SD卡
	{
		LCD_ShowString(30,90,200,16,16,"SD Card Failed!");
		delay_ms(200);
		LCD_Fill(30,90,200+30,70+16,WHITE);
		delay_ms(200);		    
	}
	
	while(font_init())		//初始化字库
	{
		LCD_ShowString(30,70,200,16,16,"Font Error!");
		update_font(30,90,16,"0:");	//如果字库不存在就更新字库
		delay_ms(2000);
		LCD_Clear(WHITE);	//清屏
		break;
	}
	
	TP_Init();				//初始化触摸屏
	OSInit();  //初始化UCOS
	OSTaskCreate(start_task,  									//start_task任务
				(void*)0,    									//参数
				(OS_STK*)&START_TASK_STK[START_STK_SIZE-1], 	//任务堆栈栈顶
				START_TASK_PRIO);  								//任务优先级
	OSStart();  //开启UCOS
}

//START任务
void start_task(void *pdata)
{
	OS_CPU_SR cpu_sr;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC,ENABLE);	//开启CRC时钟				//在所有窗口上使用存储设备
	
	GUI_Init();  										//emwin初始化
	WM_SetCreateFlags(WM_CF_MEMDEV); //启动所有窗口的存储设备
	
	OSStatInit(); //初始化统计任务
	OS_ENTER_CRITICAL();  //进入临界区,关闭中断
	
	OSTaskCreate(emwin_maintask,(void*)0,(OS_STK*)&EMWINDEMO_TASK_STK[EMWINDEMO_STK_SIZE-1],EMWINDEMO_TASK_PRIO);  //WMEINDEMO任务优					
	OSTaskCreate(touch_task,(void*)0,(OS_STK*)&TOUCH_TASK_STK[TOUCH_STK_SIZE-1],TOUCH_TASK_PRIO); 	//触摸屏任务
	OSTaskCreate(led0_task,(void*)0,(OS_STK*)&LED0_TASK_STK[LED0_STK_SIZE-1],LED0_TASK_PRIO); 		//LED0任务
	OSTaskCreate(fontupdata_task,(void*)0,(OS_STK*)&FONTUPDATA_TASK_STK[FONTUPDATA_STK_SIZE-1],FONTUPDATA_TASK_PRIO); //字库更新任务
	OSTaskCreate(key_task,(void*)0,(OS_STK*)&KEY_TASK_STK[KEY_STK_SIZE-1],KEY_TASK_PRIO); //按键任务
	
	OSTaskSuspend(OS_PRIO_SELF); //挂起start任务
	OS_EXIT_CRITICAL();  //退出临界区,开中断					
}

//EMWINDEMO任务
void emwin_maintask(void *pdata)
{
	//更换皮肤
	BUTTON_SetDefaultSkin(BUTTON_SKIN_FLEX);
	CHECKBOX_SetDefaultSkin(CHECKBOX_SKIN_FLEX);
	DROPDOWN_SetDefaultSkin(DROPDOWN_SKIN_FLEX);
	FRAMEWIN_SetDefaultSkin(FRAMEWIN_SKIN_FLEX);
	HEADER_SetDefaultSkin(HEADER_SKIN_FLEX);
	MENU_SetDefaultSkin(MENU_SKIN_FLEX);
	MULTIPAGE_SetDefaultSkin(MULTIPAGE_SKIN_FLEX);
	PROGBAR_SetDefaultSkin(PROGBAR_SKIN_FLEX);
	RADIO_SetDefaultSkin(RADIO_SKIN_FLEX);
	SCROLLBAR_SetDefaultSkin(SCROLLBAR_SKIN_FLEX);
	SLIDER_SetDefaultSkin(SLIDER_SKIN_FLEX);
	SPINBOX_SetDefaultSkin(SPINBOX_SKIN_FLEX);
		
	GUI_CURSOR_Show();	//显示鼠标
	while(1)
	{
		jpegdisplay_demo();
	}
}

//字库更新任务
//KEY_UP键长按2s更新字库
void fontupdata_task(void *pdata)
{
	while(1)
	{
		if(WK_UP == 1)				//KEY_UP键按下
		{
			OSTimeDlyHMSM(0,0,2,0);	//延时2s
			if(WK_UP == 1)			//还是KEY_UP键
			{
				LCD_Clear(WHITE);
				OSSchedLock();		//调度器上锁
				LCD_ShowString(10,50,250,30,16,"Font Updataing,Please Wait...");
				update_font(10,70,16,"0:");//更新字库
				LCD_Clear(WHITE);
				POINT_COLOR = RED;
				LCD_ShowString(10,50,280,30,16,"Font Updata finshed,Please Restart!");
				OSSchedUnlock();	//调度器解锁
			}
		}
		OSTimeDlyHMSM(0,0,0,10);	//延时10ms
	}
}

//按键处理任务
void key_task(void *pdata)
{
	u8 key;
	while(1)
	{
		key = KEY_Scan(0);
		switch(key)
		{
			case KEY0_PRES:
				OSSchedLock();		//调度器上锁
				LED1 = 0;
				create_bmppicture("0:/SCREENSHORT/屏幕截图0.bmp",0,0,480,800);
				LED1 = 1;
				OSSchedUnlock();	//调度器解锁
				break;
		}
		OSTimeDlyHMSM(0,0,0,10);	//延时10ms
	}
}

//TOUCH任务
void touch_task(void *pdata)
{
	while(1)
	{
		GUI_TOUCH_Exec();
		OSTimeDlyHMSM(0,0,0,5);//延时5ms
	}
}

//LED0任务
void led0_task(void *pdata)
{
	while(1)
	{
		LED0 = !LED0;
		OSTimeDlyHMSM(0,0,0,500);//延时500ms
	}
}
