/**** Includes ****/
#include "../utils/utils.h"
#include "dccd_hw.h"

using namespace dccd;

/**** Private definitions ****/
/**** Private constants ****/
static const uint8_t def_dbnc_time = 10;

static const uint16_t def_pot_dead_bot = 500;
static const uint16_t def_pot_dead_top = 4500;

static const uint8_t def_cc_mode_en = 1;

static const uint16_t def_cnter_us = 900;

static const uint16_t def_out_voltage_under_treshold = 0;
static const uint16_t def_out_voltage_over_treshold = 9000;
static const uint16_t def_out_voltage_hold_time = 1000;
static const uint16_t def_out_voltage_cooldown_time = 0;

static const uint16_t def_out_current_under_treshold = 0;
static const uint16_t def_out_current_over_treshold = 6000;
static const uint16_t def_out_current_hold_time = 200;
static const uint16_t def_out_current_cooldown_time = 1000;

static const uint16_t def_battery_voltage_under_treshold = 9000;
static const uint16_t def_battery_voltage_over_treshold = 18000;
static const uint16_t def_battery_voltage_hold_time = 1000;
static const uint16_t def_battery_voltage_cooldown_time = 0;

static const uint16_t def_battery_current_under_treshold = 0;
static const uint16_t def_battery_current_over_treshold = 8000;
static const uint16_t def_battery_current_hold_time = 200;
static const uint16_t def_battery_current_cooldown_time = 1000;

static const uint16_t def_inital_bat_voltage = 12000;

/**** Private variables ****/
/**** Private function declarations ****/
/**** Public function definitions ****/
dccd::DccdHw::DccdHw(void)
{
	return;
}

dccd::DccdHw::~DccdHw(void)
{
	return;
}

void dccd::DccdHw::init(dccdHwCfg_t* cfg)
{	
	// Apply config
	bsp::Board::boardCfg_t board_cfg;
	board_cfg.pwm_f_khz = cfg->pwm_f_khz;
	board_cfg.od_common_is_pwm = 1;
	this->board_hw.init(&board_cfg);
	
	this->counter.init(0xFFFF, cfg->counter_step_us);
	this->counter.disabled = 0;
	
	this->out_voltage.init(&(this->board_hw.out_voltage), &(this->counter));
	this->out_voltage.under_treshold = def_out_voltage_under_treshold;
	this->out_voltage.over_treshold = def_out_voltage_over_treshold;
	this->out_voltage.hold_time = def_out_voltage_hold_time;
	this->out_voltage.cooldown_time = def_out_voltage_cooldown_time;
	this->out_voltage.update_ain = 0;
	this->out_voltage.auto_reset = 1;
	
	this->out_current.init(&(this->board_hw.out_current), &(this->counter));
	this->out_current.under_treshold = def_out_current_under_treshold;
	this->out_current.over_treshold = def_out_current_over_treshold;
	this->out_current.hold_time = def_out_current_hold_time;
	this->out_current.cooldown_time = def_out_current_cooldown_time;
	this->out_current.update_ain = 0;
	this->out_current.auto_reset = 1;
	
	this->battery_voltage.init(&(this->board_hw.battery_voltage), &(this->counter));
	this->battery_voltage.under_treshold = def_battery_voltage_under_treshold;
	this->battery_voltage.over_treshold = def_battery_voltage_over_treshold;
	this->battery_voltage.hold_time = def_battery_voltage_hold_time;
	this->battery_voltage.cooldown_time = def_battery_voltage_cooldown_time;
	this->battery_voltage.update_ain = 0;
	this->battery_voltage.auto_reset = 1;
	this->battery_voltage.last_read = def_inital_bat_voltage;
	
	this->battery_current.init(&(this->board_hw.battery_current), &(this->counter));
	this->battery_current.under_treshold = def_battery_current_under_treshold;
	this->battery_current.over_treshold = def_battery_current_over_treshold;
	this->battery_current.hold_time = def_battery_current_hold_time;
	this->battery_current.cooldown_time = def_battery_current_cooldown_time;
	this->battery_current.update_ain = 0;
	this->battery_current.auto_reset = 1;
	
	this->btn_up.init(&(this->board_hw.din4), 0, &(this->counter), def_dbnc_time);
	this->btn_up.update_din = 0;
	
	this->btn_down.init(&(this->board_hw.din3), 0, &(this->counter), def_dbnc_time);
	this->btn_down.update_din = 0;
	
	this->btn_mode.init(&(this->board_hw.din1), 0, &(this->counter), def_dbnc_time);
	this->btn_mode.update_din = 0;
	
	this->handbrake.init(&(this->board_hw.hvdin3), 0, &(this->counter), def_dbnc_time);
	this->handbrake.update_din = 0;
	
	this->brakes.init(&(this->board_hw.hvdin2), 1, &(this->counter), def_dbnc_time);
	this->brakes.update_din = 0;
	
	this->dimm.init(&(this->board_hw.hvdin1), 1, &(this->counter), def_dbnc_time);
	this->dimm.update_din = 0;
	
	this->pot.init(&(this->board_hw.ain2), def_pot_dead_bot, def_pot_dead_top);
	this->pot.update_ain = 0;
	
	hw::OutReg::outRegCfg_t outreg_cfg;
	outreg_cfg.pwm_high = &this->board_hw.out_pwm;
	outreg_cfg.dout_low = &this->board_hw.out_low;
	outreg_cfg.ubat = &this->board_hw.battery_voltage;
	outreg_cfg.uout = &this->board_hw.out_voltage;
	outreg_cfg.iout = &this->board_hw.out_current;
	this->outreg.init(&outreg_cfg);
	this->outreg.cc_mode_en = def_cc_mode_en;
	this->outreg.update_ain = 0;
	
	hw::LedDisplay::doutCfg_t dsp_cfg;
	dsp_cfg.led0_dout_ch = &(this->board_hw.od1);
	dsp_cfg.led1_dout_ch = &(this->board_hw.od2);
	dsp_cfg.led2_dout_ch = &(this->board_hw.od3);
	dsp_cfg.led3_dout_ch = &(this->board_hw.od4);
	dsp_cfg.led4_dout_ch = &(this->board_hw.od5);
	dsp_cfg.led5_dout_ch = &(this->board_hw.od6);
	
	this->display.init(&dsp_cfg, 0, &(this->counter), &(this->board_hw.od_pwm));
	
	// Apply configuration
	if(cfg->handbrake_pull_up)
	{
		this->board_hw.hvdin3_pull.write(1);
	}
	else this->board_hw.hvdin3_pull.write(0);
	
	if(cfg->speed_hall)
	{
		this->board_hw.freq_pull.write(1);
	}
	else this->board_hw.freq_pull.write(0);
	
	// Set initial output states
	this->outreg.write_voltage(0);
	this->outreg.write_current(0);
	this->outreg.write_on(0);
	this->outreg.write_lock(0);
	this->outreg.process();
	
	this->display.write_backlight(100);
	this->display.write(0x00);
}

void dccd::DccdHw::read(void)
{
	// Update low level inputs
	this->board_hw.read();
	
	this->counter.increment();
	
	this->out_voltage.process();
	this->out_current.process();
	this->battery_voltage.process();
	this->battery_current.process();
	
	this->btn_up.process();
	this->btn_down.process();
	this->btn_mode.process();
	this->handbrake.process();
	this->brakes.process();
	this->dimm.process();
	
	this->pot.read();
}

void dccd::DccdHw::write(void)
{
	this->display.process();
	this->outreg.process();
}

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