From 3ee219534043d3e985f3a0bd17b2b5da94353805 Mon Sep 17 00:00:00 2001 From: masc2008 Date: Fri, 3 Oct 2025 11:01:00 +0800 Subject: [PATCH] Change the button array to ADC buttons as in the board for esp32s3-korv2 (#1256) * Change the button array to ADC buttons as in the board for esp32s3-korv2 * Add MuteVol function to control audio volume --- .../esp32s3_korvo2_v3_board.cc | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/main/boards/esp32s3-korvo2-v3/esp32s3_korvo2_v3_board.cc b/main/boards/esp32s3-korvo2-v3/esp32s3_korvo2_v3_board.cc index b24dbb34..d33bedd9 100644 --- a/main/boards/esp32s3-korvo2-v3/esp32s3_korvo2_v3_board.cc +++ b/main/boards/esp32s3-korvo2-v3/esp32s3_korvo2_v3_board.cc @@ -5,6 +5,7 @@ #include "button.h" #include "config.h" #include "i2c_device.h" +#include "assets/lang_config.h" #include #include @@ -16,6 +17,16 @@ #include "esp32_camera.h" #define TAG "esp32s3_korvo2_v3" +/* ADC Buttons */ +typedef enum { + BSP_ADC_BUTTON_REC, + BSP_ADC_BUTTON_VOL_MUTE, + BSP_ADC_BUTTON_PLAY, + BSP_ADC_BUTTON_SET, + BSP_ADC_BUTTON_VOL_DOWN, + BSP_ADC_BUTTON_VOL_UP, + BSP_ADC_BUTTON_NUM +} bsp_adc_button_t; // Init ili9341 by custom cmd static const ili9341_lcd_init_cmd_t vendor_specific_init[] = { @@ -41,6 +52,10 @@ static const ili9341_lcd_init_cmd_t vendor_specific_init[] = { class Esp32S3Korvo2V3Board : public WifiBoard { private: Button boot_button_; + Button* adc_button_[BSP_ADC_BUTTON_NUM]; +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + adc_oneshot_unit_handle_t bsp_adc_handle = NULL; +#endif i2c_master_bus_handle_t i2c_bus_; LcdDisplay* display_; esp_io_expander_handle_t io_expander_ = NULL; @@ -127,7 +142,103 @@ private: ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO)); } + void ChangeVol(int val) { + auto codec = GetAudioCodec(); + auto volume = codec->output_volume() + val; + if (volume > 100) { + volume = 100; + } + if (volume < 0) { + volume = 0; + } + codec->SetOutputVolume(volume); + GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume)); + } + + void MuteVol() { + auto codec = GetAudioCodec(); + auto volume = codec->output_volume(); + if (volume > 1) { + volume = 0; + } else { + volume = 50; + } + codec->SetOutputVolume(volume); + GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume)); + } + void InitializeButtons() { + button_adc_config_t adc_cfg = {}; + adc_cfg.adc_channel = ADC_CHANNEL_4; // ADC1 channel 0 is GPIO5 +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + const adc_oneshot_unit_init_cfg_t init_config1 = { + .unit_id = ADC_UNIT_1, + }; + adc_oneshot_new_unit(&init_config1, &bsp_adc_handle); + adc_cfg.adc_handle = &bsp_adc_handle; +#endif + adc_cfg.button_index = BSP_ADC_BUTTON_REC; + adc_cfg.min = 2310; // middle is 2410mV + adc_cfg.max = 2510; + adc_button_[0] = new AdcButton(adc_cfg); + + adc_cfg.button_index = BSP_ADC_BUTTON_VOL_MUTE; + adc_cfg.min = 1880; // middle is 1980mV + adc_cfg.max = 2080; + adc_button_[1] = new AdcButton(adc_cfg); + + adc_cfg.button_index = BSP_ADC_BUTTON_PLAY; + adc_cfg.min = 1550; // middle is 1650mV + adc_cfg.max = 1750; + adc_button_[2] = new AdcButton(adc_cfg); + + adc_cfg.button_index = BSP_ADC_BUTTON_SET; + adc_cfg.min = 1015; // middle is 1115mV + adc_cfg.max = 1215; + adc_button_[3] = new AdcButton(adc_cfg); + + adc_cfg.button_index = BSP_ADC_BUTTON_VOL_DOWN; + adc_cfg.min = 720; // middle is 820mV + adc_cfg.max = 920; + adc_button_[4] = new AdcButton(adc_cfg); + + adc_cfg.button_index = BSP_ADC_BUTTON_VOL_UP; + adc_cfg.min = 280; // middle is 380mV + adc_cfg.max = 480; + adc_button_[5] = new AdcButton(adc_cfg); + + auto volume_up_button = adc_button_[BSP_ADC_BUTTON_VOL_UP]; + volume_up_button->OnClick([this]() {ChangeVol(10);}); + volume_up_button->OnLongPress([this]() { + GetAudioCodec()->SetOutputVolume(100); + GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME); + }); + + auto volume_down_button = adc_button_[BSP_ADC_BUTTON_VOL_DOWN]; + volume_down_button->OnClick([this]() {ChangeVol(-10);}); + volume_down_button->OnLongPress([this]() { + GetAudioCodec()->SetOutputVolume(0); + GetDisplay()->ShowNotification(Lang::Strings::MUTED); + }); + + auto volume_mute_button = adc_button_[BSP_ADC_BUTTON_VOL_MUTE]; + volume_mute_button->OnClick([this]() {MuteVol();}); + + auto play_button = adc_button_[BSP_ADC_BUTTON_PLAY]; + play_button->OnClick([this]() { + ESP_LOGI(TAG, " TODO %s:%d\n", __func__, __LINE__); + }); + + auto set_button = adc_button_[BSP_ADC_BUTTON_SET]; + set_button->OnClick([this]() { + ESP_LOGI(TAG, "TODO %s:%d\n", __func__, __LINE__); + }); + + auto rec_button = adc_button_[BSP_ADC_BUTTON_REC]; + rec_button->OnClick([this]() { + ESP_LOGI(TAG, "TODO %s:%d\n", __func__, __LINE__); + }); + boot_button_.OnClick([this]() {}); boot_button_.OnClick([this]() { auto& app = Application::GetInstance(); if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {