#define F_CPU 8'000'000UL

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


volatile uint32_t timer_counter = 0;

ISR(TIMER0_OVF_vect) {
    ++timer_counter;
}

void init_timer() {
    TCCR0A = 0b00000000;
    TCCR0B = 0b00000010;
    TCNT0 = 0;
    TIMSK  = 0b00000010;
}

void stop_timer() {
    TCCR0B &= ~0b00000010;
}

void resume_timer() {
    TCCR0B |= 0b00000010;
}

uint32_t micros() {
    return (timer_counter << 8) + TCNT0;
}

uint32_t millis() {
    return micros() / 1000;
}

#define FREQ 4200
#define PERIOD (1'000'000 / FREQ)

char PATTERN[] = { 0b001, 0b011, 0b010, 0b110, 0b100, 0b101 };

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

    // Buttons
    // DDRD = 0xef;
    // PORTD = 0x10;

    init_timer();
    sei();

    uint16_t mark = micros();
    while(true) {
        for(uint8_t i=0; i<sizeof(PATTERN) / sizeof(char); i++) {
            PORTB = PATTERN[i];
            while(mark < micros() && micros() - mark < PERIOD); 
        }
    }
}
