// Knihovna pro komunukaci s programem na PC prostrednictvim etheretu - UDP

#include "UDP_komunikace.h"
#include "udp.h"
#include "tcp.h"
#include "My_ADC.h"
#include "My_PWM.h"
#include "My_rtc.h"
#include "stm32f10x_tim.h"
#include "PIN_config.h"
#include "planovac.h"
#include "user_interface.h"
#include <stdlib.h>

// Definice
#define MAXSENSORS 				3
#define TEPLOTA_LED_ID			0
#define TEPLOTA_TRANZ_ID		1
#define TEPLOTA_EXT_ID			2

// Externi promenne
extern uint16_t ADC_HodnotyPrumer[6];			// Jiz zprumerovana namerena hodnota pres 200 vzorku
extern uint16_t PWM_max[5];
extern uint16_t FANtemp[4];
extern uint16_t FANval[4];
extern uint16_t SHUTDOWNtemp;
extern uint8_t AutomatickyRezim;

extern int16_t Teplota[MAXSENSORS];
extern uint16_t HumidityRAW;
extern uint8_t Humidity;

extern uint8_t FANmode;

extern uint8_t TCP_Status;					// Stav stavoveho automatu TCP spojeni (pouze jedno spojeni soucasne)
extern uint32_t TCP_Timeout[TCP_TIMEOUT_COUNT];

extern uint32_t Planovac_Timestamp;						// Format: [0-16] ... cas ve vternach (17 bitu)
														// 		   [17-21] .. den			  (5 bitu)
														//		   [22-25] .. mesic			  (4 bity)
														//    	   [26-31] .. rok			  (6 bitu)
														// 31       24       16              0
														// RRRRRRMM MMDDDDDC CCCCCCCC CCCCCCCC
extern uint8_t Planovac_Kanal[5];						// [0-6] ... Kanal 1 (0-100%)
														// [0-6] ... Kanal 2 (0-100%)
														// [0-6] ... Kanal 3 (0-100%)
														// [0-6] ... Kanal 4 (0-100%)
														// [0-6] ... Kanal 5 (0-100%)
														// [7] ..... Kanal 6 (0-OFF/1-ON)
extern uint8_t PlanovacStav;
// Hodiny a datum
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;
/* -------------------------------------------
 Funkce pro zpracovani prijateho UDP datagramu
 ------------------------------------------ */
uint8_t UDPKom_ProcessData(uint8_t *RX_buffer)
{
	uint8_t PocetZaznamu, i;
	uint16_t Parametr, Hodnota;

	// Overeni o jaky typ dat se jedna
	if(RX_buffer[OFFSET_UDP_Kom_Typ]==UDPKom_Zadost)			// Zadost o data
	{
		return(UDPKom_Zadost);
	}
	else if(RX_buffer[OFFSET_UDP_Kom_Typ]==UDPKom_Nastaveni)	// Datagram obsahuje parametry s nastavenim
	{
		PocetZaznamu = RX_buffer[OFFSET_UDP_Kom_Delka];			// Zjisteni kolik parametru obsahuje datagram
		for(i=0;i<PocetZaznamu;i++)								// Prochazeni jednotlivich zaznamu
		{
			Parametr = RX_buffer[(OFFSET_UDP_Kom_Parametr+3*i)];
			Hodnota = (RX_buffer[(OFFSET_UDP_Kom_Hodnota+3*i)+0]<<8) | RX_buffer[(OFFSET_UDP_Kom_Hodnota+3*i)+1];
			switch(Parametr)									// Dekodovani parametru
			{
				case UDPKom_Param_PWM1max:
				{
					PWM_max[0] = Hodnota;
					break;
				}
				case UDPKom_Param_PWM2max:
				{
					PWM_max[1] = Hodnota;
					break;
				}
				case UDPKom_Param_PWM3max:
				{
					PWM_max[2] = Hodnota;
					break;
				}
				case UDPKom_Param_PWM4max:
				{
					PWM_max[3] = Hodnota;
					break;
				}
				case UDPKom_Param_PWM5max:
				{
					PWM_max[4] = Hodnota;
					break;
				}
				case UDPKom_Param_PWM1set:
				{
					if(Hodnota<=PWM_max[0])
					{
						PWM_Set_LED_1(Hodnota);
					}
					break;
				}
				case UDPKom_Param_PWM2set:
				{
					if(Hodnota<=PWM_max[1])
					{
						PWM_Set_LED_2(Hodnota);
					}
					break;
				}
				case UDPKom_Param_PWM3set:
				{
					if(Hodnota<=PWM_max[2])
					{
						PWM_Set_LED_3(Hodnota);
					}
					break;
				}
				case UDPKom_Param_PWM4set:
				{
					if(Hodnota<=PWM_max[3])
					{
						PWM_Set_LED_4(Hodnota);
					}
					break;
				}
				case UDPKom_Param_PWM5set:
				{
					if(Hodnota<=PWM_max[4])
					{
						PWM_Set_LED_5(Hodnota);
					}
					break;
				}
				case UDPKom_Param_FANset:
				{
					PWM_Set_FAN(Hodnota);
					break;
				}
				case UDPKom_Param_FANtemp1:
				{
					FANtemp[0] = Hodnota;
					break;
				}
				case UDPKom_Param_FANtemp2:
				{
					FANtemp[1] = Hodnota;
					break;
				}
				case UDPKom_Param_FANtemp3:
				{
					FANtemp[2] = Hodnota;
					break;
				}
				case UDPKom_Param_FANtemp4:
				{
					FANtemp[3] = Hodnota;
					break;
				}
				case UDPKom_Param_FANval1:
				{
					FANval[0] = Hodnota;
					break;
				}
				case UDPKom_Param_FANval2:
				{
					FANval[1] = Hodnota;
					break;
				}
				case UDPKom_Param_FANval3:
				{
					FANval[2] = Hodnota;
					break;
				}
				case UDPKom_Param_FANval4:
				{
					FANval[3] = Hodnota;
					break;
				}
				case UDPKom_Param_Backlight:
				{
					if(Hodnota==48000)
					{
						Backlight_ON();
					}
					else if(Hodnota==0)
					{
						Backlight_OFF();
					}					
					break;
				}
				case UDPKom_Param_FANforced:
				{
					if((Hodnota<=48000)&(Hodnota!=1))
					{
						FANmode=1;
						PWM_Set_FAN(Hodnota);
					}
					else
					{
						FANmode=0;
					}
					break;
				}
				case UDPKom_Param_TimeHOUR:
				{
					SetTimeRAW(0);
					SetTimeRAW(GetTimeRAW()+3600*Hodnota);
					PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
					break;
				}
				case UDPKom_Param_TimeMINUTE:
				{
					SetTimeRAW(GetTimeRAW()+60*Hodnota);
					PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
					break;
				}
				case UDPKom_Param_TimeSECOND:
				{
					SetTimeRAW(GetTimeRAW()+Hodnota);
					PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
					break;
				}
				case UDPKom_Param_TimeDAY:
				{
					RTC_SetDate(Hodnota,0,0);
					PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
					break;
				}
				case UDPKom_Param_TimeMONTH:
				{
					RTC_SetDate(0,Hodnota,0);
					PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
					break;
				}
				case UDPKom_Param_TimeYEAR:
				{
					RTC_SetDate(0,0,Hodnota);
					PlanovacStav|=(1<<PLANOVAC_RESTART);			// Restart planovace (aby se spravne nacetl dalsi plan)
					break;
				}
				case UDPKom_Param_LED6set:
				{
					if (Hodnota==0)
					{
						PWM_Set_LED_6_OFF();
					}
					else if (Hodnota==48000)
					{
						PWM_Set_LED_6_ON();
					}
					break;
				}
				case UDPKom_Param_LEDmode:
				{
					Hodnota&=0x3F;
					AutomatickyRezim=Hodnota;
					break;
				}
				case UDPKom_Param_FlashSensorErase:
				{
					if(Hodnota==1)
					{
						UserInterface_FlashSensorErase();
					}
					break;
				}
			}
		}
	}
	return(0);
}

/* -------------------------------------------
 Funkce pro odeslani parametru UDP datagramem
 na zarizeni identifikovane podle IP a ETH
 adresy v prichozim bufferu
 ------------------------------------------ */
uint8_t UDPKom_SendDataFromRX(uint8_t *RX_buffer, uint8_t *TX_buffer)
{
	uint16_t DelkaDat;
	uint16_t i=0;
	uint16_t Hodnota;
	uint32_t Hodnota32;

	// Vytvoreni hlavicky dat
	TX_buffer[UDPKom_OFFSET_UploadIdent+0] = (UDPKom_SyncWord&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadIdent+1] = UDPKom_SyncWord&0x00FF;
	TX_buffer[UDPKom_OFFSET_UploadType] = UDPKom_Odpoved;
		
	// Naplneni daty
	Hodnota = PWM_Get_LED_1();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PWM1;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota = PWM_Get_LED_2();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PWM2;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota = PWM_Get_LED_3();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PWM3;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = PWM_Get_LED_4();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PWM4;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota = PWM_Get_LED_5();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PWM5;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_HodnotyPrumer[ADC_VALUE_LED1];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_CurrRAW1;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota = ADC_HodnotyPrumer[ADC_VALUE_LED2];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_CurrRAW2;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota = ADC_HodnotyPrumer[ADC_VALUE_LED3];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_CurrRAW3;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_HodnotyPrumer[ADC_VALUE_LED4];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_CurrRAW4;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_HodnotyPrumer[ADC_VALUE_LED5];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_CurrRAW5;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_ConvertTomA(ADC_HodnotyPrumer[ADC_VALUE_LED1]);
	
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Curr1;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota = ADC_ConvertTomA(ADC_HodnotyPrumer[ADC_VALUE_LED2]);
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Curr2;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_ConvertTomA(ADC_HodnotyPrumer[ADC_VALUE_LED3]);
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Curr3;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_ConvertTomA(ADC_HodnotyPrumer[ADC_VALUE_LED4]);
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Curr4;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_ConvertTomA(ADC_HodnotyPrumer[ADC_VALUE_LED5]);
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Curr5;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if(Teplota[TEPLOTA_LED_ID]>=0)
	{
		Hodnota = (uint16_t)Teplota[TEPLOTA_LED_ID];
	}
	else
	{
		Hodnota = 10000+(uint16_t)abs(Teplota[TEPLOTA_LED_ID]);
	}
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Temp1;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if(Teplota[TEPLOTA_TRANZ_ID]>=0)
	{
		Hodnota = (uint16_t)Teplota[TEPLOTA_TRANZ_ID];
	}
	else
	{
		Hodnota = 10000+(uint16_t)abs(Teplota[TEPLOTA_TRANZ_ID]);
	}
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Temp2;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if(Teplota[TEPLOTA_EXT_ID]>=0)
	{
		Hodnota = (uint16_t)Teplota[TEPLOTA_EXT_ID];
	}
	else
	{
		Hodnota = 10000+(uint16_t)abs(Teplota[TEPLOTA_EXT_ID]);
	}
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Temp3;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = HumidityRAW;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_HumRAW;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = Humidity;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Hum;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = PWM_Get_FAN();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_FAN;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = ADC_HodnotyPrumer[ADC_VALUE_TEMP];				// 32°C = 1725	
	Hodnota32 = (((uint32_t)100*((uint32_t)3288-(uint32_t)Hodnota))/(uint32_t)53)+(uint32_t)250;
	Hodnota = Hodnota32/10;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_Temp4;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota = TCP_Status;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_TCPState;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if (TCP_Timeout[TCP_TIMEOUTID_HANDSHAKE]!=0xFFFFFFFF)
		Hodnota = 1;
	else
		Hodnota = 0;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_TCPTimeout1;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if (TCP_Timeout[TCP_TIMEOUTID_CONNECTION]!=0xFFFFFFFF)
		Hodnota = 1;
	else
		Hodnota = 0;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_TCPTimeout2;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if (TCP_Timeout[TCP_TIMEOUTID_ACK]!=0xFFFFFFFF)
		Hodnota = 1;
	else
		Hodnota = 0;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_TCPTimeout3;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	if (TCP_Timeout[TCP_TIMEOUTID_FINWAIT2]!=0xFFFFFFFF)
		Hodnota = 1;
	else
		Hodnota = 0;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_TCPTimeout4;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
				
	Hodnota=((Planovac_Timestamp>>17)&0x0F)<<8;
	Hodnota|=((Planovac_Timestamp>>22)&0x0F);
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData1;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=((Planovac_Timestamp>>26)&0x0F)<<8;
	Hodnota|=((Planovac_Timestamp&0x1FFFF)/(60*60));
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData2;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=(((Planovac_Timestamp&0x1FFFF)%(60*60))/(60))<<8;
	Hodnota|=((Planovac_Timestamp&0x1FFFF)%(60*60)%(60));
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData3;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=(uint16_t)Time_Den<<8;
	Hodnota|=(uint16_t)Time_Mesic;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData4;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=(uint16_t)(Time_Rok-2010)<<8;
	Hodnota|=(uint16_t)Time_Hodiny;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData5;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=(uint16_t)Time_Minuty<<8;
	Hodnota|=(uint16_t)Time_Vteriny;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData6;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=(uint16_t)Planovac_Kanal[0]<<8;
	Hodnota|=(uint16_t)Planovac_Kanal[1];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData7;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota=(uint16_t)Planovac_Kanal[2]<<8;
	Hodnota|=(uint16_t)Planovac_Kanal[3];
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;
	
	Hodnota=(uint16_t)(Planovac_Kanal[4]&0x7F)<<8;
	Hodnota|=(uint16_t)Planovac_Kanal[4]>>7;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData9;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=(uint16_t)PlanovacStav<<8;
	Hodnota|=0x00;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UDPKom_Upload_PlanovacData10;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	Hodnota=PWM_Get_LED6();
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = UPPKom_Upload_LED6;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = (Hodnota&0xFF00)>>8;
	TX_buffer[UDPKom_OFFSET_UploadData+i++] = Hodnota&0x00FF;

	// Doplneni delky dat do hlavicky
	DelkaDat = (i)/3;
	TX_buffer[UDPKom_OFFSET_UploadDelka] = DelkaDat;
	
	// Vypocteni a ulozeni delky cele UDP zpravy vcetne hlavicky kom. protokolu
	DelkaDat = (i) + UDPKom_Upload_HeaderLen;	
	UDP_Build_Datagram_From_RX(RX_buffer, TX_buffer, "", DelkaDat, 1);	

	return(0);
}

/* -------------------------------------------
 Funkce pro overeni platnosti UDP datagramu
 - kontrola Identyfikacnich bytu
 ------------------------------------------ */
uint8_t UDPKom_CheckIdent(uint8_t *RX_buffer)
{
	if(RX_buffer[OFFSET_UDP_Kom_Ident+0]==(uint8_t)((UDPKom_SyncWord&0xFF00)>>8)&
	   RX_buffer[OFFSET_UDP_Kom_Ident+1]==(uint8_t)(UDPKom_SyncWord&0x00FF))
	{
		return(1);
	}
	else
	{
		return(0);
	}
}

/* Konece souboru */
