/*
 * adc.c
 *
 *  Created on: 08.02.2019
 *      Author: julian
 */

#include "adc.h"
#include "main.h"

#include <avr/io.h>
#include <avr/interrupt.h>

// global adc instance
adc_t adc;

/* adc driver */
void adc_init(uint8_t * channels) {
	adc.current_channel = 0;
	adc.channel_map = channels;

	ADMUX = channels[0];
	ADCSRA = (1<<ADATE) | (1<<ADEN) | 7; // enable adc, maximum prescaler value
}

ISR(ADC_vect) {
	adc.buffer[adc.current_channel] = ADC;
	adc.current_channel++;

	if (adc.current_channel == ADC_BUFFER_SIZE) {
		adc.current_channel = 0;
	}

	ADMUX = adc.channel_map[adc.current_channel];
}

