#define F_CPU 8'000'000UL

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


int8_t plus(int8_t a, int8_t b) {
    return a + b;
}

int8_t minus(int8_t a, int8_t b) {
    return a - b;
}

int8_t multiply(int8_t a, int8_t b) {
    return a * b;
}

int8_t divide(int8_t a, int8_t b) {
    return a / b;
}

int8_t(*ops[])(int8_t, int8_t) = { plus, minus, multiply, divide };

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

    // Buttons
    DDRD = 0x00;
    PORTD = 0xff;

    bool change_mode = true;
    uint8_t mode = 0;

    while(true) {
        if((PIND & 0x01) != change_mode) {
            _delay_ms(50);
            if((PIND & 0x01) != change_mode) {
                change_mode = !change_mode;
                if(change_mode) {
                    ++mode;
                    mode %= 4;
                }
            }
        }

        uint64_t inp = ~PIND >> 1;
        int8_t a, b, c;
        a = inp & 0x07;
        b = inp >> 3 & 0x07;

        c = ops[mode](a, b); 

        PORTB = (1 << (mode + 4)) | (c & 0x0f);
    }
}
