Skip to content

Commit 3ee2195

Browse files
authored
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
1 parent 2fd0ff0 commit 3ee2195

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

main/boards/esp32s3-korvo2-v3/esp32s3_korvo2_v3_board.cc

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "button.h"
66
#include "config.h"
77
#include "i2c_device.h"
8+
#include "assets/lang_config.h"
89

910
#include <esp_log.h>
1011
#include <esp_lcd_panel_vendor.h>
@@ -16,6 +17,16 @@
1617
#include "esp32_camera.h"
1718

1819
#define TAG "esp32s3_korvo2_v3"
20+
/* ADC Buttons */
21+
typedef enum {
22+
BSP_ADC_BUTTON_REC,
23+
BSP_ADC_BUTTON_VOL_MUTE,
24+
BSP_ADC_BUTTON_PLAY,
25+
BSP_ADC_BUTTON_SET,
26+
BSP_ADC_BUTTON_VOL_DOWN,
27+
BSP_ADC_BUTTON_VOL_UP,
28+
BSP_ADC_BUTTON_NUM
29+
} bsp_adc_button_t;
1930

2031
// Init ili9341 by custom cmd
2132
static const ili9341_lcd_init_cmd_t vendor_specific_init[] = {
@@ -41,6 +52,10 @@ static const ili9341_lcd_init_cmd_t vendor_specific_init[] = {
4152
class Esp32S3Korvo2V3Board : public WifiBoard {
4253
private:
4354
Button boot_button_;
55+
Button* adc_button_[BSP_ADC_BUTTON_NUM];
56+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
57+
adc_oneshot_unit_handle_t bsp_adc_handle = NULL;
58+
#endif
4459
i2c_master_bus_handle_t i2c_bus_;
4560
LcdDisplay* display_;
4661
esp_io_expander_handle_t io_expander_ = NULL;
@@ -127,7 +142,103 @@ class Esp32S3Korvo2V3Board : public WifiBoard {
127142
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
128143
}
129144

145+
void ChangeVol(int val) {
146+
auto codec = GetAudioCodec();
147+
auto volume = codec->output_volume() + val;
148+
if (volume > 100) {
149+
volume = 100;
150+
}
151+
if (volume < 0) {
152+
volume = 0;
153+
}
154+
codec->SetOutputVolume(volume);
155+
GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
156+
}
157+
158+
void MuteVol() {
159+
auto codec = GetAudioCodec();
160+
auto volume = codec->output_volume();
161+
if (volume > 1) {
162+
volume = 0;
163+
} else {
164+
volume = 50;
165+
}
166+
codec->SetOutputVolume(volume);
167+
GetDisplay()->ShowNotification(Lang::Strings::VOLUME + std::to_string(volume));
168+
}
169+
130170
void InitializeButtons() {
171+
button_adc_config_t adc_cfg = {};
172+
adc_cfg.adc_channel = ADC_CHANNEL_4; // ADC1 channel 0 is GPIO5
173+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
174+
const adc_oneshot_unit_init_cfg_t init_config1 = {
175+
.unit_id = ADC_UNIT_1,
176+
};
177+
adc_oneshot_new_unit(&init_config1, &bsp_adc_handle);
178+
adc_cfg.adc_handle = &bsp_adc_handle;
179+
#endif
180+
adc_cfg.button_index = BSP_ADC_BUTTON_REC;
181+
adc_cfg.min = 2310; // middle is 2410mV
182+
adc_cfg.max = 2510;
183+
adc_button_[0] = new AdcButton(adc_cfg);
184+
185+
adc_cfg.button_index = BSP_ADC_BUTTON_VOL_MUTE;
186+
adc_cfg.min = 1880; // middle is 1980mV
187+
adc_cfg.max = 2080;
188+
adc_button_[1] = new AdcButton(adc_cfg);
189+
190+
adc_cfg.button_index = BSP_ADC_BUTTON_PLAY;
191+
adc_cfg.min = 1550; // middle is 1650mV
192+
adc_cfg.max = 1750;
193+
adc_button_[2] = new AdcButton(adc_cfg);
194+
195+
adc_cfg.button_index = BSP_ADC_BUTTON_SET;
196+
adc_cfg.min = 1015; // middle is 1115mV
197+
adc_cfg.max = 1215;
198+
adc_button_[3] = new AdcButton(adc_cfg);
199+
200+
adc_cfg.button_index = BSP_ADC_BUTTON_VOL_DOWN;
201+
adc_cfg.min = 720; // middle is 820mV
202+
adc_cfg.max = 920;
203+
adc_button_[4] = new AdcButton(adc_cfg);
204+
205+
adc_cfg.button_index = BSP_ADC_BUTTON_VOL_UP;
206+
adc_cfg.min = 280; // middle is 380mV
207+
adc_cfg.max = 480;
208+
adc_button_[5] = new AdcButton(adc_cfg);
209+
210+
auto volume_up_button = adc_button_[BSP_ADC_BUTTON_VOL_UP];
211+
volume_up_button->OnClick([this]() {ChangeVol(10);});
212+
volume_up_button->OnLongPress([this]() {
213+
GetAudioCodec()->SetOutputVolume(100);
214+
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
215+
});
216+
217+
auto volume_down_button = adc_button_[BSP_ADC_BUTTON_VOL_DOWN];
218+
volume_down_button->OnClick([this]() {ChangeVol(-10);});
219+
volume_down_button->OnLongPress([this]() {
220+
GetAudioCodec()->SetOutputVolume(0);
221+
GetDisplay()->ShowNotification(Lang::Strings::MUTED);
222+
});
223+
224+
auto volume_mute_button = adc_button_[BSP_ADC_BUTTON_VOL_MUTE];
225+
volume_mute_button->OnClick([this]() {MuteVol();});
226+
227+
auto play_button = adc_button_[BSP_ADC_BUTTON_PLAY];
228+
play_button->OnClick([this]() {
229+
ESP_LOGI(TAG, " TODO %s:%d\n", __func__, __LINE__);
230+
});
231+
232+
auto set_button = adc_button_[BSP_ADC_BUTTON_SET];
233+
set_button->OnClick([this]() {
234+
ESP_LOGI(TAG, "TODO %s:%d\n", __func__, __LINE__);
235+
});
236+
237+
auto rec_button = adc_button_[BSP_ADC_BUTTON_REC];
238+
rec_button->OnClick([this]() {
239+
ESP_LOGI(TAG, "TODO %s:%d\n", __func__, __LINE__);
240+
});
241+
boot_button_.OnClick([this]() {});
131242
boot_button_.OnClick([this]() {
132243
auto& app = Application::GetInstance();
133244
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {

0 commit comments

Comments
 (0)