#include "gas_sensor.h"
#include "bsp/bsp.h"

static gas_t _gas;
void gas_sensor_init(void){
	_gas.voltage = 0;
	_gas.lowpass = 5;
	HAL_ADC1_ChanConfig(SPEED_SENSOR_ADC_CHANNEL);
	gas_sample_voltage();
}

void gas_sample_voltage(void){
	u32 vadc = 0;
	u32 max = 0, min = 0xFFFFFFF;
	int count = 8;
	for (int i = 0; i < count; i++) {
		u32 one = HAL_ADC1_ReadValue(SPEED_SENSOR_ADC_CHANNEL);
		vadc += one;
		if (min > one) {
			min = one;
		}
		if (max < one) {
			max = one;
		}
	}
	vadc -= (max + min);
	vadc = vadc / (count - 2);
	_gas.voltage = ((float)vadc)/(65536.0f) * ADC_REFERENCE_VOLTAGE;
}


float gas_get_value(void){

	return _gas.voltage/ADC_REFERENCE_VOLTAGE;
}

