

#include "digitaltube.h"
#include "shell.h"
#include "string.h"

//-------------------------------------------------------------

/*
	    a
	*********
	*		*
   f*		*b
	*	g	*
	*********
	*		*
   e*		*c
	*	d	*  **p
	*********  **
*/

// 数码管的段定义
#define LED_A	0x01
#define LED_B	0x02
#define LED_C	0x04
#define LED_D	0x08
#define LED_E	0x10
#define LED_F	0x20
#define LED_G	0x40
#define LED_P	0x80

//-------------------------------------------------------------

// 显示字符定义

// 数字
static const u8 g_ledNumList[10] =
{
	(LED_A|LED_B|LED_C|LED_D|LED_E|LED_F|	0)	,	// '0',
	(LED_B|LED_C)								,	// '1',
	(LED_A|LED_B|LED_D|LED_E|LED_G)				,	// '2',
	(LED_A|LED_B|LED_C|LED_D|LED_G)				,	// '3',
	(LED_B|LED_C|LED_F|LED_G)					,	// '4',
	(LED_A|LED_C|LED_D|LED_F|LED_G)				,	// '5',
	(LED_A|LED_C|LED_D|LED_E|LED_F|LED_G)		,	// '6',
	(LED_A|LED_B|LED_C)							,	// '7',
	(LED_A|LED_B|LED_C|LED_D|LED_E|LED_F|LED_G)	,	// '8',
	(LED_A|LED_B|LED_C|LED_D|LED_F|LED_G)		,	// '9',
};

// 字母
static const u8 g_ledCharList[26] =
{
	(LED_A|LED_B|LED_C|LED_E|LED_F|LED_G)		,	// 'A',
	(LED_C|LED_D|LED_E|LED_F|LED_G)				,	// 'B',
	(LED_A|LED_D|LED_E|LED_F)					,	// 'C',
	(LED_B|LED_C|LED_D|LED_E|LED_G)				,	// 'D',
	(LED_A|LED_D|LED_E|LED_F|LED_G)				,	// 'E',
	(LED_A|LED_E|LED_F|LED_G)					,	// 'F',
	(LED_A|LED_C|LED_D|LED_E|LED_F)				,	// 'G',
	(LED_B|LED_C|LED_E|LED_F|LED_G)				,	// 'H',
	(LED_E)										,	// 'I',
	(LED_B|LED_C|LED_D|LED_E)					,	// 'J',
	(LED_B|LED_D|LED_E|LED_F|LED_G)				,	// 'K',
	(LED_D|LED_E|LED_F)							,	// 'L',
	(LED_A|LED_C|LED_E|LED_G)					,	// 'M',
	(LED_C|LED_E|LED_G)							,	// 'N',
	(LED_C|LED_D|LED_E|LED_G)					,	// 'O',
	(LED_A|LED_B|LED_E|LED_F|LED_G)				,	// 'P',
	(LED_A|LED_B|LED_C|LED_F|LED_G)				,	// 'Q',
	(LED_E|LED_G)								,	// 'R',
	(LED_A|LED_C|LED_F|LED_G)					,	// 'S',
	(LED_D|LED_E|LED_F|LED_G)					,	// 'T',
	(LED_B|LED_C|LED_D|LED_E|LED_F)				,	// 'U',
	(LED_B|LED_C|LED_D|LED_E|LED_F|LED_G)		,	// 'V',
	(LED_A|LED_C|LED_D|LED_E)					,	// 'W',
	(LED_B|LED_D|LED_F|LED_G)					,	// 'X',
	(LED_B|LED_C|LED_D|LED_F|LED_G)				,	// 'Y',
	(LED_A|LED_D|LED_G)							,	// 'Z',
};

typedef struct
{
	u8 ch;
	u8 code;
}SignMap;

#define SIGN_NUM	5
// 符号定义
static const SignMap g_ledSignList[SIGN_NUM] =
{
	{'\0', (0)}, 								// 空字符
	{' ', (0)}, 								// 空格
	{'`', (0)}, 								// 显示为空
	{'-', (LED_G)}, 							// 减号（横杠）
	{'_', (LED_D)}, 							// 下画线
};

// 得到LED显示值
u8 GetLedValue(u8 ch)
{
	u8 code = 0;
	if (ch >= '0' && ch <= '9')		// 数字
	{
		code = g_ledNumList[ch-'0'];
	}
	else if (ch >= 'a' && ch <= 'z')		// 小写字母
	{
		code = g_ledCharList[ch-'a'];
	}
	else if (ch >= 'A' && ch <= 'Z')		// 大写字母
	{
		code = g_ledCharList[ch-'A'];
	}
	else
	{
		int i;
		for (i = 0; i < SIGN_NUM; i++)
		{
			if (g_ledSignList[i].ch == ch)
			{
				code = g_ledSignList[i].code;
				break;
			}
		}
	}

	return (code & 0x7F);
}

// 添加点的信息
u8 AddDota(u8 ch)
{
	return ch |= LED_P;
}

//-------------------------------------------------------------

typedef struct
{
	u8 ledvalue[LED_NUM];	// 显示值
	u8 dotvalue[LED_NUM];	// 显示点
	u8 dispBuf[LED_NUM];	// 显示值
	u8 displev;				// 显示亮度
}LedCtrl;

LedCtrl g_ledctrl;

void TestLeds(char * para1, char * para2);
void MakeLedsDisp(void);

void InitDigialtubes(void)
{
	memset(&g_ledctrl, 0, sizeof(LedCtrl));
	g_ledctrl.displev = DISP_LEV(3)|DISP_ON;

	TM1637SetLedDisp(0);

	MakeLedsDisp();
	TM1637WriteLedData(LED_REG_BEG_ADDR, g_ledctrl.dispBuf, g_ledctrl.displev);

	TM1637SetLedDisp(5);

	//AddShellCmd("LED", "test leds", TestLeds);
}

void MakeLedsDisp(void)
{
	int i;
	u8 disp;
	for (i = 0; i < LED_NUM; i++)
	{
		disp = GetLedValue(g_ledctrl.ledvalue[i]);
		if (g_ledctrl.dotvalue[i] == '.')
		{
			disp = AddDota(disp);
		}
		g_ledctrl.dispBuf[i] = disp;
	}
}

void DisplayOnDig(char * strBuf, char * dotBuf)
{
	int len;
	memset(g_ledctrl.ledvalue, 0, LED_NUM);
	memset(g_ledctrl.dotvalue, 0, LED_NUM);

	if (strBuf != NULL)
	{
		len = strlen(strBuf);
		if (len > LED_NUM)
		{
			len = LED_NUM;
		}
		memcpy(g_ledctrl.ledvalue, strBuf, len);
	}

	if (dotBuf != NULL)
	{
		len = strlen(dotBuf);
		if (len > LED_NUM)
		{
			len = LED_NUM;
		}
		memcpy(g_ledctrl.dotvalue, dotBuf, len);
	}

	MakeLedsDisp();
	TM1637WriteLedData(LED_REG_BEG_ADDR, g_ledctrl.dispBuf, g_ledctrl.displev);
}

//-------------------------------------------------------------
// 测试功能

void TestLeds(char * para1, char * para2)
{
	int p1, p2;

	if (para1 == NULL || para2 == NULL)
	{
		return;
	}

	printf("para1=%s, para2=%s\r\n", para1, para2);

	p1 = 0;
	p2 = 0;
	if (strcmp(para1, "") != 0)
	{
		p1 = atoi(para1);
		if (strcmp(para2, "") != 0)
		{
			p2 = atoi(para2);
		}
	}

	if (p1 == p2)
	{
	}

	//--------------------------------

	{
		if (p1 == 0)
		{
			if (p2 == 100)
			{
				memset(&g_ledctrl, 0, sizeof(LedCtrl));
				g_ledctrl.displev = DISP_LEV(3)|DISP_ON;
			}
		}
		else if (p1 > 0 && p1 <= 5)
		{
			g_ledctrl.ledvalue[p1-1] = p2;
		}
		else if (p1 > 10 && p1 <= 15)
		{
			g_ledctrl.dotvalue[p1-11] = p2;
		}
		else if (p1 == 100)
		{
			memset(g_ledctrl.ledvalue, 0, LED_NUM);
			memcpy(g_ledctrl.ledvalue, para2, LED_NUM);
		}

		MakeLedsDisp();
		TM1637WriteLedData(LED_REG_BEG_ADDR, g_ledctrl.dispBuf, g_ledctrl.displev);
	}

}

//-------------------------------------------------------------




