#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 } };

void vTaskTimer(void *pvParameters) {
	portTickType prevTime;
	CASOVAC buffer;
	unsigned char i, pocetSprav;
	prevTime = xTaskGetTickCount();
	for (;;) {
		vTaskDelayUntil(&prevTime, 100); //Zobudenie kazdych 100ms
		pocetSprav = uxQueueMessagesWaiting(casovacQueue);
		for (i = 0; i < pocetSprav; i++) {
			(void) xQueueReceive(casovacQueue,&buffer,portMAX_DELAY);
			buffer.ms100--;
			//xprintf("\n\rTimer: %d  MS:%d Opak: %d",i,buffer.ms100,buffer.opakovanie);

			if (buffer.ms100 == 0) {
				if (buffer.opakovanie > 0) {
					buffer.ms100 = buffer.ms100orig;
					buffer.opakovanie--;
					buffer.funkcia(buffer.opakovanie);
					xQueueSend(casovacQueue,&buffer,portMAX_DELAY);
				} else if (buffer.opakovanie == 0) {
					buffer.ms100 = buffer.ms100orig;
					//buffer.funkcia(0);

				} else {
					buffer.ms100 = buffer.ms100orig;
					xQueueSend(casovacQueue,&buffer,portMAX_DELAY);
					buffer.funkcia(-1);
				}

			}else{
				xQueueSend(casovacQueue,&buffer,portMAX_DELAY);
			}

		}

	}
}

/*
 **==========================================================================================
 ** 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);
	xTaskCreate(vTaskTimer,"Timer",250,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);

	}

}
