/*
 * ll_consolehandler.cpp
 *
 *  Created on: Nov 7, 2019
 *      Author: abody
 */

#include "f4ll/consolehandler.h"
#include <f4ll_c/strutil.h>

namespace f4ll {

ConsoleHandler::ConsoleHandler(USART_TypeDef *usart, DMA_TypeDef *dma, uint32_t streamRx, uint32_t streamTx)
: UsartCore(usart, dma, streamRx, streamTx)
{
}

void ConsoleHandler::ReceiverIdle(void) {}
void ConsoleHandler::TransmissionComplete(void) {}
void ConsoleHandler::FramingError(void) {}
void ConsoleHandler::Overrun(void) {}
void ConsoleHandler::RxDmaTransferComplete(void) {}
void ConsoleHandler::RxDmaHalfTransfer(void) {}
void ConsoleHandler::RxDmaError(DmaHelper::DmaErrorType reason) {}
void ConsoleHandler::TxDmaTransferComplete(void)
{
	LL_USART_EnableIT_TC(m_usart);
	LL_DMA_DisableStream(m_txDma.GetDma(), m_txDma.GetStream());
}
void ConsoleHandler::TxDmaHalfTransfer(void) {}
void ConsoleHandler::TxDmaError(DmaHelper::DmaErrorType reason) {}


#define ADDINFO(b,s,u) \
	b += strcpy_ex(b,s); \
	b += uitodec(b,u);

void ConsoleHandler::PrintStats(uint8_t id, PacketUsart &usart)
{
	char ids[] = " : ";
	char *buffer = m_buffer;
	PacketUsart::Stats const &stats(usart.GetStats());

	ids[0] = id + '0';
	buffer += strcpy_ex(buffer, ids);
	ADDINFO(buffer, " s: ", stats.sent);
	ADDINFO(buffer, " r: ", stats.rcvd);
	ADDINFO(buffer, " sk: ", stats.skiped);
	ADDINFO(buffer, " or: ", stats.overrun);
	ADDINFO(buffer, " he: ", stats.hdrError);
	ADDINFO(buffer, " pe: ", stats.payloadErrror);
	buffer += strcpy_ex(buffer,",0x");
	buffer += uitohex(buffer, stats.pep1, 8);
	buffer += strcpy_ex(buffer,",0x");
	buffer += uitohex(buffer, stats.pep2, 8);
	ADDINFO(buffer, " rde: ", stats.rxDmaError);
	ADDINFO(buffer, " tde: ", stats.txDmaError);
	ADDINFO(buffer, " pmh: ", stats.premature_hdr);
	ADDINFO(buffer, " pmp: ", stats.premature_payload);
	buffer += strcpy_ex(buffer, "\r\n");

	SetupTransmit(m_buffer, buffer - m_buffer + 1);
}


} /* namespace f4ll */

