#include "includes.h"

// 显示一个8段码
// pos: Fld1为0~3（从左到右，真值表1~4)，Fld2为4~7（真值表5~8），
//		Fld3为8~10（真值表9~11），Fld4为11~13（真值表12~14)
void Disp_DispChar(u8 pos, char c)
{
	if(pos == 0)
		HT1621_WriteChar(c, 381, 371, 373, 384, 383, 382, 372);
	else if(pos == 1)
		HT1621_WriteChar(c, 361, 351, 353, 364, 363, 362, 352); 
	else if(pos == 2)
		HT1621_WriteChar(c, 341, 331, 333, 344, 343, 342, 332); 
	else if(pos == 3)
		HT1621_WriteChar(c, 321, 311, 313, 324, 323, 322, 312); 
	else if(pos == 4)
		HT1621_WriteChar(c, 221, 211, 213, 224, 223, 222, 212); 
	else if(pos == 5)
		HT1621_WriteChar(c, 201, 191, 193, 204, 203, 202, 192); 
	else if(pos == 6)
		HT1621_WriteChar(c, 181, 171, 173, 184, 183, 182, 172); 
	else if(pos == 7)
		HT1621_WriteChar(c, 161, 151, 153, 164, 163, 162, 152); 
	else if(pos == 8)
		HT1621_WriteChar(c, 14, 24, 22, 11, 12, 13, 23); 
	else if(pos == 9)
		HT1621_WriteChar(c, 34, 44, 42, 31, 32, 33, 43); 
	else if(pos == 10)
		HT1621_WriteChar(c, 54, 64, 62, 51, 52, 53, 63); 
	else if(pos == 11)
		HT1621_WriteChar(c, 84, 94, 92, 81, 82, 83, 93); 
	else if(pos == 12)
		HT1621_WriteChar(c, 104, 114, 112, 101, 102, 103, 113); 
	else if(pos == 13)
		HT1621_WriteChar(c, 124, 134, 132, 121, 122, 123, 133); 
}

// 显示差压数据
void Disp_Fld1Text(s8 *text)
{
	u8 len = strlen(text), i = 0, c;
	
	if(len < 4)	// 千位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(0, c);

	if(len < 3)	// 百位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(1, c);

	if(len < 2)	// 十位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(2, c);

	if(len < 1)	// 个位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(3, c);
}
	
// 显示小数点
void Disp_Fld1PointOn(u8 pos)
{
	Disp_Fld1PointOff();

	if(pos == 1)				// 小数点1位
		HT1621_WriteBuf(334, 1);
	else if(pos == 2)		// 小数点2位
		HT1621_WriteBuf(354, 1);
	else if(pos == 3)		// 小数点3位
		HT1621_WriteBuf(374, 1);
}
	
void Disp_Fld1PointOff()
{
	HT1621_WriteBuf(334, 0);
	HT1621_WriteBuf(354, 0);
	HT1621_WriteBuf(374, 0);
}

// 显示x1000
void Disp_Fld1MultiOn()
{
	HT1621_WriteBuf(314, 1);
}

void Disp_Fld1MultiOff()
{
	HT1621_WriteBuf(314, 0);
}

// 显示压力数据
void Disp_Fld2Text(s8 *text)
{
	u8 len = strlen(text), i = 0, c;
	
	if(len < 4)	// 千位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(4, c);

	if(len < 3)	// 百位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(5, c);

	if(len < 2)	// 十位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(6, c);

	if(len < 1)	// 个位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(7, c);
}

// 显示小数点
void Disp_Fld2PointOn(u8 pos)
{
	Disp_Fld2PointOff();
	
	if(pos == 1)
		HT1621_WriteBuf(174, 1);
	else if(pos == 2)
		HT1621_WriteBuf(194, 1);
	else if(pos == 3)
		HT1621_WriteBuf(214, 1);
}
	
void Disp_Fld2PointOff()
{
	HT1621_WriteBuf(174, 0);
	HT1621_WriteBuf(194, 0);
	HT1621_WriteBuf(214, 0);
}

// 显示单位
void Disp_Fld2UnitOn(TPresVacuUnit unit)
{
	Disp_Fld2UnitOff();
	
	if(unit == UNIT_PV_PA)
		HT1621_WriteBuf(141, 1);
	else if(unit == UNIT_PV_MPA)
		HT1621_WriteBuf(142, 1);
}

void Disp_Fld2UnitOff()
{
	HT1621_WriteBuf(141, 0);
	HT1621_WriteBuf(142, 0);
}

// 显示温度数据
void Disp_Fld3Text(s8 *text)
{
	u8 len = strlen(text), i = 0, c;
	
	// 符号位
	if(len < 4)
		HT1621_WriteBuf(74, 0);
	else
	{
		HT1621_WriteBuf(74, 1);
		i++;
	}
	
	if(len < 3)	// 百位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(8, c);
	
	if(len < 2)	// 十位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(9, c);

	if(len < 1)	// 个位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(10, c);
}

// 显示小数点
void Disp_Fld3PointOn(u8 pos)
{
	Disp_Fld3PointOff();
	
	if(pos == 1)
		HT1621_WriteBuf(41, 1);
	else if(pos == 2)
		HT1621_WriteBuf(21, 1);
}
	
void Disp_Fld3PointOff()
{
	HT1621_WriteBuf(41, 0);
	HT1621_WriteBuf(21, 0);
}

// 显示单位
void Disp_Fld3UnitOn(TPresTempUnit unit)
{
	Disp_Fld3UnitOff();
	
	if(unit == UNIT_PT_C)
		HT1621_WriteBuf(73, 1);
	else if(unit == UNIT_PT_F)
		HT1621_WriteBuf(72, 1);
	else if(unit == UNIT_PT_MPA)
		HT1621_WriteBuf(61, 1);
}

void Disp_Fld3UnitOff()
{
	HT1621_WriteBuf(73, 0);
	HT1621_WriteBuf(72, 0);
	HT1621_WriteBuf(61, 0);
}

// 显示温度数据
void Disp_Fld4Text(s8 *text)
{
	u8 len = strlen(text), i = 0, c;
	
	// 符号位
	if(len < 4)
		HT1621_WriteBuf(71, 0);
	else
	{
		HT1621_WriteBuf(71, 1);
		i++;
	}
	
	if(len < 3)	// 百位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(11, c);
	
	if(len < 2)	// 十位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(12, c);

	if(len < 1)	// 个位
		c = ' ';
	else 
		c = text[i++];
	Disp_DispChar(13, c);
}

// 显示小数点
void Disp_Fld4PointOn(u8 pos)
{
	Disp_Fld4PointOff();
	
	if(pos == 1)
		HT1621_WriteBuf(111, 1);
	else if(pos == 2)
		HT1621_WriteBuf(91, 1);
}
	
void Disp_Fld4PointOff()
{
	HT1621_WriteBuf(111, 0);
	HT1621_WriteBuf(91, 0);
}

// 显示单位
void Disp_Fld4UnitOn(TPresTempUnit unit)
{
	Disp_Fld4UnitOff();
	
	if(unit == UNIT_PT_C)
		HT1621_WriteBuf(144, 1);
	else if(unit == UNIT_PT_F)
		HT1621_WriteBuf(143, 1);
	else if(unit == UNIT_PT_MPA)
		HT1621_WriteBuf(131, 1);
}

void Disp_Fld4UnitOff()
{
	HT1621_WriteBuf(144, 0);
	HT1621_WriteBuf(143, 0);
	HT1621_WriteBuf(131, 0);
}

// 差压显示切换
void Disp_SwitchDiffUnit(TDiffUnit unit)
{
	HT1621_WriteBuf(293, 0); // %
	HT1621_WriteBuf(294, 0); // L
	HT1621_WriteBuf(291, 0); // kg
	HT1621_WriteBuf(292, 0); // kPa
	HT1621_WriteBuf(301, 0); // mmH2O
	HT1621_WriteBuf(302, 0); // inH2O
	HT1621_WriteBuf(303, 0); // PSI
	HT1621_WriteBuf(304, 0); // CAL

	if(unit == UNIT_CAL)
		HT1621_WriteBuf(304, 1);
	else if(unit == UNIT_PSI)
		HT1621_WriteBuf(303, 1);
	else if(unit == UNIT_INH2O)
		HT1621_WriteBuf(302, 1);
	else if(unit == UNIT_MMH2O)
		HT1621_WriteBuf(301, 1);
	else if(unit == UNIT_KPA)
		HT1621_WriteBuf(292, 1);
	else if(unit == UNIT_KG)
		HT1621_WriteBuf(291, 1);
	else if(unit == UNIT_L)
		HT1621_WriteBuf(294, 1);
	else	// 百分比%
		HT1621_WriteBuf(293, 1);
}	

// 显示液源
void Disp_SwitchSource(TDispSource src)
{
	HT1621_WriteBuf(282, 0);
	HT1621_WriteBuf(283, 0);
	HT1621_WriteBuf(284, 0);
	HT1621_WriteBuf(274, 0);
	HT1621_WriteBuf(273, 0);
	HT1621_WriteBuf(272, 0);
	
	if(src == SOURCE_LNG)
		HT1621_WriteBuf(282, 1);
	else if(src == SOURCE_O2)
		HT1621_WriteBuf(283, 1);
	else if(src == SOURCE_N2)
		HT1621_WriteBuf(284, 1);
	else if(src == SOURCE_AR)
		HT1621_WriteBuf(274, 1);
	else if(src == SOURCE_CO2)
		HT1621_WriteBuf(273, 1);
	else if(src == SOURCE_H2)
		HT1621_WriteBuf(272, 1);
}

void Disp_WriteNeedle(u8 val)
{
	HT1621_WriteBuf(414, val >= 1);
	HT1621_WriteBuf(413, val >= 2);
	HT1621_WriteBuf(412, val >= 3);
	HT1621_WriteBuf(411, val >= 4);
	HT1621_WriteBuf(401, val >= 5);
	HT1621_WriteBuf(402, val >= 6);
	HT1621_WriteBuf(403, val >= 7);
	HT1621_WriteBuf(404, val >= 8);
	HT1621_WriteBuf(394, val >= 9);
	HT1621_WriteBuf(393, val >= 10);
}

// 写电量数据
void Disp_WriteBattery(u8 val)
{
	HT1621_WriteBuf(251, 1);
	HT1621_WriteBuf(244, val >= 1);
	HT1621_WriteBuf(243, val >= 2);
	HT1621_WriteBuf(242, val >= 3);
	HT1621_WriteBuf(241, val >= 4);
}

// 写信号强度
void Disp_WriteStrength(u8 val)
{
	HT1621_WriteBuf(262, 1);
	HT1621_WriteBuf(263, val >= 1);
	HT1621_WriteBuf(264, val >= 2);
	HT1621_WriteBuf(254, val >= 3);
	HT1621_WriteBuf(253, val >= 4);
}	

// 写通讯状态
void Disp_WriteCommunication(u8 val)
{
	HT1621_WriteBuf(252, val != 0);
}

// 写告警状态
void Disp_WriteAlarm(u8 val)
{
	HT1621_WriteBuf(392, val != 0);
}

// 写错误状态
void Disp_WriteError(u8 val)
{
	HT1621_WriteBuf(391, val != 0);
}

// 写充装状态
void Disp_WriteFill(u8 val)
{
	HT1621_WriteBuf(281, val != 0);
}

// 写充电状态
void Disp_WritePower(u8 val)
{
	HT1621_WriteBuf(154, val != 0);
}

// 写RF状态
void Disp_WriteRF(u8 val)
{
	HT1621_WriteBuf(261, val != 0);
}

// 写GPS状态
void Disp_WriteGPS(u8 val)
{
	HT1621_WriteBuf(271, val != 0);
}

// 清屏
void Disp_Clear()
{
	memset(&VideoBuf, 0, sizeof(VideoBuf));
}

// 设置闪烁
void Disp_SetBlinkVisible(u8 visible)
{
	VideoBuf.blink_visible = visible;
}

void Disp_Fld1Blink(u8 blink)
{
	VideoBuf.fld1_blink = blink;
}

void Disp_Fld2Blink(u8 blink)
{
	VideoBuf.fld2_blink = blink;
}

void Disp_Fld3Blink(u8 blink)
{
	VideoBuf.fld3_blink = blink;
}

void Disp_Fld4Blink(u8 blink)
{
	VideoBuf.fld4_blink = blink;
}

void Disp_BatteryBlink(u8 blink)
{
	VideoBuf.battery_blink = blink;
}

// ********************************************
// 以下为窗口界面实现
// ********************************************

// 设置焦点显示
void Disp_SetCursor(u8 pos, u8 width)
{
	VideoBuf.cursor_pos = pos;
	VideoBuf.cursor_width = width;
}

void Disp_ClearCursor()
{
	VideoBuf.cursor_pos = 0;
	VideoBuf.cursor_width = 0;
}

// 选择框函数
// 刷新显示
void SelectBox_Refresh(TSelectBox *pBox)
{
	u8 i;
	
	for(i = 0; i < strlen(pBox->items[pBox->idx]) && i < pBox->width; i++)
		Disp_DispChar(pBox->pos + i, pBox->items[pBox->idx][i]);
	for(; i < pBox->width; i++)
		Disp_DispChar(pBox->pos + i, ' ');
}

// 创建选择框
void SelectBox_Create(TSelectBox *pBox, u8 pos, u8 width, u8 count, const char items[][5], u8 idx)
{
	u8 i;
	
	memset(pBox, 0, sizeof(TSelectBox));
	
	pBox->pos = pos;
	pBox->width = width;
	pBox->count = count;
	for(i = 0; i < count; i++)
		strcpy(pBox->items[i], items[i]);
	pBox->idx = idx;

	// 刷新显示
	SelectBox_Refresh(pBox);
}

// 设置只读属性
void SelectBox_ReadOnly(TSelectBox *pBox)
{
	pBox->read_only = 1;
}

// 设置隐藏属性
void SelectBox_Hide(TSelectBox *pBox)
{
	pBox->hide = 1;
}

// 选择框处理按键
u8 SelectBox_OnKey(TSelectBox *pBox, u8 key)
{
	u8 idx = pBox->idx;
	
	if(key == KEY_FUNC)
		return key;			// 留给上一级处理

	if(key == KEY_UP)
	{
		if(!pBox->read_only)
		{
			if(pBox->idx == pBox->count - 1)
				pBox->idx = 0;
			else
				pBox->idx++;
		}
	}
	else if(key == KEY_DOWN)
	{
		if(!pBox->read_only)
		{
			if(pBox->idx == 0)
				pBox->idx = pBox->count - 1;
			else
				pBox->idx--;
		}
	}
			
	// 刷新显示
	if(idx != pBox->idx)
		SelectBox_Refresh(pBox);
	
	return KEY_INVALID;		// 已处理
}

// 选择框获得焦点
void SelectBox_SetFocus(TSelectBox *pBox)
{
	Disp_SetCursor(pBox->pos, pBox->width);
}

// 编辑框函数

// 刷新显示
void EditBox_Refresh(TEditBox *pEdit)
{
	u8 i, j;
	
	for(i = pEdit->left_idx, j = 0; i < strlen(pEdit->text) && i < pEdit->len && j < pEdit->width; i++, j++)
		Disp_DispChar(pEdit->pos + j, pEdit->text[i]);
	for(; j < pEdit->width; j++)
		Disp_DispChar(pEdit->pos + j, ' ');
}

// 创建编辑框
void EditBox_Create(TEditBox *pEdit, u8 pos, u8 width, u8 len, char *text)
{
	memset(pEdit, 0, sizeof(TEditBox));
	
	pEdit->pos = pos;
	pEdit->width = width;
	pEdit->len = len;
	strcpy(pEdit->text, text);
	pEdit->left_idx = 0;
	pEdit->curr_idx = 0;
	pEdit->read_only = 0;
	
	// 刷新显示
	EditBox_Refresh(pEdit);
}

// 设置只读属性
void EditBox_ReadOnly(TEditBox *pEdit)
{
	pEdit->read_only = 1;
}

// 设置隐藏属性
void EditBox_Hide(TEditBox *pEdit)
{
	pEdit->hide = 1;
}

// 编辑框处理按键
u8 EditBox_OnKey(TEditBox *pEdit, u8 key)
{
	u8 idx = pEdit->curr_idx;
	u8 c = pEdit->text[idx];
	
	if(key == KEY_FUNC)
		return key;			// 跳出EditBox，留给上一级处理
	
	if(key == KEY_UP)
	{
		if(!pEdit->read_only)
		{
			if(pEdit->curr_idx == 0 && (pEdit->text[pEdit->curr_idx] == '+' || pEdit->text[pEdit->curr_idx] == '-'))
			{
				if(pEdit->text[pEdit->curr_idx] == '+')
					pEdit->text[pEdit->curr_idx] = '-';
				else
					pEdit->text[pEdit->curr_idx] = '+';
			}
			else if(pEdit->text[pEdit->curr_idx] == '9' || !isdigit(pEdit->text[pEdit->curr_idx]))
				pEdit->text[pEdit->curr_idx] = '0';
			else
				pEdit->text[pEdit->curr_idx]++;
		}
	}
	else if(key == KEY_DOWN)
	{
		if(!pEdit->read_only)
		{
			if(pEdit->curr_idx == 0 && (pEdit->text[pEdit->curr_idx] == '+' || pEdit->text[pEdit->curr_idx] == '-'))
			{
				if(pEdit->text[pEdit->curr_idx] == '+')
					pEdit->text[pEdit->curr_idx] = '-';
				else
					pEdit->text[pEdit->curr_idx] = '+';
			}
			else if(pEdit->text[pEdit->curr_idx] == '0' || !isdigit(pEdit->text[pEdit->curr_idx]))
				pEdit->text[pEdit->curr_idx] = '9';
			else
				pEdit->text[pEdit->curr_idx]--;
		}
	}
	else if(key == KEY_SET)
	{
		// 光标移动
		if(pEdit->curr_idx == pEdit->len - 1)
		{
			pEdit->curr_idx = 0;
			pEdit->left_idx = 0;
		}
		else
		{
			pEdit->curr_idx++;
			// 处理滚动
			if(pEdit->curr_idx >= pEdit->left_idx + pEdit->width)
				pEdit->left_idx++;
		}
		// 设置光标
		Disp_SetCursor(pEdit->pos + (pEdit->curr_idx - pEdit->left_idx), 1);
	}
			
	// 刷新显示
	if(idx != pEdit->curr_idx || c != pEdit->text[pEdit->curr_idx])
		EditBox_Refresh(pEdit);
	
	return KEY_INVALID;		// 已处理
}

// 编辑框获得焦点
void EditBox_SetFocus(TEditBox *pEdit)
{
	pEdit->left_idx = 0;
	pEdit->curr_idx = 0;
	Disp_SetCursor(pEdit->pos, 1);
}

// 按钮函数

// 刷新显示
void Button_Refresh(TButton *pButton)
{
	u8 i;
	
	for(i = 0; i < strlen(pButton->caption) && i < pButton->width; i++)
		Disp_DispChar(pButton->pos + i, pButton->caption[i]);
	for(; i < pButton->width; i++)
		Disp_DispChar(pButton->pos + i, ' ');
}

// 创建按钮
void Button_Create(TButton *pButton, u8 pos, u8 width, char *caption)
{
	memset(pButton, 0, sizeof(TButton));
	
	pButton->pos = pos;
	pButton->width = width;
	strcpy(pButton->caption, caption);
}

// 设置隐藏属性
void Button_Hide(TButton *pButton)
{
	pButton->hide = 1;
}

// 按钮处理按键
u8 Button_OnKey(TButton *pButton, u8 key)
{
	if(key == KEY_FUNC)
		return key;			// 跳出Button，留给上一级处理
	if(key == KEY_SET)
		return key;			// 执行，留给上一级处理

	return KEY_INVALID;		// 已处理
}

// 按钮获得焦点
void Button_SetFocus(TButton *pButton)
{
	Disp_SetCursor(pButton->pos, pButton->width);
}
