#include "riadenie.h"
#include "printf-stdarg.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "kom_modul.h"
#include "LCD.h"
#include "string.h"
#include "console_reader.h"

void LCDwrite2(char *data){
	LCD_Clear();
	LCD_PutString(0,data);

}
struct init_PRIKAZ const commandSSS[]={{"LCD",LCDwrite2},{0,NULL}};


/*
 **==========================================================================================
 ** Function: vStartControlTask
 ** Arguments: unsigned char
 ** Return:  void
 ** Comment: Startovanie taskov potrebnych na kontrol
**===========================================================================================
*/
void vStartControlTask(unsigned char priority) {

	xTaskCreate(vTaskControl,"Control",150,NULL,priority,NULL);

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

/*
 **==========================================================================================
 ** Function: command_parser
 ** Arguments: char* - buffer prikaz napr. LCD napisTOTO
 ** Return:  void
 ** Comment: Analyzuje vstupny buffer a ak je najdeny prikaz tak sa vykona
**===========================================================================================
*/
void command_parser(char *buffer) {
	uint8 najdenyPrikaz,temp,temp2;

	for (temp = 0; commandSSS[temp].meno != 0; temp++) {

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

		if (strcmp(buffer, commandSSS[temp].meno) == 0) {

			if (najdenyPrikaz == 127) {
				commandSSS[temp].funkcia(buffer + temp2 + 1);
			} else
			commandSSS[temp].funkcia(NULL);

			return;
		}
		if (najdenyPrikaz == 127)
		buffer[temp2] = ' ';
	}
	xprintf("\n\rUnknow commandSSS")
	;

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


void vTaskControl(void *pvParameters) {
	char buffer[sizeQueue];
	uint8 i=0;
	LCD_Clear();
	LCD_PutCenterString(0,"Prilozte Kartu!");
	for (;;) {
		(void) xQueueReceive(komInSocketQueue,buffer,portMAX_DELAY);

		buffer[strlen(buffer)]='\0';

		xprintf("\n\r");
		for(i=0;i<strlen(buffer)+1;i++){
			xprintf("%x ",buffer[i]);
		}
		xprintf("%s",buffer);
		command_parser(buffer);


	}

}
