/*
 * Sensors.c
 *
 * Created: 18.04.2021 21:14:11
 *  Author: vstrasil
 */ 

#include "Header/Sensors.h"

void sensors_init(){
		ADCA.CTRLB|=ADC_CURRLIMIT1_bm|ADC_CONMODE_bm;
		ADCA.REFCTRL|=ADC_REFSEL_INT1V_gc;
		ADCA.EVCTRL=0x00;
		ADCA.PRESCALER|=ADC_PRESCALER2_bm;
		ADCA.INTFLAGS=ADC_CH0IF_bm;
		ADCA.INTFLAGS=ADC_CH1IF_bm;
		ADCA.INTFLAGS=ADC_CH2IF_bm;
		ADCA.INTFLAGS=ADC_CH3IF_bm;
		ADCA.CH0.CTRL|=ADC_CH_INPUTMODE0_bm;
		ADCA.CH1.CTRL|=ADC_CH_INPUTMODE0_bm;
		ADCA.CH2.CTRL|=ADC_CH_INPUTMODE0_bm;
		ADCA.CH3.CTRL|=ADC_CH_INPUTMODE0_bm;
		ADCA.CTRLA=ADC_ENABLE_bm;
}

int sensor_ch0(uint8_t aSENSOR){
	ADCA.CH0.MUXCTRL=aSENSOR;
	ADCA.CTRLA|=ADC_CH0START_bm;
	while(!(ADCA_INTFLAGS & ADC_CH0IF_bm));
	ADCA.INTFLAGS=ADC_CH0IF_bm;
	return ADCA_CH0_RES;
}

int sensor_ch1(uint8_t aSENSOR){
	ADCA.CH1.MUXCTRL=aSENSOR;
	ADCA.CTRLA|=ADC_CH1START_bm;
	while(!(ADCA_INTFLAGS & ADC_CH1IF_bm));
	ADCA.INTFLAGS=ADC_CH1IF_bm;
	return ADCA_CH1_RES;
}

int sensor_ch2(uint8_t aSENSOR){
	ADCA.CH2.MUXCTRL=aSENSOR;
	ADCA.CTRLA|=ADC_CH2START_bm;
	while(!(ADCA_INTFLAGS & ADC_CH2IF_bm));
	ADCA.INTFLAGS=ADC_CH2IF_bm;
	return ADCA_CH2_RES;
}

int sensor_ch3(uint8_t aSENSOR){
	ADCA.CH3.MUXCTRL=aSENSOR;
	ADCA.CTRLA|=ADC_CH3START_bm;
	while(!(ADCA_INTFLAGS & ADC_CH3IF_bm));
	ADCA.INTFLAGS=ADC_CH3IF_bm;
	return ADCA_CH3_RES;
}

uint8_t is_battery_ok(){
	if (sensor_ch3(BAT)>1500)
	{
		return 1;
	} 
	else
	{
		return 0;
	}
}