/**** Includes ****/
#include "../utils/utils.h"
#include "../utils/interpolate.h"
#include "potentiometer.h"

using namespace hw;

/**** Private definitions ****/
/**** Private constants ****/
/**** Private variables ****/
/**** Private function declarations ****/

/**** Public function definitions ****/
hw::Potentiometer::Potentiometer(void)
{
	return;
}

hw::Potentiometer::~Potentiometer(void)
{
	return;
}

void hw::Potentiometer::init(bsp::AnalogIn* ain_ch, uint16_t low_deadzone, uint16_t high_deadzone)
{
	this->ain_ch = ain_ch;
	this->low_deadzone = low_deadzone;
	this->high_deadzone = high_deadzone;
	this->last_percent = 0;
	this->update_ain = 1;
}

uint8_t hw::Potentiometer::read(void)
{
	// Update analog input
	if(this->update_ain) this->ain_ch->read();
	
	// Calculate percent
	if(this->ain_ch->last_read <= this->low_deadzone) this->last_percent = 0;
	else if(this->ain_ch->last_read >= this->high_deadzone ) this->last_percent = 100;
	else this->last_percent = util::interpolate(this->ain_ch->last_read, this->low_deadzone, this->high_deadzone, 0, 100);
	
	return this->last_percent;
}

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