/*
*********************************************************************************************************
*
*	模块名称 : 主界面模块。
*	文件名称 : main_menu.c
*	版    本 : V1.0
*	说    明 :
*	修改记录 :
*		版本号  日期       作者    说明
*		v1.0    2012-08-08 armfly  ST固件库V3.5.0版本。
*
*	Copyright (C), 2012-2013, 安富莱电子 www.armfly.com
*
*********************************************************************************************************
*/

#include "bsp.h"			/* printf函数定向输出到串口，所以必须包含这个文件 */
#include "images.h"			/* 图标文件 */
#include "main_menu.h"
#include "main.h"

#define VER_INFO	"安富莱STM32-X3出厂测试程序(版本V1.1)"
#define KEY_INFO	"按K3键校准触摸屏 按K1键切换背景"

#define ICON_LEFT	16
#define ICON_TOP	25

/* 主窗口图标坐标 (2行,每行6个) */
enum
{
	/* 第1行 */
	ICON1_X	= ICON_LEFT,
	ICON1_Y	= ICON_TOP,
	ICON1_H	= ICON_HEIGHT,
	ICON1_W	= ICON_WIDTH,

		ICON2_X	= ICON_LEFT + ICON_STEP_X,
		ICON2_Y	= ICON_TOP,
		ICON2_H	= ICON_HEIGHT,
		ICON2_W	= ICON_WIDTH,

			ICON3_X	= ICON_LEFT + 2 * ICON_STEP_X,
			ICON3_Y	= ICON_TOP,
			ICON3_H	= ICON_HEIGHT,
			ICON3_W	= ICON_WIDTH,

				ICON4_X	= ICON_LEFT + 3 * ICON_STEP_X,
				ICON4_Y	= ICON_TOP,
				ICON4_H	= ICON_HEIGHT,
				ICON4_W	= ICON_WIDTH,

					ICON5_X	= ICON_LEFT + 4 * ICON_STEP_X,
					ICON5_Y	= ICON_TOP,
					ICON5_H	= ICON_HEIGHT,
					ICON5_W	= ICON_WIDTH,


						ICON6_X	= ICON_LEFT + 5 * ICON_STEP_X,
						ICON6_Y	= ICON_TOP,
						ICON6_H	= ICON_HEIGHT,
						ICON6_W	= ICON_WIDTH,
	/* 第2行 */
	ICON7_X	= ICON_LEFT,
	ICON7_Y	= ICON_TOP + ICON_STEP_Y,
	ICON7_H	= ICON_HEIGHT,
	ICON7_W	= ICON_WIDTH,

		ICON8_X	= ICON_LEFT + 1 * ICON_STEP_X,
		ICON8_Y	= ICON_TOP + ICON_STEP_Y,
		ICON8_H	= ICON_HEIGHT,
		ICON8_W	= ICON_WIDTH,

			ICON9_X	= ICON_LEFT + 2 * ICON_STEP_X,
			ICON9_Y	= ICON_TOP + ICON_STEP_Y,
			ICON9_H	= ICON_HEIGHT,
			ICON9_W	= ICON_WIDTH,

				ICON10_X	= ICON_LEFT + 3 * ICON_STEP_X,
				ICON10_Y	= ICON_TOP + ICON_STEP_Y,
				ICON10_H	= ICON_HEIGHT,
				ICON10_W	= ICON_WIDTH,

					ICON11_X	= ICON_LEFT + 4 * ICON_STEP_X,
					ICON11_Y	= ICON_TOP + ICON_STEP_Y,
					ICON11_H	= ICON_HEIGHT,
					ICON11_W	= ICON_WIDTH,	
					
						ICON12_X	= ICON_LEFT + 5 * ICON_STEP_X,
						ICON12_Y	= ICON_TOP + ICON_STEP_Y,
						ICON12_H	= ICON_HEIGHT,
						ICON12_W	= ICON_WIDTH,		

	/* 第3行 */
	ICON13_X	= ICON_LEFT,
	ICON13_Y	= ICON_TOP + 2 * ICON_STEP_Y,
	ICON13_H	= ICON_HEIGHT,
	ICON13_W	= ICON_WIDTH,

		ICON14_X	= ICON_LEFT + 1 * ICON_STEP_X,
		ICON14_Y	= ICON_TOP + 2 * ICON_STEP_Y,
		ICON14_H	= ICON_HEIGHT,
		ICON14_W	= ICON_WIDTH,

			ICON15_X	= ICON_LEFT + 2 * ICON_STEP_X,
			ICON15_Y	= ICON_TOP + 2 * ICON_STEP_Y,
			ICON15_H	= ICON_HEIGHT,
			ICON15_W	= ICON_WIDTH,

				ICON16_X	= ICON_LEFT + 3 * ICON_STEP_X,
				ICON16_Y	= ICON_TOP + 2 * ICON_STEP_Y,
				ICON16_H	= ICON_HEIGHT,
				ICON16_W	= ICON_WIDTH,

					ICON17_X	= ICON_LEFT + 4 * ICON_STEP_X,
					ICON17_Y	= ICON_TOP + 2 * ICON_STEP_Y,
					ICON17_H	= ICON_HEIGHT,
					ICON17_W	= ICON_WIDTH,	
					
						ICON18_X	= ICON_LEFT + 5 * ICON_STEP_X,
						ICON18_Y	= ICON_TOP + 2 * ICON_STEP_Y,
						ICON18_H	= ICON_HEIGHT,
						ICON18_W	= ICON_WIDTH,	
};


extern const unsigned char acRecorder[48 * 48 * 4];
extern const unsigned char acTape[48 * 48 * 4];
extern const unsigned char acCamera[48 * 48 * 4];
extern const unsigned char acChiLun[48 * 48 * 4];
extern const unsigned char acchujiao[48 * 48 * 4];

#define ICONS_COUNT     11
static const ICON_T s_tMainIcons[ICONS_COUNT] =
{	
	{ID_ICON, ICON1_X, ICON1_Y, ICON1_H, ICON1_W, (uint16_t *)achwinfo, "硬件信息"},
	{ID_ICON, ICON2_X, ICON2_Y, ICON2_H, ICON2_W, (uint16_t *)acLCD, "触摸屏和按键"},
	{ID_ICON, ICON3_X, ICON3_Y, ICON3_H, ICON3_W, (uint16_t *)acUSBDisk, "模拟U盘"},
	{ID_ICON, ICON4_X, ICON4_Y, ICON4_H, ICON4_W, (uint16_t *)acADC, "RA8875字库"},	
	{ID_ICON, ICON5_X, ICON5_Y, ICON5_H, ICON5_W, (uint16_t *)acchujiao, "MEMS传感器"},
	{ID_ICON, ICON6_X, ICON6_Y, ICON6_H, ICON6_W, (uint16_t *)acGPS, "GPS定位模块"},
	{ID_ICON, ICON7_X, ICON7_Y, ICON7_H, ICON7_W, (uint16_t *)acPhone, "GPRS模块"},
	{ID_ICON, ICON8_X, ICON8_Y, ICON8_H, ICON8_W, (uint16_t *)acMP3, "MP3模块"},
};

/* 图标点击后，切换程序状态 */
static const uint8_t s_RetStatus[ICONS_COUNT] =
{
	MS_HARDWARE_INFO,	/* 硬件信息 */
	MS_TEST_TOUCH,		/* 测试触摸屏 */
	MS_MASS_STORAGE,	/* SD卡，NAND模拟U盘 */	
	MS_RA8875,			/* RA8875字库 */	
	
	MS_MEMS,			/* MEMS 传感器测试（I2C） 三轴陀螺仪，磁力计，气压计 */	
	MS_GPS,				/* GPS测试 */
	MS_GPRS,			/* GPRS模块测试 */
	MS_MP3,				/* 校准触摸状态 */
};

#define MAIN_BACK_COLOR		s_BackColor[s_ColorIndex]		/* 背景颜色 */
#define MAIN_TEXT_COLOR		s_TextColor[s_ColorIndex]		/* 图标文字颜色 */

/* 颜色方案 */
#define STYLE_COUNT			3		/* 颜色方案个数 */
static uint16_t s_BackColor[STYLE_COUNT] = /* 背景颜色，单色，未用 */
{
	CL_WHITE,		/* 白色 */
	RGB(0,78,152),		/* XP桌面蓝 */
	RGB(185,193,204),	/* 灰色 */
};

static uint16_t s_TextColor[STYLE_COUNT] = /* 图标文字颜色 */
{
	CL_BLACK,		/* 黑色 - 海滩 */
	CL_WHITE,		/* 白色 - 绿叶 */
	CL_WHITE,		/* 绿色 - 水珠 */
};
static uint16_t s_ColorIndex = 0;	/* 当前选中的颜色方案序号 */

/* 
背景图片（RGB565格式，存放在NOR Flash）
NOR Flash 基地址 0x6400 0000

0x0020 0000   255K字节 480x272_海滩.bin  + 0x3FC00
0x0023 FC00   255K字节 480x272_树叶.bin
0x0027 F800   255K字节 480x272_水珠.bin

0x002B F400   755K字节 800x480_海滩.bin  + 0xBB800
0x0037 AC00   755K字节 800x480_树叶.bin
0x0043 6400   755K字节 800x480_水珠.bin
*/
static uint16_t *s_pBmp480x272[3] = 
{
	(uint16_t *)(0x64000000 + 0x00200000), 
	(uint16_t *)(0x64000000 + 0x0023FC00), 
	(uint16_t *)(0x64000000 + 0x0027F800), 
};
static uint16_t *s_pBmp800x480[3] = 
{
	(uint16_t *)(0x64000000 + 0x002BF400), 
	(uint16_t *)(0x64000000 + 0x0037AC00), 
	(uint16_t *)(0x64000000 + 0x00436400),
};


/*
*********************************************************************************************************
*	函 数 名: MainMenu
*	功能说明: 主界面
*	形    参：无
*	返 回 值: 状态字
*********************************************************************************************************
*/
uint8_t MainMenu(void)
{
	uint8_t ucKeyCode;		/* 按键代码 */
	uint8_t ucTouch;		/* 触摸事件 */
	uint8_t fRefresh;		/* 刷屏请求标志,1表示需要刷新 */
	FONT_T tFont;			/* 定义一个字体结构体变量，用于设置字体参数 */
	FONT_T tIconFont;		/* 定义一个字体结构体变量，用于图标文本 */

	int16_t tpX, tpY;
	uint16_t i;

	#if 1
		LCD_ClrScr(MAIN_BACK_COLOR);  	/* 清屏，背景蓝色 */	
	#else
		if (g_LcdWidth == 800)
		{
			LCD_DrawBMP(0, 0, g_LcdHeight, g_LcdWidth, s_pBmp480x272[s_ColorIndex]);
		}
		else
		{
			LCD_DrawBMP(0, 0, g_LcdHeight, g_LcdWidth, s_pBmp800x480[s_ColorIndex]);
		}
	#endif

	/* 设置字体参数 */
	{
		tFont.FontCode = FC_ST_16;	/* 字体代码 16点阵 */
		tFont.FrontColor = MAIN_TEXT_COLOR;	/* 字体颜色 */
		tFont.BackColor = CL_MASK;	/* 文字背景颜色 */
		tFont.Space = 0;				/* 文字间距，单位 = 像素 */

		tIconFont.FontCode = FC_ST_12;		/* 字体代码 16点阵 */
		tIconFont.FrontColor = MAIN_TEXT_COLOR;	/* 字体颜色 */
		tIconFont.BackColor = CL_MASK;		/* 文字背景颜色 */
		tIconFont.Space = 1;				/* 文字间距，单位 = 像素 */
	}

	LCD_DispStr(5, 3, VER_INFO, &tFont);			/* 在(8,3)坐标处显示一串汉字 */	
	LCD_DispStr(5, g_LcdHeight - 13, KEY_INFO, &tIconFont);			/* 在(8,3)坐标处显示一串汉字 */

	fRefresh = 1;	/* 1表示需要刷新LCD */
	while (1)
	{
		bsp_Idle();

		if (fRefresh)
		{
			fRefresh = 0;

			/* 显示图标阵列 */
			{
				for (i = 0; i < ICONS_COUNT; i++)
				{
					//LCD_DrawIcon(&s_tMainIcons[i], &tIconFont, 0);	/* 0 表示正常显示， 1表示选中 */
					LCD_DrawIcon32(&s_tMainIcons[i], &tIconFont, 0);	/* 0 表示正常显示， 1表示选中 */					
				}
			}
		}

		ucTouch = TOUCH_GetKey(&tpX, &tpY);	/* 读取触摸事件 */
		if (ucTouch != TOUCH_NONE)
		{
			switch (ucTouch)
			{
				case TOUCH_DOWN:		/* 触笔按下事件 */
					/* 绘制图标阵列 */
					{
						for (i = 0; i < ICONS_COUNT; i++)
						{
							if (TOUCH_InRect(tpX, tpY, s_tMainIcons[i].Left, s_tMainIcons[i].Top,
								 s_tMainIcons[i].Height, s_tMainIcons[i].Width))
							{
								BEEP_KeyTone();		/* 按键提示音 */
								LCD_DrawIcon32(&s_tMainIcons[i], &tIconFont, 1);	/* 0 表示正常显示， 1表示选中 */
							}
						}
					}
					break;

				case TOUCH_MOVE:		/* 触笔移动事件 */
					break;

				case TOUCH_RELEASE:		/* 触笔释放事件 */
					for (i = 0; i < ICONS_COUNT; i++)
					{
						if (TOUCH_InRect(tpX, tpY, s_tMainIcons[i].Left, s_tMainIcons[i].Top,
							 s_tMainIcons[i].Height, s_tMainIcons[i].Width))
						{
							return s_RetStatus[i];	/* 返回程序状态 */
						}
					}
					
					/* 绘制图标阵列,消除选中状态 */
					{
						for (i = 0; i < ICONS_COUNT; i++)
						{
							LCD_DrawIcon32(&s_tMainIcons[i], &tIconFont, 0);	/* 0 表示正常显示， 1表示选中 */
						}
					}					
					break;

				default:
					break;
			}
		}

		ucKeyCode = bsp_GetKey();	/* 读取键值, 无键按下时返回 KEY_NONE = 0 */
		if (ucKeyCode != KEY_NONE)
		{
			/* 有键按下 */
			switch (ucKeyCode)
			{
				case  KEY_DOWN_K3:	/* K3键 */
					return MS_CALIBRATION;	/* 进入触摸界面 */
					//break;

				case  KEY_DOWN_K1:	/* K1键 */
					if (++s_ColorIndex >= STYLE_COUNT)
					{
						s_ColorIndex = 0;
					}
					/* 重新绘制背景 */
					{
						#if 1
							LCD_ClrScr(MAIN_BACK_COLOR);  	/* 清屏，背景蓝色 */	
						#else
							if (g_LcdWidth == 800)
							{
								LCD_DrawBMP(0, 0, g_LcdHeight, g_LcdWidth, s_pBmp480x272[s_ColorIndex]);
							}
							else
							{
								LCD_DrawBMP(0, 0, g_LcdHeight, g_LcdWidth, s_pBmp800x480[s_ColorIndex]);
							}
						#endif

						/* 设置字体参数 */
						{
							tFont.FontCode = FC_ST_16;	/* 字体代码 16点阵 */
							tFont.FrontColor = MAIN_TEXT_COLOR;	/* 字体颜色 */
							tFont.BackColor = CL_MASK;	/* 文字背景颜色 */
							tFont.Space = 0;				/* 文字间距，单位 = 像素 */

							tIconFont.FontCode = FC_ST_12;		/* 字体代码 16点阵 */
							tIconFont.FrontColor = MAIN_TEXT_COLOR;	/* 字体颜色 */
							tIconFont.BackColor = CL_MASK;		/* 文字背景颜色 */
							tIconFont.Space = 1;				/* 文字间距，单位 = 像素 */
						}	

						LCD_DispStr(5, 3, VER_INFO, &tFont);			/* 在(8,3)坐标处显示一串汉字 */	
						LCD_DispStr(5, g_LcdHeight - 13, KEY_INFO, &tIconFont);			/* 在(8,3)坐标处显示一串汉字 */						
					}	
					fRefresh = 1;
					break;
								
				default:
					break;
			}
		}
	}
}

/***************************** 安富莱电子 www.armfly.com (END OF FILE) *********************************/
