Changed potis

This commit is contained in:
CommanderRedYT
2022-10-26 18:11:12 +02:00
parent ef7e605eb7
commit 71906ba511

View File

@@ -73,6 +73,18 @@ void readPotis()
// sum += analogRead(pin); // sum += analogRead(pin);
// return sum / sampleCount; // return sum / sampleCount;
// }; // };
constexpr auto sampleMultipleTimes = [](adc_channel_t channel){
int sum{};
const auto sampleCount = configs.sampleCount.value();
for (int i = 0; i < sampleCount; i++)
{
int value;
if (const auto result = adc_oneshot_read(adc1_handle, channel, &value); result != ESP_OK)
ESP_LOGE(TAG, "adc_oneshot_read_channel() failed with %s", esp_err_to_name(result));
sum += value;
}
return sum / sampleCount;
};
raw_gas = std::nullopt; raw_gas = std::nullopt;
raw_brems = std::nullopt; raw_brems = std::nullopt;
@@ -100,19 +112,13 @@ void readPotis()
#ifdef FEATURE_ADC_IN #ifdef FEATURE_ADC_IN
if (!raw_gas) if (!raw_gas)
{ {
int raw; raw_gas = sampleMultipleTimes(ADC_CHANNEL_GAS);
if (const auto result = adc_oneshot_read(adc1_handle, ADC_CHANNEL_GAS, &raw); result == ESP_OK) ESP_LOGD(TAG, "raw_gas: %d", *raw_gas);
raw_gas = raw;
else
ESP_LOGE(TAG, "adc_oneshot_read() failed with %s", esp_err_to_name(result));
} }
if (!raw_brems) if (!raw_brems)
{ {
int raw; raw_brems = sampleMultipleTimes(ADC_CHANNEL_BREMS);
if (const auto result = adc_oneshot_read(adc1_handle, ADC_CHANNEL_BREMS, &raw); result == ESP_OK) ESP_LOGD(TAG, "raw_brems: %d", *raw_brems);
raw_brems = raw;
else
ESP_LOGE(TAG, "adc_oneshot_read() failed with %s", esp_err_to_name(result));
} }
#endif #endif