/*
 * SysCommons.cpp
 *
 *  Created on: 03.02.2017
 *      Author: Justus_RETHEM
 */

#include "SysCommons.h"

SysCommons::SysCommons() {
	// TODO Auto-generated constructor stub
	posPortPtr = 0;
	posBitMsk = 0;
	negPortPtr = 0;
	negBitMsk = 0;

	posPortPtrFb = 0;
	posBitMskFb = 0;
	negPortPtrFb = 0;
	negBitMskFb = 0;

	pwmOrTolMode = TOL_MODE;
	availableFlag = 0;
}

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

double SysCommons::CalcVoltageFromAdcDigits(unsigned int adcDigits)
{
	double res;
	double doubAdcDigits = ((double)adcDigits);
	res = (((doubAdcDigits*9.0)/4096)-5)*201.0;
	return res;
	//res = (((doubAdcDigits*9.9)/4096)-5)*194.41;
}

double SysCommons::CalcIpercFromSgndAdcDigits(signed int adcDigits)
{
	double doubAdcDigits = ((double)adcDigits);
	doubAdcDigits *= ADC_TO_I_PERC_FAC;
	return doubAdcDigits;
}


int SysCommons::RemOfstFromCur(unsigned int rawCurAdc)
{
	return ((signed int)rawCurAdc)-CURNT_OFST;
}

void SysCommons::PutPosZwKrsRef(unsigned int* Uref)
{
	UposRefZwkrs = Uref;
}

unsigned int SysCommons::GetPosZwKrsRawVal()
{
	return *UposRefZwkrs;
}

void SysCommons::PutNegZwKrsRef(unsigned int* Uref)
{
	UnegRefZwkrs = Uref;
}

unsigned int SysCommons::GetNegZwKrsRawVal()
{
	return *UnegRefZwkrs;
}

void SysCommons::SetPosPortAndPin(unsigned int* portPtr, unsigned int pinMask)
{
	posPortPtr = portPtr;
	posBitMsk = pinMask;
}

void SysCommons::SetNegPortAndPin(unsigned int* portPtr, unsigned int pinMask)
{
	negPortPtr = portPtr;
	negBitMsk = pinMask;
}

void SysCommons::SetPosPinHigh()
{
	if((posPortPtr == 0) || (posBitMsk == 0)) return;
	*posPortPtr |= posBitMsk;
}

void SysCommons::SetPosPinLow()
{
	if((posPortPtr == 0) || (posBitMsk == 0)) return;
	*posPortPtr &= (~posBitMsk);
}

void SysCommons::SetNegPinHigh()
{
	if((negPortPtr == 0) || (negBitMsk == 0)) return;
	*negPortPtr |= negBitMsk;
}

void SysCommons::SetNegPinLow()
{
	if((negPortPtr == 0) || (negBitMsk == 0)) return;
	*negPortPtr &= (~negBitMsk);
}

void SysCommons::SetPosPortAndPinFeedBack(unsigned int* portPtr, unsigned int pinMask)
{
	posPortPtrFb = portPtr;
	posBitMskFb = pinMask;
}

void SysCommons::SetNegPortAndPinFeedBack(unsigned int* portPtr, unsigned int pinMask)
{
	negPortPtrFb = portPtr;
	negBitMskFb = pinMask;
}

unsigned char SysCommons::GetPosPinFeedBack()
{
	if(*posPortPtrFb & posBitMskFb) return 1;
	return 0;
}

unsigned char SysCommons::GetNegPinFeedBack()
{
	if(*negPortPtrFb & negBitMskFb) return 1;
	return 0;
}

unsigned int SysCommons::GetPosPinCmd()
{
	return (*posPortPtr & posBitMskFb);
}

unsigned int SysCommons::GetNegPinCmd()
{
	return (*negPortPtr & negBitMskFb);
}

void SysCommons::SetAvailableFlag()
{
	availableFlag = 1;
}

void SysCommons::ClrAvailableFlag()
{
	availableFlag = 0;
}

unsigned char SysCommons::GetAvailableFlag()
{
	return availableFlag;
}

unsigned int SysCommons::SetToPWMmode()
{
	pwmOrTolMode = PWM_MODE;
}

unsigned int SysCommons::SetToTolMode()
{
	pwmOrTolMode = TOL_MODE;
}

unsigned int SysCommons::GetMode()
{
	return pwmOrTolMode;
}

void SysCommons::PutActualUeffSollDigit(unsigned int uEffDigit)
{
	myActualUeffDigits = uEffDigit;
}

unsigned int SysCommons::GetActualUeffSollDigit()
{
	return myActualUeffDigits;
}

void SysCommons::PutActualIeffSollDigit(signed int iEffDigit)
{
	myActualIeffDigits = iEffDigit;
}

signed int SysCommons::GetActualIeffSollDigit()
{
	return myActualIeffDigits;
}

void SysCommons::PutActualIbandDigit(signed int iEffDigit)
{
	myActualIbandDigits = iEffDigit;
}

signed int SysCommons::GetActualIbandDigit()
{
	return myActualIbandDigits;
}
