#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <inttypes.h>

/**** Initialize the USART ****/
void USART_Init(){ // constants for different baud rates and behaviours
	UCSR0B = (1<<TXEN0)|(1<<RXEN0);
	UCSR0C = 0x86;
	UBRR0H = 0x00;
	UBRR0L = 0x01;
}


unsigned char read_byte() {
	unsigned char c;
	while ((UCSR0A & (1<<RXC0) ) == 0);
	c = UDR0;
	UCSR0A |= (1<<RXC0);
	return (c);
}

void send_byte(unsigned char c) {
	UDR0 = c;
	while ((UCSR0A & (1<<TXC0) ) == 0);
	UCSR0A |= (1<<TXC0);
}

void getNextCmd(unsigned char *command){
	unsigned char head, middle, end;
	unsigned int ignore = 1;

	while(ignore == 1){

		head = read_byte();

		if(head >= 0xC0 && head <= 0xCF){
			// ignore = 1;
		} else if(head >= 0xB0 && head <= 0xBF){
			middle = read_byte();
			if(middle == 0x00 || middle == 0x20){
				// ignore = 1;
			} else {
				end = read_byte();
				ignore = 0;
			}
		} else if(head >= 0x80) {
			middle = read_byte();
			end = read_byte();
			ignore = 0;
		}
	}

	command[0] = head;
	command[1] = middle;
	command[2] = end;

}

void sendCmd(unsigned char * nextCmd){
	unsigned int byteCtr = 0;

	while(byteCtr < 3){
		send_byte(nextCmd[byteCtr++]);
	}
}

int main() {
	DDRB=0xFF;
	
	delay(1000);
	
	
	
	return 0;
}
