@@ -183,21 +183,67 @@ static esp_err_t ntc_voltage_to_temperature(ntc_device_handle_t ntc_handle, uint
183183 return ESP_FAIL ;
184184}
185185
186+ #ifdef CONFIG_ENABLE_NTC_ADC_FILTER
187+ static esp_err_t adc_oneshot_get_filtered_result (adc_oneshot_unit_handle_t handle , adc_cali_handle_t cali_handle , adc_channel_t chan , int * filtered_result )
188+ {
189+ NTC_DRIVER_CHECK (filtered_result != NULL , "Pointer of filtered_result is invalid" , ESP_FAIL );
190+ int vol [CONFIG_NTC_FILTER_WINDOW_SIZE ] = {0 };
191+ int avg = 0 ;
192+ for (int i = 0 ; i < sizeof (vol ) / sizeof (vol [0 ]); i ++ ) {
193+ int raw = 0 ;
194+ NTC_DRIVER_CHECK (adc_oneshot_read (handle , chan , & raw ) == ESP_OK , "adc oneshot read fail" , ESP_FAIL );
195+ ESP_LOGV (TAG , "raw: %d" , raw );
196+ NTC_DRIVER_CHECK (adc_cali_raw_to_voltage (cali_handle , raw , & vol [i ]) == ESP_OK , "adc calibration fail" , ESP_FAIL );
197+ ESP_LOGV (TAG , "vol: %d" , vol [i ]);
198+ avg += vol [i ];
199+ }
200+ // calculate the average value
201+ avg /= sizeof (vol ) / sizeof (vol [0 ]);
202+ * filtered_result = avg ;
203+ // calculate the standard deviation
204+ int std_vol = 0 ;
205+ for (int i = 0 ; i < sizeof (vol ) / sizeof (vol [0 ]); i ++ ) {
206+ std_vol += (vol [i ] - avg ) * (vol [i ] - avg );
207+ }
208+ std_vol = (int )sqrt (std_vol / (sizeof (vol ) / sizeof (vol [0 ])));
209+ // filter the result, if the difference between the value and the average value is greater than the standard deviation, the value is discarded
210+ int count = 0 ;
211+ int filtered_vol = 0 ;
212+ for (int i = 0 ; i < sizeof (vol ) / sizeof (vol [0 ]); i ++ ) {
213+ if (abs (vol [i ] - avg ) < std_vol ) {
214+ count ++ ;
215+ filtered_vol += vol [i ];
216+ } else {
217+ ESP_LOGV (TAG , "filtered out: %d" , vol [i ]);
218+ }
219+ }
220+ if (count > 0 ) {
221+ * filtered_result = filtered_vol / count ;
222+ }
223+
224+ return ESP_OK ;
225+ }
226+ #endif
227+
186228esp_err_t ntc_dev_get_temperature (ntc_device_handle_t ntc_handle , float * temperature )
187229{
188- int voltage , adc_raw = 0 ;
230+ int voltage = 0 ;
189231
190232 if (ntc_handle == NULL ) {
191233 ESP_LOGW (TAG , "Pointer of handle is invalid" );
192234 return ESP_ERR_INVALID_ARG ;
193235 }
194236
195237 ntc_driver_dev_t * ndd = (ntc_driver_dev_t * )ntc_handle ;
238+ #ifdef CONFIG_ENABLE_NTC_ADC_FILTER
239+ esp_err_t ret = adc_oneshot_get_filtered_result (ndd -> adc_handle , ndd -> adc_cali_handle , ndd -> s_ntc_config .channel , & voltage );
240+ #else
241+ int adc_raw = 0 ;
196242 esp_err_t ret = adc_oneshot_read (ndd -> adc_handle , ndd -> s_ntc_config .channel , & adc_raw );
197243 NTC_DRIVER_CHECK (ret == ESP_OK , "adc oneshot read fail" , ESP_FAIL );
198244 NTC_DRIVER_CHECK (adc_raw != 0 , "adc raw data equal to 0" , ESP_FAIL );
199-
200245 ret = adc_cali_raw_to_voltage (ndd -> adc_cali_handle , adc_raw , & voltage );
246+ #endif
201247 NTC_DRIVER_CHECK (ret == ESP_OK , "adc calibration raw to voltage fail" , ESP_FAIL );
202248 NTC_DRIVER_CHECK (voltage != 0 , "voltage equal to 0" , ESP_FAIL );
203249 ntc_voltage_to_temperature (ndd , voltage , temperature );
0 commit comments