#include "includes.h"

// 显示一个8段码
// pos: Fld1为0~4（从左到右，真值表1~5)
void Disp_DispChar(u8 pos, char c)
{
	if(pos == 0)
		HT1621_WriteChar(c, 11, 21, 23, 14, 13, 12, 22); 
	else if(pos == 1)
		HT1621_WriteChar(c, 31, 41, 43, 34, 33, 32, 42); 
	else if(pos == 2)
		HT1621_WriteChar(c, 51, 61, 63, 54, 53, 52, 62); 
	else if(pos == 3)
		HT1621_WriteChar(c, 71, 81, 83, 74, 73, 72, 82); 
}

// 显示数据
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_Fld1PointOff()
{
	HT1621_WriteBuf(24, 0);
	HT1621_WriteBuf(44, 0);
	HT1621_WriteBuf(64, 0);
}	
// 显示小数点
void Disp_Fld1PointOn(u8 pos)
{
	Disp_Fld1PointOff();

	if(pos == 1)			// 小数点1位
		HT1621_WriteBuf(24, 1);
	else if(pos == 2)		// 小数点2位
		HT1621_WriteBuf(44, 1);
	else if(pos == 3)		// 小数点3位
		HT1621_WriteBuf(64, 1);
}
	
//void Disp_Fld1PointOff()
//{
//	HT1621_WriteBuf(124, 0);
//	HT1621_WriteBuf(104, 0);
//	HT1621_WriteBuf(84, 0);
//	HT1621_WriteBuf(64, 0);
//}

// 显示单位切换
void Disp_SwitchDiffUnit(TDiffUnit unit)
{
	Disp_Fld1UnitOff();

//	if(unit == UNIT_KPA)
//		HT1621_WriteBuf(161, 1);
//	else if(unit == UNIT_MMH2O)
//		HT1621_WriteBuf(294, 1);
//	else	// UNIT_INH2O
//		HT1621_WriteBuf(144, 1);
	
			HT1621_WriteBuf(131, 1);//D
		  HT1621_WriteBuf(84, 1); // K
			HT1621_WriteBuf(94, 1);//PA
}	

void Disp_SwitchPresUnit(TPresUnit unit)
{
	Disp_Fld1UnitOff();
//	HT1621_WriteBuf(163, 1);
//	HT1621_WriteBuf(164, 1);
		HT1621_WriteBuf(93, 1);	// MPA
	HT1621_WriteBuf(132, 1);	//P
}

void Disp_SwitchTempUnit(TTempUnit unit)
{
	Disp_Fld1UnitOff();
//	HT1621_WriteBuf(162, 1);
			HT1621_WriteBuf(133, 1);	//T
		HT1621_WriteBuf(92, 1); // ℃
}

void Disp_SwitchVacuUnit(TVacuUnit unit)
{
	Disp_Fld1UnitOff();
	//HT1621_WriteBuf(164, 1);
			HT1621_WriteBuf(131, 1);//D
			HT1621_WriteBuf(94, 1);//PA
}

void Disp_Fld1UnitOff()
{
//	HT1621_WriteBuf(294, 0);	// mmH2O
//	HT1621_WriteBuf(161, 0);	// kPa
//	HT1621_WriteBuf(162, 0);	// ℃
//	HT1621_WriteBuf(163, 0);	// M
//	HT1621_WriteBuf(164, 0);	// Pa
//	HT1621_WriteBuf(144, 0);	// inH2O
	HT1621_WriteBuf(94, 0); // Pa
	HT1621_WriteBuf(84, 0); // K	
	HT1621_WriteBuf(92, 0); // ℃
	HT1621_WriteBuf(93, 0); // MPa
  HT1621_WriteBuf(131, 0);//D
  HT1621_WriteBuf(132, 0);	//P
	HT1621_WriteBuf(133, 0);	//T
}

// 显示通道切换
void Disp_ChannelOn(uint8_t channel)
{
//	Disp_ChannelOff();
//	if(channel == 1)
//		HT1621_WriteBuf(151, 1);
//	else if(channel == 2)
//		HT1621_WriteBuf(152, 1);
//	else if(channel == 3)
//		HT1621_WriteBuf(153, 1);
//	else if(channel == 4)
//		HT1621_WriteBuf(154, 1);
}

void Disp_ChannelOff()
{
//	HT1621_WriteBuf(151, 0);	// 1
//	HT1621_WriteBuf(152, 0);	// 2
//	HT1621_WriteBuf(153, 0);	// 3
//	HT1621_WriteBuf(154, 0);	// 4
}

// 显示液源
void Disp_SwitchSource(TDispSource src)
{
}

void Disp_WriteNeedle(u8 val)
{
//	static uint8_t last_val = 0;
//	const uint16_t pinCom[51] = 
//	{
//		292, 291, 
//		281, 282, 283, 284, 274, 273, 272, 271,
//		261, 262, 263, 264, 254, 253, 252, 251,
//		241, 242, 243, 244, 234, 233, 232, 231,
//		221, 222, 223, 224, 214, 213, 212, 211,
//		201, 202, 203, 204, 194, 193, 192, 191,
//		181, 182, 183, 184, 174, 173, 172, 171,
//		311
//	};
//		
//	// 清除原来的指针
//	if(last_val < 51)
//		HT1621_WriteBuf(pinCom[last_val], 0);
//	else
//		HT1621_WriteBuf(pinCom[50], 0);

//	// 写新的指针
//	if(val < 51)
//		HT1621_WriteBuf(pinCom[val], 1);
//	else
//		HT1621_WriteBuf(pinCom[50], 1);
//	
//	// 保存指针位置
//	last_val = val;
}

// 写电量数据
void Disp_WriteBattery(u8 val)
{
	HT1621_WriteBuf(101, val <= 1); //S15
	HT1621_WriteBuf(111, val >= 0); //S14
	HT1621_WriteBuf(103, val >= 1); //S10
	HT1621_WriteBuf(102, val >= 2); //S11
	HT1621_WriteBuf(113, val >= 3); //S12
	HT1621_WriteBuf(112, val >= 4); //S13
}

// 写信号强度
void Disp_WriteStrength(u8 val)
{
	if(val>0)
	HT1621_WriteBuf(124, 1);
	HT1621_WriteBuf(123, val >= 1);//S6
	HT1621_WriteBuf(122, val >= 2);//S5
	HT1621_WriteBuf(121, val >= 3);
//	HT1621_WriteBuf(341, val >= 4);
}	

// 写通讯状态
void Disp_WriteCommunication(u8 val)
{
	HT1621_WriteBuf(134, val >= 1);//DWON
	HT1621_WriteBuf(104, val >= 1);//UP
}

// 写告警状态
void Disp_WriteAlarm(u8 val)
{
}

// 写错误状态
void Disp_WriteError(u8 val)
{
}

// 写充装状态
void Disp_WriteFill(u8 val)
{
}

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

// 写RF状态
void Disp_WriteRF(u8 val)
{
}

// 写GPS状态
void Disp_WriteGPS(u8 val)
{
}

// 清屏
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_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->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->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);
}
