/**** Includes ****/
#include "cfg_mem.h"
#include "../bsp/mcu/mcu_hal.h"

using namespace logic;

/**** Private definitions ****/
/**** Private constants ****/
static const uint16_t addr_btn_force       = 0x0000;
static const uint16_t addr_bmode           = 0x0001;
static const uint16_t addr_pot_mode        = 0x0002;
static const uint16_t addr_dsp_brigth      = 0x0003;
static const uint16_t addr_dsp_dimm        = 0x0004;
static const uint16_t addr_brake_force     = 0x0005;
static const uint16_t addr_max_hbrake_time = 0x0006;
static const uint16_t addr_lock_current    = 0x0008;
static const uint16_t addr_max_out_voltage = 0x000A;
static const uint16_t addr_min_out_voltage = 0x000C;

static const uint8_t def_btn_force = 0;
static const uint8_t def_pot_mode = 0;
static const uint8_t def_bmode = 0;
static const uint8_t def_dimm = 50;
static const uint8_t def_brigth = 100;
static const uint8_t def_brake_force = 100;
static const uint16_t def_max_hbrake_time = 1000;
static const uint16_t def_lock_current = 4500;
static const uint16_t def_max_out_voltage = 7000;
static const uint16_t def_min_out_voltage = 200;

static const uint16_t lim_saved_lock_current = 6000;
static const uint16_t lim_saved_max_voltage = 8000;
static const uint16_t lim_saved_min_voltage = 1000;

/**** Private variables ****/
/**** Private function declarations ****/
/**** Public function definitions ****/
logic::CfgMemory::CfgMemory(void)
{	
	this->mem_btn_force    = 0;
	this->mem_bmode        = 0;
	this->mem_pot_mode     = 0;
	this->mem_dsp_brigth   = 0;
	this->mem_dsp_dimm     = 0;
	this->mem_brake_force     = 0;
	this->mem_max_hbrake_time = 0;
	this->mem_lock_current    = 0;
	this->mem_max_out_voltage = 0;
	this->mem_min_out_voltage = 0;
	
	this->restore();
}

logic::CfgMemory::~CfgMemory(void)
{
	return;
}

void logic::CfgMemory::init(void)
{
	this->mem_btn_force       = mcu::eeprom_read8b(addr_btn_force);
	this->mem_bmode           = mcu::eeprom_read8b(addr_bmode);
	this->mem_pot_mode        = mcu::eeprom_read8b(addr_pot_mode);
	this->mem_dsp_brigth      = mcu::eeprom_read8b(addr_dsp_brigth);
	this->mem_dsp_dimm        = mcu::eeprom_read8b(addr_dsp_dimm);
	this->mem_brake_force     = mcu::eeprom_read8b(addr_brake_force);
	this->mem_max_hbrake_time = mcu::eeprom_read16b(addr_max_hbrake_time);
	this->mem_lock_current    = mcu::eeprom_read16b(addr_lock_current);
	this->mem_max_out_voltage = mcu::eeprom_read16b(addr_max_out_voltage);
	this->mem_min_out_voltage = mcu::eeprom_read16b(addr_min_out_voltage);
	
	// Validate EEPROM data
	if(this->mem_btn_force > 100)
	{
		this->mem_btn_force = def_btn_force;
		mcu::eeprom_write8b(addr_btn_force, this->mem_btn_force);
	};
	
	if(this->mem_bmode > 2)
	{
		this->mem_bmode = def_bmode;
		mcu::eeprom_write8b(addr_bmode, this->mem_bmode);
	};
	
	if(this->mem_pot_mode > 1)
	{
		this->mem_pot_mode = def_pot_mode;
		mcu::eeprom_write8b(addr_pot_mode, this->mem_pot_mode);
	};
	
	if(this->mem_dsp_brigth > 100)
	{
		this->mem_dsp_brigth = def_brigth;
		mcu::eeprom_write8b(addr_dsp_brigth, this->mem_dsp_brigth);
	};
	
	if(this->mem_dsp_dimm > 100)
	{
		this->mem_dsp_dimm = def_dimm;
		mcu::eeprom_write8b(addr_dsp_dimm, this->mem_dsp_dimm);
	};
	
	if(this->mem_brake_force > 100)
	{
		this->mem_brake_force = def_brake_force;
		mcu::eeprom_write8b(addr_brake_force, this->mem_brake_force);
	};
	
	/*
	No wrong value
	if(this->mem_max_hbrake_time > 1000)
	{
		this->mem_max_hbrake_time = def_max_hbrake_time;
		mcu::eeprom_write16b(addr_lock_current, this->mem_max_hbrake_time);
	};
	*/
	
	if(this->mem_lock_current > lim_saved_lock_current)
	{
		this->mem_lock_current = def_lock_current;
		mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current);
	};
	
	if(this->mem_max_out_voltage > lim_saved_max_voltage)
	{
		this->mem_max_out_voltage = def_max_out_voltage;
		mcu::eeprom_write16b(addr_max_out_voltage, this->mem_max_out_voltage);
	};
	
	if(this->mem_min_out_voltage > lim_saved_min_voltage)
	{
		this->mem_min_out_voltage = def_min_out_voltage;
		mcu::eeprom_write16b(addr_min_out_voltage, this->mem_min_out_voltage);
	};
	
	this->restore();
}

void logic::CfgMemory::save(void)
{
	if(this->btn_force != this->mem_btn_force)
	{
		this->mem_btn_force = this->btn_force;
		mcu::eeprom_write8b(addr_btn_force, this->mem_btn_force);
	};	
	
	if(this->bmode != this->mem_bmode)
	{
		this->mem_bmode = this->bmode;
		mcu::eeprom_write8b(addr_bmode, this->mem_bmode);
	};
}

void logic::CfgMemory::save_all(void)
{
	this->save();
	
	if(this->pot_mode != this->mem_pot_mode)
	{
		this->mem_pot_mode = this->pot_mode;
		mcu::eeprom_write8b(addr_pot_mode, this->mem_pot_mode);
	};
	
	if(this->dsp_brigth != this->mem_dsp_brigth)
	{
		this->mem_dsp_brigth = this->dsp_brigth;
		mcu::eeprom_write8b(addr_dsp_brigth, this->mem_dsp_brigth);
	};
	
	if(this->dsp_dimm != this->mem_dsp_dimm)
	{
		this->mem_dsp_dimm = this->dsp_dimm;
		mcu::eeprom_write8b(addr_dsp_dimm, this->mem_dsp_dimm);
	};
	
	if(this->brake_force != this->mem_brake_force)
	{
		this->mem_brake_force = this->brake_force;
		mcu::eeprom_write8b(addr_brake_force, this->mem_brake_force);
	};
	
	if(this->max_hbrake_time != this->mem_max_hbrake_time)
	{
		this->mem_max_hbrake_time = this->max_hbrake_time;
		mcu::eeprom_write16b(addr_max_hbrake_time, this->mem_max_hbrake_time);
	};
	
	if(this->lock_current != this->mem_lock_current)
	{
		this->mem_lock_current = this->lock_current;
		mcu::eeprom_write16b(addr_lock_current, this->mem_lock_current);
	};
	
	if(this->max_out_voltage != this->mem_max_out_voltage)
	{
		this->mem_max_out_voltage = this->max_out_voltage;
		mcu::eeprom_write16b(addr_max_out_voltage, this->mem_max_out_voltage);
	};
	
	if(this->min_out_voltage != this->mem_min_out_voltage)
	{
		this->mem_min_out_voltage = this->min_out_voltage;
		mcu::eeprom_write16b(addr_min_out_voltage, this->mem_min_out_voltage);
	};
}

void logic::CfgMemory::restore(void)
{
	this->btn_force    = this->mem_btn_force;
	this->bmode        = this->mem_bmode;
	this->pot_mode     = this->mem_pot_mode;
	this->dsp_brigth   = this->mem_dsp_brigth;
	this->dsp_dimm     = this->mem_dsp_dimm;
	this->brake_force     = this->mem_brake_force;
	this->max_hbrake_time = this->mem_max_hbrake_time;
	this->lock_current    = this->mem_lock_current;
	this->max_out_voltage = this->mem_max_out_voltage;
	this->min_out_voltage = this->mem_min_out_voltage;
}

/**** Private function definitions ****/
