#include "header.h"

// MACROS
// USART
#define USART_BAUDRATE		115200
#define INPUT_SIZE			7

// GLOBAL VARIABLES
volatile char color_input[INPUT_SIZE + 1] = {0};
volatile int input_index = 0;

//
// FUNCTIONS
//
void uart_init() {
	UBRR0H = (unsigned char) (BAUD_PRESCALER(USART_BAUDRATE) >> 8);					// 20.11.5 : UBRRnL and UBRRnH – USART Baud Rate Registers
	UBRR0L = (unsigned char) BAUD_PRESCALER(USART_BAUDRATE);
	UCSR0C |= ASYNCHRONOUS | PARITY_DISABLED | STOP_ONE_BIT | DATA_EIGHT_BIT;		// 20.11.4 : set Frame Format
	UCSR0B |= RECEIVER_ENABLED | TRANSMITTER_ENABLED | INTERRUPT_RECEIVER_ENABLED;	// 20.11.3 : enable Receiver and Transmitter, and interrupt on receiver
	SREG |= ENABLE_GLOBAL_INTERRUPT;												// 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable
}

char uart_rx(void) {
	while (TEST(UCSR0A, RXC0) == 0);												// 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data)
	return UDR0;																	// 20.11.1 : get data in buffer, UDRn – USART I/O Data Register (read and write)
}

void uart_tx(char c) {
	while (TEST(UCSR0A, UDRE0) == 0);												// 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty)
	UDR0 = (unsigned char) c;														// 20.11.1 : Put data into buffer, UDRn – USART I/O Data Register (read and write)
}

// void uart_printstr(const char* str) {
// 	while (*str) {
// 		uart_tx(*str);
// 		str++;
// 	}
// }

ISR(USART_RX_vect) {																// Table 12-7 : we select the code for USART Receive
	char received_char = UDR0;														// read received character
	if (received_char == '\b' || received_char == 127) {							// if backspace is received
		if (input_index <= 0) {
			return;
		}
		if (input_index >= INPUT_SIZE) {
			return;
		}
		remove_last_character();
		input_index--;
		uart_tx('\b');  // Move cursor back
		uart_tx(' ');   // Erase the character on screen
		uart_tx('\b');  // Move cursor back again
	} else if (received_char == '\n' || received_char == '\r') {					// if enter is received
		if (input_index != INPUT_SIZE) {
			return;
		}
		set_color((char *)color_input);
		reset_input();
	} else {																		// any other character
		if (input_index >= INPUT_SIZE) {
			return;
		}
		if (!is_valid_color_input(received_char)) {
			return;
		}
		fill_str(received_char);
		uart_tx(received_char);
		input_index++;
	}
}

int is_valid_color_input(char c) {
	if (input_index == 0) {
		if (c == '#') {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	if (input_index >= INPUT_SIZE) {
		return FALSE;
	}
	if (c >= '0' && c <= '9') {
		return TRUE;
	} else if (c >= 'A' && c <= 'F') {
		return TRUE;
	} else if (c >= 'a' && c <= 'f') {
		return TRUE;
	}
	return FALSE;
}

void fill_str(char input) {
	if (input_index >= INPUT_SIZE) {
		return;
	}
	color_input[input_index] = input;
}

void remove_last_character() {
	color_input[input_index] = 0;
}

int strlength(volatile char *str) {
	int length = 0;
	while (*str) {
		length++;
		str++;
	}
	return length;
}

void reset_input() {
	for(int i = 0; i < INPUT_SIZE; i++) {
		color_input[i] = 0;
	}
	uart_tx('\r');
	uart_tx('\n');
	input_index = 0;
}