/*
*********************************************************************************************************
*
*	模块名称 : 数字小键盘界面
*	文件名称 : main.c
*	版    本 : V1.0
*	说    明 : 数字小键盘界面主程序。弹出数字小键盘用于输入。
*
*	修改记录 :
*		版本号  日期        作者     说明
*		V1.0    2013-01-01 armfly  正式发布
*
*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
*
*********************************************************************************************************
*/

#include "bsp.h"
#include "num_pad.h"

/* 数字小键盘 窗口 */
enum
{
	/* 数字小键盘窗口位置 */
	WIN_NUM_X		= 100,
	WIN_NUM_Y		= 8,

	/* 数字按钮(0-9,.*)高度和宽度,全部一样 */
	BUTTON_NUM_H = 40,
	BUTTON_NUM_W = 70,

	/* 控制按钮（退格,CLR,RET,OK）高度和宽度,全部一样 */
	BUTTON_CTRL_H = 40,
	BUTTON_CTRL_W = 80,

	/* 数字小键盘窗口大小 */
	WIN_NUM_H		= 249,
	WIN_NUM_W		= (BUTTON_NUM_W * 3) + BUTTON_CTRL_W + 8 + 6,

	/* 输入数字编辑框 */
	EDIT_NUM_X	= WIN_NUM_X + 4,
	EDIT_NUM_Y	= WIN_NUM_Y + 34,
	EDIT_NUM_H	= 40,
	EDIT_NUM_W	= WIN_NUM_W - 8,

	/* 数字1 按钮 */
	BUTTON_NUM1_X	= WIN_NUM_X + 4,
	BUTTON_NUM1_Y	= EDIT_NUM_Y + 43,

	BUTTON_X_SPACE = BUTTON_NUM_W + 2,
	BUTTON_Y_SPACE = BUTTON_NUM_H + 2,

	/* 数字2 按钮 */
	BUTTON_NUM2_X	= BUTTON_NUM1_X + BUTTON_X_SPACE,
	BUTTON_NUM2_Y	= BUTTON_NUM1_Y,

	/* 数字3 按钮 */
	BUTTON_NUM3_X	= BUTTON_NUM1_X + 2 * BUTTON_X_SPACE,
	BUTTON_NUM3_Y	= BUTTON_NUM1_Y,

	/* <- 按钮 */
	BUTTON_NUM_BK_X	= BUTTON_NUM1_X + 3 * BUTTON_X_SPACE,
	BUTTON_NUM_BK_Y	= BUTTON_NUM1_Y,

	/* 数字4 按钮 */
	BUTTON_NUM4_X	= BUTTON_NUM1_X,
	BUTTON_NUM4_Y	= BUTTON_NUM1_Y + BUTTON_Y_SPACE,

	/* 数5 按钮 */
	BUTTON_NUM5_X	= BUTTON_NUM1_X + BUTTON_X_SPACE,
	BUTTON_NUM5_Y	= BUTTON_NUM1_Y + BUTTON_Y_SPACE,

	/* 数字6 按钮 */
	BUTTON_NUM6_X	= BUTTON_NUM1_X + 2 * BUTTON_X_SPACE,
	BUTTON_NUM6_Y	= BUTTON_NUM1_Y + BUTTON_Y_SPACE,

	/* CLR 按钮 */
	BUTTON_NUM_CLR_X	= BUTTON_NUM1_X + 3 * BUTTON_X_SPACE,
	BUTTON_NUM_CLR_Y	= BUTTON_NUM1_Y + BUTTON_Y_SPACE,

	/* 数字7 按钮 */
	BUTTON_NUM7_X	= BUTTON_NUM1_X,
	BUTTON_NUM7_Y	= BUTTON_NUM1_Y + 2 * BUTTON_Y_SPACE,

	/* 数字8 按钮 */
	BUTTON_NUM8_X	= BUTTON_NUM1_X + BUTTON_X_SPACE,
	BUTTON_NUM8_Y	= BUTTON_NUM1_Y + 2 * BUTTON_Y_SPACE,

	/* 数字9 按钮 */
	BUTTON_NUM9_X	= BUTTON_NUM1_X + 2 * BUTTON_X_SPACE,
	BUTTON_NUM9_Y	= BUTTON_NUM1_Y + 2 * BUTTON_Y_SPACE,

	/* RET 按钮 */
	BUTTON_NUM_RET_X	= BUTTON_NUM1_X + 3 * BUTTON_X_SPACE,
	BUTTON_NUM_RET_Y	= BUTTON_NUM1_Y + 2 * BUTTON_Y_SPACE,

	/* 数字. 按钮 */
	BUTTON_NUM_DOT_X	= BUTTON_NUM1_X,
	BUTTON_NUM_DOT_Y	= BUTTON_NUM1_Y + 3 * BUTTON_Y_SPACE,

	/* 数字0 按钮 */
	BUTTON_NUM0_X	= BUTTON_NUM1_X + BUTTON_X_SPACE,
	BUTTON_NUM0_Y	= BUTTON_NUM1_Y + 3 * BUTTON_Y_SPACE,

	/* * 按钮 */
	BUTTON_NUM_ST_X	= BUTTON_NUM1_X + 2 * BUTTON_X_SPACE,
	BUTTON_NUM_ST_Y	= BUTTON_NUM1_Y + 3 * BUTTON_Y_SPACE,

	/* OK按钮 */
	BUTTON_NUM_OK_X	= BUTTON_NUM1_X + 3 * BUTTON_X_SPACE,
	BUTTON_NUM_OK_Y	= BUTTON_NUM1_Y + 3 * BUTTON_Y_SPACE,

	/* 输入数字最大长度 */
	INPUT_MAX_LEN	= 32,
};

/*　输入无效窗口　*/
enum
{
	WIN_INVALID_X = WIN_NUM_X + 15,
	WIN_INVALID_Y = WIN_NUM_Y + 90,
	WIN_INVALID_H = 100,
	WIN_INVALID_W = WIN_NUM_W - 40,

	LABLE_INVALID_X = WIN_INVALID_X + 5,
	LABLE_INVALID_Y = WIN_INVALID_Y + 38,
};

/*
*********************************************************************************************************
*	函 数 名: InputNumber
*	功能说明: 数字小键盘
*	形    参：
*			 _ucMode : 工作模式， 0 表示IP地址； 1表示输入普通整数
*			_Caption : 数字小键盘界面的窗口标题
*			_pParam  : 输出参数
*	返 回 值: 1表示输入有效  0 表示输入无效
*********************************************************************************************************
*/
uint8_t InputNumber(NUMPAD_MODE_E _Mode, char *_Caption, void *_pParam)
{
	uint8_t ucTouch;		/* 触摸事件 */
	uint8_t fRefresh;		/* 刷屏请求标志,1表示需要刷新 */
	int16_t tpX, tpY;
	FONT_T tFont;
	FONT_T tFontEdit;
	FONT_T tFontBtn;

	BUTTON_T tBtn;
	EDIT_T tEdit;
	WIN_T tWin;

	char cEditBuf[INPUT_MAX_LEN+2];	/* 编辑框内数值 */
	uint8_t ucCursor;	/* 光标 */
	uint8_t ucKeyValue = 0xFF;

	ucCursor = 0;
	cEditBuf[0] = 0;
	fRefresh = 1;	/* 1表示需要刷新LCD */
	while (1)
	{
		bsp_Idle();

		if (fRefresh)
		{
			fRefresh = 0;

			/* 构建主界面 */
			{
				/* 窗体字体 */
				tFont.FontCode = FC_ST_16;
				tFont.BackColor = CL_MASK;	/* 透明色 */
				tFont.FrontColor = CL_WHITE;
				tFont.Space = 0;

				tWin.Font = &tFont;
				tWin.Left = WIN_NUM_X;
				tWin.Top = WIN_NUM_Y;
				tWin.Height = WIN_NUM_H;
				tWin.Width = WIN_NUM_W;
				//strcpy(tWin.Caption, _Caption);
				tWin.pCaption = _Caption;
				LCD_DrawWin(&tWin);

				/* 输入数字编辑框 */
				{
					tFontEdit.FontCode = FC_ST_16;
					tFontEdit.BackColor = CL_MASK;	/* 透明色 */
					tFontEdit.FrontColor = CL_RED;
					tFontEdit.Space = 0;

					tEdit.Font = &tFontEdit;

					tEdit.Left = EDIT_NUM_X;
					tEdit.Top = EDIT_NUM_Y;
					tEdit.Height = EDIT_NUM_H;
					tEdit.Width = EDIT_NUM_W;

					//strcpy(tEdit.Caption, cEditBuf);
					tEdit.pCaption = cEditBuf;
					LCD_DrawEdit(&tEdit);
				}

				/* 绘制数字按钮阵列 */
				{
					tFontBtn.FontCode = FC_ST_16;
					tFontBtn.BackColor = CL_MASK;	/* 透明色 */
					tFontBtn.FrontColor = CL_BLUE;
					tFontBtn.Space = 0;

					tBtn.Font = &tFontBtn;

					tBtn.Focus = 0;	/* 焦点 */

					/* 数字键宽度一致，控制键稍宽一些 */
					tBtn.Height = BUTTON_NUM_H;
					tBtn.Width = BUTTON_NUM_W;

					tBtn.Left = BUTTON_NUM1_X;
					tBtn.Top = BUTTON_NUM1_Y;
					tBtn.pCaption = "1";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM2_X;
					tBtn.Top = BUTTON_NUM2_Y;
					tBtn.pCaption = "2";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM3_X;
					tBtn.Top = BUTTON_NUM3_Y;
					tBtn.pCaption = "3";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM4_X;
					tBtn.Top = BUTTON_NUM4_Y;
					tBtn.pCaption = "4";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM5_X;
					tBtn.Top = BUTTON_NUM5_Y;
					tBtn.pCaption = "5";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM6_X;
					tBtn.Top = BUTTON_NUM6_Y;
					tBtn.pCaption = "6";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM7_X;
					tBtn.Top = BUTTON_NUM7_Y;
					tBtn.pCaption = "7";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM8_X;
					tBtn.Top = BUTTON_NUM8_Y;
					tBtn.pCaption = "8";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM9_X;
					tBtn.Top = BUTTON_NUM9_Y;
					tBtn.pCaption = "9";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM0_X;
					tBtn.Top = BUTTON_NUM0_Y;
					tBtn.pCaption = "0";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM_ST_X;
					tBtn.Top = BUTTON_NUM_ST_Y;
					tBtn.pCaption = "*";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM_DOT_X;
					tBtn.Top = BUTTON_NUM_DOT_Y;
					tBtn.pCaption = ".";
					LCD_DrawButton(&tBtn);

					/* 数字键宽度一致，控制键稍宽一些 */
					tBtn.Height = BUTTON_CTRL_H;
					tBtn.Width = BUTTON_CTRL_W;

					tBtn.Left = BUTTON_NUM_BK_X;
					tBtn.Top = BUTTON_NUM_BK_Y;
					tBtn.pCaption = "←";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM_CLR_X;
					tBtn.Top = BUTTON_NUM_CLR_Y;
					tBtn.pCaption = "CLR";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM_RET_X;
					tBtn.Top = BUTTON_NUM_RET_Y;
					tBtn.pCaption = "RET";
					LCD_DrawButton(&tBtn);

					tBtn.Left = BUTTON_NUM_OK_X;
					tBtn.Top = BUTTON_NUM_OK_Y;
					tBtn.pCaption = "OK";
					LCD_DrawButton(&tBtn);
				}
			}
		}

		ucTouch = TOUCH_GetKey(&tpX, &tpY);	/* 读取触摸事件 */
		if (ucTouch != TOUCH_NONE)
		{
			switch (ucTouch)
			{
				case TOUCH_DOWN:		/* 触笔按下事件 */
					ucKeyValue = 0xFF;
					tBtn.Focus = 1;	/* 焦点 */

					tBtn.Height = BUTTON_CTRL_H;
					tBtn.Width = BUTTON_CTRL_W;
					if (TOUCH_InRect(tpX, tpY, BUTTON_NUM_BK_X, BUTTON_NUM_BK_Y, BUTTON_CTRL_H, BUTTON_CTRL_W))
					{
						BEEP_KeyTone();		/* 按键提示音 */
						ucKeyValue = 'B';

						/* 退格 按钮 */
						tBtn.Left = BUTTON_NUM_BK_X;
						tBtn.Top = BUTTON_NUM_BK_Y;
						tBtn.pCaption = "←";
						LCD_DrawButton(&tBtn);
					}
					else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM_CLR_X, BUTTON_NUM_CLR_Y, BUTTON_CTRL_H, BUTTON_CTRL_W))
					{
						BEEP_KeyTone();		/* 按键提示音 */
						ucKeyValue = 'C';

						/* CLR 按钮 */
						tBtn.Left = BUTTON_NUM_CLR_X;
						tBtn.Top = BUTTON_NUM_CLR_Y;
						tBtn.pCaption = "CLR";
						LCD_DrawButton(&tBtn);
					}
					else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM_RET_X, BUTTON_NUM_RET_Y, BUTTON_CTRL_H, BUTTON_CTRL_W))
					{
						BEEP_KeyTone();		/* 按键提示音 */
						ucKeyValue = 'R';

						/* RET 按钮 */
						tBtn.Left = BUTTON_NUM_RET_X;
						tBtn.Top = BUTTON_NUM_RET_Y;
						tBtn.pCaption = "RET";
						LCD_DrawButton(&tBtn);
					}
					else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM_OK_X, BUTTON_NUM_OK_Y, BUTTON_CTRL_H, BUTTON_CTRL_W))
					{
						BEEP_KeyTone();		/* 按键提示音 */
						ucKeyValue = 'K';

						/* OK 按钮 */
						tBtn.Left = BUTTON_NUM_OK_X;
						tBtn.Top = BUTTON_NUM_OK_Y;
						tBtn.pCaption = "OK";
						LCD_DrawButton(&tBtn);
					}
					else
					{
						/* 处理数字键 */						
						tBtn.Height = BUTTON_NUM_H;
						tBtn.Width = BUTTON_NUM_W;

						if (TOUCH_InRect(tpX, tpY, BUTTON_NUM1_X, BUTTON_NUM1_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							
							ucKeyValue = '1';

							/* 1 按钮 */
							tBtn.Left = BUTTON_NUM1_X;
							tBtn.Top = BUTTON_NUM1_Y;
							tBtn.pCaption = "1";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM2_X, BUTTON_NUM2_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '2';

							tBtn.Left = BUTTON_NUM2_X;
							tBtn.Top = BUTTON_NUM2_Y;
							tBtn.pCaption = "2";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM3_X, BUTTON_NUM3_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '3';

							tBtn.Left = BUTTON_NUM3_X;
							tBtn.Top = BUTTON_NUM3_Y;
							tBtn.pCaption = "3";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM4_X, BUTTON_NUM4_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '4';

							tBtn.Left = BUTTON_NUM4_X;
							tBtn.Top = BUTTON_NUM4_Y;
							tBtn.pCaption = "4";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM5_X, BUTTON_NUM5_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '5';

							tBtn.Left = BUTTON_NUM5_X;
							tBtn.Top = BUTTON_NUM5_Y;
							tBtn.pCaption = "5";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM6_X, BUTTON_NUM6_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '6';

							tBtn.Left = BUTTON_NUM6_X;
							tBtn.Top = BUTTON_NUM6_Y;
							tBtn.pCaption = "6";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM7_X, BUTTON_NUM7_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '7';

							tBtn.Left = BUTTON_NUM7_X;
							tBtn.Top = BUTTON_NUM7_Y;
							tBtn.pCaption = "7";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM8_X, BUTTON_NUM8_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '8';

							tBtn.Left = BUTTON_NUM8_X;
							tBtn.Top = BUTTON_NUM8_Y;
							tBtn.pCaption = "8";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM9_X, BUTTON_NUM9_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '9';

							tBtn.Left = BUTTON_NUM9_X;
							tBtn.Top = BUTTON_NUM9_Y;
							tBtn.pCaption = "9";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM0_X, BUTTON_NUM0_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							BEEP_KeyTone();		/* 按键提示音 */
							ucKeyValue = '0';

							tBtn.Left = BUTTON_NUM0_X;
							tBtn.Top = BUTTON_NUM0_Y;
							tBtn.pCaption = "0";
							LCD_DrawButton(&tBtn);
						}
						else if (TOUCH_InRect(tpX, tpY, BUTTON_NUM_DOT_X, BUTTON_NUM_DOT_Y, BUTTON_NUM_H, BUTTON_NUM_W))
						{
							if (_Mode == NUMPAD_IP)
							{
								BEEP_KeyTone();		/* 按键提示音 */
								ucKeyValue = '.';

								tBtn.Left = BUTTON_NUM_DOT_X;
								tBtn.Top = BUTTON_NUM_DOT_Y;
								tBtn.pCaption = ".";
								LCD_DrawButton(&tBtn);
							}
						}
					}
					/* 触摸按键判断完毕 */

					/* 无键按下 */
					if (ucKeyValue == 0xFF)
					{
						break;
					}

					/* 处理特殊功能键 */
					if (ucKeyValue == 'B')	/* 退格 */
					{
						if (ucCursor > 0)
						{
							ucCursor--;
						}
					}
					else if (ucKeyValue == 'C')	/* 清零 */
					{
						ucCursor = 0;
					}
					else if (ucKeyValue == 'R')	/* 返回 */
					{
						return 0;
					}
					else if (ucKeyValue == 'K')	/* 确认 */
					{
						;	/* 等待触摸释放后再确认 */
					}
					else	/* 数字键 */
					{
						if (ucCursor < INPUT_MAX_LEN)
						{
							cEditBuf[ucCursor++] = ucKeyValue;
						}
						else
						{
							cEditBuf[ucCursor - 1] = ucKeyValue;	/* 光标不移动，更新最后1个数字 */
						}
					}
					cEditBuf[ucCursor] = 0;
					tEdit.pCaption = cEditBuf;
					LCD_DrawEdit(&tEdit);
					break;

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

				case TOUCH_RELEASE:		/* 触笔释放事件 */
					if (TOUCH_InRect(tpX, tpY, BUTTON_NUM_OK_X, BUTTON_NUM_OK_Y, BUTTON_NUM_H, BUTTON_NUM_W))
					{
						/* 点击ok按钮 */
						if (_Mode == NUMPAD_IP)			/* IP地址输入模式. 将IP地址的4个字段解码为4个整数 */
						{
							int32_t iIP;
							uint8_t ucDotNum;	/* 小数点个数，用于判断数据合法 */
							int16_t i;
							NUMPAD_IP_T *pIP = (NUMPAD_IP_T *)_pParam;	/* IP地址输入模式 */

							ucDotNum = 0;
							for (i = 0; i < ucCursor; i++)
							{
								if (cEditBuf[i] == '.')
								{
									ucDotNum++;
								}
							}
							if (ucDotNum != 3)	/* 必须包括3个小数点 */
							{
								DispInvlidInput();	/* 显示输入无效 */

								fRefresh = 1;
							}
							else
							{
								/* 有3个小数点，开始解码IP地址 */
								ucDotNum = 0;
								iIP = 0;
								for (i = 0; i < ucCursor; i++)
								{
									if ((cEditBuf[i] == '.') || (i == ucCursor - 1))
									{
										if (i == ucCursor - 1)
										{
											iIP = iIP * 10 + cEditBuf[i] - '0';
										}
										if (iIP <= 255)
										{
											pIP->ip_buf[ucDotNum] = iIP;
											ucDotNum++;

											if (ucDotNum == 4)
											{
												return 1;	/* 返回正确结果 */
											}
											iIP = 0;
										}
										else
										{
											DispInvlidInput();	/* 显示输入无效 */
											fRefresh = 1;
											break;
										}
									}
									else
									{
										iIP = iIP * 10 + cEditBuf[i] - '0';
									}
								}
							}
						}
						else if (_Mode == NUMPAD_TEL)			/* 电话号码输入模式。 直接复制结果（小数点已提前禁止） */
						{
							if (ucCursor < 3)
							{
								DispInvlidInput();	/* 显示输入无效 */
								fRefresh = 1;
								break;
							}

							strcpy((char *)_pParam, cEditBuf);
							return 1;	/* 返回正确结果 */
						}
						else		/* NUMPAD_INT - 带小数点整数输入模式 【该分支还未调试】*/
						{
							int32_t iRet;
							uint8_t ucDotNum;	/* 小数点个数，用于判断数据合法 */
							int16_t i;
							NUMPAD_INT_T *pInt = (NUMPAD_INT_T *)_pParam;	/* 带小数点整数输入模式 */


							ucDotNum = 0;
							for (i = 0; i < ucCursor; i++)
							{
								if (cEditBuf[i] == '.')
								{
									ucDotNum++;
								}
							}
							if (ucDotNum > 1)
							{
								/* 小数点个数大于1 */
								iRet = -1;
							}
							else if (ucDotNum == 1)	/* 1个小数点 */
							{
								int32_t iDotValue;
								int32_t iMult;

								/* 先处理小数点前面的数字 */
								iRet = 0;
								for (i = 0; i <  ucCursor; i++)
								{
									if (cEditBuf[i] == '.')
									{
										break;
									}
									iRet *= 10;
									iRet += cEditBuf[i] - '0';
								}
								iRet *= 1000;

								/* 再处理小数点后面的数字 */
								iDotValue = 0;
								iMult = 100;
								i++;
								for ( ; i <  ucCursor; i++)
								{
									iDotValue += iMult * (cEditBuf[i] - '0');
									iMult /= 10;
								}

								iRet += iDotValue;
							}
							else
							{
								/* 全部整数 */
								iRet = 0;
								for (i = 0; i <  ucCursor; i++)
								{
									iRet *= 10;
									iRet += cEditBuf[i] - '0';
								}
								iRet *= 1000;
							}

							if ((ucCursor == 0) || (iRet < pInt->Min) || (iRet > pInt->Max ))
							{
								DispInvlidInput();	/* 显示输入无效 */

								fRefresh = 1;
							}
							else
							{
								return 1;	/* 返回有效输入 */
							}
						}
					}
					else
					{
						if (ucKeyValue == 0xFF)
						{
							break;
						}

						/* 取消焦点按钮 */
						tBtn.Focus = 0;	/* 焦点 */

						tBtn.Height = BUTTON_CTRL_H;
						tBtn.Width = BUTTON_CTRL_W;
						if (ucKeyValue == 'B')
						{
							/* 退格 按钮 */
							tBtn.Left = BUTTON_NUM_BK_X;
							tBtn.Top = BUTTON_NUM_BK_Y;
							tBtn.pCaption = "←";
							LCD_DrawButton(&tBtn);
						}
						else if (ucKeyValue == 'C')
						{
							/* CLR 按钮 */
							tBtn.Left = BUTTON_NUM_CLR_X;
							tBtn.Top = BUTTON_NUM_CLR_Y;
							tBtn.pCaption = "CLR";
							LCD_DrawButton(&tBtn);
						}
						else if (ucKeyValue == 'R')
						{
							/* RET 按钮 */
							tBtn.Left = BUTTON_NUM_RET_X;
							tBtn.Top = BUTTON_NUM_RET_Y;
							tBtn.pCaption = "RET";
							LCD_DrawButton(&tBtn);
						}
						else if (ucKeyValue == 'K')
						{
							/* OK 按钮 */
							tBtn.Left = BUTTON_NUM_OK_X;
							tBtn.Top = BUTTON_NUM_OK_Y;
							tBtn.pCaption = "OK";
							LCD_DrawButton(&tBtn);
						}
						else 	/* 处理数字键 */
						{
							tBtn.Height = BUTTON_NUM_H;
							tBtn.Width = BUTTON_NUM_W;

							if (ucKeyValue == '1')
							{
								tBtn.Left = BUTTON_NUM1_X;
								tBtn.Top = BUTTON_NUM1_Y;
								tBtn.pCaption = "1";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '2')
							{
								tBtn.Left = BUTTON_NUM2_X;
								tBtn.Top = BUTTON_NUM2_Y;
								tBtn.pCaption = "2";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '3')
							{
								tBtn.Left = BUTTON_NUM3_X;
								tBtn.Top = BUTTON_NUM3_Y;
								tBtn.pCaption = "3";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '4')
							{
								tBtn.Left = BUTTON_NUM4_X;
								tBtn.Top = BUTTON_NUM4_Y;
								tBtn.pCaption = "4";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '5')
							{
								tBtn.Left = BUTTON_NUM5_X;
								tBtn.Top = BUTTON_NUM5_Y;
								tBtn.pCaption = "5";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '6')
							{
								ucKeyValue = '6';

								tBtn.Left = BUTTON_NUM6_X;
								tBtn.Top = BUTTON_NUM6_Y;
								tBtn.pCaption = "6";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '7')
							{
								ucKeyValue = '7';

								tBtn.Left = BUTTON_NUM7_X;
								tBtn.Top = BUTTON_NUM7_Y;
								tBtn.pCaption = "7";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '8')
							{
								ucKeyValue = '8';

								tBtn.Left = BUTTON_NUM8_X;
								tBtn.Top = BUTTON_NUM8_Y;
								tBtn.pCaption = "8";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '9')
							{
								ucKeyValue = '9';

								tBtn.Left = BUTTON_NUM9_X;
								tBtn.Top = BUTTON_NUM9_Y;
								tBtn.pCaption = "9";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '0')
							{
								ucKeyValue = '0';

								tBtn.Left = BUTTON_NUM0_X;
								tBtn.Top = BUTTON_NUM0_Y;
								tBtn.pCaption = "0";
								LCD_DrawButton(&tBtn);
							}
							else if (ucKeyValue == '.')
							{
								ucKeyValue = '.';

								tBtn.Left = BUTTON_NUM_DOT_X;
								tBtn.Top = BUTTON_NUM_DOT_Y;
								tBtn.pCaption = ".";
								LCD_DrawButton(&tBtn);
							}
						}
					}
					break;
			}
		}
	}
}

/*
*********************************************************************************************************
*	函 数 名：DispInvlidInput
*	功能说明：显示输入无效小窗口，延迟2秒
*	形    参：无
*	返 回 值：无
*********************************************************************************************************
*/
void DispInvlidInput(void)
{
	WIN_T tWin;
	LABEL_T tLable;
	FONT_T tFont;

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

	/* 绘制主窗体 */
	tWin.Font = &tFont;

	tWin.Left = WIN_INVALID_X;
	tWin.Top = WIN_INVALID_Y;
	tWin.Height = WIN_INVALID_H;
	tWin.Width = WIN_INVALID_W;
	tWin.pCaption = "Warning";
	LCD_DrawWin(&tWin);

	tLable.Font = &tFont;
	tLable.Left = LABLE_INVALID_X;
	tLable.Top = LABLE_INVALID_Y;
	tLable.MaxLen = 0;
	tLable.pCaption = "输入无效!";
	LCD_DrawLabel(&tLable);

	bsp_DelayMS(2000);
}

/*
*********************************************************************************************************
*	函 数 名: ClearWinNumPad
*	功能说明：清除数字键盘窗口（用于主界面重绘)
*	形    参：_usColor : 底色
*	返 回 值：无
*********************************************************************************************************
*/
void ClearWinNumPad(uint16_t _usColor)
{
	LCD_Fill_Rect(WIN_NUM_X, WIN_NUM_Y, WIN_NUM_H, WIN_NUM_W, _usColor);
}

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