#ifndef COM_RS485_H_
#define COM_RS485_H_

//	chyba je vsetko vedsie ako 0

// definicie pre frontu v tomto tasku
// dlzka fronty pre com_RS485 task
	#define 		lenght_Queue_in_com_RS485	5		// dlzka fronty

// definicia strukrury pre frontu 
	typedef	struct{
		unsigned char	Mesage_ID;
		char	Data[MAX_PACKET_LENGHT];
	}t_com_queue_struct;
	
// typy spravy vo fronte
	#define C_QUEUE_RX_MES		1		// fronta s datami od RX
	#define	C_QUEUE_TX_SEND		2
	#define	C_QUEUE_TX_SEND_RQS	3		// rozsah 10 az n co je velkost RX RQS bufera
	
// *******************************************************

// **************** definicie pre TX Request bufeer *********************

// TX buffer
#define	TX_BUFF_SIZE			4		// velkost TX request bufera 
#define	TIME_RQS_REPEAT			100		// cas v ms za ktori sa poksi znova odoslat request ak neprisla odpoved
#define TX_MAX_NUM_RQS_REPEAT	10		// maximalny pocet opakovani requestu

// struktura jednej bunky TX buffera
typedef	struct
{
	t_com_queue_struct	mesage;		// ulozeny packet
				bool	is_use;							// urcuje ci je dana bunka pouzita
//	unsigned	char	lenght;							// dlzka packetu 
	unsigned	char	packet_number;					//
	xTimerHandle		xTimer;							// pouzity timer
	unsigned	char	send_count;						// pocet pokusov o odoslatie
}t_TX_buff;
// *******************************************************

// definicia riadiaceho vystupu RX/TX pre RS485
	#define	RS485_TX		ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_PIN_2); RS485_mode_TX= true	// mod vysielania
	#define	RS485_RX		ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0); RS485_mode_TX= false			// mod primania


// Funkcie
//*****************************************************************************
unsigned long	ComRS485Init( void );												// inicializacia pre tuto kniznicu 
unsigned long	SendPacket( const char * packet, unsigned char lenght );			// odosiela packet
unsigned long	SendRqsPacket( const t_com_queue_struct * mesage );					// ulozi spravu s packetom do REQ buffera a nuluje timer a odosiela ho do ffronty ako C_QUEUE_TX_SEND
unsigned char	CalcCrc( const char * packet, unsigned char lenght );				// vypocet CRC pre dlzku packetu to vrati ctc spocitane okrem bunky crc
unsigned char	GenPacketNumber(void);												// generuje packet number
unsigned long	sendToComRS485Queue( const t_com_queue_struct *mesage, unsigned long xTicksToWait );		// odosle spravu do fronty pre ComRS485 task

#endif /*COM_RS485_H_*/
