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

using namespace util;

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

util::VCounter::~VCounter(void)
{
	return;
}

void util::VCounter::init(uint16_t top, uint16_t step_us)
{
	this->counter = 0;
	this->top = top;
	this->step_us = step_us;
	this->disabled = 1;
}

void util::VCounter::reset(void)
{
	this->counter = 0;
}

void util::VCounter::increment(void)
{
	if(this->disabled) return;
	this->counter++;
	if(this->counter > this->top) this->counter = 0;
}

uint16_t util::VCounter::read(void)
{
	return this->counter;
}

uint32_t util::VCounter::read_ms(void)
{
	return this->convert_ms(this->counter);
}

uint16_t util::VCounter::read_top(void)
{
	return this->top;
}

uint32_t util::VCounter::convert_ms(uint16_t raw)
{
	if(this->step_us==0) return 0;
	
	uint32_t out = (uint32_t)raw * (uint32_t)this->step_us;
	return out/1000;
}


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

