
//-------------------------------------------------------------------------------
// File Name:	debug.c
// Brief: 
// Version:		1.0.0
// Create Date: 2018/03/20
// Create by: 	Marshal Lee
// Copyright: 
//				Copyright (c)  2017, Richpeace Co., LTD.
//				All rights reserved.
//
// Modify by:	Marshal Lee
// Modify Date:	2018/03/20
//-------------------------------------------------------------------------------

#define _IN_DEBUG_C

#include "debug.h"
#include "inout.h"
#include "shell.h"
#include "delay.h"


//--------------------------------------------------------------------
void TestInputs(char * para1, char * para2);
void TestOutput(char * para1, char * para2);
//--------------------------------------------------------------------

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

void InitDebug(void)
{
	AddShellCmd("INPUT", "input detect", TestInputs);
	AddShellCmd("OUTPUT", "output control", TestOutput);

#if (0)
	do
	{
		TestOutput("1", "1000");
	}while(1);
#endif
}




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

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

char g_inputName[INPUT_NUM][INPUT_STR_LEN] = 
{
	"输入1",
	"输入2",
	"输入3",
	"输入4",
	"输入5",
	"输入6",
	"输入7",
	"输入8",
};

#define	INPUT_OP(cs, name)	do\
{\
	temp = name();\
	if (temp != inputsta[cs])\
	{\
		inputsta[cs] = temp;\
		printf("%d. (%s) is %d\r\n", inidx++, g_inputName[cs], temp);\
	}\
}while(0)


void TestInputs(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)
	{
	}

	//--------------------------------
	{
		int inidx = 0;
		u8 inputsta[INPUT_NUM];
		u8 temp;
		memset(inputsta, 0xff, INPUT_NUM);

		do
		{
			INPUT_OP(0, GetInput1Status);
			INPUT_OP(1, GetInput2Status);
			INPUT_OP(2, GetInput3Status);
			INPUT_OP(3, GetInput4Status);
			INPUT_OP(4, GetInput5Status);
			INPUT_OP(5, GetInput6Status);
			INPUT_OP(6, GetInput7Status);
			INPUT_OP(7, GetInput8Status);

			DelayMs(10);

			if (IsConsoleCancel() != 0 || p1 == 0)		// 终端输入停止
			{
				break;
			}

		}while(p1 != 0);
	}
}

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

char g_outputName[OUTPUT_NUM][OUTPUT_STR_LEN] = 
{
	"输出1",
	"输出2",
	"输出3",
	"输出4",
	"输出5",
	"输出6",
	"输出7",
	"输出8",
};


#define	TOUT_OP(cs, name, dl)	do\
{\
	if (p2 == 0)\
	{\
		Set##name##Off();\
		printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
	}\
	else if (p2 == 1)\
	{\
		Set##name##On();\
		printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
	}\
	else if (p2 == 2)\
	{\
		Set##name##On();\
		printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
		DelayMs(dl);\
		Set##name##Off();\
		printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
	}\
	else if (p2 > 0)\
	{\
		count = dl*p2;\
		while(count > 0) \
		{\
			count--;\
			Set##name##On();\
			printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
			delay = (p2+9)/10;\
			do \
			{ \
				DelayMs(10);\
				if (IsConsoleCancel() != 0) \
				{\
					break;\
				}\
				delay--;\
			}while(delay != 0);\
			if (delay != 0)\
			{\
				break;\
			}\
			Set##name##Off();\
			printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
			delay = (p2+9)/10;\
			delay *= 4;\
			do \
			{ \
				DelayMs(10);\
				if (IsConsoleCancel() != 0) \
				{\
					break;\
				}\
				delay--;\
			}while(delay != 0);\
			if (delay != 0)\
			{\
				break;\
			}\
		}\
		Set##name##Off();\
		printf("%d. (%s) is %d\r\n", cs+1, g_outputName[cs], Get##name##Status());\
	}\
}while(0)


void TestOutput(char * para1, char * para2)
{
	int p1, p2;
	int count, delay;

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

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

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

	if (p1 == p2)
	{
	}

	//--------------------------------
	if (p1 == 0)
	{
		if (p2 == 0)
		{
			TOUT_OP(0, Output1, 1000);
			TOUT_OP(1, Output2, 1000);
			TOUT_OP(2, Output3, 1000);
			TOUT_OP(3, Output4, 1000);
			TOUT_OP(4, Output5, 1000);
			TOUT_OP(5, Output6, 1000);
			TOUT_OP(6, Output7, 1000);
			TOUT_OP(7, Output8, 1000);
		}
		else if (p2 == 1)
		{
			
		}
	}
	else if (p1 < 100)
	{
		switch(p1)
		{
		case 1:
			TOUT_OP(0, Output1, 1000);
			break;
		case 2:
			TOUT_OP(1, Output2, 1000);
			break;
		case 3:
			TOUT_OP(2, Output3, 1000);
			break;
		case 4:
			TOUT_OP(3, Output4, 1000);
			break;
		case 5:
			TOUT_OP(4, Output5, 1000);
			break;
		case 6:
			TOUT_OP(5, Output6, 1000);
			break;
		case 7:
			TOUT_OP(6, Output7, 1000);
			break;
		case 8:
			TOUT_OP(7, Output8, 1000);
			break;
		default:
			break;
		}
	}
}

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

void SetInputNameString(const void* pDat)
{
	memcpy(g_inputName, pDat, INPUT_NUM*INPUT_STR_LEN);	
}

void SetOutputNameString(const void* pDat)
{
	memcpy(g_outputName, pDat, OUTPUT_NUM*OUTPUT_STR_LEN);	
}
