#include "IDIBUS_NEXTION.h"

IdiBus_NEXTION::IdiBus_NEXTION (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress)
{
	BRIDGE.MMES_Error = 0;
	BRIDGE.RXlen = 0;
}

IdiBus_NEXTION::IdiBus_NEXTION () : IdiBusSlave()
{
	BRIDGE.MMES_Error = 0;
	BRIDGE.RXlen = 0;
}

IDIBUS_NEXTION_DATA_TYPE IdiBus_NEXTION::getData(void)
{
	return this->BRIDGE;
}

uint8_t IdiBus_NEXTION::getBuf()
{
	IdiBusMMES MMES(this->Address);
	MMES.DEV.NUM	= IDIBUS_NEXTION_CNTRL_DEV;
	MMES.CHNL.NUM   = 0;
	MMES.ComFunc    = 0x02;	//Get Buf 
	uint8_t RequestError = this->SendRequestMMES(&MMES);
	if      ( RequestError   != IDIER_NOPE )          { this->BRIDGE.MMES_Error = RequestError;   }
	else {
		this->BRIDGE.MMES_Error = MMES.RxData[0];
		this->BRIDGE.RXlen = MMES.RxDataLength-1;
		memcpy(this->BRIDGE.RXbuf,&MMES.RxData[1],this->BRIDGE.RXlen);
	}
	return this->BRIDGE.MMES_Error;
}

uint8_t IdiBus_NEXTION::sendBuf(uint8_t *buf, uint8_t len)
{
	IdiBusMMES MMES(this->Address);
	MMES.DEV.NUM	= IDIBUS_NEXTION_CNTRL_DEV;
	MMES.CHNL.NUM   = 0;
	MMES.ComFunc    = 0x01;	//Send Buf 
	MMES.TxDataLength = len;
	MMES.TxData = buf;
	uint8_t RequestError = this->SendRequestMMES(&MMES);

	if      ( RequestError   != IDIER_NOPE )          { this->BRIDGE.MMES_Error = RequestError;   }
	else if ( MMES.RxDataLength == 1 )                { this->BRIDGE.MMES_Error = MMES.RxData[0]; }
	else {
		this->BRIDGE.MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT;
	}
	return this->BRIDGE.MMES_Error;
}
