#include "ntc_sensor.h"
#include "bsp/bsp.h"

static ntc_t _ntc;
void ntc_sensor_init(void) {
	_ntc.temp_avg = 0;
	_ntc.low_pass_filter = 0.5f;
	HAL_ADC1_ChanConfig(TEMP_SENSOR_ADC_CHANNEL);
	ntc_sensor_sample();
}

void ntc_sensor_sample(void){
	u16 w_temp = HAL_ADC1_ReadValue(TEMP_SENSOR_ADC_CHANNEL);
    w_temp -= ( s32 )(V0_V *65536/ ADC_REFERENCE_VOLTAGE );
    w_temp *= (ADC_REFERENCE_VOLTAGE/dV_dT);
    w_temp = w_temp / 65536 + ( s32 )( T0_C );

	_ntc.temp_avg = w_temp * _ntc.low_pass_filter + _ntc.temp_avg * (1.0f - _ntc.low_pass_filter);

}


int ntc_sensor_temperature(void){
	return _ntc.temp_avg;
}

