From 55960996126d430ffade83398e5aa00b9f9d1c1b Mon Sep 17 00:00:00 2001 From: thek4n Date: Wed, 10 Jun 2026 01:00:06 +0300 Subject: [PATCH] refactor --- main/main.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/main/main.c b/main/main.c index 6e9544d..5db8d41 100644 --- a/main/main.c +++ b/main/main.c @@ -58,15 +58,13 @@ #define AP_MAX_CONN 4 #define AP_CHANNEL 6 -#define ADC_CHAN0 ADC_CHANNEL_4 -#define ADC_CHAN1 ADC_CHANNEL_5 +#define SENSOR_CHAN ADC_CHANNEL_4 #define ADC_ATTEN_DB ADC_ATTEN_DB_12 #define THRESHOLD_UP_NVS_NAME "threshold_up" #define THRESHOLD_LOW_NVS_NAME "threshold_low" #define NVS_PARTITION "nvs" -#define SENSOR_ADC_CHAN 0 #define MAX_JSON_CONTENT 512 #define FILTER_SAMPLES 5 @@ -279,8 +277,7 @@ static app_error_t adc_init(void) { .atten = ADC_ATTEN_DB, .bitwidth = ADC_BITWIDTH_DEFAULT, }; - CHECK_ERROR(adc_oneshot_config_channel(adc_handle, ADC_CHAN0, &config), APP_ERR_ADC_INIT_FAIL); - CHECK_ERROR(adc_oneshot_config_channel(adc_handle, ADC_CHAN1, &config), APP_ERR_ADC_INIT_FAIL); + CHECK_ERROR(adc_oneshot_config_channel(adc_handle, SENSOR_CHAN, &config), APP_ERR_ADC_INIT_FAIL); adc_cali_line_fitting_config_t cali_config = { .unit_id = ADC_UNIT_1, @@ -302,17 +299,8 @@ static app_error_t adc_init(void) { return ERR_OK; } -static int adc_read_raw(uint8_t channel) { - adc_channel_t adc_channel; - - if (channel == 0) { - adc_channel = ADC_CHAN0; - } else if (channel == 1) { - adc_channel = ADC_CHAN1; - } else { - ESP_LOGE(TAG, "Invalid ADC channel: %d", channel); - return -1; - } +static int adc_read_raw() { + adc_channel_t adc_channel = SENSOR_CHAN; CHECK_PTR(adc_handle, APP_ERR_ADC_INIT_FAIL); @@ -321,8 +309,8 @@ static int adc_read_raw(uint8_t channel) { return raw_value; } -static int adc_read_voltage(uint8_t channel) { - int raw_value = adc_read_raw(channel); +static int adc_read_voltage() { + int raw_value = adc_read_raw(); if (raw_value < 0) return -1; if (is_calibrated) { @@ -337,7 +325,7 @@ static int adc_read_voltage(uint8_t channel) { static int read_voltage_filtered(void) { int sum = 0; for (int i = 0; i < FILTER_SAMPLES; i++) { - int voltage = adc_read_voltage(SENSOR_ADC_CHAN); + int voltage = adc_read_voltage(); if (voltage < 0) return -1; sum += voltage; vTaskDelay(pdMS_TO_TICKS(10));