#include "console_reader.h"
#include "printf-stdarg.h"
//#include <stdarg.h>
#include <string.h>
extern int vykon,maxVykon;
void funkcia(char* data)
{
	char text[10];
	uint8 i;
	xprintf("\n\r");
	i = consoleRead(text,10);
	xprintf("\n\rDlzka textu: %s je:%d",text,i);
	xprintf("\n\r %s", data);


}
/*
 **==========================================================================================
 ** Function: cpuVykon
 ** Arguments: char*
 ** Return:  void
 ** Comment: Zobrazy vyuzitie CPU za poslednych 5sec
**===========================================================================================
*/

void cpuVykon(char* data) {
	int tVykon,tMaxVykon;
	tVykon=vykon;
	tMaxVykon=maxVykon;
	xprintf("\n\rVykon: %d \n\r Max Vykon: %d", tVykon,tMaxVykon);
}
/*--END OF FUNCTION: cpuVykon-----------------------------------------------------*/

void heap(char *data){
	xprintf("\n\rHeap size/free: %d/%d",configTOTAL_HEAP_SIZE,xPortGetFreeHeapSize());

}

struct init const prikazy[] =
{ "heap", heap, "sin", funkcia, "cos", funkcia, "atan", funkcia, "ln",
		funkcia, "exp", funkcia, "sqrt", funkcia,"cpu",cpuVykon, 0, NULL };

/*
 **==========================================================================================
 ** Function: startConsole_reader
 ** Arguments: int
 ** Return:  void
 ** Comment: nastartovanie tasky console reader
 **===========================================================================================
 */
void vStartConsole_reader(const int priority, const int stack)
{

	xTaskCreate(console_reader,"ConReadUart0",stack,NULL,priority,NULL);
	xTaskCreate(console_reader_uart1,"Uart1",stack,NULL,priority,NULL);

}
/*--END OF FUNCTION: startConsole_reader-----------------------------------------------------*/

/*
 **==========================================================================================
 ** Function: console_reader
 ** Arguments: void*
 ** Return:  void
 ** Comment: Tento proces vacsinu casu caka na priajate data z Uart0 aked ich prijme tak vyhodnocuje a posiela dalej
 **===========================================================================================
 */
void console_reader(void *pvParameters)
{
	uart0_TComData c;
	char buffer[size_of_console_command_buffer];
	unsigned char index = 0;

	unsigned char temp, najdenyPrikaz;

	uart0_SendChar('>');
	buffer[0] = '\0';

	for (;;)
	{

		xSemaphoreTake(uart0_Mutex.getCharMutex,portMAX_DELAY);
		(void) uart0_RecvChar(&c);

		switch (c)
		{
		case 9: //Tabulator
			najdenyPrikaz = 127;
			for (temp = 0; prikazy[temp].meno != 0; temp++)
			{

				if (strncmp(buffer, prikazy[temp].meno, index) == 0)
				{
					if (najdenyPrikaz == 127)
					{
						najdenyPrikaz = temp;
						najdenyPrikaz |= 0x80;
					}
					else
					{
						if (najdenyPrikaz & 0x80)
						{
							najdenyPrikaz &= 0x7f;
							xprintf("\n\r%s", prikazy[najdenyPrikaz].meno)
							;
						}
						xprintf("\n\r%s", prikazy[temp].meno)
						;
					}
				}
			}
			if (najdenyPrikaz & 0x80)
			{
				najdenyPrikaz &= 0x7f;
				strcpy(buffer, prikazy[najdenyPrikaz].meno);
				index = strlen(prikazy[najdenyPrikaz].meno);
				xprintf("\r>%s", buffer)
				;
			}
			else if (najdenyPrikaz != 127)
			{
				xprintf("\n\r>%s", buffer)
				;
			}
			break;
		case 8: //BackSpace
			if (index != 0)
			{
				index--;
				uart0_SendChar(c);
				uart0_SendChar(' ');
				uart0_SendChar(c);
				buffer[index] = '\0';
			}
			break;

		case '\r':
			if (index > 0)
			{
				buffer[index] = '\0';

				for (temp = 0; prikazy[temp].meno != 0; temp++)
				{
					char temp2;

					for (temp2 = 0; (buffer[temp2] != '\0' && buffer[temp2]
							!= ' '); temp2++)
						;
					if (buffer[temp2] == ' ')
					{
						buffer[temp2] = '\0';
						najdenyPrikaz = 127;
					}
					else
						najdenyPrikaz = 128;

					if (strcmp(buffer, prikazy[temp].meno) == 0)
					{
						prikazy[temp].funkcia(prikazy[temp].meno);
						if (najdenyPrikaz == 127)
						{
							prikazy[temp].funkcia(buffer + temp2 + 1);
							buffer[temp2] = ' ';
						}
						goto koniec;
					}
					if (najdenyPrikaz == 127)
						buffer[temp2] = ' ';
				}
				xprintf("\n\rUnknow command")
				;
			}
			koniec:

			uart0_SendChar('\r');
			uart0_SendChar('\n');
			uart0_SendChar('>');
			index = 0;
			buffer[0] = '\0';

			break;
		case 224:
			/*	if (getch() == 72)
			 {
			 if (history[0] != '\0')
			 {

			 strcpy(buffer, history);
			 temp = index;
			 index = strlen(buffer);

			 printf("\n\r>%s", buffer);

			 }
			 }*/
			break;

		default:
			if (index < size_of_console_command_buffer - 1)
			{
				buffer[index] = (char) c;
				index++;
				buffer[index] = '\0';
				uart0_SendChar(c);
			}
			break;
		}

	}

}
/*--END OF FUNCTION: enclosing_method-----------------------------------------------------*/
/*
 **==========================================================================================
 ** Function: consoleRead
 ** Arguments: char*, const uint8
 ** Return:  uint8
 ** Comment: Nacitanie retazca z konzoly, retazec je ukonceny enterom
 **===========================================================================================
 */
uint8 consoleRead(char *buffer, const uint8 max)
{
	uart0_TComData c;
	unsigned char index = 0;
	buffer[0] = '\0';

	for (;;)
	{

		xSemaphoreTake(uart0_Mutex.getCharMutex,portMAX_DELAY);
		(void) uart0_RecvChar(&c);
		switch (c)
		{
		case 9: //Tabulator
			break;
		case 8: //BackSpace
			if (index != 0)
			{
				index--;
				uart0_SendChar(c);
				uart0_SendChar(' ');
				uart0_SendChar(c);
				buffer[index] = '\0';
			}
			break;

		case '\r':
			return index;
			break;

		default:
			if (index < max)
			{
				buffer[index] = (char) c;
				index++;
				buffer[index] = '\0';
				uart0_SendChar(c);
			}
			break;
		}

	}

}
/*--END OF FUNCTION: consoleRead-----------------------------------------------------*/
/*
**==========================================================================================
** Function: console_reader
** Arguments: void*
** Return:  void
** Comment: Tento proces vacsinu casu caka na priajate data z Uart1 aked ich prijme tak vyhodnocuje a posiela dalej
**===========================================================================================
*/
void console_reader_uart1(void *pvParameters)
{
	uart1_TComData c,buffer[20];
	word i;
	for (;;)
	{
		/*xSemaphoreTake(uart1_Mutex.getCharMutex,portMAX_DELAY);
		(void) uart1_RecvChar(&c);
		uart1_RecvBlock();
		(void) uart1_SendChar(c);*/
		(void)uart1_RecvBlock(buffer,10,&i,portMAX_DELAY);
		buffer[10]='\0';
		xprintf("%s",buffer);
	}
}
	/*--END OF FUNCTION: consoleRead-----------------------------------------------------*/
