/*
 * mainloop.cpp
 *
 *  Created on: Sep 11, 2019
 *      Author: abody
 */

#include "../Application/application.h"

#include <f4ll_cpp/ili9341.h>
#include <initializer_list>
#include "main.h"

#include "../Application/globals.h"
#include "../Application/strutil.h"

#define BORDER 60
#define BARWIDTH 2

using namespace f4ll_cpp;

void AppInit()
{
	static Application	m;

	m.Start();
}


uint8_t SpiIo(SPI_TypeDef *spi, uint8_t data)
{
	while(!LL_SPI_IsActiveFlag_TXE(spi));
	LL_SPI_TransmitData8(spi, data);
	while(!LL_SPI_IsActiveFlag_RXNE(spi));
	return LL_SPI_ReceiveData8(spi);
}

uint16_t ReadTouch(SPI_TypeDef *spi, bool x)
{
	static uint8_t const CMD_X_READ=0b10010000;
	static uint8_t const CMD_Y_READ=0b11010000;

	uint16_t tmp;

	LL_GPIO_ResetOutputPin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin);
	SpiIo(spi, x ? CMD_X_READ: CMD_Y_READ);
	tmp = (uint16_t)SpiIo(spi, 0) << 8;
	tmp |= SpiIo(spi, 0);
	LL_GPIO_SetOutputPin(TOUCH_CS_GPIO_Port, TOUCH_CS_Pin);

	return tmp >> 3;
}

Application::Application()
: GlobalsInitializer(&m_console)
, Task(3, &Application::Loop)
, m_console(USART1, DMA2, LL_DMA_STREAM_2, LL_DMA_STREAM_7, this, nullptr)
{
}

//void Application::TaskFn(void *taskObj)
//{
//	static_cast<Application*>(reinterpret_cast<fsl::Task<Application, APPLICATION_STACK_SIZE>*>(taskObj))->Loop();
//}

void Application::Loop()
{
	Ili9341Fsmc &lcd(Ili9341Fsmc::Init(nullptr, nullptr, DMA2, LL_DMA_STREAM_4, false));
	lcd.SetScrollMode(true);

	lcd.FillRect(Ili9341Fsmc::ILI9341_BLACK, false);

	LL_SPI_Enable(SPI2);

	for(int i = 0; i < 100; ++i) {
		m_console.SendLine("Lofasz a seggedbe!\r\n");
	}

	for(;;) {
		if(m_lineReceived && m_transmissionCompleted) {
			m_transmissionCompleted = false;
			const char *line = const_cast<const char*>(m_rcvdBuffer->buffer);
			m_console.SendLine(line, m_rcvdBuffer->len);
			lcd.Print(line);
			lcd.Print("\r\n", 2);
			m_lineReceived = false;
			m_rcvdBuffer->busy = false;
		}

		if(LL_GPIO_IsInputPinSet(PENIRQ_GPIO_Port, PENIRQ_Pin))
			LL_GPIO_SetOutputPin(LED1_GPIO_Port, LED1_Pin);
		else {
			LL_GPIO_ResetOutputPin(LED1_GPIO_Port, LED1_Pin);

			uint16_t x = ReadTouch(SPI2, true);
			uint16_t y = ReadTouch(SPI2, false);
			unsigned len = strcpy_ex(m_appBuffer, "X: ");
			len += uitodec(m_appBuffer+len, x);
			len += strcpy_ex(m_appBuffer + len, ",  Y: ");
			len += uitodec(m_appBuffer + len, y);
			len += strcpy_ex(m_appBuffer + len, "\r\n");
			lcd.Print(m_appBuffer, len);
			m_console.SendLine(m_appBuffer, len);
		}
	}
}

void Application::LineReceived(void *userParam, SerialConsole<257>::Buffer *buffer)
{
	m_lineReceived = true;
	m_rcvdBuffer = buffer;
}

void Application::TransmissionComplete(void *userParam, SerialConsole<257>::Buffer *buffer)
{
	m_transmissionCompleted = true;
}

