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

using namespace dccd;

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

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

void dccd::UserForce::init(dccd::DccdHw* dccd_hw)
{	
	this->hardware = dccd_hw;
	this->is_new_btn_force = 0;
	this->btn_repeat_time = 0;
	this->force = 0;
	this->pot_mode = 0;
	this->btn_force = 0;
	this->btn_step = 20;
}

void dccd::UserForce::cfg_debounce(uint16_t dbnc_time)
{
	this->hardware->btn_up.dbnc_lim = dbnc_time;
	this->hardware->btn_down.dbnc_lim = dbnc_time;
}

void dccd::UserForce::cfg_pot(uint16_t dead_bot, uint16_t dead_top)
{
	if(this->pot_mode==0) return;
	this->hardware->pot.high_deadzone = dead_top;
	this->hardware->pot.low_deadzone = dead_bot;
}

void dccd::UserForce::write_force(uint8_t force)
{
	if(force > 100) force = 100;
	this->btn_force = force;
	this->force = force;
}

uint8_t dccd::UserForce::process(void)
{
	if(this->pot_mode)
	{
		this->force = this->hardware->pot.last_percent;
		return this->force;
	};
	
	if((this->hardware->btn_up.state==1)&&((this->hardware->btn_up.is_new)||((this->hardware->btn_up.time_read() >= this->btn_repeat_time)&&(this->btn_repeat_time!=0))))
	{
		this->hardware->btn_up.time_reset();
		this->hardware->btn_up.is_new = 0;
		// Increase user force
		this->btn_force += this->btn_step;
		if(this->btn_force > 100) this->btn_force = 100;
		is_new_btn_force = 1;
	};
	
	if((this->hardware->btn_down.state==1)&&((this->hardware->btn_down.is_new)||((this->hardware->btn_down.time_read() >= this->btn_repeat_time)&&(this->btn_repeat_time!=0))))
	{
		this->hardware->btn_down.time_reset();
		this->hardware->btn_down.is_new = 0;
		// Decrease user force
		this->btn_force -= this->btn_step;
		if(this->btn_force > 100) this->btn_force = 0;
		is_new_btn_force = 1;
	};

	this->force = this->btn_force;
	return this->force;
}

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