#include "avr/interrupt.h"
#include "avr/io.h"
#include "joystick.hpp"

volatile uint16_t joy_x = 0;
volatile uint16_t joy_y = 0;
static uint8_t curr_chan = 0;

Joystick::Joystick() {
    DDRF &= ~((1 << PF0) | (1 << PF1));
    PORTF |= (1 << PF0) | (1 << PF1);
}

uint16_t Joystick::getX() { return joy_x; }

uint16_t Joystick::getY() { return joy_y; }

ISR(ADC_vect) {
    uint16_t value = ADC;

    if (curr_chan == 0) {
        joy_x = value;
        curr_chan = 1;

        ADMUX = (ADMUX & 0xF0) | PF1;
    } else {
        joy_y = value;
        curr_chan = 0;

        ADMUX = (ADMUX & 0xF0) | PF0;
    }
}
