#define F_CPU 8'000'000UL

#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>


void uart_start() {
    UCSRA = 0b00000000;
    UCSRB = 0b00011000;
    UCSRC = 0b00000110;
    UBRRL = 51; // baud_rate = 9600
    UBRRH = 0;
}

void uart_stop() {
    UCSRB = 0b00000000;
}

bool uart_read(uint8_t *data) {
    uint8_t status, res;
    while(!(UCSRA & (1 << RXC)));

    status = UCSRA;
    res = UDR;

    if(status & ((1 << FE) | (1 << DOR) | (1 << UPE)))
        return false;

    *data = res;
    return true;
}

void uart_write(uint8_t data) {
    while(!(UCSRA & (1 << UDRE)));
    UDR = data;
}

int main() {
    // Leds
    // DDRB = 0xff;
    // PORTB = 0x00;

    // Buttons
    // DDRD = 0x01;
    // PORTD = 0x01;

    uart_start();

    uint8_t inp;
    while(true) {
        if(!uart_read(&inp))
            while(1);
        uart_write(inp);
    }
}
