#include "IDIBUS_4ADC.h"

IdiBus_4ADC::IdiBus_4ADC (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress)
{
	for (uint8_t I=0; I<IDIBUS_4ADC_CH_COUNT; I++)
	{
		this->CH[I].MMES_Error = 0;
		this->CH[I].Voltage = 0;
		//this->CH[I].STATUS.Enable = 0;
	}
}

IdiBus_4ADC::IdiBus_4ADC () : IdiBusSlave()
{
	for (uint8_t I=0; I<IDIBUS_4ADC_CH_COUNT; I++)
	{
		this->CH[I].MMES_Error = 0;
		this->CH[I].Voltage = 0;
		//this->CH[I].STATUS.Enable = 0;
	}
}

IDIBUS_4ADC_DATA_TYPE IdiBus_4ADC::getChData(uint8_t ChNum){
	if ( ChNum < IDIBUS_4ADC_CH_COUNT ) { return this->CH[ChNum]; }
	else								{ return this->CH[IDIBUS_4ADC_CH4_NUM]; }
}

uint8_t IdiBus_4ADC::getChVoltage(uint8_t ChNum)
{
	if (ChNum >= IDIBUS_4ADC_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; }
	IdiBusMMES MMES(this->Address);
	MMES.DEV.NUM	= IDIBUS_4ADC_CNTRL_DEV;
	MMES.CHNL.NUM   = ChNum;
	MMES.ComFunc    = 0x02;	//Get Value
	uint8_t ChDataSize = 2;		//uint16_t
	uint8_t RequestError = this->SendRequestMMES(&MMES);
	if      ( RequestError   != IDIER_NOPE )          { this->CH[ChNum].MMES_Error = RequestError;   }
	else if ( MMES.RxDataLength == 1 )                { this->CH[ChNum].MMES_Error = MMES.RxData[0]; }
	else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; }
	else {
		this->CH[ChNum].MMES_Error = MMES.RxData[0];
		if ( this->CH[ChNum].MMES_Error == IDIER_NOPE )
		{
			this->CH[ChNum].Voltage = (uint16_t) (	 ((uint16_t)MMES.RxData[1]<<8) |
												     ((uint16_t)MMES.RxData[2]));
		}
	}
	return this->CH[ChNum].MMES_Error;

}

void IdiBus_4ADC::getAllAdcVoltage(void)
{
	IdiBusMMES MMES(this->Address);
	MMES.DEV.NUM       = IDIBUS_4ADC_CNTRL_DEV;
	MMES.DEV.ALLCH     = 1;
	MMES.ComFunc       = 0x02;
	uint8_t ChDataSize = 2;		//uint16_t
	uint8_t RequestError = this->SendRequestMMES(&MMES);
	if      ( RequestError != IDIER_NOPE )            { this->setAllChErr(RequestError); return; }
	else if (MMES.RxDataLength < IDIBUS_4ADC_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; }
	else {
		uint8_t ExpDataLength = IDIBUS_4ADC_CH_COUNT; // Errors
		for (uint8_t I=0; I<IDIBUS_4ADC_CH_COUNT; I++) {  if ( MMES.RxData[I] == IDIER_NOPE ) { ExpDataLength = (uint16_t)(ExpDataLength + ChDataSize); }  }
		if ( ExpDataLength != MMES.RxDataLength )     {  this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return;  }
	}
	uint8_t ErrorPos = 0;
	uint8_t DataPos  = ErrorPos + IDIBUS_4ADC_CH_COUNT;
	for (uint8_t I = 0; I < IDIBUS_4ADC_CH_COUNT; I++)
	{
		this->CH[I].MMES_Error = MMES.RxData[ErrorPos];
		ErrorPos = (uint8_t)(ErrorPos + 1);
		if ( this->CH[I].MMES_Error == IDIER_NOPE )
		{
			this->CH[I].Voltage = (uint16_t) (
			(uint16_t)((uint16_t)MMES.RxData[DataPos] << 8)  |
			(uint16_t)((uint16_t)MMES.RxData[DataPos+1])      );
			DataPos  = (uint8_t)(DataPos  + ChDataSize);
		}
	}
}

void IdiBus_4ADC::setAllChErr(uint8_t ErrCode)
{
	for (uint8_t I=0; I<IDIBUS_4ADC_CH_COUNT; I++) { this->CH[I].MMES_Error = ErrCode; }
}

void IdiBus_4ADC::initAllChs(void)
{
	//uint8_t TxData[2];
	//TxData[0] = (uint8_t)(CurrentLimit >> 8);
	//TxData[1] = (uint8_t)(CurrentLimit);
	IdiBusMMES MMES(this->Address);
	MMES.DEV.NUM       = IDIBUS_4ADC_CNTRL_DEV;
	MMES.DEV.ALLCH	   = 1;
	MMES.ComFunc       = 0x01;
	uint8_t RequestError = this->SendRequestMMES(&MMES);
	if      ( RequestError != IDIER_NOPE )             { this->setAllChErr(RequestError); }
	else if (MMES.RxDataLength != IDIBUS_4ADC_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); }
	else
	{
		for (uint8_t I = 0; I < IDIBUS_4ADC_CH_COUNT; I++)
		{
			this->CH[I].MMES_Error = MMES.RxData[I];
			//if ( this->CH[I].MMES_Error == IDIER_NOPE )
		}
	}
}

