diff --git a/main/espFeatures/adcFeature.h b/main/espFeatures/adcFeature.h index 387757f..4426207 100644 --- a/main/espFeatures/adcFeature.h +++ b/main/espFeatures/adcFeature.h @@ -8,9 +8,7 @@ #include "driver/adc.h" - -template -class AdcFeature : public Next { +template class AdcFeature : public Next { using PinConfig = Next::PlatformInfo::PinConfig; static std::pair getAnalogPin(int pin) { @@ -23,20 +21,34 @@ class AdcFeature : public Next { } class Adc { - public: + public: void configure(int pin, int atten = static_cast(ADC_ATTEN_DB_12)) { int adcNum; int channel; std::tie(adcNum, channel) = getAnalogPin(pin); - esp_err_t err = adc1_config_width(static_cast(ADC_WIDTH_BIT_DEFAULT)); - if (err != ESP_OK) { - throw std::runtime_error(esp_err_to_name(err)); - } - - err = adc1_config_channel_atten(static_cast(channel), static_cast(atten)); - if (err != ESP_OK) { - throw std::runtime_error(esp_err_to_name(err)); + if (adcNum == 1) { + esp_err_t err = adc1_config_width( + static_cast(ADC_WIDTH_BIT_DEFAULT)); + if (err != ESP_OK) { + throw std::runtime_error(esp_err_to_name(err)); + } + + err = adc1_config_channel_atten( + static_cast(channel), + static_cast(atten)); + if (err != ESP_OK) { + throw std::runtime_error(esp_err_to_name(err)); + } + } else if (adcNum == 2) { + esp_err_t err = adc2_config_channel_atten( + static_cast(channel), + static_cast(atten)); + if (err != ESP_OK) { + throw std::runtime_error(esp_err_to_name(err)); + } + } else { + throw std::runtime_error("Unsupported ADC unit"); } } @@ -45,39 +57,66 @@ class AdcFeature : public Next { int channel; std::tie(adcNum, channel) = getAnalogPin(pin); - return adc1_get_raw(static_cast(channel)) >> (ADC_WIDTH_BIT_DEFAULT - 10); + int raw_val = 0; + + if (adcNum == 1) { + raw_val = adc1_get_raw(static_cast(channel)); + } else if (adcNum == 2) { + esp_err_t err = adc2_get_raw( + static_cast(channel), + static_cast(ADC_WIDTH_BIT_DEFAULT), + &raw_val); + if (err != ESP_OK) { + throw std::runtime_error(esp_err_to_name(err)); + } + } else { + throw std::runtime_error("Unsupported ADC unit"); + } + + return raw_val >> (ADC_WIDTH_BIT_DEFAULT - 10); } }; -public: + public: Adc adc; void initialize() { Next::initialize(); jac::FunctionFactory ff(this->context()); - jac::Module& adcModule = this->newModule("adc"); + jac::Module &adcModule = this->newModule("adc"); jac::Object attten = jac::Object::create(this->context()); - attten.defineProperty("Db0", jac::Value::from(this->context(), static_cast(ADC_ATTEN_DB_0))); - attten.defineProperty("Db2_5", jac::Value::from(this->context(), static_cast(ADC_ATTEN_DB_2_5))); - attten.defineProperty("Db6", jac::Value::from(this->context(), static_cast(ADC_ATTEN_DB_6))); - attten.defineProperty("Db12", jac::Value::from(this->context(), static_cast(ADC_ATTEN_DB_12))); - - adcModule.addExport("configure", ff.newFunctionVariadic([this](std::vector args) { - if (args.size() < 1 || args.size() > 2) { - throw std::runtime_error("Invalid number of arguments"); - } - - int pin = args[0].to(); - int atten = static_cast(ADC_ATTEN_DB_12); - if (args.size() == 2) { - atten = args[1].to(); - } - - adc.configure(pin, atten); - })); - adcModule.addExport("read", ff.newFunction(noal::function(&Adc::read, &adc))); + attten.defineProperty( + "Db0", jac::Value::from(this->context(), + static_cast(ADC_ATTEN_DB_0))); + attten.defineProperty( + "Db2_5", jac::Value::from(this->context(), + static_cast(ADC_ATTEN_DB_2_5))); + attten.defineProperty( + "Db6", jac::Value::from(this->context(), + static_cast(ADC_ATTEN_DB_6))); + attten.defineProperty( + "Db12", jac::Value::from(this->context(), + static_cast(ADC_ATTEN_DB_12))); + + adcModule.addExport( + "configure", + ff.newFunctionVariadic([this](std::vector args) { + if (args.size() < 1 || args.size() > 2) { + throw std::runtime_error("Invalid number of arguments"); + } + + int pin = args[0].to(); + int atten = static_cast(ADC_ATTEN_DB_12); + if (args.size() == 2) { + atten = args[1].to(); + } + + adc.configure(pin, atten); + })); + adcModule.addExport("read", + ff.newFunction(noal::function(&Adc::read, &adc))); adcModule.addExport("Attenuation", attten); } }; diff --git a/main/platform/esp32s3.h b/main/platform/esp32s3.h index 5db8b40..e10326b 100644 --- a/main/platform/esp32s3.h +++ b/main/platform/esp32s3.h @@ -30,8 +30,19 @@ struct PlatformInfo { { 7, { 1, ADC1_GPIO7_CHANNEL } }, { 8, { 1, ADC1_GPIO8_CHANNEL } }, { 9, { 1, ADC1_GPIO9_CHANNEL } }, - { 10, { 1, ADC1_GPIO10_CHANNEL } } - }; + { 10, { 1, ADC1_GPIO10_CHANNEL } }, + + { 11, { 2, ADC2_GPIO11_CHANNEL } }, + { 12, { 2, ADC2_GPIO12_CHANNEL } }, + { 13, { 2, ADC2_GPIO13_CHANNEL } }, + { 14, { 2, ADC2_GPIO14_CHANNEL } }, + { 15, { 2, ADC2_GPIO15_CHANNEL } }, + { 16, { 2, ADC2_GPIO16_CHANNEL } }, + { 17, { 2, ADC2_GPIO17_CHANNEL } }, + { 18, { 2, ADC2_GPIO18_CHANNEL } }, + { 19, { 2, ADC2_GPIO19_CHANNEL } }, + { 20, { 2, ADC2_GPIO20_CHANNEL } } + }; static inline const std::set INTERRUPT_PINS = DIGITAL_PINS; static inline constexpr int DEFAULT_I2C_SDA_PIN = 0; static inline constexpr int DEFAULT_I2C_SCL_PIN = 1;