/*
 * deff_packet_lib.c
 *
 * Created: 1.3.2012 16:58:04
 *  Author: JIRKO
 */ 
#include <avr/io.h>
#include <avr/eeprom.h>			// pracas s eeprom
#include "deff_packet_lib.h"					// kniznica pre pracu s packetom
#include "deff_eeprom_lib.h"					// kniznica kede su konstanty pre pracu s eeprom

#include <avr/interrupt.h>		// Prerusenia

//****************   Definicia globalnych premennych   ***********************************************
uint8_t my_ID_RAM;
uint8_t master_ID_RAM;

	
uint8_t	my_ID(void)									// vracia hodnotu my_ID vicitanu z eeprom
{
	return my_ID_RAM;		// vycitam z eeprom moje ID
}

void	save_my_ID(uint8_t ID)						// uklada hodnotu my_ID do eeprom
{
	eeprom_update_byte((uint8_t*)EEADR_MY_ID, ID);			// ukladam ID do eeprom 
}

uint8_t	master_ID(void)								// vracia hodnotu master_ID vycitanu z eeprom
{
	return master_ID_RAM;		// vycitam z eeprom master ID
}

void	save_master_ID(uint8_t ID)							 // uklabam hodnotu mastr_ID do eeprom
{
	eeprom_update_byte((uint8_t*)EEADR_MASTER_ID, ID);			// ukladam ID do eeprom
}

void	init_packet_lib(void)		// inicializacia kniznice packet_lib
{
	my_ID_RAM= ((uint8_t)eeprom_read_byte((uint8_t*)EEADR_MY_ID));	// vycitam z eeprom moje ID
	master_ID_RAM= ((uint8_t)eeprom_read_byte((uint8_t*)EEADR_MASTER_ID));		// vycitam z eeprom master ID	
}

uint8_t	gen_packet_n(void)
{	
	static uint8_t		my_packet_number= 10;		// paket number generovane pre toto zariadenie
	my_packet_number++;
	if ( my_packet_number < 10 ) my_packet_number= 10;
	return  my_packet_number;
}


void fill_out_packet(t_packet *out_packet, uint8_t dest_ID, uint8_t sorce_ID, uint8_t lenght, uint8_t packet_number, uint8_t comand, uint8_t sign)
{	
	out_packet->start_of_packet= START_OF_PACKET;
	out_packet->dest_ID= dest_ID;
	out_packet->sorce_ID= sorce_ID;
	out_packet->lenght= lenght;
	out_packet->packet_number= packet_number;
	out_packet->comand= comand;
	out_packet->sign= sign;	
}