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

using namespace bsp;

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

/**** Public function definitions ****/
bsp::CVOut::CVOut(void)
{
	return;
}

bsp::CVOut::~CVOut(void)
{
	return;
}

void bsp::CVOut::init(AnalogIn* ain_ch, Halfbridge* out_ch)
{
	this->ain_ch = ain_ch;
	this->out_ch = out_ch;
	
	this->voltage = 0;
	
	this->disabled = 1;
	this->process_ain = 1;
}

void bsp::CVOut::process(void)
{
	// Update analog input
	if(this->process_ain) this->ain_ch->read();
	
	// Check to HiZ output
	if(this->disabled)
	{
		this->out_ch->write_hiz();
		return;
	};
	
	// Calculate and apply necessary PWM
	this->out_ch->write(util::sat_ratio(this->voltage, this->ain_ch->last_read));
}

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