/*
 * OnePhaseBase.cpp
 *
 *  Created on: 09.01.2017
 *      Author: Justus_RETHEM
 */

#include "OnePhaseBase.h"

OnePhaseBase::OnePhaseBase() {
	// TODO Auto-generated constructor stub
	//myPwmVal = 327680140;
}

OnePhaseBase::~OnePhaseBase() {
	// TODO Auto-generated destructor stub
}

void OnePhaseBase::Run()
{
	myPwmRegRef[0] = myPwmVal; //2000 << 16 to fit in the PWM Duty cycle reg
}

void OnePhaseBase::SetPwm(unsigned int pwmDyty)
{
	Uint32 pwm;
	pwm = (Uint32)pwmDyty;
	myPwmVal = pwm << 16;
}

signed int OnePhaseBase::GetZwKrsU()
{
	return CalcVoltageFromAdcDigits(GetPosZwKrsRawVal());
}

void OnePhaseBase::PutUref(unsigned int* Uref)
{
	myUref = Uref;
}

unsigned int OnePhaseBase::GetRawUadc()
{
	return (*myUref);
}

unsigned int OnePhaseBase::GetU()
{
	return  __f32toui16r(CalcVoltageFromAdcDigits(*myUref));
}

void OnePhaseBase::PutIref(unsigned int* Iref)
{
	myIref = Iref;
}

unsigned int OnePhaseBase::GetRawIadc()
{
	return *myIref;
}

signed int OnePhaseBase::GetIinPerc()
{
	return __f32toi16r(CalcIpercFromSgndAdcDigits(RemOfstFromCur(*myIref)));
}

void OnePhaseBase::PutDAC_I_Set(unsigned int* dacRef)
{
	myDAC_I_Set_Ref = dacRef;
}

void OnePhaseBase::PutDAC_I_Band(unsigned int* dacRef)
{
	myDAC_I_Band_Ref = dacRef;
}

void OnePhaseBase::PutDAC_I_Set_Val(signed int val)
{
	unsigned int tmp = 0;
	unsigned int absDiffFrmZero;
	if(val > 8191)val =8191;
	if(val < -8192)val = -8192;
	if(val < 0)
	{
		absDiffFrmZero = (unsigned int)(-val);
		tmp = 8192 - absDiffFrmZero;

		/*tmp |= val;
		tmp -= 32768;//eliminate sign
		tmp = tmp^0x7FFF;
		tmp++;
		tmp = 32768 - tmp;*/
	}
	else
	{
		absDiffFrmZero = ((unsigned int)val);
		tmp = absDiffFrmZero;
		tmp += 8192;
	}


	*(myDAC_I_Set_Ref) = tmp;
	//absDiffFrmZero += 8192;
	//if(absDiffFrmZero < 8202) absDiffFrmZero = 8202;
	//absDiffFrmZero = absDiffFrmZero >> 1;
	//*(myDAC_I_Band_Ref) = absDiffFrmZero;
}


void OnePhaseBase::PutDAC_I_Band_Val(unsigned int val)
{
	*(myDAC_I_Band_Ref) = val;
}

void OnePhaseBase::PutPwmRegRef(volatile Uint32* pwmRegRef)
{
	myPwmRegRef = pwmRegRef;
}

volatile Uint32* OnePhaseBase::GetPwmRegRef()
{
	return myPwmRegRef;
}
/*
void OnePhaseBase::PutPwmChnl(EPWM_REGS* pwmR, unsigned int ab)
{
	myPwmChnl.pwmRegs = pwmR;
	myPwmChnl.aORb = ab;
}*/
