/* Zdrojovy kod pro uzivatelske rozhrani */

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

// Include FW knihovny -------------------------------------
#include "stm32f10x_flash.h"	// Kníhovna pro praci s FLASH - pomocne funkce pro knihovnu EEPROM

// Include vlastnich knihoven ------------------------------
#include "user_interface.h"
#include "PIN_config.h"
#include "LCD_HD44780.h"		// Knihovna pro praci s LCD
#include "DS18B20.h"			// Knihovna pro praci s teplotnimi cidly DS18B20
#include "menu.h"				// Knihovna pro generovani menu na LCD
#include "eeprom.h"				// Knihovna pro emulaci EEPROM v interni FLASH mikrokontroleru (posledni 2 kb ze 128kb)
#include "My_usart.h"
#include "My_PWM.h"
#include "My_ADC.h"
#include "My_rtc.h"
#include "server.h"
#include "net.h"
#include "flash_save.h"			// Knihovna pro ukladani a nacitani nastaveni z FLASH
#include "planovac.h"
#include "delay.h"

//#define DEBUG

// Globalni promenne
extern FLASH_Status FlashStatus;
extern uint16_t EEPROM_VirtAdd[3];	/* Virtual address defined by the user: 0xFFFF value is prohibited */

// Exportovane promenne teplotnich cidel
extern volatile u8 PocetCidel;								// Pocet teplotnich cidel nalezenych pomoci funkce find sensors
extern int16_t Teplota[MAXSENSORS];								// Teplota ve formatu int -> 25,3 = 253
extern uint8_t gSensorIDs[MAXSENSORS][DS18X20_SP_SIZE];			// Tabulka ROM kodu jednotlivych cidel nactenych pres funkci find sensor
extern uint16_t FLASHSensorIDs[MAXSENSORS][DS18X20_SP_SIZE];	// Tabulka ROM kodu jednotlivych cidel nactenych z emulovane EEPROM pameti
																// FLASHSensorIDs[0][] ... Cidlo na chladici 1 - LED
																// FLASHSensorIDs[1][] ... Cidlo na chladici 2 - Tranzistory
																// FLASHSensorIDs[2][] ... Externi cidlo na kabelu
extern int16_t TeplotaHistorie[MAXSENSORS][2];
extern uint8_t PovoleniMeritTeplotu;
extern uint8_t TeplotaInicializace;

extern uint16_t FANtemp[4];
extern uint16_t FANval[4];
extern uint16_t SHUTDOWNtemp;

// Exportovane promenne cidla vlhkosti
extern uint8_t Humidity;
extern uint8_t HumidityHistorie[2];

// Exportovane promenne menu
extern uint8_t AktualniUroven[MENU_MAX_UROVEN];

// Exportovane promenne PWM
extern uint16_t PWM_max[5];

// Exportovane promenne ADC (mereni proudu)
extern uint16_t ADC_HodnotyPrumer[6];

// Exportovane promenne planovace
extern uint8_t AutomatickyRezim;

// Exportovane promenne casu
extern uint8_t Time_Hodiny;
extern uint8_t Time_Minuty;
extern uint8_t Time_Vteriny;
extern uint8_t Time_Den;
extern uint8_t Time_Mesic;
extern uint16_t Time_Rok;
extern uint8_t Time_DenVTydnu;

// Exportovane promenne serveru
extern uint8_t my_MAC[MAC_ADR_SIZE];						// MAC adresa serveru
extern uint8_t my_IP[IP_ADR_SIZE];							// IP adresa serveru
extern uint8_t my_MASK[IP_ADR_SIZE];						// Maska podsite serveru
extern uint8_t my_GATE[IP_ADR_SIZE];						// Vychozi brana serveru
extern uint16_t my_HTTP_Port;								// Port HTTP serveru
extern uint16_t my_UDP_Port;								// Port UDP serveru
extern uint16_t my_Telnet_Port;								// Port Telnet serveru
extern uint8_t ServerEnable;

extern uint8_t PlanovacStav;
/* --------------------------------------------
 Funkce zobrazovani a vykonaani mistniho
 uzivatelskeho rozhrani
 ------------------------------------------- */
void UserInterface_Run(void)
{
	uint8_t Hodnota=0;
	uint8_t i;
	uint16_t MenuID;
	const uint16_t Mocniny[5] = {10000, 1000, 100, 10, 1};

	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Menu start",1);
		USART1_FreeMUTEX();
	#endif

	while((Hodnota!=MENU_UROVEN_EXIT))	
	{
		Hodnota = Menu_Run(0);
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("Navracena struktura: ",0);
			USART1_SendNumber(AktualniUroven[0]);
			USART1_Write('>');
			USART1_SendNumber(AktualniUroven[1]);
			USART1_Write('>');
			USART1_SendNumber(AktualniUroven[2]);
			USART1_Write('>');
			USART1_SendNumber(AktualniUroven[3]);
			USART1_Write('>');
			USART1_SendNumber(AktualniUroven[4]);
			USART1_Send_Enter();
			USART1_FreeMUTEX();
		#endif
		
		/* Prepocet pole urovne na jedinne ID cislo */
		MenuID = 0;
		for(i=0;i<MENU_MAX_UROVEN;i++)
		{
			if(AktualniUroven[i]<10)
			{
				MenuID += AktualniUroven[i]*Mocniny[i];
			}
		}
		if(MenuID<11000)
		{
			continue;
		}
		switch(MenuID)
		{
			case 11000:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();					
					USART1_Send("MENU: Zobrazit teploty",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_ShowTemp();
				break;
			}
			case 12000:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();					
					USART1_Send("MENU: Zobrazit relativni vlhkost",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_ShowHumidity();
				break;
			}
			case 13000:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();					
					USART1_Send("MENU: Zobrazit proudy",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_ShowCurrent();
				break;
			}
			case 21100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();					
					USART1_Send("MENU: Kanal 1: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<0);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_1(PWM_max[0]);				
				break;
			}
			case 21200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 1: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<0);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_1(0);
				break;
			}
			case 21300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 1: SET",1);
					USART1_FreeMUTEX();		
				#endif
				AutomatickyRezim&= ~(1<<0);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				UserInterface_Power_Set(1);
				break;
			}
			case 21400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 1: AUTO",1);		
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim|=(1<<0);						// Aktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 22100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 2: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<1);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_2(PWM_max[1]);
				break;
			}
			case 22200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 2: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<1);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_2(0);
				break;
			}
			case 22300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 2: SET",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<1);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				UserInterface_Power_Set(2);
				break;
			}
			case 22400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 2: AUTO",1);
					USART1_FreeMUTEX();		
				#endif
				AutomatickyRezim|=(1<<1);						// Aktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 23100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 3: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<2);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_3(PWM_max[2]);
				break;
			}
			case 23200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 3: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<2);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_3(0);
				break;
			}
			case 23300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 3: SET",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<2);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				UserInterface_Power_Set(3);
				break;
			}
			case 23400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 3: AUTO",1);		
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim|=(1<<2);						// Aktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 24100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 4: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<3);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_4(PWM_max[3]);
				break;
			}
			case 24200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 4: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<3);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_4(0);
				break;
			}
			case 24300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 4: SET",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<3);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				UserInterface_Power_Set(4);
				break;
			}
			case 24400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 4: AUTO",1);		
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim|=(1<<3);						// Aktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 25100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 5: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<4);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_5(PWM_max[4]);
				break;
			}
			case 25200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 5: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<4);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_5(0);
				break;
			}
			case 25300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 5: SET",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<4);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				UserInterface_Power_Set(5);
				break;
			}
			case 25400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 5: AUTO",1);		// Aktivace automatickeho rezimu (podle planovace)
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim|=(1<<4);
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 26100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 6: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<5);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_6_ON();
				break;
			}
			case 26200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 6: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim&= ~(1<<5);						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_6_OFF();
				break;
			}
			case 26300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Kanal 6: AUTO",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim|=(1<<5);						// Aktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 27100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vsechny kanaly: ON",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim=0x00;						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_1(PWM_max[0]);
				PWM_Set_LED_2(PWM_max[1]);
				PWM_Set_LED_3(PWM_max[2]);
				PWM_Set_LED_4(PWM_max[3]);
				PWM_Set_LED_5(PWM_max[4]);
				PWM_Set_LED_6_ON();
				break;
			}
			case 27200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vsechny kanaly: OFF",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim=0x00;						// Deaktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				PWM_Set_LED_1(0);
				PWM_Set_LED_2(0);
				PWM_Set_LED_3(0);
				PWM_Set_LED_4(0);
				PWM_Set_LED_5(0);
				PWM_Set_LED_6_OFF();
				break;
			}
			case 27300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vsechny kanaly: SET",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim=0x00;						// Dektivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 27400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vsechny kanaly: AUTO",1);
					USART1_FreeMUTEX();
				#endif
				AutomatickyRezim=0xFF;						// Aktivace automatickeho rezimu (podle planovace)
				FlashSave_Save16(FLASHSAVE_LEDREZIM, AutomatickyRezim);
				break;
			}
			case 31000:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Pridat plan",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_PridatPlan(0xFF);
				break;
			}
			case 32000:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Editovat plan",1);
					USART1_FreeMUTEX();
				#endif
				i=UserInterface_ProchazetPlany();
				if(i<PLANOVAC_MAXZAZNAMU)
				{
					UserInterface_PridatPlan(i);
				}
				break;
			}
			case 33000:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Smazat plan",1);
					USART1_FreeMUTEX();
				#endif
				i=UserInterface_ProchazetPlany();
				if(i<PLANOVAC_MAXZAZNAMU)
				{
					LCD_HD44780_Clear();
					Planovac_SmazatZaznam(i);
					LCD_HD44780_GoTo(0,0);
					LCD_HD44780_PutString("  Plan smazan");
					vTaskDelay(1300*portTICK_FOR_1MS);
				}
				break;
			}
			case 41100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni casu",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetTime();
				break;
			}
			case 41200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni datumu",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetDate();
				break;
			}
			case 42100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni IP",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetIP(SET_IP);
				break;
			}
			case 42200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni masky",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetIP(SET_MASK);
				break;
			}
			case 42300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni Brany",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetIP(SET_GATE);
				break;
			}
			case 42400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni MAC",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetMAC();
				break;
			}
			case 43100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni portu HTTP",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetPort(SET_PORTHTTP);
				break;
			}
			case 43200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni portu UDP",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetPort(SET_PORTUDP);
				break;
			}
			case 43300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastaveni portu Telnetu",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_SetPort(SET_PORTTELNET);
				break;
			}
			case 43400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Zapnout HTTP server",1);
					USART1_FreeMUTEX();
				#endif
				ServerEnable|=(1<<SERVER_ENABLE_WWW);
				ServerEnable|=(1<<SERVER_ENABLE_WWW2);
				FlashSave_Save16(FLASHSAVE_SERVERENABLE, ServerEnable);
				break;
			}
			case 43500:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vypnout HTTP server",1);
					USART1_FreeMUTEX();
				#endif
				ServerEnable&=~(1<<SERVER_ENABLE_WWW);
				ServerEnable&=~(1<<SERVER_ENABLE_WWW2);
				FlashSave_Save16(FLASHSAVE_SERVERENABLE, ServerEnable);
				break;
			}
			case 43600:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Zapnout UDP server",1);
					USART1_FreeMUTEX();
				#endif
				ServerEnable|=(1<<SERVER_ENABLE_UDP);
				FlashSave_Save16(FLASHSAVE_SERVERENABLE, ServerEnable);
				break;
			}
			case 43700:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vypnout UDP server",1);
					USART1_FreeMUTEX();
				#endif
				ServerEnable&=~(1<<SERVER_ENABLE_WWW);
				FlashSave_Save16(FLASHSAVE_SERVERENABLE, ServerEnable);
				break;
			}
			case 43800:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Zapnout ping server",1);
					USART1_FreeMUTEX();
				#endif
				ServerEnable|=(1<<SERVER_ENABLE_PING);
				FlashSave_Save16(FLASHSAVE_SERVERENABLE, ServerEnable);
				break;
			}
			case 43900:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Vypnout ping server",1);
					USART1_FreeMUTEX();
				#endif
				ServerEnable&=~(1<<SERVER_ENABLE_PING);
				FlashSave_Save16(FLASHSAVE_SERVERENABLE, ServerEnable);
				break;
			}
			case 44100:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastavit maximum PWM 1",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_PWM_Set(SET_PWM_MAX+1);
				break;
			}
			case 44200:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastavit maximum PWM 2",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_PWM_Set(SET_PWM_MAX+2);
				break;
			}
			case 44300:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastavit maximum PWM 3",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_PWM_Set(SET_PWM_MAX+3);
				break;
			}
			case 44400:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastavit maximum PWM 4",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_PWM_Set(SET_PWM_MAX+4);
				break;
			}
			case 44500:
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("MENU: Nastavit maximum PWM 5",1);
					USART1_FreeMUTEX();
				#endif
				UserInterface_PWM_Set(SET_PWM_MAX+5);
				break;
			}
		}
	}

	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Menu stop",1);
		USART1_FreeMUTEX();
	#endif
}

/* --------------------------------------------
 Funkce pro inicializaci uzivatelskeho rozhrani
 ------------------------------------------- */
void UserInterface_Init(void)
{
}

/* --------------------------------------------
 Pocatecni vyhledani a inicializace teplotnich
 cidel s nabidkou vybrani jejich funkce
 ------------------------------------------- */
void UserInterface_SensorInit(void)
{
	uint8_t i,j,k;
	uint16_t EEPROM_Status=0;
	uint8_t nalezeno=0;							// Seznam ID v EEPROM ke kterym byly nalezeny cidla
	uint8_t nenalezeno=0x07;					// Seznam ID v RAM (nalezenych cidel) ke kterym nebyl nalezen zaznam
												// v EEPROM
	// Nahrani dat (informace o cidlech) z virtualni EEPROM pameti do RAM
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Nahravam EEPROM");
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Nahravani EEPROM",1);
		USART1_FreeMUTEX();
	#endif
	FLASH_Unlock();								// Odemceni FLASH pameti pro zapis
	EE_Init();									// Inicializace emulatoru EEPROM
	for(i=0;i<MAXSENSORS;i++)					// Nacteni ROM kodu z EEPROM do RAM
	{
		for(j=0;j<DS18X20_SP_SIZE;j++)
		{
			EEPROM_Status |= EE_ReadVariable(EEPROM_VirtAdd[AddrCidla]+(i*DS18X20_SP_SIZE)+j,&FLASHSensorIDs[i][j]);	
		}
	}	

	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();	
		for(j=0;j<MAXSENSORS;j++)
		{
			USART1_Send(" > EEPROM SN(",0);
			USART1_SendNumber(j);
			USART1_Send("): ",0);
			for(i=0;i<DS18X20_SP_SIZE;i++)
			{
				USART1_Send_Hex(FLASHSensorIDs[j][i]);
			}
			USART1_Send_Enter();
		}
		USART1_FreeMUTEX();
	#endif


	/* Vyhledani teplotnich cidel */
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Hledam cidla...");	
	j=0;
	do
	{
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("Hledani cidel...",1);
			USART1_FreeMUTEX();
		#endif
		PocetCidel = DS_18B20_Init();
		LCD_HD44780_GoTo(15,0);
		if(j>0)
		{
			LCD_HD44780_PutInt16(j,1);
		}
		vTaskDelay(100*portTICK_FOR_1MS);
		j++;
	}while((PocetCidel == 0)&(j<8));
	LCD_HD44780_GoTo(0,1);
	if(PocetCidel==0)										// Nebyla nalezena zadna cidla - nebude se provadet mereni!
	{		
		PovoleniMeritTeplotu=false;
		LCD_HD44780_PutString("> Cidla nenalez.");
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("Cidla nenalezena",1);
			USART1_FreeMUTEX();
		#endif
		vTaskDelay(2000*portTICK_FOR_1MS);
	}
	else													// Nejaka cidla nalezena
	{
		PovoleniMeritTeplotu=true;
		LCD_HD44780_PutString("> Pocet cid.:");
		LCD_HD44780_PutInt16(PocetCidel,2);
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("Pocet cidel: ",0);
			USART1_SendNumber(PocetCidel);
			USART1_Send_Enter();
			USART1_FreeMUTEX();
		#endif
		
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("> Detekovana cidla:",1);
			for(j=0;j<MAXSENSORS;j++)
			{
				USART1_Send(" > RAM SN(",0);
				USART1_SendNumber(j);
				USART1_Send("): ",0);
				for(i=0;i<DS18X20_SP_SIZE;i++)
				{
					USART1_Send_Hex(gSensorIDs[j][i]);
				}
				USART1_Send_Enter();
			}
			USART1_FreeMUTEX();
		#endif
		
		vTaskDelay(1000*portTICK_FOR_1MS);
		LCD_HD44780_GoTo(0,1);
		LCD_HD44780_PutString("                ");
		LCD_HD44780_GoTo(0,1);
		LCD_HD44780_TeplotaNaLCD(Teplota[0]);
		LCD_HD44780_TeplotaNaLCD(Teplota[1]);
		LCD_HD44780_TeplotaNaLCD(Teplota[2]);
		vTaskDelay(1000*portTICK_FOR_1MS);
		
		/* Kontrola jestli nalezena cidla jsou ty, ktera jsou ulozena v EEPROM */
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("Porovnavani SN cidel s EEPROM",1);
			USART1_FreeMUTEX();
		#endif
		// k ... index prochazejici seznam cidel v EEPROM (zapamatovanych)
		// j ... pomocny index k prochazeni bytu serioveho cisla
		// i ... index prochazejici seznam cidel v RAM (prave detekovanych)
		for(k=0;k<MAXSENSORS;k++)													// Prochazeni ROM koduv seznamu ulozenem v EEPROM
		{
			#ifdef DEBUG
				USART1_WaitForMUTEX();
				USART1_SetMUTEX();
				USART1_Send("Kontrola EEPROM(",0);
				USART1_SendNumber(k);
				USART1_Send(")",0);
				USART1_Send_Enter();
				USART1_FreeMUTEX();
			#endif
			for(i=0;i<PocetCidel;i++)												// Prochazeni ROM kodu v seznamu prave vyhledanych cidel
			{
				for(j=0;j<DS18X20_SP_SIZE;j++)										// Prochazeni bytu v ROM kodu
				{
					if (FLASHSensorIDs[k][j]!=gSensorIDs[i][j]) break;				// Pokud se byty neshoduji -> pokracuj na dalsi ROM kod v seznamu vyhledanych
					if (j>=(DS18X20_SP_SIZE-1))										// Kod v EEROM odpovida jednomu z kodu v seznamu vyhledanych
					{
						nalezeno |= (1<<k);											// Zaznamenani, ze byl nalezen ROM kod na k-te pozici v tabulce FLASHSensorIDs
						nenalezeno &= ~(1<<i);										// Smazani cidla ze seznamu nenalezenych
						#ifdef DEBUG
							USART1_WaitForMUTEX();
							USART1_SetMUTEX();
							USART1_Send("> Nalezeno zname cidlo EEPROM:",0);
							USART1_SendNumber(k);
							USART1_Send_Enter();
							USART1_FreeMUTEX();
						#endif
						goto HledaniDalsiSensor;									// Zacatek hledani dalsiho senzoru z FLASH ve vyhledanem seznamu
					}
				}
			}
HledaniDalsiSensor:		
		}
		
		/* Zpracovani vysledku vyhledavani */
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("ZPRACOVANI",1);
			USART1_Send("nalezeno=",0);
			USART1_SendNumber(nalezeno);
			USART1_Send_Enter();
			USART1_Send("nenalezeno=",0);
			USART1_SendNumber(nenalezeno);
			USART1_Send_Enter();
			USART1_FreeMUTEX();
		#endif
		if(nalezeno==0x07)					// Kontrola jestli byly nalezeny vsechny cidla s ROM kody z tabulky FLASHSensorIDs
		{									// Vsechny senzory byly rozpoznany - neni treba provadet zadne prirazeni
			LCD_HD44780_GoTo(0,1);
			LCD_HD44780_PutString("> Cidla rozpozn.");
			#ifdef DEBUG
				USART1_WaitForMUTEX();
				USART1_SetMUTEX();
				USART1_Send("Cidla rozpoznana",1);
				USART1_FreeMUTEX();
			#endif
			vTaskDelay(1000*portTICK_FOR_1MS);
		}
		else								// Nektery ze senzoru nebyl nalezen - nutno priradit neobsazenou funkci
		{
			LCD_HD44780_GoTo(0,1);
			LCD_HD44780_PutString("> Cast. rozpozn.");			
			#ifdef DEBUG
				USART1_WaitForMUTEX();
				USART1_SetMUTEX();
				USART1_Send("Cidla castecne rozpoznana",1);
				USART1_FreeMUTEX();
			#endif
			vTaskDelay(800*portTICK_FOR_1MS);
			// k ... index prochazejici seznam cidel v EEPROM (zapamatovanych)
			// j ... index prochazejici seznam cidel v RAM (prave detekovanych)
			// i ... index funkce (pole do ktere se vybrany SN ulozi v EEPROM)
			for(k=0;k<MAXSENSORS;k++)
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("> (EEPROM) k=",0);
					USART1_SendNumber(k);
					USART1_Send_Enter();
					USART1_FreeMUTEX();
				#endif
				LCD_HD44780_Clear();
				// nalezeno = Seznam ID v EEPROM ke kterym byly nalezeny cidla
				// nenalezeno = Seznam ID v RAM (detekovanych cidel) ke kterym nebyl nalezen zaznam v EEPROM
				if(!(nalezeno & (1<<k)))			// Overeni jestli cidlo na pozici 'k' v EEPROM bylo na sbernici nalezeno 
				{									// - pokud ne, tak se zeptat k cemu ho priradit
					for(j=0;j<PocetCidel;j++)		// Najde prvni cidlo v seznamu, u ktereho nebyl nalezen zaznam v EEPROM
					{
						if(nenalezeno & (1<<j)) 
						{
							#ifdef DEBUG
								USART1_WaitForMUTEX();
								USART1_SetMUTEX();
								USART1_Send("> (RAM nenalezeno) j=",0);
								USART1_SendNumber(j);
								USART1_Send_Enter();
								USART1_FreeMUTEX();
							#endif
							break;
						}
						
					}
					#ifdef DEBUG
						USART1_WaitForMUTEX();
						USART1_SetMUTEX();
						USART1_Send("> (RAM po hledani) j=",0);
						USART1_SendNumber(j);
						USART1_Send_Enter();
						USART1_FreeMUTEX();
					#endif
					if(j>(PocetCidel-1))
					{
						#ifdef DEBUG
							USART1_WaitForMUTEX();
							USART1_SetMUTEX();
							USART1_Send("> Nebylo nalezeno zadne dalsi neprirazene cidlo",1);
							USART1_FreeMUTEX();
						#endif
						break;
					}
					LCD_HD44780_GoTo(0,0);
					LCD_HD44780_PutString("Cidlo: ");
					LCD_HD44780_TeplotaNaLCD(Teplota[j]);
					LCD_HD44780_PutChar(Char_StupenCelsia);
					LCD_HD44780_PutChar(' ');
PrirazeniHledatZnova:
					i=0;
PrirazeniHledatZnova2:
					for(;i<MAXSENSORS;i++)				// Hledani neprirazene "funkce" pro zvolene cidlo
					{
						if(!(nalezeno & (1<<i)))
						{
							vTaskDelay(30*portTICK_FOR_1MS);
							if(TL_NAHORU_Read_0())
							{
								PEZO_BEEP();
								vTaskDelay(30*portTICK_FOR_1MS);
								while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);	// Cekani na uvolneni tlacitka
								continue;					// Najde v poradi dalsi volnou "funkci" aktualne zvoleneho cidla
							}
							break;
						}
					}
					if(i>=MAXSENSORS) goto PrirazeniHledatZnova;	// Pokud se doslo na konec -> skok na zacatek seznamu						
					LCD_HD44780_GoTo(0,1);
					switch(i)								// Zobrazeni 
					{
						case 0:
						{
							LCD_HD44780_PutString("> Chladic LED   ");
							break;
						}
						case 1:
						{
							LCD_HD44780_PutString("> Chladic Tranz.");
							break;
						}
						case 2:
						{
							LCD_HD44780_PutString("> Externi cidlo ");
							break;
						}
					}
					while(1)
					{
						if(TL_NAHORU_Read_0()) 
						{
							PEZO_BEEP();
							goto PrirazeniHledatZnova2;	// Zobrazeni dalsi volen "funkce" v poradi
						}
						if(TL_OK_Read_0())
						{
							/* Dopsat funkci pro ulozeni SN do FLASH */
							PEZO_BEEP();
							LCD_HD44780_GoTo(0,1);
							LCD_HD44780_PutString("> Ukladam...    ");
							#ifdef DEBUG
								USART1_WaitForMUTEX();
								USART1_SetMUTEX();
								USART1_Send("Ukladam...",1);
								USART1_FreeMUTEX();
							#endif
							for(j=0;j<DS18X20_SP_SIZE;j++)
							{
								FLASHSensorIDs[i][j] = gSensorIDs[k][j];
								EE_WriteVariable(EEPROM_VirtAdd[AddrCidla]+(i*DS18X20_SP_SIZE)+j,FLASHSensorIDs[i][j]);
							}
							nalezeno |= (1<<i);						// Vyrazeni ulozene funkce z nabidky pro prirazeni
							nenalezeno &= ~(1<<k);					// Nastaveni cidla v RAM jako znameho (s prirazenou funkci)
							
							LCD_HD44780_GoTo(0,1);
							LCD_HD44780_PutString("> Ulozeno       ");
							#ifdef DEBUG
								USART1_WaitForMUTEX();
								USART1_SetMUTEX();
								USART1_Send("> Cidlo ulozeno RAM id: ",0);
								USART1_SendNumber(k);
								USART1_Send(" do EEPROM id: ",0);
								USART1_SendNumber(i);
								USART1_Send_Enter();
								USART1_FreeMUTEX();
							#endif
							while(TL_OK_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);								
							PEZO_BEEP();
							vTaskDelay(1000*portTICK_FOR_1MS);
							break;
						}
						// Prekreslovani namerene teploty vybraneho cidla
												
						LCD_HD44780_GoTo(7,0);
						LCD_HD44780_TeplotaNaLCD(Teplota[j]);
						LCD_HD44780_PutChar(Char_StupenCelsia);
						vTaskDelay(1*portTICK_FOR_1MS);					
					}
				}			
			}
		}
	}
	FLASH_Lock();								// Zamce FLASH pamet pro zapis - nebude fungovat zapis do virtualni EEPROM

	/* Prekopirovani Flash tabulky sensoru do RAM - se kterou pak pracuje zbytek programu */
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Kopiruji EEPROM tabulku do RAM",1);
		USART1_FreeMUTEX();
	#endif
	for(i=0;i<MAXSENSORS;i++)
	{
		for(j=0;j<DS18X20_SP_SIZE;j++)
		{
			gSensorIDs[i][j] = FLASHSensorIDs[i][j];
		}
	}
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Preskladana tabulka cidel v RAM:",1);
		for(j=0;j<MAXSENSORS;j++)
		{
			USART1_Send(" > SN(",0);
			USART1_SendNumber(j);
			USART1_Send("): ",0);
			for(i=0;i<DS18X20_SP_SIZE;i++)
			{
				USART1_Send_Hex(gSensorIDs[j][i]);
			}
			USART1_Send_Enter();
		}
		USART1_FreeMUTEX();
	#endif
	/* Smazani historie nejvyzsi a nejnizsi teploty */
	TeplotaHistorie[0][0]=DS18X20_INVALID_TEMP;
	TeplotaHistorie[0][1]=DS18X20_INVALID_TEMP;
	TeplotaHistorie[1][0]=DS18X20_INVALID_TEMP;
	TeplotaHistorie[1][1]=DS18X20_INVALID_TEMP;
	TeplotaHistorie[2][0]=DS18X20_INVALID_TEMP;
	TeplotaHistorie[2][1]=DS18X20_INVALID_TEMP;
	TeplotaInicializace = false;
}

void UserInterface_FlashSensorErase(void)
{
	uint8_t i,j;	

	FLASH_Unlock();
	for(i=0;i<MAXSENSORS;i++)
	{
		for(j=0;j<DS18X20_SP_SIZE;j++)
		{		
			EE_WriteVariable(EEPROM_VirtAdd[AddrCidla]+(i*DS18X20_SP_SIZE)+j,0x00);
		}
	}
	FLASH_Lock();
}

/* --------------------------------------------
 Menu pridat plan do planovace
 ------------------------------------------- */
void UserInterface_PridatPlan(uint8_t ID)
{
	uint8_t Zacatek[5]={0,0,0,0,0};
	uint8_t Konec[5]={0,0,0,0,0};
	uint8_t Opakovani=0;
	uint8_t NastaveniLED[6]={0,0,0,0,0,0};

	if(ID<PLANOVAC_MAXZAZNAMU)
	{
		Planovac_GetZaznam(ID,Zacatek,Konec,&Opakovani,NastaveniLED);	
	}

	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Zacatek planu");	
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("Cas: --:--");
	Zacatek[0] = UserInterfaceEdit_int16(Zacatek[0], 23, 2, 5, 1);
	Zacatek[1] = UserInterfaceEdit_int16(Zacatek[1], 59, 2, 8, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("Datum:--.--.----");
	Zacatek[2] = UserInterfaceEdit_int16(Zacatek[2], 31, 2, 6, 1);
	if(Zacatek[2]==0)
		goto PRIDATPLAN_KONEC;
	Zacatek[3] = UserInterfaceEdit_int16(Zacatek[3], 12, 2, 9, 1);
	if(Zacatek[3]==0)
		goto PRIDATPLAN_KONEC;
	LCD_HD44780_GoTo(12,1);
	LCD_HD44780_PutString("20");
	Zacatek[4] = UserInterfaceEdit_int16(Zacatek[4], 60, 2, 14, 1);
	if(Zacatek[4]==0)
		goto PRIDATPLAN_KONEC;

	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Opakovani planu");	
	
	while(TL_OK_Read_0()==0)
	{
		switch(Opakovani)
		{
			case 0:
			{
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("> Deaktivovat  ");
				break;
			}
			case 1:
			{
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("> Jednou       ");
				break;
			}
			case 2:
			{
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("> Kazdy den    ");
				break;
			}
			case 3:
			{
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("> Kazdy tyden  ");
				break;
			}
			case 4:
			{
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("> Kazdy mesic  ");
				break;
			}
			case 5:
			{
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("> Kazdy rok    ");
				break;
			}
		}
		if(TL_NAHORU_Read_0())												// Obsluha tlacitka NAHORU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			Opakovani++;
			if(Opakovani>5)
			{
				Opakovani=0;
			}
		}
		if(TL_DOLU_Read_0())												// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			Opakovani--;
			if(Opakovani>5)
			{
				Opakovani=5;
			}
		}
	}
	PEZO_BEEP();
	vTaskDelay(30*portTICK_FOR_1MS);
	while(TL_OK_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
	vTaskDelay(50*portTICK_FOR_1MS);
	if((Opakovani==0) | (Opakovani==1))
	{
		goto PRIDATPLAN_NASTAVENI_LED;
	}

	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Konec planu");	
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("Cas: --:--");
	Konec[0] = UserInterfaceEdit_int16(Konec[0], 23, 2, 5, 1);
	Konec[1] = UserInterfaceEdit_int16(Konec[1], 59, 2, 8, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("Datum:--.--.----");
	Konec[2] = UserInterfaceEdit_int16(Konec[2], 31, 2, 6, 1);
	if(Konec[2]==0)
		goto PRIDATPLAN_KONEC;
	Konec[3] = UserInterfaceEdit_int16(Konec[3], 12, 2, 9, 1);
	if(Konec[3]==0)
		goto PRIDATPLAN_KONEC;
	LCD_HD44780_GoTo(12,1);
	LCD_HD44780_PutString("20");
	Konec[4] = UserInterfaceEdit_int16(Konec[4], 60, 2, 14, 1);
	if(Konec[4]==0)
		goto PRIDATPLAN_KONEC;

PRIDATPLAN_NASTAVENI_LED:
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Nastaveni vykonu");	
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("645nm: --- %");
	NastaveniLED[0]=UserInterfaceEdit_int16(NastaveniLED[0], 100, 3, 7, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("660nm:");
	NastaveniLED[1]=UserInterfaceEdit_int16(NastaveniLED[1], 100, 3, 7, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("735nm:");
	NastaveniLED[2]=UserInterfaceEdit_int16(NastaveniLED[2], 100, 3, 7, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("\"bila\": --- %");
	NastaveniLED[3]=UserInterfaceEdit_int16(NastaveniLED[3], 100, 3, 8, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("455nm: --- % ");
	NastaveniLED[4]=UserInterfaceEdit_int16(NastaveniLED[4], 100, 3, 7, 1);
	LCD_HD44780_GoTo(0,1);	
	LCD_HD44780_PutString("385nm:");
	while(TL_OK_Read_0()==0)
	{
		switch(NastaveniLED[5])
		{
			case 0:
			{
				LCD_HD44780_GoTo(6,1);
				LCD_HD44780_PutString("Vypnout   ");
				break;
			}
			case 1:
			{
				LCD_HD44780_GoTo(6,1);
				LCD_HD44780_PutString("Zapnout   ");
				break;
			}			
		}
		if(TL_NAHORU_Read_0())												// Obsluha tlacitka NAHORU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			NastaveniLED[5]++;
			if(NastaveniLED[5]>1)
			{
				NastaveniLED[5]=0;
			}
		}
		if(TL_DOLU_Read_0())												// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			NastaveniLED[5]--;
			if(NastaveniLED[5]>1)
			{
				NastaveniLED[5]=1;
			}
		}
	}
	PEZO_BEEP();
	vTaskDelay(30*portTICK_FOR_1MS);
	while(TL_OK_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
	vTaskDelay(50*portTICK_FOR_1MS);
	
	LCD_HD44780_Clear();
	if(ID<PLANOVAC_MAXZAZNAMU)
	{
		Planovac_EditovatZaznam(ID,Zacatek[2],Zacatek[3],Zacatek[4],Zacatek[0],Zacatek[1],
						  	  	   Konec[2],Konec[3],Konec[4]+2000,Konec[0],Konec[1],Opakovani,NastaveniLED);
	}
	else
	{
		Planovac_PridatZaznam(Zacatek[2],Zacatek[3],Zacatek[4],Zacatek[0],Zacatek[1],
							  Konec[2],Konec[3],Konec[4]+2000,Konec[0],Konec[1],Opakovani,NastaveniLED);
	}
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("  Plan ulozen");
	vTaskDelay(1300*portTICK_FOR_1MS);
	return;
PRIDATPLAN_KONEC:
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString(" Plan neulozen");
	vTaskDelay(1300*portTICK_FOR_1MS);
	return;
}
/* --------------------------------------------
 Funkce zobrazuje ulozene plany a vrati ID 
 vybraneho
 ------------------------------------------- */
uint8_t UserInterface_ProchazetPlany(void)
{
	uint8_t ID=0;
	uint8_t Stranka=0;
	uint8_t Zacatek[5];
	uint8_t Konec[5];
	uint8_t Opakovani;
	uint8_t NastaveniKanalu[6];
	uint8_t Nalezeno=0;
	uint16_t i;

	LCD_HD44780_Clear();
	while(1)
	{
PROCHAZETPLANY_DALSI:
		if(Planovac_ExistujiciZaznam(ID)==0)
		{
			if((ID>=PLANOVAC_MAXZAZNAMU) & (Nalezeno==0))
			{
				LCD_HD44780_Clear();
				LCD_HD44780_GoTo(0,0);
				LCD_HD44780_PutString("Nebyly nalezeny");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("  zadne plany");
				
				vTaskDelay(300*portTICK_FOR_1MS);
				PEZO_BEEP();
				PEZO_BEEP();
				PEZO_BEEP();
				vTaskDelay(200*portTICK_FOR_1MS);
				PEZO_BEEP();
				PEZO_BEEP();
				PEZO_BEEP();
				vTaskDelay(1200*portTICK_FOR_1MS);
				return(0xFF);
			}
			ID++;
			goto PROCHAZETPLANY_DALSI;
		}
		Nalezeno=1;
		Planovac_GetZaznam(ID,Zacatek,Konec,&Opakovani,NastaveniKanalu);
		LCD_HD44780_GoTo(0,0);	
		LCD_HD44780_PutInt16(ID+1,2);
		LCD_HD44780_PutChar('/');
		LCD_HD44780_PutInt16(PLANOVAC_MAXZAZNAMU,2);
		LCD_HD44780_PutString("> ");
		switch(Stranka)
		{
			case 0:
			{
				LCD_HD44780_PutString("Zacatek  ");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("Cas: ");
				LCD_HD44780_PutInt16(Zacatek[3],2);
				LCD_HD44780_PutChar(':');
				LCD_HD44780_PutInt16(Zacatek[4],2);
				LCD_HD44780_PutString("      ");
				break;
			}
			case 1:
			{
				LCD_HD44780_PutString("Zacatek  ");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("Datum:");
				LCD_HD44780_PutInt16(Zacatek[0],2);
				LCD_HD44780_PutChar('.');
				LCD_HD44780_PutInt16(Zacatek[1],2);
				LCD_HD44780_PutChar('.');
				LCD_HD44780_PutInt16(2000+Zacatek[2],4);				
				break;
			}
			case 2:
			{
				LCD_HD44780_PutString("Konec    ");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("Cas: ");
				LCD_HD44780_PutInt16(Konec[3],2);
				LCD_HD44780_PutChar(':');
				LCD_HD44780_PutInt16(Konec[4],2);
				LCD_HD44780_PutString("      ");
				break;
			}
			case 3:
			{
				LCD_HD44780_PutString("Konec    ");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutString("Datum:");
				LCD_HD44780_PutInt16(Konec[0],2);
				LCD_HD44780_PutChar('.');
				LCD_HD44780_PutInt16(Konec[1],2);
				LCD_HD44780_PutChar('.');
				LCD_HD44780_PutInt16(2000+Konec[2],4);				
				break;
			}
			case 4:
			{
				LCD_HD44780_PutString("Opakovani");
				LCD_HD44780_GoTo(0,1);
				switch(Opakovani)
				{
					case 0:
					{						
						LCD_HD44780_PutString("Deaktivovat  ");
						break;
					}
					case 1:
					{					
						LCD_HD44780_PutString("Jednou       ");
						break;
					}
					case 2:
					{						
						LCD_HD44780_PutString("Kazdy den    ");
						break;
					}
					case 3:
					{
						LCD_HD44780_PutString("Kazdy tyden  ");
						break;
					}
					case 4:
					{
						LCD_HD44780_PutString("Kazdy mesic  ");
						break;
					}
					case 5:
					{
						LCD_HD44780_PutString("Kazdy rok    ");
						break;
					}
				}
				break;
			}
			case 5:
			{
				LCD_HD44780_PutString("LED 645nm");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutInt16(NastaveniKanalu[0],3);
				LCD_HD44780_PutString(" %");				
				LCD_HD44780_PutString("           ");
				break;
			}
			case 6:
			{
				LCD_HD44780_PutString("LED 660nm");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutInt16(NastaveniKanalu[1],3);
				LCD_HD44780_PutString(" %");				
				LCD_HD44780_PutString("           ");
				break;
			}
			case 7:
			{
				LCD_HD44780_PutString("LED 735nm");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutInt16(NastaveniKanalu[2],3);
				LCD_HD44780_PutString("%");				
				LCD_HD44780_PutString("            ");
				break;
			}
			case 8:
			{
				LCD_HD44780_PutString("LED bila ");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutInt16(NastaveniKanalu[3],3);
				LCD_HD44780_PutString("%");				
				LCD_HD44780_PutString("            ");
				break;
			}
			case 9:
			{
				LCD_HD44780_PutString("LED 455nm");
				LCD_HD44780_GoTo(0,1);
				LCD_HD44780_PutInt16(NastaveniKanalu[4],3);
				LCD_HD44780_PutString("%");				
				LCD_HD44780_PutString("            ");
				break;
			}
			case 10:
			{
				LCD_HD44780_PutString("LED 385nm");
				LCD_HD44780_GoTo(0,1);
				if(NastaveniKanalu[5]==0)
				{
					LCD_HD44780_PutString("Vypnuto         ");
				}
				if(NastaveniKanalu[5]==1)
				{
					LCD_HD44780_PutString("Zapnuto         ");
				}
				break;
			}
		}
		for(i=0;i<1600;i++)
		{
			vTaskDelay(1*portTICK_FOR_1MS);
			if(TL_NAHORU_Read_0())												// Obsluha tlacitka NAHORU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				ID++;				
				if(ID>PLANOVAC_MAXZAZNAMU)
				{
					ID=PLANOVAC_MAXZAZNAMU-1;
				}
				Stranka=0;
				break;
			}
			else if(TL_DOLU_Read_0())											// Obsluha tlacitka DOLU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				ID--;
				if(ID>PLANOVAC_MAXZAZNAMU)
				{
					ID=0;
				}
				Stranka=0;
				break;
			}
			else if(TL_OK_Read_0())												// Obsluha tlacitka DOLU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				return(ID);
			}
			else if(TL_ZPET_Read_0())											// Obsluha tlacitka DOLU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_ZPET_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);		// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				return(0xFF);
			}
		}
		Stranka++;
		if(Stranka>10)
		{
			Stranka=0;
		}
	}
}

/* --------------------------------------------
 Manualni nastaveni PWM hodnoty
 ------------------------------------------- */
void UserInterface_Power_Set(uint8_t Kanal)
{
	uint16_t hodnota=0;
	uint16_t maximum=0;
	
	/* Nacteni predchozi hodnoty */
	switch(Kanal)
	{
		case 1:
		{
			hodnota=PWM_Get_LED_1();
			maximum=PWM_max[0];
			break;
		}
		case 2:
		{
			hodnota=PWM_Get_LED_2();
			maximum=PWM_max[1];
			break;
		}
		case 3:
		{
			hodnota=PWM_Get_LED_3();
			maximum=PWM_max[2];
			break;
		}
		case 4:
		{
			hodnota=PWM_Get_LED_4();
			maximum=PWM_max[3];
			break;
		}
		case 5:
		{
			hodnota=PWM_Get_LED_5();
			maximum=PWM_max[4];
			break;
		}
		default:
		{
			return;
		}		
	}	
	hodnota=((uint32_t)hodnota*100)/maximum;
	/* Zjisteni uzivatelem zadane hodnoty */
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Aktualne: ");
	LCD_HD44780_PutInt16(hodnota,3);
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("Nastavit: ");	

	hodnota = UserInterfaceEdit_int16(hodnota, 100, 3, 10, 1);	
	
	hodnota = ((uint32_t)hodnota*(uint32_t)maximum)/100;					// Prepocet 0-100% na PWM 0-48000
	/* Ulozeni nastavene hodnoty */
	switch(Kanal)
	{
		case 1:
		{
			PWM_Set_LED_1(hodnota);
			break;
		}
		case 2:
		{
			PWM_Set_LED_2(hodnota);
			break;
		}
		case 3:
		{
			PWM_Set_LED_3(hodnota);
			break;
		}
		case 4:
		{
			PWM_Set_LED_4(hodnota);
			break;
		}
		case 5:
		{
			PWM_Set_LED_5(hodnota);
			break;
		}
		default:
		{
			return;
		}
	}
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno    ");
	vTaskDelay(600*portTICK_FOR_1MS);
	LCD_HD44780_Clear();
}

/* --------------------------------------------
 Manualni nastaveni PWM hodnoty
 ------------------------------------------- */
void UserInterface_PWM_Set(uint8_t Kanal)
{
	uint16_t hodnota=0;
	uint16_t maximum=0;
	
	/* Nacteni predchozi hodnoty */
	switch(Kanal)
	{
		case 1:
		{
			hodnota=PWM_Get_LED_1();
			maximum=PWM_max[0];
			break;
		}
		case 2:
		{
			hodnota=PWM_Get_LED_2();
			maximum=PWM_max[1];
			break;
		}
		case 3:
		{
			hodnota=PWM_Get_LED_3();
			maximum=PWM_max[2];
			break;
		}
		case 4:
		{
			hodnota=PWM_Get_LED_4();
			maximum=PWM_max[3];
			break;
		}
		case 5:
		{
			hodnota=PWM_Get_LED_5();
			maximum=PWM_max[4];
			break;
		}
		case SET_PWM_MAX+1:
		{
			hodnota=PWM_max[0];
			break;
		}
		case SET_PWM_MAX+2:
		{
			hodnota=PWM_max[1];
			break;
		}
		case SET_PWM_MAX+3:
		{
			hodnota=PWM_max[2];
			break;
		}
		case SET_PWM_MAX+4:
		{
			hodnota=PWM_max[3];
			break;
		}
		case SET_PWM_MAX+5:
		{
			hodnota=PWM_max[4];
			break;
		}
	}	
	
	/* Zjisteni uzivatelem zadane hodnoty */
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Aktualne: ");
	LCD_HD44780_PutInt16(hodnota,5);
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("Nastavit: ");	

	if (Kanal<SET_PWM_MAX)
	{
		hodnota = UserInterfaceEdit_int16(hodnota, maximum, 5, 10, 1);
	}
	else if (Kanal>SET_PWM_MAX)
	{
		hodnota = UserInterfaceEdit_int16(hodnota, 48000, 5, 10, 1);
	}

	
	/* Ulozeni nastavene hodnoty */
	switch(Kanal)
	{
		case 1:
		{
			PWM_Set_LED_1(hodnota);
			break;
		}
		case 2:
		{
			PWM_Set_LED_2(hodnota);
			break;
		}
		case 3:
		{
			PWM_Set_LED_3(hodnota);
			break;
		}
		case 4:
		{
			PWM_Set_LED_4(hodnota);
			break;
		}
		case 5:
		{
			PWM_Set_LED_5(hodnota);
			break;
		}
	}
	if((Kanal>=SET_PWM_MAX)&(Kanal<=SET_PWM_MAX+5))
	{
		PWM_max[Kanal-SET_PWM_MAX-1]=hodnota;
		FlashSave_Save16(FLASHSAVE_PWMMAX_1+(Kanal-SET_PWM_MAX-1), PWM_max[Kanal-SET_PWM_MAX-1]);
	}
			
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno    ");
	vTaskDelay(600*portTICK_FOR_1MS);
	LCD_HD44780_Clear();
}

/* -------------------------------------------
 Funkce vypise na LCD namerene teploty vcetne 
 maxima a minima od pripojeni napajeni a dovoli
 listovat mezi cidly
 ------------------------------------------ */
void UserInterface_ShowTemp(void)
{
	uint8_t i=0;
	uint32_t Hodnota32=0;
	LCD_HD44780_Clear();
		
	while(1)
	{
		// Zobrazeni typu zobrazovane teploty
		LCD_HD44780_GoTo(0,0);
		switch(i)
		{
			case 0:
			{
				LCD_HD44780_PutString("LED diody    ");
				break;
			}
			case 1:
			{
				LCD_HD44780_PutString("Tranzistory  ");
				break;
			}
			case 2:
			{
				LCD_HD44780_PutString("Externi cidlo");
				break;
			}
			case 3:
			{
				LCD_HD44780_PutString("System (MPU) ");
				break;
			}
		}
		while(1)
		{
			// Zobrazeni teplot
			LCD_HD44780_GoTo(0,1);
			if(i<3)
			{
				LCD_HD44780_TeplotaNaLCD(Teplota[i]);			// Aktualni hodnota
			}
			else
			{
				Hodnota32 = ADC_HodnotyPrumer[ADC_VALUE_TEMP];	// 32°C = 1725	
				Hodnota32 = (((uint32_t)100*((uint32_t)3288-(uint32_t)Hodnota32))/(uint32_t)53)+(uint32_t)250;
				Hodnota32 = Hodnota32/10;
				LCD_HD44780_TeplotaNaLCD(Hodnota32);			// Aktualni hodnota
			}
			LCD_HD44780_TeplotaNaLCD(TeplotaHistorie[i][0]);	// Maximum
			LCD_HD44780_TeplotaNaLCD(TeplotaHistorie[i][1]);	// Minimum
			LCD_HD44780_PutChar(Char_StupenCelsia);

			// Obsluha tlacitek
			if(TL_NAHORU_Read_0())							// Obsluha tlacitka NAHORU
			{													
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				i--;
				if(i>MAXSENSORS)
					i=MAXSENSORS;
				break;
			}	
			else if(TL_DOLU_Read_0())							// Obsluha tlacitka DOLU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				i++;
				if(i>MAXSENSORS)
					i=0;
				break;
			}
			else if(TL_ZPET_Read_0())							// Obsluha tlacitka NAHORU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_ZPET_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				goto UserInterface_ShowTemp_Exit;
			}
			else
			{
				vTaskDelay(20*portTICK_FOR_1MS);
			}
		}
	}
UserInterface_ShowTemp_Exit:	
	LCD_HD44780_Clear();
}

/* -------------------------------------------
 Funkce vypise na LCD namerenou relativni 
 vzdusnou vlhkost
 ------------------------------------------ */
void UserInterface_ShowHumidity(void)
{
	LCD_HD44780_Clear();
	
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Relat.v. vlhkost");

	while(1)
	{		
		// Zobrazeni vlhkosti
		LCD_HD44780_GoTo(1,1);
		if(Humidity<=100)
		{
			if(Humidity<100)
			{			
				LCD_HD44780_PutChar(' ');
				LCD_HD44780_PutInt16(Humidity,2);
			}
			else
			{
				LCD_HD44780_PutInt16(Humidity,3);
			}			
		}
		else
		{
			LCD_HD44780_PutString("Er");			
		}
		LCD_HD44780_PutChar('%');
		LCD_HD44780_PutChar(' ');
		if(HumidityHistorie[0]<=100)
		{				
			if(HumidityHistorie[0]<100)
			{			
				LCD_HD44780_PutChar(' ');
				LCD_HD44780_PutInt16(HumidityHistorie[0],2);
			}
			else
			{
				LCD_HD44780_PutInt16(HumidityHistorie[0],3);
			}
		}
		else
		{
			LCD_HD44780_PutString("Er");
		}
		LCD_HD44780_PutChar('%');
		LCD_HD44780_PutChar(' ');
		if(HumidityHistorie[1]<=100)
		{
			if(HumidityHistorie[1]<100)
			{			
				LCD_HD44780_PutChar(' ');
				LCD_HD44780_PutInt16(HumidityHistorie[1],2);
			}
			else
			{
				LCD_HD44780_PutInt16(HumidityHistorie[1],3);
			}
		}
		else
		{
			LCD_HD44780_PutString("Er");
		}
		LCD_HD44780_PutChar('%');

		// Obsluha tlacitek
		if(TL_ZPET_Read_0())							// Obsluha tlacitka NAHORU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_ZPET_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			break;
		}	
	}
	LCD_HD44780_Clear();
}

/* -------------------------------------------
 Funkce vypise na LCD namerene proudy 
 v jednotlivych kanalech
 ------------------------------------------ */
void UserInterface_ShowCurrent(void)
{
	uint8_t i=0;
	uint16_t j=0;
	uint32_t Hodnota32=0;
	uint32_t Prumer=0;
	LCD_HD44780_Clear();
	
	while(1)
	{
		Prumer=0;
		j=0;
		// Zobrazeni typu zobrazovane teploty
		LCD_HD44780_GoTo(0,0);
		switch(i)
		{
			case 0:
			{
				LCD_HD44780_PutString("1. Kanal   645nm");
				break;
			}
			case 1:
			{
				LCD_HD44780_PutString("2. Kanal   660nm");
				break;
			}
			case 2:
			{
				LCD_HD44780_PutString("3. Kanal   735nm");
				break;
			}
			case 3:
			{
				LCD_HD44780_PutString("4. Kanal    bila");
				break;
			}
			case 4:
			{
				LCD_HD44780_PutString("5. Kanal   455nm");
				break;
			}
		}
		LCD_HD44780_GoTo(0,1);
		LCD_HD44780_PutString("> Wait");
		while(1)
		{
			// Prepocet na mA
			Hodnota32 = ADC_HodnotyPrumer[i];
			Hodnota32 = (8058*Hodnota32)/12;										// Vypocet skutecneho ubytku napeti
			Hodnota32 = Hodnota32/1667;												// Vypocet skutecneho proudu		
			
      // Prumerovani			
			Prumer+=Hodnota32;
			j++;
			// Zobrazeni proudu
			if(j==300)
			{
				Prumer=Prumer/300;
				LCD_HD44780_GoTo(0,1);
				if(Prumer<=10)
				{
					LCD_HD44780_PutInt16(0, 4);
				}
				else
				{										
					LCD_HD44780_PutInt16(Prumer, 4);					
				}
				
				LCD_HD44780_PutString("mA");
				j=0;									   // Vyresetovani prumerovani
				Prumer=0;
			}
	
			// Obsluha tlacitek
			if(TL_NAHORU_Read_0())							// Obsluha tlacitka NAHORU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				i--;
				if(i>4)
					i=4;
				break;
			}	
			else if(TL_DOLU_Read_0())							// Obsluha tlacitka DOLU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				i++;
				if(i>4)
					i=0;
				break;
			}
			else if(TL_ZPET_Read_0())							// Obsluha tlacitka NAHORU
			{												
				PEZO_BEEP();
				vTaskDelay(30*portTICK_FOR_1MS);
				while(TL_ZPET_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
				vTaskDelay(50*portTICK_FOR_1MS);
				goto UserInterface_ShowCurrent_Exit;
			}	
			else
			{
				vTaskDelay(1*portTICK_FOR_1MS);
			}
		}
	}
UserInterface_ShowCurrent_Exit:	
	LCD_HD44780_Clear();
}

/* --------------------------------------------
 Manualni nastaveni casu
 ------------------------------------------- */
void UserInterface_SetTime(void)
{
	uint8_t Hodiny=Time_Hodiny;
	uint8_t Minuty=Time_Minuty;
	uint8_t Vteriny=0;
		
	
	/* Zjisteni uzivatelem zadane hodnoty */
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Nastavit cas:");
	LCD_HD44780_GoTo(3,1);
	LCD_HD44780_PutString("--:--:--");	

	Hodiny = (uint8_t)UserInterfaceEdit_int16(Hodiny, 23, 2, 3, 1);
	Minuty = (uint8_t)UserInterfaceEdit_int16(Minuty, 59, 2, 6, 1);
	Vteriny = (uint8_t)UserInterfaceEdit_int16(Vteriny, 59, 2, 9, 1);

	SetTime(Hodiny,Minuty,Vteriny);
	PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)

	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno    ");
	vTaskDelay(400*portTICK_FOR_1MS);
	
	LCD_HD44780_Clear();
}

/* --------------------------------------------
 Manualni nastaveni datumu
 - Nekontroluje spravnost zadaneho data	!
 ------------------------------------------- */
void UserInterface_SetDate(void)
{
	uint8_t Den=Time_Den;
	uint8_t Mesic=Time_Mesic;
	uint8_t Rok=(Time_Rok-2000);
			
	/* Zjisteni uzivatelem zadane hodnoty */
	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("Nastavit datum:");
	LCD_HD44780_GoTo(3,1);
	LCD_HD44780_PutString("--.--.20--");	

	Den = (uint8_t)UserInterfaceEdit_int16(Den, 31, 2, 3, 1);
	Mesic = (uint8_t)UserInterfaceEdit_int16(Mesic, 12, 2, 6, 1);
	Rok = (uint8_t)UserInterfaceEdit_int16(Rok, 50, 2, 11, 1);

	RTC_SetDate(Den,Mesic,2000+Rok);
	PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
	
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno    ");
	vTaskDelay(400*portTICK_FOR_1MS);
	
	LCD_HD44780_Clear();
}

/* --------------------------------------------
  Funkce pro editaci IP adresy, masky podsite 
  nebo IP adresy vychozi brany
 ------------------------------------------- */
void UserInterface_SetIP(uint8_t typ)
{
	uint8_t	adresa[IP_ADR_SIZE]={0,0,0,0};
	uint8_t i;

	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	switch(typ)
	{
		case SET_IP:
		{
			LCD_HD44780_PutString("IP adresa:      ");
			LCD_HD44780_GoTo(0,1);
			for(i=0;i<IP_ADR_SIZE;i++)
			{
				LCD_HD44780_PutInt16(my_IP[i],3);
				LCD_HD44780_PutChar('.');
			}
			break;
		}
		case SET_MASK:
		{
			LCD_HD44780_PutString("Maska podsite:  ");
			LCD_HD44780_GoTo(0,1);
			for(i=0;i<IP_ADR_SIZE;i++)
			{
				LCD_HD44780_PutInt16(my_MASK[i],3);
				LCD_HD44780_PutChar('.');
			}
			break;
		}
		case SET_GATE:
		{
			LCD_HD44780_PutString("IP vychozi brany");
			LCD_HD44780_GoTo(0,1);
			for(i=0;i<IP_ADR_SIZE;i++)
			{
				LCD_HD44780_PutInt16(my_GATE[i],3);
				LCD_HD44780_PutChar('.');
			}
			break;
		}
	}
	LCD_HD44780_GoTo(0,15);
	LCD_HD44780_PutChar(' ');
	vTaskDelay(1500*portTICK_FOR_1MS);

	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("---.---.---.--- ");

	adresa[0]=(uint8_t)UserInterfaceEdit_int16(adresa[0], 255, 3, 0, 1);
	adresa[1]=(uint8_t)UserInterfaceEdit_int16(adresa[1], 255, 3, 4, 1);
	adresa[2]=(uint8_t)UserInterfaceEdit_int16(adresa[2], 255, 3, 8, 1);
	adresa[3]=(uint8_t)UserInterfaceEdit_int16(adresa[3], 255, 3, 12, 1);

	/* Pokud byla adresa nastavena na 0.0.0.0 tak neukladat novou hodnotu */
	if((adresa[0]==0)&(adresa[1]==0)&(adresa[2]==0)&(adresa[3]==0))
	{
		LCD_HD44780_GoTo(0,1);
		LCD_HD44780_PutString("   Neulozeno    ");
		vTaskDelay(800*portTICK_FOR_1MS);
		goto UserInterface_SetIP_Exit;
	}
	
	/* Ulozeni */
	for(i=0;i<IP_ADR_SIZE;i++)
	{
		switch(typ)
		{
			case SET_IP:
			{
				my_IP[i]=adresa[i];
				if(i%2==0)
				{
					FlashSave_Save8_high(FLASHSAVE_IP_0_1+(i/2), my_IP[i]);
				}
				else
				{
					FlashSave_Save8_low(FLASHSAVE_IP_0_1+(i/2), my_IP[i]);
				}
				break;
			}
			case SET_MASK:
			{
				my_MASK[i]=adresa[i];
				if(i%2==0)
				{
					FlashSave_Save8_high(FLASHSAVE_MASKA_0_1+(i/2), my_MASK[i]);
				}
				else
				{
					FlashSave_Save8_low(FLASHSAVE_MASKA_0_1+(i/2), my_MASK[i]);
				}
				break;
			}
			case SET_GATE:
			{			
				my_GATE[i]=adresa[i];
				if(i%2==0)
				{
					FlashSave_Save8_high(FLASHSAVE_GATE_0_1+(i/2), my_GATE[i]);
				}
				else
				{
					FlashSave_Save8_low(FLASHSAVE_GATE_0_1+(i/2), my_GATE[i]);
				}
				break;
			}
		}
	}
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno     ");
	vTaskDelay(800*portTICK_FOR_1MS);
UserInterface_SetIP_Exit:
	LCD_HD44780_Clear();
}

/* --------------------------------------------
  Funkce pro editaci IP adresy, masky podsite 
  nebo IP adresy vychozi brany
 ------------------------------------------- */
void UserInterface_SetPort(uint8_t typ)
{
	uint16_t port=0;

	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	switch(typ)
	{
		case SET_PORTHTTP:
		{
			LCD_HD44780_PutString("Port HTTP: ");
			LCD_HD44780_GoTo(11,0);			
			LCD_HD44780_PutInt16(my_HTTP_Port,5);
		}
		case SET_PORTUDP:
		{
			LCD_HD44780_PutString("Port UDP:  ");
			LCD_HD44780_GoTo(11,0);
			LCD_HD44780_PutInt16(my_UDP_Port,5);
			break;
		}
		case SET_PORTTELNET:
		{
			LCD_HD44780_PutString("Por.Telnet:");
			LCD_HD44780_GoTo(11,0);
			LCD_HD44780_PutInt16(my_Telnet_Port,5);
			break;
		}
	}

	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("Novy port: -----");

	port=UserInterfaceEdit_int16(port, 65535, 5, 11, 1);

	/* Pokud byla adresa nastavena na 0.0.0.0 tak neukladat novou hodnotu */
	if(port==0)
	{
		LCD_HD44780_GoTo(0,1);
		LCD_HD44780_PutString("   Neulozeno    ");
		vTaskDelay(800*portTICK_FOR_1MS);
		goto UserInterface_SetPort_Exit;
	}
	
	/* Ulozeni */
	switch(typ)
	{
		case SET_PORTHTTP:
		{
			my_HTTP_Port=port;
			FlashSave_Save16(FLASHSAVE_HTTPPORT, my_HTTP_Port);
			break;
		}
		case SET_PORTUDP:
		{
			my_UDP_Port=port;
			FlashSave_Save16(FLASHSAVE_UDPPORT, my_UDP_Port);
			break;
		}
		case SET_PORTTELNET:
		{			
			my_Telnet_Port=port;
			FlashSave_Save16(FLASHSAVE_TELNETPORT, my_Telnet_Port);
			break;
		}
	}
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno     ");
	vTaskDelay(800*portTICK_FOR_1MS);
UserInterface_SetPort_Exit:
	LCD_HD44780_Clear();
}

/* --------------------------------------------
  Funkce pro editaci MAC adresy
 ------------------------------------------- */
void UserInterface_SetMAC(void)
{
	uint8_t	adresa[MAC_ADR_SIZE]={0,0,0,0,0,0};
	uint8_t i;

	LCD_HD44780_Clear();
	LCD_HD44780_GoTo(0,0);
	LCD_HD44780_PutString("MAC adresa:     ");
	LCD_HD44780_GoTo(0,1);
	for(i=0;i<MAC_ADR_SIZE;i++)
	{
		LCD_HD44780_HexToLCD(my_MAC[i]);
	}
	
	vTaskDelay(1500*portTICK_FOR_1MS);

	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("HEX ------------");

	adresa[0]=(uint8_t)UserInterfaceEdit_hex8(adresa[0], 4, 1);
	adresa[1]=(uint8_t)UserInterfaceEdit_hex8(adresa[1], 6, 1);
	adresa[2]=(uint8_t)UserInterfaceEdit_hex8(adresa[2], 8, 1);
	adresa[3]=(uint8_t)UserInterfaceEdit_hex8(adresa[3], 10, 1);
	adresa[4]=(uint8_t)UserInterfaceEdit_hex8(adresa[4], 12, 1);
	adresa[5]=(uint8_t)UserInterfaceEdit_hex8(adresa[5], 14, 1);

	/* Pokud byla adresa nastavena na 0.0.0.0 tak neukladat novou hodnotu */
	if((adresa[0]==0)&(adresa[1]==0)&(adresa[2]==0)&(adresa[3]==0)&(adresa[4]==0)&(adresa[5]==0))
	{
		LCD_HD44780_GoTo(0,1);
		LCD_HD44780_PutString("   Neulozeno    ");
		vTaskDelay(800*portTICK_FOR_1MS);
		goto UserInterface_SetMAC_Exit;
	}
	
	/* Ulozeni */
	for(i=0;i<IP_ADR_SIZE;i++)
	{
		my_MAC[i]=adresa[i];
		if(i%2==0)
		{
			FlashSave_Save8_high(FLASHSAVE_MAC_0_1+(i/2), my_MAC[i]);
		}
		else
		{
			FlashSave_Save8_low(FLASHSAVE_MAC_0_1+(i/2), my_MAC[i]);
		}
	}
	LCD_HD44780_GoTo(0,1);
	LCD_HD44780_PutString("    Ulozeno     ");
	ServerInit();										// Inicializace ENC cipu (aby se do nej nahrala MAC adresa)
	vTaskDelay(800*portTICK_FOR_1MS);
UserInterface_SetMAC_Exit:
	LCD_HD44780_Clear();
}

/* --------------------------------------------
  Funkce pro editaci jedne 16-bit hodnoty na
  LCD pomoci tlacitek
 ------------------------------------------- */
uint16_t UserInterfaceEdit_int16(uint16_t hodnota, uint16_t maximum, uint8_t cifer, uint8_t x, uint8_t y)
{
	uint8_t i=0;
	uint16_t Cifry[5]={0,0,0,0,0};
	uint16_t MaxHodnota[5];
	uint16_t zaloha;
	const uint16_t Mocniny[5] = {10000,1000,100,10,1};

	/* Vypsani pocatecni hodnoty na LCD */
	LCD_HD44780_GoTo(x,y);								// Nastaveni zacatku vykreslovani 
	LCD_HD44780_PutInt16(hodnota,cifer);				// Vypsani pocatecni hodnoty na LCD

	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send_Enter();
		USART1_Send("Vychozi hodnota:",0);
		USART1_SendInteger(hodnota);
		USART1_Send(" Maximalni hodnota:",0);
		USART1_SendInteger(maximum);
		USART1_Send_Enter();
		USART1_FreeMUTEX();
	#endif

	/* Prepocet int16 na jednotlive cifry*/
	#ifdef DEBUG
		USART1_Send("Cifry hodnota: ",0);
	#endif
	zaloha = hodnota;
	for(i=0;i<5;i++)
	{
		Cifry[i] = hodnota/Mocniny[i];
		hodnota = hodnota%Mocniny[i];
		#ifdef DEBUG			
			USART1_Write('0'+Cifry[i]);			
		#endif
	}
	#ifdef DEBUG
		USART1_Send_Enter();
		USART1_Send("Cifry maximum: ",0);
	#endif
	hodnota=zaloha;
	
	zaloha = maximum;
	for(i=0;i<5;i++)
	{
		MaxHodnota[i] = maximum/Mocniny[i];
		maximum = hodnota%Mocniny[i];
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Write('0'+MaxHodnota[i]);			
			USART1_FreeMUTEX();
		#endif
	}	
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send_Enter();
		USART1_FreeMUTEX();
	#endif
	maximum=zaloha;

	/* Vlastni editace jednotlivych radu */
	LCD_HD44780_Cursor(1);								// Zapnuti blikajiciho urzoru
	i = 5-cifer;										// Nastaveni pocatecniho editovaneho radu
	while(i<5)
	{
		LCD_HD44780_GoTo(x+i-(5-cifer),y);

		if(TL_NAHORU_Read_0())							// Obsluha tlacitka NAHORU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			Cifry[i]++;
			if(Cifry[i]>9)
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("> Reset UP 1",1);
					USART1_FreeMUTEX();
			 	#endif
			 	Cifry[i]=0;
			}
			else if(UserInterface_CompareMax(Cifry, maximum))
			{
			 	#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("> Reset UP 2",1);
					USART1_FreeMUTEX();
			 	#endif
				Cifry[i]=0;
			}

			LCD_HD44780_PutChar('0'+Cifry[i]);
		}
		else if(TL_DOLU_Read_0())						// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			Cifry[i]--;
			if(Cifry[i]>9)
			{
				Cifry[i]=9;
			}			
				while(UserInterface_CompareMax(Cifry, maximum))
				{
					#ifdef DEBUG
						USART1_WaitForMUTEX();
						USART1_SetMUTEX();
						USART1_Send("> Reset DOWN",1);
						USART1_FreeMUTEX();
				 	#endif
					Cifry[i]--;
				}

			LCD_HD44780_PutChar('0'+Cifry[i]);
		}
		else if(TL_OK_Read_0())							// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_OK_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);						// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);	 		
			i++;										// Posun na dalsi rad (na nizsi)
		}
		else if(TL_ZPET_Read_0())						// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_ZPET_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);	 		
			LCD_HD44780_Cursor(0);						// Vypnuti blikajiciho urzoru
			return(hodnota);							// Ukonceni editace beze zmeny hodnoty
		}
	}
	LCD_HD44780_Cursor(0);								// Vypnuti blikajiciho urzoru
	
	/* Zpetny prepocet jednotlivych cifer na int16 */
	hodnota = 0;
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Scitani cifer: ",0);
		USART1_FreeMUTEX();
	#endif
	for(i=0;i<5;i++)
	{
		hodnota += Cifry[i]*Mocniny[i];
		#ifdef DEBUG			
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_SendInteger(hodnota);
			USART1_Send_Enter();
			USART1_FreeMUTEX();
		#endif
	}
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Nova hodnota: ",0);
		USART1_SendInteger(hodnota);
		USART1_Send_Enter();
		USART1_FreeMUTEX();
	#endif
	return(hodnota);
}

/* --------------------------------------------
  Funkce pro editaci jedne 8-bit hodnoty ve 
  formatu HEX na LCD pomoci tlacitek
 ------------------------------------------- */
uint8_t UserInterfaceEdit_hex8(uint8_t hodnota, uint8_t x, uint8_t y)
{
	uint8_t i=0;
	uint16_t Cifry[2]={0,0};

	/* Vypsani pocatecni hodnoty na LCD */
	LCD_HD44780_GoTo(x,y);								// Nastaveni zacatku vykreslovani 
	LCD_HD44780_HexToLCD(hodnota);						// Vypsani pocatecni hodnoty na LCD

	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send_Enter();
		USART1_Send("Vychozi hodnota:",0);
		USART1_Send_Hex(hodnota);
		USART1_Send_Enter();
		USART1_FreeMUTEX();
	#endif

	/* Vlastni editace jednotlivych radu */
	LCD_HD44780_Cursor(1);								// Zapnuti blikajiciho urzoru
	while(i<2)
	{
		LCD_HD44780_GoTo(x+i,y);

		if(TL_NAHORU_Read_0())							// Obsluha tlacitka NAHORU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_NAHORU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			Cifry[i]++;
			if(Cifry[i]>15)
			{
				#ifdef DEBUG
					USART1_WaitForMUTEX();
					USART1_SetMUTEX();
					USART1_Send("> Reset UP 1",1);
					USART1_FreeMUTEX();
			 	#endif
			 	Cifry[i]=0;
			}
			
			if(Cifry[i]<10)
				LCD_HD44780_PutChar('0'+Cifry[i]);
			else
				LCD_HD44780_PutChar('A'+(Cifry[i]-10));
		}
		else if(TL_DOLU_Read_0())						// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_DOLU_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);
			Cifry[i]--;
			if(Cifry[i]>15)
			{
				Cifry[i]=15;
			}

			if(Cifry[i]<10)
				LCD_HD44780_PutChar('0'+Cifry[i]);
			else
				LCD_HD44780_PutChar('A'+(Cifry[i]-10));
		}
		else if(TL_OK_Read_0())							// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_OK_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);						// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);	 		
			i++;										// Posun na dalsi rad (na nizsi)
		}
		else if(TL_ZPET_Read_0())						// Obsluha tlacitka DOLU
		{												
			PEZO_BEEP();
			vTaskDelay(30*portTICK_FOR_1MS);
			while(TL_ZPET_Read_0()) vTaskDelay(1*portTICK_FOR_1MS);					// Cekani na uvolneni tlacitka
			vTaskDelay(50*portTICK_FOR_1MS);	 		
			LCD_HD44780_Cursor(0);						// Vypnuti blikajiciho kurzoru
			return(hodnota);							// Ukonceni editace beze zmeny hodnoty
		}
	}
	LCD_HD44780_Cursor(0);								// Vypnuti blikajiciho urzoru
	
	/* Zpetny prepocet jednotlivych cifer na int8 */
	hodnota = 0;
	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Scitani cifer: ",0);
		USART1_FreeMUTEX();
	#endif
	
	hodnota += Cifry[1];								// Pricteni druhe cifry x0 - xF
	#ifdef DEBUG			
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send_Hex(hodnota);
		USART1_Send_Enter();
		USART1_FreeMUTEX();
	#endif

	hodnota += Cifry[0]*16;								// Pricteni druhe cifry 0x - Fx
	#ifdef DEBUG			
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send_Hex(hodnota);
		USART1_Send_Enter();
		USART1_Send("Nova hodnota: ",0);
		USART1_Send_Hex(hodnota);
		USART1_Send_Enter();
		USART1_FreeMUTEX();
	#endif
	return(hodnota);
}

/* --------------------------------------------
  Funkce pro overeni jestli je jedno cislo vetsi
  nez druhe pri zadavani pomoci jednotlivych cifer
 ------------------------------------------- */
uint8_t UserInterface_CompareMax(uint16_t *Cislo, uint16_t maximum)
{
	uint16_t hodnota = 0;
	uint16_t Mocniny[5] = {10000,1000,100,10,1};
	uint8_t i;
	
	/* Overeni preteceni 16-bit*/
	if((Cislo[0]>6)|((Cislo[0]==6)&(((Cislo[1]*1000)+(Cislo[2]*100)+(Cislo[3]*10)+(Cislo[4])>(uint16_t)5535))))
	{
		#ifdef DEBUG
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_Send("Scitani cifer: Preteceni",1);
			USART1_FreeMUTEX();
		#endif
		return(1);
	}

	#ifdef DEBUG
		USART1_WaitForMUTEX();
		USART1_SetMUTEX();
		USART1_Send("Scitani cifer: ",1);
		USART1_FreeMUTEX();
	#endif
	for(i=0;i<5;i++)
	{
		hodnota += Cislo[i]*Mocniny[i];
		#ifdef DEBUG			
			USART1_WaitForMUTEX();
			USART1_SetMUTEX();
			USART1_SendInteger(hodnota);
			USART1_Send_Enter();
			USART1_FreeMUTEX();
		#endif
	}
	if(hodnota>maximum)
	{
		return(1);
	}
	else
	{
		return(0);
	}
}

/* Konec souboru */
