/****************************************************************************
 Title	:	TERMINAL FOR VISUALIZATION AND CONTROL CONDITIONS IN HOUSE
 Author	:	Patrik Szalay <xszala01@stud.feec.vutbr.cz>
 File	:	UI.c
 Software:	CodeWarrior Development Studio Version: 10.6 Build Id:140329
 Target	:	MC9S08LH64

 DESCRIPTION
       Code for the terminal's user interface

 USAGE
       
*****************************************************************************/
#include "UI.h"
static UIState menuState = HOME;
static UICursor selected = CUR_NONE;
static bool enableNavigate = false, setup_AutoSwitchingEnable = true, setup_systTypeGas = true;
static time setup_time;
static decimal setup_roomDayTemp = 2100, setup_roomNightTemp = 1900, setup_roomTempHyst = 100, setup_waterMaxTemp = 6500, setup_waterTempDiff = 1500, setup_AutoSwitchingOn = 1300, setup_AutoSwitchingOff = 1600, setup_degPerHour = 200;
static int setup_DayStart = 6, setup_DayEnd = 23, setup_waterPumpTime = 5;
bool refreshTime = false;
MeasErr MeasError;

void refreshUI(void)
{
	if (!disp_Bcklght_en)
	{
		menuState = HOME;
	}
	switch (menuState)
	{
	case HOME: UIhomeScreen(); break;
	case BCKLGHT: UIhomeScreen(); break;
	case MENU_MAIN: UIMenu_Main(); break;
	case MENU_SETUP: UIMenu_Setup(); break;
	case MENU_DATETIME: UIMenu_DateTime(); break;
	case MENU_TEMPERATURE: UIMenu_Temperature(); break;
	case MENU_ROOMTEMP: UIMenu_RoomTemperature(); break;
	case MENU_WATERTEMP: UIMenu_WaterTemperature(); break;
	case MENU_PROGRAM: UIMenu_Program(); break;
	case MENU_MODESELECT: UIMenu_ModeSelect(); break;
	case MENU_AUTOSWITCHING: UIMenu_AutoSwitching(); break;
	case MENU_STATUS: UIMenu_Status(); break;
	case MENU_RESET: UIMenu_Reset(); break;

	default: UIhomeScreen(); break;
	}
}

void UIhomeScreen(void)
{
	char buffer[30];
	
	lcd_command(LCD_DISP_ON);
		
	
	Num_aux((int)(getRoom_Temp() / 100));
	Time_HM(clock_now.hours, clock_now.minutes);
	Set_week_day(clock_now.dayOfWeek);
	
	clockRefreshFromRTC();
	lcd_gotoxy(0,0);
	(void) sprintf(buffer,"%02d/%02d/%02d ",clock_now.day, clock_now.month,clock_now.year - 2000);
	lcd_puts(buffer);
	lcd_gotoxy(9,0);
	switch(clock_now.dayOfWeek)
	{
		case MO: lcd_puts("MO "); break;
		case TU: lcd_puts("TU "); break;
		case WE: lcd_puts("WE "); break;
		case TH: lcd_puts("TH "); break;
		case FR: lcd_puts("FR "); break;
		case SA: lcd_puts("SA "); break;
		case SU: lcd_puts("SU "); break;
	}
	(void) sprintf(buffer,"%02d:%02d:%02d",clock_now.hours,clock_now.minutes,clock_now.seconds);
	lcd_gotoxy(12,0);
	lcd_puts(buffer);
	
	lcd_gotoxy(0, 1);
	lcd_puts("Room:");
	if (!MeasError.roomTemp)
	{
		if(((int)getRoom_Temp() / 100) == 0 && (int)getRoom_Temp() < 0)
		{
			(void)sprintf(buffer, "-%2d.%01d", (int)getRoom_Temp() / 100, abs((int)getRoom_Temp() % 100 / 10));
		}
		else
		{
			(void)sprintf(buffer, "%3d.%01d", (int)getRoom_Temp() / 100, abs((int)getRoom_Temp() % 100 / 10));
		}
		lcd_puts(buffer);
		lcd_putc(LCD_deg_char);
		lcd_puts("C ");
	}
	else
	{
		lcd_puts("DISCONN");
	}
	if (SCIS_SCISCONN)
	{
		if (SCIConnCheck(GUI))
		{
			lcd_puts(" PC");
		}
		else if (SCIConnCheck(MATLAB))
		{
			lcd_puts("MAT");
		}
		lcd_putc(LCD_connected_A_char);
		lcd_putc(LCD_connected_B_char);
	}
	else
	{
		lcd_puts("     ");
	}
	getHeater_State() ? lcd_putc(LCD_heat_char) : lcd_puts(" ");
	getPump_State() ? lcd_putc(LCD_pump_char) : lcd_puts(" ");
	lcd_gotoxy(0, 2);
	lcd_puts("Water:");
	if (!MeasError.waterOutTemp)
	{
		(void)sprintf(buffer, "%2d.%01d", (int)getWater_Out_Temp() / 100, abs((int)getWater_Out_Temp() % 100 / 10));
		lcd_puts(buffer);
		lcd_putc(LCD_deg_char);
		lcd_puts("C   ");
	}
	else
	{
		lcd_puts("DISCONN  ");
	}
	if(getDayState() == DAY)
	{
		lcd_puts("  Day");
	}
	else
	{
		lcd_puts("Night");
	}
	lcd_gotoxy(0, 3);
	lcd_puts("Out: ");
	if (!MeasError.outdoorTemp && !MeasError.outdoorHum)
	{
		if(((int)getOutDoor_Temp() / 100) == 0 && (int)getOutDoor_Temp() < 0)
		{
			(void)sprintf(buffer, "-%2d.%01d", (int)getOutDoor_Temp() / 100, abs((int)getOutDoor_Temp() % 100 / 10));
		}
		else
		{
			(void)sprintf(buffer, "%3d.%01d", (int)getOutDoor_Temp() / 100, abs((int)getOutDoor_Temp() % 100 / 10));
		}
		lcd_puts(buffer);
		lcd_putc(LCD_deg_char);
		lcd_puts("C  ");
		(void)sprintf(buffer, "%3d", (int)getOutDoor_Hum() / 100);
		lcd_puts(buffer);
		lcd_puts("%RH");
	}
	else
	{
		lcd_puts("DISCONN       ");
	}

}

void cursor(byte button)
{
	if (enableNavigate)
	{
		enableNavigate = false;
		switch (menuState)
		{
		case HOME:{
					  if (button == ENTER || button == UP || button == DOWN || button == LEFT || button == RIGHT)
					  {
						  menuState = BCKLGHT;
						  lcd_Bcklght_on();
						  selected = CUR_NONE;
					  }
					  break;
		}
		case BCKLGHT:{
						 if (button == ENTER)
						 {
							 menuState = MENU_MAIN;
							 lcd_clrscr();
							 selected = CUR_Setup;
						 }
						 else if (button == UP || button == DOWN)
						 {
							 menuState = MENU_TEMPERATURE;
							 lcd_clrscr();
							 selected = CUR_RoomTempSetup;
						 }
						 lcd_Bcklght_on();
						 break;
		}
		case MENU_MAIN:{
						   if (button == ENTER)
						   {
							   if (selected == CUR_Setup)
							   {
								   menuState = MENU_SETUP;
								   lcd_clrscr();
								   selected = CUR_DateTimeSetup;
							   }
							   else if (selected == CUR_Status)
							   {
								   menuState = MENU_STATUS;
								   lcd_clrscr();
								   selected = CUR_NONE;
							   }
							   else if (selected == CUR_Reset)
							   {
								   menuState = MENU_RESET;
								   lcd_clrscr();
								   selected = CUR_No;
							   							   }
							   else if (selected == CUR_Return)
							   {
								   menuState = BCKLGHT;
								   lcd_clrscr();
								   selected = CUR_NONE;
							   }
						   }
						   else if (button == DOWN)
						   {
							   if (selected == CUR_Setup)
							   {
								   selected = CUR_Status;
							   }
							   else if (selected == CUR_Status)
							   {
								   selected = CUR_Reset;
							   }
							   else if (selected == CUR_Reset)
							   {
								   selected = CUR_Return;
							   }
							   else if (selected == CUR_Return)
							   {
								   selected = CUR_Setup;
							   }
							   
						   }
						   else if (button == UP)
						   {
							   if (selected == CUR_Setup)
							   {
								   selected = CUR_Return;
							   }
							   else if (selected == CUR_Status)
							   {
								   selected = CUR_Setup;
							   }
							   else if (selected == CUR_Reset)
							   {
								   selected = CUR_Status;
							   }
							   else if (selected == CUR_Return)
							   {
								   selected = CUR_Reset;
							   }
						   }
						   lcd_Bcklght_on();
						   break;
		}
		case MENU_SETUP:{
							if (button == ENTER)
							{
								if (selected == CUR_DateTimeSetup)
								{
									menuState = MENU_DATETIME;
									lcd_clrscr();
									selected = CUR_DateSetup;
									setup_time = clock_now;
								}
								else if (selected == CUR_TemperaturesSetup)
								{
									menuState = MENU_TEMPERATURE;
									lcd_clrscr();
									selected = CUR_RoomTempSetup;
								}
								else if (selected == CUR_ProgramSetup)
								{
									menuState = MENU_PROGRAM;
									lcd_clrscr();
									selected = CUR_ModeSelectSetup;
								}
								else if (selected == CUR_Return)
								{
									menuState = MENU_MAIN;
									lcd_clrscr();
									selected = CUR_Return;
								}
							}
							else if (button == DOWN)
							{
								if (selected == CUR_DateTimeSetup)
								{
									selected = CUR_TemperaturesSetup;
								}
								else if (selected == CUR_TemperaturesSetup)
								{
									selected = CUR_ProgramSetup;
								}
								else if (selected == CUR_ProgramSetup)
								{
									selected = CUR_Return;
								}
								else if (selected == CUR_Return)
								{
									selected = CUR_DateTimeSetup;
								}
							}
							else if (button == UP)
							{
								if (selected == CUR_DateTimeSetup)
								{
									selected = CUR_Return;
								}
								else if (selected == CUR_TemperaturesSetup)
								{
									selected = CUR_DateTimeSetup;
								}
								else if (selected == CUR_ProgramSetup)
								{
									selected = CUR_TemperaturesSetup;
								}
								else if (selected == CUR_Return)
								{
									selected = CUR_ProgramSetup;
								}
							}
							lcd_Bcklght_on();
							break;
		}
		case MENU_DATETIME:{
							   if (button == ENTER)
							   {
								   if (selected == CUR_DateSetup)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_D;
								   }
								   else if (selected == CUR_TimeSetup)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Time_H;
								   }
								   else if (selected == CUR_Date_D || selected == CUR_Date_M || selected == CUR_Date_Y || selected == CUR_Date_DOW)
								   {
									   lcd_command(LCD_DISP_ON);
									   selected = CUR_DateSetup;
								   }
								   else if (selected == CUR_Time_H || selected == CUR_Time_M)
								   {
									   lcd_command(LCD_DISP_ON);
									   selected = CUR_TimeSetup;
								   }
								   else if (selected == CUR_Save)
								   {
									   menuState = MENU_SETUP;
									   selected = CUR_Return;
									   lcd_command(LCD_DISP_ON);
									   refreshTime = true;
									   clock_now = setup_time;
									   lcd_clrscr();
									   selected = CUR_Return;
								   }
								   else if (selected == CUR_Return)
								   {
									   lcd_command(LCD_DISP_ON);
									   menuState = MENU_SETUP;
									   lcd_clrscr();
									   selected = CUR_Return;
								   }
							   }
							   else if (button == DOWN)
							   {
								   if (selected == CUR_DateSetup)
								   {
									   selected = CUR_TimeSetup;
								   }
								   else if (selected == CUR_TimeSetup)
								   {
									   selected = CUR_Save;
								   }
								   else if (selected == CUR_Save)
								   {
									   selected = CUR_Return;
								   }
								   else if (selected == CUR_Return)
								   {
									   selected = CUR_DateSetup;
								   }
								   else if (selected == CUR_Date_D)
								   {
									   setup_time.day--;
									   if ((setup_time.day < 1 && ((setup_time.month == 1) || (setup_time.month == 3) || (setup_time.month == 5) || (setup_time.month == 7) || (setup_time.month == 8) || (setup_time.month == 10) || (setup_time.month == 12))))
										   setup_time.day = 31;
									   else if ((setup_time.day < 1 && ((setup_time.month == 4) || (setup_time.month == 6) || (setup_time.month == 9) || (setup_time.month == 11))))
										   setup_time.day = 30;
									   else if ((setup_time.day < 1 && (setup_time.month == 2) && setup_time.year % 4 == 0))
										   setup_time.day = 29;
									   else if ((setup_time.day < 1 && (setup_time.month == 2) && setup_time.year % 4 != 0))
										   setup_time.day = 28;

								   }
								   else if (selected == CUR_Date_M)
								   {
									   setup_time.month--;
									   if (setup_time.month < 1)
										   setup_time.month = 12;
								   }
								   else if (selected == CUR_Date_Y)
								   {
									   setup_time.year--;
								   }
								   else if (selected == CUR_Date_DOW)
								   {
									   setup_time.dayOfWeek--;
									   if (setup_time.dayOfWeek < 1)
										   setup_time.dayOfWeek = 7;
								   }
								   else if (selected == CUR_Time_H)
								   {
									   setup_time.hours--;
									   if (setup_time.hours == 255)
										   setup_time.hours = 23;
								   }
								   else if (selected == CUR_Time_M)
								   {
									   setup_time.minutes--;
									   if (setup_time.minutes == 255)
										   setup_time.minutes = 59;
								   }
							   }
							   else if (button == UP)
							   {
								   if (selected == CUR_DateSetup)
								   {
									   selected = CUR_Return;
								   }
								   else if (selected == CUR_TimeSetup)
								   {
									   selected = CUR_DateSetup;
								   }
								   else if (selected == CUR_Save)
								   {
									   selected = CUR_TimeSetup;
								   }
								   else if (selected == CUR_Return)
								   {
									   selected = CUR_Save;
								   }
								   else if (selected == CUR_Date_D)
								   {
									   setup_time.day++;
									   if ((setup_time.day > 31 && ((setup_time.month == 1) || (setup_time.month == 3) || (setup_time.month == 5) || (setup_time.month == 7) || (setup_time.month == 8) || (setup_time.month == 10) || (setup_time.month == 12)))
										   || (setup_time.day > 30 && ((setup_time.month == 4) || (setup_time.month == 6) || (setup_time.month == 9) || (setup_time.month == 11))) || (setup_time.day > 29 && (setup_time.month == 2) && setup_time.year % 4 == 0)
										   || (setup_time.day > 28 && (setup_time.month == 2) && setup_time.year % 4 != 0))
										   setup_time.day = 1;

								   }
								   else if (selected == CUR_Date_M)
								   {
									   setup_time.month++;
									   if (setup_time.month > 12)
										   setup_time.month = 1;
								   }
								   else if (selected == CUR_Date_Y)
								   {
									   setup_time.year++;
								   }
								   else if (selected == CUR_Date_DOW)
								   {
									   setup_time.dayOfWeek++;
									   if (setup_time.dayOfWeek > 7)
										   setup_time.dayOfWeek = 1;
								   }
								   else if (selected == CUR_Time_H)
								   {
									   setup_time.hours++;
									   if (setup_time.hours > 23)
										   setup_time.hours = 0;
								   }
								   else if (selected == CUR_Time_M)
								   {
									   setup_time.minutes++;
									   if (setup_time.minutes > 59)
										   setup_time.minutes = 0;
								   }

							   }
							   else if (button == LEFT)
							   {
								   if (selected == CUR_Date_D)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_DOW;
								   }
								   else if (selected == CUR_Date_M)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_D;
								   }
								   else if (selected == CUR_Date_Y)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_M;
								   }
								   else if (selected == CUR_Date_DOW)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_Y;
								   }
								   if (selected == CUR_Time_H)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Time_M;
								   }
								   else if (selected == CUR_Time_M)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Time_H;
								   }
							   }
							   else if (button == RIGHT)
							   {
								   if (selected == CUR_Date_D)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_M;
								   }
								   else if (selected == CUR_Date_M)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_Y;
								   }
								   else if (selected == CUR_Date_Y)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Date_DOW;
								   }
								   if (selected == CUR_Time_H)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_Time_M;
								   }
							   }
							   lcd_Bcklght_on();
							   break;
		}
		case MENU_TEMPERATURE:{
								  if (button == ENTER)
								  {
									  if (selected == CUR_RoomTempSetup)
									  {
										  menuState = MENU_ROOMTEMP;
										  lcd_clrscr();
										  selected = CUR_DayTempSetup;
										  setup_roomDayTemp = getRegulateToDayTemp();
										  setup_roomNightTemp = getRegulateToNightTemp();
										  setup_roomTempHyst = getRegulationHyst();
									  }
									  else if (selected == CUR_WaterTempSetup)
									  {
										  menuState = MENU_WATERTEMP;
										  lcd_clrscr();
										  selected = CUR_WaterMaxTempSetup;
										  setup_waterMaxTemp = getRegulateToMaxWaterTemp();
										  setup_waterTempDiff = getWaterDiff();
										  setup_waterPumpTime = (int)getPumpDelayTime();
									  }
									  else if (selected == CUR_Return)
									  {
										  menuState = MENU_SETUP;
										  lcd_clrscr();
										  selected = CUR_Return;
									  }
								  }
								  else if (button == DOWN)
								  {
									  if (selected == CUR_RoomTempSetup)
									  {
										  selected = CUR_WaterTempSetup;
									  }
									  else if (selected == CUR_WaterTempSetup)
									  {
										  selected = CUR_Return;
									  }
									  else if (selected == CUR_Return)
									  {
										  selected = CUR_RoomTempSetup;
									  }
								  }
								  else if (button == UP)
								  {
									  if (selected == CUR_RoomTempSetup)
									  {
										  selected = CUR_Return;
									  }
									  else if (selected == CUR_WaterTempSetup)
									  {
										  selected = CUR_RoomTempSetup;
									  }
									  else if (selected == CUR_Return)
									  {
										  selected = CUR_WaterTempSetup;
									  }
								  }
								  lcd_Bcklght_on();
								  break;
		}
		case MENU_ROOMTEMP:{
							   if (button == ENTER)
							   {
								   if (selected == CUR_DayTempSetup)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_DayTemp;
								   }
								   else if (selected == CUR_NightTempSetup)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_NightTemp;
								   }
								   else if (selected == CUR_RoomTempHystSetup)
								   {
									   lcd_command(LCD_DISP_ON_CURSOR);
									   selected = CUR_TempHyst;
								   }
								   else if (selected == CUR_DayTemp)
								   {
									   lcd_command(LCD_DISP_ON);
									   selected = CUR_DayTempSetup;
								   }
								   else if (selected == CUR_NightTemp)
								   {
									   lcd_command(LCD_DISP_ON);
									   selected = CUR_NightTempSetup;
								   }
								   else if (selected == CUR_TempHyst)
								   {
									   lcd_command(LCD_DISP_ON);
									   selected = CUR_RoomTempHystSetup;
								   }
								   else if (selected == CUR_Save)
								   {
									   menuState = MENU_TEMPERATURE;
									   selected = CUR_Return;
									   lcd_command(LCD_DISP_ON);
									   setRegulateToDayTemp(setup_roomDayTemp);
									   setRegulateToNightTemp(setup_roomNightTemp);
									   setRegulationHyst(setup_roomTempHyst);
									   lcd_clrscr();
									   selected = CUR_Return;
								   }
								   else if (selected == CUR_Return)
								   {
									   lcd_command(LCD_DISP_ON);
									   menuState = MENU_TEMPERATURE;
									   lcd_clrscr();
									   selected = CUR_Return;
								   }
							   }
							   else if (button == DOWN)
							   {
								   if (selected == CUR_DayTempSetup)
								   {
									   selected = CUR_NightTempSetup;
								   }
								   else if (selected == CUR_NightTempSetup)
								   {
									   selected = CUR_RoomTempHystSetup;
								   }
								   else if (selected == CUR_RoomTempHystSetup)
								   {
									   selected = CUR_Save;
								   }
								   else if (selected == CUR_Save)
								   {
									   selected = CUR_Return;
								   }
								   else if (selected == CUR_Return)
								   {
									   selected = CUR_DayTempSetup;
								   }
								   else if (selected == CUR_DayTemp)
								   {
									   setup_roomDayTemp -= ROOMTEMP_STEP;
									   if (setup_roomDayTemp < ROOMTEMP_MIN)
										   setup_roomDayTemp = ROOMTEMP_MAX;
								   }
								   else if (selected == CUR_NightTemp)
								   {
									   setup_roomNightTemp -= ROOMTEMP_STEP;
									   if (setup_roomNightTemp < ROOMTEMP_MIN)
										   setup_roomNightTemp = ROOMTEMP_MAX;
								   }
								   else if (selected == CUR_TempHyst)
								   {
									   setup_roomTempHyst -= ROOMTEMP_HYST_STEP;
									   if (setup_roomTempHyst < ROOMTEMP_HYST_MIN)
									   {
										   setup_roomTempHyst = ROOMTEMP_HYST_MAX;
									   }
								   }
							   }
							   else if (button == UP)
							   {
								   if (selected == CUR_DayTempSetup)
								   {
									   selected = CUR_Return;
								   }
								   else if (selected == CUR_NightTempSetup)
								   {
									   selected = CUR_DayTempSetup;
								   }
								   else if (selected == CUR_RoomTempHystSetup)
								   {
									   selected = CUR_NightTempSetup;
								   }
								   else if (selected == CUR_Save)
								   {
									   selected = CUR_RoomTempHystSetup;
								   }
								   else if (selected == CUR_Return)
								   {
									   selected = CUR_Save;
								   }
								   else if (selected == CUR_DayTemp)
								   {
									   setup_roomDayTemp += ROOMTEMP_STEP;
									   if (setup_roomDayTemp > ROOMTEMP_MAX)
										   setup_roomDayTemp = ROOMTEMP_MIN;
								   }
								   else if (selected == CUR_NightTemp)
								   {
									   setup_roomNightTemp += ROOMTEMP_STEP;
									   if (setup_roomNightTemp > ROOMTEMP_MAX)
										   setup_roomNightTemp = ROOMTEMP_MIN;
								   }
								   else if (selected == CUR_TempHyst)
								   {
									   setup_roomTempHyst += ROOMTEMP_HYST_STEP;
									   if (setup_roomTempHyst > ROOMTEMP_HYST_MAX)
									   {
										   setup_roomTempHyst = ROOMTEMP_HYST_MIN;
									   }
								   }

							   }
							   lcd_Bcklght_on();
							   break;
		}
		case MENU_WATERTEMP:{
								if (button == ENTER)
								{
									if (selected == CUR_WaterMaxTempSetup)
									{
										lcd_command(LCD_DISP_ON_CURSOR);
										selected = CUR_WaterMaxTemp;
									}
									else if (selected == CUR_WaterTempHystSetup)
									{
										lcd_command(LCD_DISP_ON_CURSOR);
										selected = CUR_WaterTempHyst;
									}
									else if (selected == CUR_WaterPumpTimeSetup)
									{
										lcd_command(LCD_DISP_ON_CURSOR);
										selected = CUR_WaterPumpTime;
									}
									else if (selected == CUR_WaterMaxTemp)
									{
										lcd_command(LCD_DISP_ON);
										selected = CUR_WaterMaxTempSetup;
									}
									else if (selected == CUR_WaterTempHyst)
									{
										lcd_command(LCD_DISP_ON);
										selected = CUR_WaterTempHystSetup;
									}
									else if (selected == CUR_WaterPumpTime)
									{
										lcd_command(LCD_DISP_ON);
										selected = CUR_WaterPumpTimeSetup;
									}
									else if (selected == CUR_Save)
									{
										menuState = MENU_TEMPERATURE;
										selected = CUR_Return;
										lcd_command(LCD_DISP_ON);
										setRegulateToMaxWaterTemp(setup_waterMaxTemp);
										setWaterDiff(setup_waterTempDiff);
										setPumpDelayTime((byte)setup_waterPumpTime);
										lcd_clrscr();
										selected = CUR_Return;
									}
									else if (selected == CUR_Return)
									{
										lcd_command(LCD_DISP_ON);
										menuState = MENU_TEMPERATURE;
										lcd_clrscr();
										selected = CUR_Return;
									}
								}
								else if (button == DOWN)
								{
									if (selected == CUR_WaterMaxTempSetup)
									{
										selected = CUR_WaterTempHystSetup;
									}
									else if (selected == CUR_WaterTempHystSetup)
									{
										selected = CUR_WaterPumpTimeSetup;
									}
									else if (selected == CUR_WaterPumpTimeSetup)
									{
										selected = CUR_Save;
									}
									else if (selected == CUR_Save)
									{
										selected = CUR_Return;
									}
									else if (selected == CUR_Return)
									{
										selected = CUR_WaterMaxTempSetup;
									}
									else if (selected == CUR_WaterMaxTemp)
									{
										setup_waterMaxTemp -= WATER_STEP;
										if (setup_waterMaxTemp < WATER_MIN)
											setup_waterMaxTemp = WATER_MAX;
									}
									else if (selected == CUR_WaterTempHyst)
									{
										setup_waterTempDiff -= WATER_DIFF_STEP;
										if (setup_waterTempDiff < WATER_DIFF_MIN)
											setup_waterTempDiff = WATER_DIFF_MAX;
									}
									else if (selected == CUR_WaterPumpTime)
									{
										setup_waterPumpTime -= PUMP_TIME_STEP;
										if (setup_waterPumpTime < PUMP_TIME_MIN)
											setup_waterPumpTime = PUMP_TIME_MAX;
									}
								}
								else if (button == UP)
								{
									if (selected == CUR_WaterMaxTempSetup)
									{
										selected = CUR_Return;
									}
									else if (selected == CUR_WaterTempHystSetup)
									{
										selected = CUR_WaterMaxTempSetup;
									}
									else if (selected == CUR_WaterPumpTimeSetup)
									{
										selected = CUR_WaterTempHystSetup;
									}
									else if (selected == CUR_Save)
									{
										selected = CUR_WaterPumpTimeSetup;
									}
									else if (selected == CUR_Return)
									{
										selected = CUR_Save;
									}
									else if (selected == CUR_WaterMaxTemp)
									{
										setup_waterMaxTemp += WATER_STEP;
										if (setup_waterMaxTemp > WATER_MAX)
											setup_waterMaxTemp = WATER_MIN;
									}
									else if (selected == CUR_WaterTempHyst)
									{
										setup_waterTempDiff += WATER_DIFF_STEP;
										if (setup_waterTempDiff > WATER_DIFF_MAX)
											setup_waterTempDiff = WATER_DIFF_MIN;
									}
									else if (selected == CUR_WaterPumpTime)
									{
										setup_waterPumpTime += PUMP_TIME_STEP;
										if (setup_waterPumpTime > PUMP_TIME_MAX)
											setup_waterPumpTime = PUMP_TIME_MIN;
									}

								}
								lcd_Bcklght_on();
								break;
		}

		case MENU_PROGRAM:{
							  if (button == ENTER)
							  {
								  if (selected == CUR_ModeSelectSetup)
								  {
									  menuState = MENU_MODESELECT;
									  setup_DayStart = getDayTempStart();
									  setup_DayEnd = getDayTempEnd();
									  setup_systTypeGas = getSystType();
									  lcd_clrscr();
									  selected = CUR_ModeTimeSetup;
								  }
								  else if (selected == CUR_AutoSwitchingSetup)
								  {
									  menuState = MENU_AUTOSWITCHING;
									  setup_AutoSwitchingOn = getOnAverageTemp();
									  setup_AutoSwitchingOff = getOffAverageTemp();
									  setup_AutoSwitchingEnable = getAutoSwitchingState();
									  lcd_clrscr();
									  selected = CUR_AutoSwitchingOnSetup;
								  }
								  else if (selected == CUR_Return)
								  {
									  menuState = MENU_SETUP;
									  lcd_clrscr();
									  selected = CUR_Return;
								  }
							  }
							  else if (button == DOWN)
							  {
								  if (selected == CUR_ModeSelectSetup)
								  {
									  selected = CUR_AutoSwitchingSetup;
								  }
								  else if (selected == CUR_AutoSwitchingSetup)
								  {
									  selected = CUR_Return;
								  }
								  else if (selected == CUR_Return)
								  {
									  selected = CUR_ModeSelectSetup;
								  }
							  }
							  else if (button == UP)
							  {
								  if (selected == CUR_ModeSelectSetup)
								  {
									  selected = CUR_Return;
								  }
								  else if (selected == CUR_AutoSwitchingSetup)
								  {
									  selected = CUR_ModeSelectSetup;
								  }
								  else if (selected == CUR_Return)
								  {
									  selected = CUR_AutoSwitchingSetup;
								  }
							  }
							  lcd_Bcklght_on();
							  break;
		}
		case MENU_MODESELECT:{
								 if (button == ENTER)
								 {
									 if (selected == CUR_ModeTimeSetup)
									 {
										 lcd_command(LCD_DISP_ON_CURSOR);
										 selected = CUR_DayStart;
									 }
									 else if (selected == CUR_SystTypeSetup)
									 {
										 lcd_command(LCD_DISP_ON_CURSOR);
										 selected = CUR_SystType;
									 }
									 else if (selected == CUR_DayStart || selected == CUR_DayEnd)
									 {
										 lcd_command(LCD_DISP_ON);
										 selected = CUR_ModeTimeSetup;
									 }
									 else if (selected == CUR_SystType)
									 {
										 lcd_command(LCD_DISP_ON);
										 selected = CUR_SystTypeSetup;
									 }
									 else if (selected == CUR_Save)
									 {
										 menuState = MENU_PROGRAM;
										 setDayTempStart(setup_DayStart);
										 setDayTempEnd(setup_DayEnd);
										 setSystType(setup_systTypeGas);
										 lcd_clrscr();
										 selected = CUR_Return;
									 }
									 else if (selected == CUR_Return)
									 {
									 	 menuState = MENU_PROGRAM;
									 	 selected = CUR_Return;
									 }
								 }
								 else if (button == DOWN)
								 {
									 if (selected == CUR_ModeTimeSetup)
									 {
										 selected = CUR_SystTypeSetup;
									 }
									 else if (selected == CUR_SystTypeSetup)
									 {
										 selected = CUR_Save;
									 }
									 else if (selected == CUR_Save)
									 {
										 selected = CUR_Return;
									 }
									 else if (selected == CUR_Return)
									 {
										 selected = CUR_ModeTimeSetup;
									 }
									 else if (selected == CUR_DayStart)
									 {
										 setup_DayStart--;
										 if (setup_DayStart < 0)
										 {
											 setup_DayStart = 23;
										 }
									 }
									 else if (selected == CUR_DayEnd)
									 {
										 setup_DayEnd--;
										 if (setup_DayEnd < 0)
										 {
											 setup_DayEnd = 23;
										 }
									 }
									 else if (selected == CUR_SystType)
									 {
										 if(setup_systTypeGas == false) setup_systTypeGas = true;
										 else setup_systTypeGas = false;
										 /*setup_degPerHour--;
										 if (setup_degPerHour < DEG_PER_HOUR_MIN)
										 {
											 setup_degPerHour = DEG_PER_HOUR_MAX;
										 }*/
									 }
								 }
								 else if (button == UP)
								 {
									 if (selected == CUR_ModeTimeSetup)
									 {
										 selected = CUR_Return;
									 }
									 else if (selected == CUR_SystTypeSetup)
									 {
										 selected = CUR_ModeTimeSetup;
									 }
									 else if (selected == CUR_Save)
									 {
										 selected = CUR_SystTypeSetup;
									 }
									 else if (selected == CUR_Return)
									 {
										 selected = CUR_Save;
									 }
									 else if (selected == CUR_DayStart)
									 {
										 setup_DayStart++;
										 if (setup_DayStart > 23)
										 {
											 setup_DayStart = 0;
										 }
									 }
									 else if (selected == CUR_DayEnd)
									 {
										 setup_DayEnd++;
										 if (setup_DayEnd > 23)
										 {
											 setup_DayEnd = 0;
										 }
									 }
									 else if (selected == CUR_SystType)
									 {
										 if(setup_systTypeGas == false) setup_systTypeGas = true;
										 else setup_systTypeGas = false;
										 /*setup_degPerHour++;
										 if (setup_degPerHour > DEG_PER_HOUR_MAX)
										 {
											 setup_degPerHour = DEG_PER_HOUR_MIN;
										 }*/
									 }
								 }
								 else if (button == LEFT || button == RIGHT)
								 {
									 if (selected == CUR_DayStart)
									 {
										 selected = CUR_DayEnd;
									 }
									 else if (selected == CUR_DayEnd)
									 {
										 selected = CUR_DayStart;
									 }
								 }
								 lcd_Bcklght_on();
								 break;
		}
		case MENU_AUTOSWITCHING:
		{
								   if (button == ENTER)
								   {
									   if (selected == CUR_AutoSwitchingOnSetup)
									   {
										   lcd_command(LCD_DISP_ON_CURSOR);
										   selected = CUR_AutoSwitchingOn;
									   }
									   else if (selected == CUR_AutoSwitchingOffSetup)
									   {
										   lcd_command(LCD_DISP_ON_CURSOR);
										   selected = CUR_AutoSwitchingOff;
									   }
									   else if (selected == CUR_AutoSwitchingStateSetup)
									   {
										   lcd_command(LCD_DISP_ON_CURSOR);
										   selected = CUR_AutoSwitchingState;
									   }
									   else if (selected == CUR_AutoSwitchingOn)
									   {
										   lcd_command(LCD_DISP_ON);
										   selected = CUR_AutoSwitchingOnSetup;
									   }
									   else if (selected == CUR_AutoSwitchingOff)
									   {
									   		lcd_command(LCD_DISP_ON);
									   		selected = CUR_AutoSwitchingOffSetup;
									   }
									   else if (selected == CUR_AutoSwitchingState)
									   {
									   		lcd_command(LCD_DISP_ON);
									   		selected = CUR_AutoSwitchingStateSetup;
									   }
									   else if (selected == CUR_Save)
									   {
										   menuState = MENU_PROGRAM;
										   setOnAverageTemp(setup_AutoSwitchingOn);
										   setOffAverageTemp(setup_AutoSwitchingOff);
										   setAutoSwitchingState(setup_AutoSwitchingEnable);
										   lcd_clrscr();
										   selected = CUR_Return;
									   }
									   else if (selected == CUR_Return)
									   {
										   menuState = MENU_PROGRAM;
										   selected = CUR_Return;
									   }
								   }
								   else if (button == DOWN)
								   {
									   if (selected == CUR_AutoSwitchingOnSetup)
									   {
										   selected = CUR_AutoSwitchingOffSetup;
									   }
									   else if (selected == CUR_AutoSwitchingOffSetup)
									   {
										   selected = CUR_AutoSwitchingStateSetup;
									   }
									   else if (selected == CUR_AutoSwitchingStateSetup)
									   {
										   selected = CUR_Save;
									   }									   
									   else if (selected == CUR_Save)
									   {
										   selected = CUR_Return;
									   }
									   else if (selected == CUR_Return)
									   {
										   selected = CUR_AutoSwitchingOnSetup;
									   }
									   else if (selected == CUR_AutoSwitchingOn)
									   {
										   setup_AutoSwitchingOn -= AUTOSW_STEP;
										   if (setup_AutoSwitchingOn < AUTOSW_MIN)
										   {
											   setup_AutoSwitchingOn = AUTOSW_MAX;
										   }
									   }
									   else if (selected == CUR_AutoSwitchingOff)
									   {
										   setup_AutoSwitchingOff -= AUTOSW_STEP;
										   if (setup_AutoSwitchingOff < AUTOSW_MIN)
										   {
										   		setup_AutoSwitchingOff = AUTOSW_MAX;
										   }
									   }
									   else if (selected == CUR_AutoSwitchingState)
									   {
										   if (setup_AutoSwitchingEnable == false) setup_AutoSwitchingEnable = true;
										   else setup_AutoSwitchingEnable = false;
									   }
								   }
								   else if (button == UP)
								   {
									   if (selected == CUR_AutoSwitchingOnSetup)
									   {
										   selected = CUR_Return;
									   }
									   else if (selected == CUR_AutoSwitchingOffSetup)
									   {
										   selected = CUR_AutoSwitchingOnSetup;
									   }
									   else if (selected == CUR_AutoSwitchingStateSetup)
									   {
										   selected = CUR_AutoSwitchingOffSetup;
									   }
									   else if (selected == CUR_Save)
									   {
										   selected = CUR_AutoSwitchingStateSetup;
									   }
									   else if (selected == CUR_Return)
									   {
										   selected = CUR_Save;
									   }
									   else if (selected == CUR_AutoSwitchingOn)
									   {
										   setup_AutoSwitchingOn += AUTOSW_STEP;
										   if (setup_AutoSwitchingOn > AUTOSW_MAX)
										   {
											   setup_AutoSwitchingOn = AUTOSW_MIN;
										   }
									   }
									   else if (selected == CUR_AutoSwitchingOff)
									   {
										   setup_AutoSwitchingOff += AUTOSW_STEP;
										   if (setup_AutoSwitchingOff < AUTOSW_MAX)
										   {
											   setup_AutoSwitchingOff = AUTOSW_MIN;
										   }
									   }
									   else if (selected == CUR_AutoSwitchingState)
									   {
										   if (setup_AutoSwitchingEnable == false) setup_AutoSwitchingEnable = true;
										   else setup_AutoSwitchingEnable = false;
									   }
								   }
								   lcd_Bcklght_on();
								   break;
		}
		case MENU_STATUS:{
						   if (button == ENTER)
						   {
							   
								   menuState = MENU_MAIN;
								   lcd_clrscr();
								   selected = CUR_Return;
						   }
						   else if (button == DOWN)
						   {  

						   }
						   else if (button == UP)
						   {
							   
						   }
						   lcd_Bcklght_on();
						   break;
		}
		case MENU_RESET:{
						   if (button == ENTER)
						   {
							   if (selected == CUR_Yes)
							   {
								   lcd_clrscr();
								   restart();
							   }
							   else if (selected == CUR_No)
							   {
								   menuState = MENU_MAIN;
								   lcd_clrscr();
								   selected = CUR_Return;
							   }
						   }
						   else if (button == LEFT)
						   {
							   if (selected == CUR_Yes)
							   {
								   selected = CUR_No;
							   }
							   else if (selected == CUR_No)
							   {
								   selected = CUR_Yes;
							   }

						   }
						   else if (button == RIGHT)
						   {
							   if (selected == CUR_Yes)
							   {
								   selected = CUR_No;
							   }
							   else if (selected == CUR_No)
							   {
								   selected = CUR_Yes;
							   }
						   }
						   lcd_Bcklght_on();
						   break;
		}
		}
	}
}


void UIMenu_Main(void)
{
	lcd_gotoxy(0, 0);
	lcd_puts("MAIN MENU           ");
	lcd_gotoxy(0, 1);
	(selected == CUR_Setup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Setup              ");
	lcd_gotoxy(0, 2);
	(selected == CUR_Status) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Status             ");
	lcd_gotoxy(0, 3);
	(selected == CUR_Reset) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Reset              ");
	lcd_gotoxy(13, 3);
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");

}

void UIMenu_Setup(void)
{
	lcd_gotoxy(0, 0);
	lcd_puts("MENU>>SETUP         ");
	lcd_gotoxy(0, 1);
	(selected == CUR_DateTimeSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Date/Time          ");
	lcd_gotoxy(0, 2);
	(selected == CUR_TemperaturesSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Temperatures       ");
	lcd_gotoxy(0, 3);
	(selected == CUR_ProgramSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Program            ");
	lcd_gotoxy(13, 3);
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");


}

void UIMenu_DateTime(void)
{
	char buffer[30];
	lcd_gotoxy(0, 0);
	lcd_puts("DATE/TIME SETUP     ");

	lcd_gotoxy(0, 1);
	(selected == CUR_DateSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Date: ");
	(void)sprintf(buffer, "%02d/%02d/%04d ", setup_time.day, setup_time.month, setup_time.year);
	lcd_puts(buffer);

	lcd_gotoxy(18, 1);
	switch (setup_time.dayOfWeek)
	{
	case MO: lcd_puts("MO"); break;
	case TU: lcd_puts("TU"); break;
	case WE: lcd_puts("WE"); break;
	case TH: lcd_puts("TH"); break;
	case FR: lcd_puts("FR"); break;
	case SA: lcd_puts("SA"); break;
	case SU: lcd_puts("SU"); break;
	}

	lcd_gotoxy(0, 2);
	(selected == CUR_TimeSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Time: ");
	(void)sprintf(buffer, "%02d:%02d  ", setup_time.hours, setup_time.minutes);
	lcd_puts(buffer);

	lcd_gotoxy(13, 2);
	(selected == CUR_Save) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Save  ");

	lcd_gotoxy(0, 3);
	lcd_puts("             ");
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");
	if (selected == CUR_Date_D)
	{
		lcd_gotoxy(8, 1);
	}
	else if (selected == CUR_Date_M)
	{
		lcd_gotoxy(11, 1);
	}
	else if (selected == CUR_Date_Y)
	{
		lcd_gotoxy(16, 1);
	}
	else if (selected == CUR_Date_DOW)
	{
		lcd_gotoxy(19, 1);
	}
	else if (selected == CUR_Time_H)
	{
		lcd_gotoxy(8, 2);
	}
	else if (selected == CUR_Time_M)
	{
		lcd_gotoxy(11, 2);
	}


}

void UIMenu_Temperature(void)
{
	lcd_gotoxy(0, 0);
	lcd_puts("TEMPERATURE SETUP   ");
	lcd_gotoxy(0, 1);
	(selected == CUR_RoomTempSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Room temperatures  ");
	lcd_gotoxy(0, 2);
	(selected == CUR_WaterTempSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Water temperatures ");
	lcd_gotoxy(0, 3);
	(selected == CUR_NONE) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("                   ");
	lcd_gotoxy(13, 3);
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");
}

void UIMenu_RoomTemperature(void)
{
	char buffer[30];
	lcd_gotoxy(0, 0);
	lcd_puts("ROOM TEMPERATURES   ");

	lcd_gotoxy(0, 1);
	(selected == CUR_DayTempSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Day:  ");
	(void)sprintf(buffer, "%2d.%01d", (int)setup_roomDayTemp / 100, abs((int)setup_roomDayTemp % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C       ");

	lcd_gotoxy(0, 2);
	(selected == CUR_NightTempSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Night:");
	(void)sprintf(buffer, "%2d.%01d", (int)setup_roomNightTemp / 100, abs((int)setup_roomNightTemp % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C       ");

	lcd_gotoxy(0, 3);
	(selected == CUR_RoomTempHystSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Hyst:  ");
	(void)sprintf(buffer, "%01d.%01d", (int)setup_roomTempHyst / 100, abs((int)setup_roomTempHyst % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C ");


	lcd_gotoxy(13, 2);
	(selected == CUR_Save) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Save  ");

	lcd_gotoxy(13, 3);
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");
	if (selected == CUR_DayTemp)
	{
		lcd_gotoxy(8, 1);
	}
	else if (selected == CUR_NightTemp)
	{
		lcd_gotoxy(8, 2);
	}
	else if (selected == CUR_TempHyst)
	{
		lcd_gotoxy(8, 3);
	}

}

void UIMenu_WaterTemperature(void)
{
	char buffer[30];
	lcd_gotoxy(0, 0);
	lcd_puts("WATER TEMPERATURES");

	lcd_gotoxy(0, 1);
	(selected == CUR_WaterMaxTempSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Max:  ");
	(void)sprintf(buffer, "%2d.%01d", (int)setup_waterMaxTemp / 100, abs((int)setup_waterMaxTemp % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C      ");

	lcd_gotoxy(0, 2);
	(selected == CUR_WaterTempHystSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Diff: ");
	(void)sprintf(buffer, "%2d", (int)setup_waterTempDiff / 100);
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C  ");
	
	(selected == CUR_Save) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Save  ");

	
	lcd_gotoxy(0, 3);
	(selected == CUR_WaterPumpTimeSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Delay:");
	(void)sprintf(buffer, "%2d", setup_waterPumpTime);
	lcd_puts(buffer);
	lcd_puts("min ");
	
	
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");

	if (selected == CUR_WaterMaxTemp)
	{
		lcd_gotoxy(8, 1);
	}
	else if (selected == CUR_WaterTempHyst)
	{
		lcd_gotoxy(8, 2);
	}
	else if (selected == CUR_WaterPumpTime)
	{
		lcd_gotoxy(8, 3);
	}
}

void UIMenu_Program(void)
{
	lcd_gotoxy(0, 0);
	lcd_puts("MENU>>PROGRAM       ");
	lcd_gotoxy(0, 1);
	(selected == CUR_ModeSelectSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Mode select        ");
	lcd_gotoxy(0, 2);
	(selected == CUR_AutoSwitchingSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Auto switching     ");
	lcd_gotoxy(0, 3);
	lcd_puts("             ");
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");
}

void UIMenu_ModeSelect(void)
{
	char buffer[30];
	lcd_gotoxy(0, 0);
	lcd_puts("MODE SELECT         ");

	lcd_gotoxy(0, 1);
	(selected == CUR_ModeTimeSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Day:  ");
	(void)sprintf(buffer, "%02d:00-%02d:00", setup_DayStart, setup_DayEnd);
	lcd_puts(buffer);

	lcd_gotoxy(0, 2);
	(selected == CUR_SystTypeSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Syst.: ");
	if(setup_systTypeGas)
	{
		lcd_puts(" GAS ");
	}
	else
	{
		lcd_puts("WOOD ");
	}

	lcd_gotoxy(13, 2);
	(selected == CUR_Save) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Save  ");

	lcd_gotoxy(13, 3);
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");
	if (selected == CUR_DayStart)
	{
		lcd_gotoxy(8, 1);
	}
	else if (selected == CUR_DayEnd)
	{
		lcd_gotoxy(14, 1);
	}
	else if (selected == CUR_SystType)
	{
		lcd_gotoxy(11, 2);
	}
}


void UIMenu_AutoSwitching(void)
{
	char buffer[30];
	lcd_gotoxy(0, 0);
	lcd_puts("AUTO SWITCHING      ");

	lcd_gotoxy(0, 1);
	(selected == CUR_AutoSwitchingOnSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("On:  ");
	(void)sprintf(buffer, "%02d", (int)setup_AutoSwitchingOn / 100);
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C          ");

	lcd_gotoxy(0, 2);
	(selected == CUR_AutoSwitchingOffSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Off: ");
	(void)sprintf(buffer, "%2d", (int)setup_AutoSwitchingOff / 100);
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C   ");

	lcd_gotoxy(0, 3);
	(selected == CUR_AutoSwitchingStateSetup) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("State: ");
	if (setup_AutoSwitchingEnable)
	{
		lcd_puts(" ON");
	}
	else
	{
		lcd_puts("OFF");
	}

	lcd_gotoxy(13, 2);
	(selected == CUR_Save) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Save  ");

	lcd_gotoxy(13, 3);
	(selected == CUR_Return) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Return");
	if (selected == CUR_AutoSwitchingOn)
	{
		lcd_gotoxy(7, 1);
	}
	else if (selected == CUR_AutoSwitchingOff)
	{
		lcd_gotoxy(7, 2);
	}
	else if (selected == CUR_AutoSwitchingState)
	{
		lcd_gotoxy(10, 3);
	}
}

void UIMenu_Status(void)
{
	char buffer[30];
	lcd_gotoxy(0, 0);
	lcd_puts("STATUS  Press Enter");
	lcd_putc(LCD_up_arrow);
	lcd_gotoxy(0, 1);
	lcd_puts("State:");
	if(getDayState() == DAY)
	{
		lcd_puts("  Day");
	}
	else
	{
		lcd_puts("Night");
	}
	(void)sprintf(buffer, "%3d.%01d", (int)getRegulateToTemp() / 100, abs((int)getRegulateToTemp() % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C ");
		
	lcd_gotoxy(0, 2);
	lcd_puts("Water:");
	(void)sprintf(buffer, "%2d.%01d", (int)getWater_Out_Temp() / 100, abs((int)getWater_Out_Temp() % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C ");
	(void)sprintf(buffer, "%2d.%01d", (int)getWater_Return_Temp() / 100, abs((int)getWater_Return_Temp() % 100 / 10));
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C");
	
	lcd_gotoxy(0, 3);
	lcd_puts("Out avg.:");
	if(((int)getAverageTemp() / 100) == 0 && (int)getAverageTemp() < 0)
	{
		(void)sprintf(buffer, "-%2d.%01d", (int)getAverageTemp() / 100, abs((int)getAverageTemp() % 100 / 10));
	}
	else
	{
		(void)sprintf(buffer, "%3d.%01d", (int)getAverageTemp() / 100, abs((int)getAverageTemp() % 100 / 10));
	}	
	lcd_puts(buffer);
	lcd_putc(LCD_deg_char);
	lcd_puts("C   ");

	lcd_putc(LCD_down_arrow);
}

void UIenableNavigate(void)
{
	enableNavigate = true;
}

void UIMenu_Reset(void)
{
	lcd_gotoxy(0,0);
	lcd_puts("SYSTEM RESET        ");
	lcd_gotoxy(0,1);
	lcd_puts("  Restart system?   ");
	lcd_gotoxy(0,2);
	lcd_puts("Clears all settings!");
	lcd_gotoxy(0, 3);
	(selected == CUR_Yes) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("Yes           ");
	(selected == CUR_No) ? lcd_putc(LCD_arrow_char) : lcd_putc(' ');
	lcd_puts("No  ");

}

void restart(void)
{
	lcd_clrscr();
	lcd_gotoxy(0,0);
	lcd_puts("SYSTEM RESET        ");
	lcd_gotoxy(0,1);
	lcd_puts("Restarting system...");
	lcd_gotoxy(0,2);
	lcd_puts("                    ");
	delay(60000);
	lcd_gotoxy(0,1);
	lcd_puts("System is restarted.");
	lcd_gotoxy(0,3);
	lcd_puts("    Press ENTER!    ");
	delay(5000);
	INIT_SP_FROM_STARTUP_DESC();
}
