/*
 * ILI9341.cpp
 *
 *  Created on: Feb 11, 2024
 *      Author: Gabriel
 */

#include "ILI9341.hpp"
#include <cstring>

ILI9341::ILI9341(SPI_Peripheral* hSpi, GPIO_Pin* hNssPin, GPIO_Pin* hResetPin, GPIO_Pin* hDcrsPin, PWM_Pin* hLedPwm) :
	hSpi(hSpi), hNssPin(hNssPin), hResetPin(hResetPin), hDcrsPin(hDcrsPin), hLedPwm(hLedPwm) {

}

ILI9341::~ILI9341() {

}

// Public methods

int32_t ILI9341::init() {
	int32_t errors = 0;
	// Init pins
	errors += hNssPin->init();
	errors += hResetPin->init();
	errors += hDcrsPin->init();
	errors += hLedPwm->init();
	// Reset LCD
	errors += hResetPin->reset();
	vTaskDelay(1);
	errors += hResetPin->set();
	// Start SPI Command
	uint32_t id;
	errors += readReg24(0x04, &id);
	if(id != 0x858552){
		// Wrong ID
		return -1;
	}
	/*
	uint8_t idn[3];
	errors += readReg8(0xDA, idn);
	errors += readReg8(0xDB, idn+1);
	errors += readReg8(0xDC, idn+2);
	*/
	errors += writeBuffer(0x11, nullptr, 0);	// Sleep out
	vTaskDelay(5);
	/*
	uint32_t status;
	errors += readReg32(0x09, &status);
	*/
	errors += writeBuffer(0x29, nullptr, 0);	// Display on
	setBacklight(0);
	return errors;
}

int32_t ILI9341::setPosition(uint16_t xPosition, uint16_t yPosition, uint16_t xSize, uint16_t ySize) {
	int32_t errors = 0;
	uint8_t casetBuf[4];
	casetBuf[0] = (xPosition>>8);
	casetBuf[1] = xPosition;
	casetBuf[2] = ((xPosition+xSize-1)>>8);
	casetBuf[3] = (xPosition+xSize-1);
	errors += writeBuffer(0x2A, casetBuf, 4);
	uint8_t pasetBuf[4];
	pasetBuf[0] = (yPosition>>8);
	pasetBuf[1] = yPosition;
	pasetBuf[2] = ((yPosition+ySize-1)>>8);
	pasetBuf[3] = (yPosition+ySize-1);
	errors += writeBuffer(0x2B, pasetBuf, 4);
	return errors;
}

int32_t ILI9341::write(uint8_t* buffer, uint32_t length) {
	return writeBuffer(0x2C, buffer, length);
}



int32_t ILI9341::setBacklight(float backlight) {
	return hLedPwm->setDuty(backlight);
}

// Private methods

int32_t ILI9341::readReg8(uint8_t command, uint8_t* reg) {
	int32_t errors = 0;
	errors += hSpi->take(100);
	errors += hDcrsPin->reset();
	errors += hNssPin->reset();
	txBuffer[0] = command;
	errors += hSpi->trx(txBuffer, rxBuffer, 2);
	errors += hNssPin->set();
	errors += hSpi->give();
	if(reg != nullptr){
		*reg = rxBuffer[1];
	}
	return errors;
}

int32_t ILI9341::readReg24(uint8_t command, uint32_t* reg) {
	int32_t errors = 0;
	errors += hSpi->take(100);
	errors += hDcrsPin->reset();
	errors += hNssPin->reset();
	txBuffer[0] = command;
	errors += hSpi->trx(txBuffer, rxBuffer, 5);
	errors += hNssPin->set();
	errors += hSpi->give();
	if(reg != nullptr){
		*reg = (((uint32_t)rxBuffer[1])<<17) + (((uint32_t)rxBuffer[2])<<9) + (((uint32_t)rxBuffer[3])<<1) + (((uint32_t)rxBuffer[4])>>7);
	}
	return errors;
}

int32_t ILI9341::readReg32(uint8_t command, uint32_t* reg) {
	int32_t errors = 0;
	errors += hSpi->take(100);
	errors += hDcrsPin->reset();
	errors += hNssPin->reset();
	txBuffer[0] = command;
	errors += hSpi->trx(txBuffer, rxBuffer, 6);
	errors += hNssPin->set();
	errors += hSpi->give();
	if(reg != nullptr){
		*reg = (((uint32_t)rxBuffer[1])<<25) + (((uint32_t)rxBuffer[2])<<17) + (((uint32_t)rxBuffer[3])<<9) + (((uint32_t)rxBuffer[4])<<1) + (((uint32_t)rxBuffer[5])>>7);
	}
	return errors;
}

int32_t ILI9341::writeBuffer(uint8_t command, uint8_t* buffer, uint32_t length) {
	int32_t errors = 0;
	txBuffer[0] = command;
	errors += hSpi->take(100);
	errors += hDcrsPin->reset();
	errors += hNssPin->reset();
	errors += hSpi->transmit(txBuffer, 1);
	if(length>0){
		errors += hDcrsPin->set();
		errors += hSpi->transmit(buffer, length);
		errors += hDcrsPin->reset();
	}
	errors += hNssPin->set();
	errors += hSpi->give();
	return errors;
}
