#include <avr/io.h>

#include "adc.h"
#include "util/delay.h"

void adc_init(void) {
    ADMUX = (1 << REFS0) | PF0;

    ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADATE) | (1 << ADPS2) |
             (1 << ADPS1) | (1 << ADPS0);

    ADCSRB = 0;

    ADCSRA |= (1 << ADSC);
}

uint16_t myAnalogRead(uint8_t channel) {
    channel &= 0x07;

    ADMUX = (ADMUX & 0xF0) | channel;

    _delay_us(5);

    ADCSRA |= (1 << ADSC);

    while (ADCSRA & (1 << ADSC))
        ;

    return ADC;
}

void reload_adc() { ADCSRA |= (1 << ADSC); }
