#include <Arduino.h>

#define LED_PIN PD7
#define BUTTON1_PIN PD5
#define BUTTON2_PIN PD2

#define LED_BUILTIN_MASK (1 << PB5)
#define LED_EXT_MASK (1 << LED_PIN)
#define BUTTON1_MASK (1 << BUTTON1_PIN)
#define BUTTON2_MASK (1 << BUTTON2_PIN)

#define KNIGHT_LED_MASK 0x0F  // PB0–PB3 (pins 8–11)


unsigned long prevMillisBlink = 0;
unsigned long prevMillisDebounce1 = 0;
unsigned long prevMillisDebounce2 = 0;
unsigned long prevMillisKnight = 0;

bool ledState = false;
bool button1State = false;
bool button2State = false;
bool lastButton1Reading = false;
bool lastButton2Reading = false;

int knightIndex = 0;
bool knightForward = true;
uint16_t knightStates[] = {
    0b0001, 0b0010, 0b0100, 0b1000,  // Forward
    0b0100, 0b0010                   // Backward
};

uint8_t knightStatesMulti[] = {
    0b0001,  // 1
    0b0011,  // 12
    0b0111,  // 123
    0b1111,  // 1234
    0b1110,  // 234
    0b1100,  // 34
    0b1000,  // 4
    0b1100,  // 34
    0b1110,  // 234
    0b1111,  // 1234
    0b0111,  // 123
    0b0011   // 12
};


void setup() {
    // Ext. LED op PD7 en button1 op PD5
    DDRD |= LED_EXT_MASK;
    DDRD &= ~BUTTON1_MASK;

    // Internal LED op PB5, Knight Rider LEDs op PB0–PB3
    DDRB |= LED_BUILTIN_MASK | KNIGHT_LED_MASK;

    // Button2 op PD2 met interne pull-up
    DDRD &= ~BUTTON2_MASK;
    PORTD |= BUTTON2_MASK;  // interne pull-up aan

    Serial.begin(9600);  // voor debug indien nodig
}

void loop() {
    unsigned long currentMillis = millis();

    // ---------- LED blink 4 Hz ----------
    if (currentMillis - prevMillisBlink >= 125) {
        prevMillisBlink = currentMillis;
        ledState = !ledState;
        if (ledState) {
            PORTD |= LED_EXT_MASK;
        } else {
            PORTD &= ~LED_EXT_MASK;
        }
    }

    // ---------- Debounce Button 1 ----------
    bool reading1 = (PIND & BUTTON1_MASK);
    if (reading1 != lastButton1Reading) {
        prevMillisDebounce1 = currentMillis;
        lastButton1Reading = reading1;
    }
    if (currentMillis - prevMillisDebounce1 > 20) {
        button1State = reading1;
    }

    // Built-in LED aan als button 1 is ingedrukt
    if (button1State) {
        PORTB |= LED_BUILTIN_MASK;
    } else {
        PORTB &= ~LED_BUILTIN_MASK;
    }

    // ---------- Debounce Button 2 ----------
    bool reading2 = !(PIND & BUTTON2_MASK);
    if (reading2 != lastButton2Reading) {
        prevMillisDebounce2 = currentMillis;
        lastButton2Reading = reading2;
    }
    if (currentMillis - prevMillisDebounce2 > 20) {
        button2State = reading2;
    }

    // ---------- Knight Rider ----------
    if (currentMillis - prevMillisKnight >= 100) {
        prevMillisKnight = currentMillis;

        uint16_t value;
        if (button2State) {
            value = knightStates[knightIndex];  // niet ingedrukt → single (6 elementen)
            knightIndex = (knightIndex + 1) % 6;
        } else {
            value = knightStatesMulti[knightIndex];  // ingedrukt → multi (12 elementen)
            knightIndex = (knightIndex + 1) % 12;
        }

        PORTB &= ~KNIGHT_LED_MASK;
        PORTB |= (value & KNIGHT_LED_MASK);
    }
}
