#include "BL24C02.h"
#include "i2c.h"

SysSettings_T sys_settings = {
	.backlight_level = 80,
	.key_sound_enable = 1,
	.language_select = 0,
	.rotation = 0
};

static void BL24C02_Write(uint8_t addr, uint16_t length, uint8_t buff[])
{
	HAL_I2C_Mem_Write(&hi2c1, BL24C02_ADDRESS<<1, addr, I2C_MEMADD_SIZE_8BIT, buff, length, 10);
}

static void BL24C02_Read(uint8_t addr, uint8_t length, uint8_t buff[])
{
	HAL_I2C_Mem_Read(&hi2c1, BL24C02_ADDRESS<<1, addr, I2C_MEMADD_SIZE_8BIT, buff, length, 10);
}

static void delay_ms(uint32_t ms)
{
	HAL_Delay(ms);
}

/******************************************
EEPROM Data description:
[0x00]:0x55 for check
[0x01]:0xAA for check

[0x10]:user wrist setting, HWInterface.IMU.wrist_is_enabled
[0x11]:user ui_APPSy_EN setting

*******************************************/

//to check the Data is right and the EEPROM is working
uint8_t EEPROM_Check(void)
{
	uint8_t check_buff[2];
	delay_ms(10);
	BL24C02_Read(0,2,check_buff);
	if(check_buff[0] == 0x55 && check_buff[1] == 0xAA)
	{
		return 0;//check OK
	}
	else
	{
		check_buff[0] = 0x55;
		check_buff[1] = 0xAA;
		delay_ms(10);
		BL24C02_Write(0,2,check_buff);
		memset(check_buff,0,2);
		delay_ms(10);
		BL24C02_Read(0,2,check_buff);
		if(check_buff[0] == 0x55 && check_buff[1] == 0xAA)
			return 0;//check ok
	}
	return 1;//check erro
}


//to Save the settings
uint8_t SettingSave(uint8_t *buf, uint8_t addr, uint8_t lenth)
{
	if(addr > 1 && !EEPROM_Check())
	{
		delay_ms(10);
		BL24C02_Write(addr,lenth,buf);
		return 0;
	}
	return 1;
}


//to Get the settings
uint8_t SettingGet(uint8_t *buf, uint8_t addr, uint8_t lenth)
{
	if(addr > 1 && !EEPROM_Check())
	{
		delay_ms(10);
		BL24C02_Read(addr,lenth,buf);
		return 0;
	}
	return 1;
}
