#include "eeprom.h"

eeprom_write_buff_info eeprom_write_buffer;

void EPPROM_SLOWWRITE_INIT()
{
	eeprom_write_buffer.buff=NULL;
	eeprom_write_buffer.end=NULL;
	eeprom_write_buffer.head=NULL;
	eeprom_write_buffer.save_timeout=5;
	eeprom_write_buffer.save_busy=0;
}

void EEPROM_SLOWWRITE_SERVER()
{
	eeprom_write_buff *buff;
	char data;
	if(eeprom_write_buffer.save_busy)
		{
			if(HAL_GetTick()>eeprom_write_buffer.save_time)
			{
				eeprom_write_buffer.save_busy=0;
			}
		}else
		{
			if(eeprom_write_buffer.head!=NULL)
			{
				eeprom_write_buffer.save_busy=1;
				eeprom_write_buffer.save_time=HAL_GetTick()+eeprom_write_buffer.save_timeout;
				buff=eeprom_write_buffer.head->next;
				data=eeprom_write_buffer.head->date;
				IIC_SAND_DATE(EEPROM_ADDRESS,eeprom_write_buffer.head->add,&data,1);
				free(eeprom_write_buffer.head);
				eeprom_write_buffer.head=buff;
			}

		}
}

void EEPROM_READ_BATY(uint16_t IN_DEVICE_ADD,char *DATAS,uint16_t LONG)
{
	IIC_READ_DATE(EEPROM_ADDRESS,IN_DEVICE_ADD,DATAS,LONG);
}

void EEPROM_WRITE_BATY(uint16_t IN_DEVICE_ADD,char *DATAS,uint16_t LONG)
{
	//IIC_SAND_DATE(EEPROM_ADDRESS,IN_DEVICE_ADD,DATAS,LONG);
	uint16_t addoffset=0;


	eeprom_write_buff *buff;
	eeprom_write_buff *buff2;
	while(LONG--)
	{
		buff =(eeprom_write_buff*)malloc(sizeof(eeprom_write_buff));
		if(buff!=NULL)
		{
			buff->add=IN_DEVICE_ADD+addoffset;
			buff->date=DATAS[addoffset];
			buff->next=NULL;
		}else{return ;}
		if(eeprom_write_buffer.head==NULL)
		{
			eeprom_write_buffer.head=buff;
			eeprom_write_buffer.end=buff;
		}else
		{
			buff2=eeprom_write_buffer.end;
			buff2->next=buff;
			eeprom_write_buffer.end=buff;
		}
		addoffset++;
	}

}
