#include "includes.h"

// 显示一个8段码
// pos: Fld1为0~3（从左到右，真值表1~4)，Fld2为4~6（真值表5~7），
//		Fld3为7~9（真值表8~10），Fld4为10~12（真值表11~13)
void Disp_DispChar(u8 pos, char c)
{
	if(pos == 0)
		HT1621_WriteChar(c, 243, 233, 235, 246, 245, 244, 234);
	else if(pos == 1)
		HT1621_WriteChar(c, 223, 213, 215, 226, 225, 224, 214); 
	else if(pos == 2)
		HT1621_WriteChar(c, 203, 193, 195, 206, 205, 204, 194); 
	else if(pos == 3)
		HT1621_WriteChar(c, 183, 174, 176, 186, 185, 184, 175); 
	else if(pos == 4)
		HT1621_WriteChar(c, 131, 132, 112, 101, 111, 121, 122); 
	else if(pos == 5)
		HT1621_WriteChar(c, 133, 134, 114, 103, 113, 123, 124); 
	else if(pos == 6)
		HT1621_WriteChar(c, 135, 136, 116, 105, 115, 125, 126); 
	else if(pos == 7)
		HT1621_WriteChar(c, 41, 42, 22, 12, 21, 31, 32); 
	else if(pos == 8)
		HT1621_WriteChar(c, 43, 44, 24, 14, 23, 33, 34); 
	else if(pos == 9)
		HT1621_WriteChar(c, 45, 46, 26, 16, 25, 35, 36); 
	else if(pos == 10)
		HT1621_WriteChar(c, 66, 65, 85, 95, 86, 76, 75); 
	else if(pos == 11)
		HT1621_WriteChar(c, 64, 63, 83, 93, 84, 74, 73); 
	else if(pos == 12)
		HT1621_WriteChar(c, 62, 61, 81, 91, 82, 72, 71); 
}

// 显示差压数据
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);
}
	
// 显示小数点或x10
void Disp_Fld1PointMultiOn(u8 posOrMulti)
{
	if(posOrMulti < 1 || posOrMulti > 4)
		return;

	Disp_Fld1PointMultiOff();

	if(posOrMulti == 1)				// 小数点1位
		HT1621_WriteBuf(236, 1);
	else if(posOrMulti == 2)		// 小数点2位
		HT1621_WriteBuf(216, 1);
	else if(posOrMulti == 3)		// 小数点3位
		HT1621_WriteBuf(196, 1);
	else							// 显示x10
		HT1621_WriteBuf(145, 1);
}
	
void Disp_Fld1PointMultiOff()
{
	HT1621_WriteBuf(236, 0);
	HT1621_WriteBuf(216, 0);
	HT1621_WriteBuf(196, 0);
	HT1621_WriteBuf(145, 0);
}

void Disp_Fld1PointOff()
{
	HT1621_WriteBuf(236, 0);
	HT1621_WriteBuf(216, 0);
	HT1621_WriteBuf(196, 0);
}

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

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

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

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

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

void Disp_Fld2UnitOff()
{
	HT1621_WriteBuf(146, 0);
	HT1621_WriteBuf(106, 0);
}

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

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

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

// 显示单位
void Disp_Fld3UnitOn(TPresTempUnit unit)
{
	if(unit == UNIT_PT_C)
		HT1621_WriteBuf(55, 1);
	else if(unit == UNIT_PT_F)
		HT1621_WriteBuf(54, 1);
	else if(unit == UNIT_PT_MPA)
		HT1621_WriteBuf(56, 1);
}

void Disp_Fld3UnitOff()
{
	HT1621_WriteBuf(55, 0);
	HT1621_WriteBuf(54, 0);
	HT1621_WriteBuf(56, 0);
}

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

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

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

// 显示单位
void Disp_Fld4UnitOn(TPresTempUnit unit)
{
	if(unit == UNIT_PT_C)
		HT1621_WriteBuf(51, 1);
	else if(unit == UNIT_PT_F)
		HT1621_WriteBuf(52, 1);
	else if(unit == UNIT_PT_MPA)
		HT1621_WriteBuf(53, 1);
}

void Disp_Fld4UnitOff()
{
	HT1621_WriteBuf(51, 0);
	HT1621_WriteBuf(52, 0);
	HT1621_WriteBuf(53, 0);
}

// 差压显示切换
void Disp_SwitchDiffUnit(TDiffUnit unit)
{
	HT1621_WriteBuf(182, 0); // %
	HT1621_WriteBuf(181, 0); // L
	HT1621_WriteBuf(171, 0); // m3
	HT1621_WriteBuf(173, 0); // kg
	HT1621_WriteBuf(172, 0); // kPa
	HT1621_WriteBuf(141, 0); // mmH2O
	HT1621_WriteBuf(142, 0); // inH2O
	HT1621_WriteBuf(144, 0); // PSI
	HT1621_WriteBuf(143, 0); // CAL

	if(unit == UNIT_CAL)
		HT1621_WriteBuf(143, 1);
	else if(unit == UNIT_PSI)
		HT1621_WriteBuf(144, 1);
	else if(unit == UNIT_INH2O)
		HT1621_WriteBuf(142, 1);
	else if(unit == UNIT_MMH2O)
		HT1621_WriteBuf(141, 1);
	else if(unit == UNIT_KPA)
		HT1621_WriteBuf(172, 1);
	else if(unit == UNIT_KG)
		HT1621_WriteBuf(173, 1);
	else if(unit == UNIT_M3)
		HT1621_WriteBuf(171, 1);
	else if(unit == UNIT_L)
		HT1621_WriteBuf(181, 1);
	else	// 百分比%
		HT1621_WriteBuf(182, 1);
}	

// 显示液源
void Disp_SwitchSource(TDispSource src)
{
	HT1621_WriteBuf(212, 0);
	HT1621_WriteBuf(201, 0);
	HT1621_WriteBuf(202, 0);
	HT1621_WriteBuf(191, 0);
	HT1621_WriteBuf(192, 0);
	HT1621_WriteBuf(156, 0);
	
	if(src == SOURCE_LNG)
		HT1621_WriteBuf(212, 1);
	else if(src == SOURCE_O2)
		HT1621_WriteBuf(201, 1);
	else if(src == SOURCE_N2)
		HT1621_WriteBuf(202, 1);
	else if(src == SOURCE_AR)
		HT1621_WriteBuf(191, 1);
	else if(src == SOURCE_CO2)
		HT1621_WriteBuf(192, 1);
	else // H2
		HT1621_WriteBuf(156, 1);
}

void Disp_WriteNeedle(u8 val)
{
	HT1621_WriteBuf(256, val >= 1);
	HT1621_WriteBuf(255, val >= 2);
	HT1621_WriteBuf(254, val >= 3);
	HT1621_WriteBuf(253, val >= 4);
	HT1621_WriteBuf(252, val >= 5);
	HT1621_WriteBuf(251, val >= 6);
	HT1621_WriteBuf(241, val >= 7);
	HT1621_WriteBuf(242, val >= 8);
	HT1621_WriteBuf(232, val >= 9);
	HT1621_WriteBuf(231, val >= 10);
}

// 写电量数据
void Disp_WriteBattery(u8 val)
{
	HT1621_WriteBuf(155, 1);
	HT1621_WriteBuf(152, val >= 1);
	HT1621_WriteBuf(151, val >= 2);
	HT1621_WriteBuf(153, val >= 3);
	HT1621_WriteBuf(154, val >= 4);
}

// 写信号强度
void Disp_WriteStrength(u8 val)
{
	HT1621_WriteBuf(161, 1);
	HT1621_WriteBuf(162, val >= 1);
	HT1621_WriteBuf(163, val >= 2);
	HT1621_WriteBuf(164, val >= 3);
	HT1621_WriteBuf(165, val >= 4);
}	

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

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

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

// 写充装状态
void Disp_WriteFill(u8 val)
{
	HT1621_WriteBuf(211, 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;
}
