/**** Includes ****/
#include "../utils/utils.h"
#include "mcu/mcu_hal.h"
#include "pwm_out.h"

using namespace bsp;

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

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

bsp::PwmOut::~PwmOut(void)
{
	this->last_duty = 0;
}

void bsp::PwmOut::init(uint8_t pwm_ch, uint8_t max_dc)
{
	this->pwm_ch  = pwm_ch;
	
	this->last_duty = 0;
	
	if(max_dc>100) max_dc = 100;
	
	this->max_dc = util::percent_to_16b(max_dc);
}

void bsp::PwmOut::write(uint16_t numerator)
{
	// Update target
	if(numerator > this->max_dc) numerator = this->max_dc;
	this->last_duty = numerator;
	
	// Set PWM
	mcu::pwm_write(this->pwm_ch, numerator);
}

void bsp::PwmOut::write(uint8_t percent)
{
	// Convert to numerator/0xFFFF
	this->write(util::percent_to_16b(percent));
}

uint16_t bsp::PwmOut::get_set_duty(void)
{
	return this->last_duty;
}

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