Skip to content

Commit fd62357

Browse files
committed
Upgrade components
1 parent b00c5b5 commit fd62357

File tree

10 files changed

+208
-164
lines changed

10 files changed

+208
-164
lines changed

main/boards/common/button.cc

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,28 @@
11
#include "button.h"
22

3+
#include <button_gpio.h>
34
#include <esp_log.h>
45

5-
static const char* TAG = "Button";
6-
#if CONFIG_SOC_ADC_SUPPORTED
7-
Button::Button(const button_adc_config_t& adc_cfg) {
8-
button_config_t button_config = {
9-
.type = BUTTON_TYPE_ADC,
10-
.long_press_time = 1000,
11-
.short_press_time = 50,
12-
.adc_button_config = adc_cfg
13-
};
14-
button_handle_ = iot_button_create(&button_config);
15-
if (button_handle_ == NULL) {
16-
ESP_LOGE(TAG, "Failed to create button handle");
17-
return;
18-
}
6+
#define TAG "Button"
7+
8+
Button::Button(button_handle_t button_handle) : button_handle_(button_handle) {
199
}
20-
#endif
2110

22-
Button::Button(gpio_num_t gpio_num, bool active_high) : gpio_num_(gpio_num) {
11+
Button::Button(gpio_num_t gpio_num, bool active_high, uint16_t long_press_time, uint16_t short_press_time) : gpio_num_(gpio_num) {
2312
if (gpio_num == GPIO_NUM_NC) {
2413
return;
2514
}
2615
button_config_t button_config = {
27-
.type = BUTTON_TYPE_GPIO,
28-
.long_press_time = 1000,
29-
.short_press_time = 50,
30-
.gpio_button_config = {
31-
.gpio_num = gpio_num,
32-
.active_level = static_cast<uint8_t>(active_high ? 1 : 0)
33-
}
16+
.long_press_time = long_press_time,
17+
.short_press_time = short_press_time
3418
};
35-
button_handle_ = iot_button_create(&button_config);
36-
if (button_handle_ == NULL) {
37-
ESP_LOGE(TAG, "Failed to create button handle");
38-
return;
39-
}
19+
button_gpio_config_t gpio_config = {
20+
.gpio_num = gpio_num,
21+
.active_level = static_cast<uint8_t>(active_high ? 1 : 0),
22+
.enable_power_save = false,
23+
.disable_pull = false
24+
};
25+
ESP_ERROR_CHECK(iot_button_new_gpio_device(&button_config, &gpio_config, &button_handle_));
4026
}
4127

4228
Button::~Button() {
@@ -50,7 +36,7 @@ void Button::OnPressDown(std::function<void()> callback) {
5036
return;
5137
}
5238
on_press_down_ = callback;
53-
iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, [](void* handle, void* usr_data) {
39+
iot_button_register_cb(button_handle_, BUTTON_PRESS_DOWN, nullptr, [](void* handle, void* usr_data) {
5440
Button* button = static_cast<Button*>(usr_data);
5541
if (button->on_press_down_) {
5642
button->on_press_down_();
@@ -63,7 +49,7 @@ void Button::OnPressUp(std::function<void()> callback) {
6349
return;
6450
}
6551
on_press_up_ = callback;
66-
iot_button_register_cb(button_handle_, BUTTON_PRESS_UP, [](void* handle, void* usr_data) {
52+
iot_button_register_cb(button_handle_, BUTTON_PRESS_UP, nullptr, [](void* handle, void* usr_data) {
6753
Button* button = static_cast<Button*>(usr_data);
6854
if (button->on_press_up_) {
6955
button->on_press_up_();
@@ -76,7 +62,7 @@ void Button::OnLongPress(std::function<void()> callback) {
7662
return;
7763
}
7864
on_long_press_ = callback;
79-
iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, [](void* handle, void* usr_data) {
65+
iot_button_register_cb(button_handle_, BUTTON_LONG_PRESS_START, nullptr, [](void* handle, void* usr_data) {
8066
Button* button = static_cast<Button*>(usr_data);
8167
if (button->on_long_press_) {
8268
button->on_long_press_();
@@ -89,7 +75,7 @@ void Button::OnClick(std::function<void()> callback) {
8975
return;
9076
}
9177
on_click_ = callback;
92-
iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, [](void* handle, void* usr_data) {
78+
iot_button_register_cb(button_handle_, BUTTON_SINGLE_CLICK, nullptr, [](void* handle, void* usr_data) {
9379
Button* button = static_cast<Button*>(usr_data);
9480
if (button->on_click_) {
9581
button->on_click_();
@@ -102,10 +88,28 @@ void Button::OnDoubleClick(std::function<void()> callback) {
10288
return;
10389
}
10490
on_double_click_ = callback;
105-
iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, [](void* handle, void* usr_data) {
91+
iot_button_register_cb(button_handle_, BUTTON_DOUBLE_CLICK, nullptr, [](void* handle, void* usr_data) {
10692
Button* button = static_cast<Button*>(usr_data);
10793
if (button->on_double_click_) {
10894
button->on_double_click_();
10995
}
11096
}, this);
11197
}
98+
99+
void Button::OnMultipleClick(std::function<void()> callback, uint8_t click_count) {
100+
if (button_handle_ == nullptr) {
101+
return;
102+
}
103+
on_multiple_click_ = callback;
104+
button_event_args_t event_args = {
105+
.multiple_clicks = {
106+
.clicks = click_count
107+
}
108+
};
109+
iot_button_register_cb(button_handle_, BUTTON_MULTIPLE_CLICK, &event_args, [](void* handle, void* usr_data) {
110+
Button* button = static_cast<Button*>(usr_data);
111+
if (button->on_multiple_click_) {
112+
button->on_multiple_click_();
113+
}
114+
}, this);
115+
}

main/boards/common/button.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33

44
#include <driver/gpio.h>
55
#include <iot_button.h>
6+
#include <button_types.h>
67
#include <functional>
78

89
class Button {
910
public:
10-
#if CONFIG_SOC_ADC_SUPPORTED
11-
Button(const button_adc_config_t& cfg);
12-
#endif
13-
Button(gpio_num_t gpio_num, bool active_high = false);
11+
Button(button_handle_t button_handle);
12+
Button(gpio_num_t gpio_num, bool active_high = false, uint16_t long_press_time = 1000, uint16_t short_press_time = 50);
1413
~Button();
1514

1615
void OnPressDown(std::function<void()> callback);
1716
void OnPressUp(std::function<void()> callback);
1817
void OnLongPress(std::function<void()> callback);
1918
void OnClick(std::function<void()> callback);
2019
void OnDoubleClick(std::function<void()> callback);
20+
void OnMultipleClick(std::function<void()> callback, uint8_t click_count = 3);
2121
private:
2222
gpio_num_t gpio_num_;
2323
button_handle_t button_handle_ = nullptr;
2424

25-
2625
std::function<void()> on_press_down_;
2726
std::function<void()> on_press_up_;
2827
std::function<void()> on_long_press_;
2928
std::function<void()> on_click_;
3029
std::function<void()> on_double_click_;
30+
std::function<void()> on_multiple_click_;
3131
};
3232

3333
#endif // BUTTON_H_

main/boards/common/dual_network_board.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class DualNetworkBoard : public Board {
4343
// 获取当前网络类型
4444
NetworkType GetNetworkType() const { return network_type_; }
4545

46+
// 获取当前活动的板卡引用
47+
Board& GetCurrentBoard() const { return *current_board_; }
48+
4649
// 重写Board接口
4750
virtual std::string GetBoardType() override;
4851
virtual void StartNetwork() override;

main/boards/df-k10/df_k10_board.cc

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ class Df_K10Board : public WifiBoard {
3131
button_handle_t btn_a;
3232
button_handle_t btn_b;
3333

34+
static Df_K10Board* instance_;
35+
36+
static uint8_t GetButtonALevel(button_driver_t *button_driver) {
37+
return instance_->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_2);
38+
}
39+
40+
static uint8_t GetButtonBLevel(button_driver_t *button_driver) {
41+
return instance_->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_12);
42+
}
43+
3444
void InitializeI2c() {
3545
// Initialize I2C peripheral
3646
i2c_master_bus_config_t i2c_bus_cfg = {
@@ -96,32 +106,27 @@ class Df_K10Board : public WifiBoard {
96106
}
97107
}
98108
void InitializeButtons() {
109+
instance_ = this;
110+
99111
// Button A
100112
button_config_t btn_a_config = {
101-
.type = BUTTON_TYPE_CUSTOM,
102113
.long_press_time = 1000,
103-
.short_press_time = 50,
104-
.custom_button_config = {
105-
.active_level = 0,
106-
.button_custom_init =nullptr,
107-
.button_custom_get_key_value = [](void *param) -> uint8_t {
108-
auto self = static_cast<Df_K10Board*>(param);
109-
return self->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_2);
110-
},
111-
.button_custom_deinit = nullptr,
112-
.priv = this,
113-
},
114+
.short_press_time = 50
115+
};
116+
button_driver_t btn_a_driver = {
117+
.enable_power_save = false,
118+
.get_key_level = GetButtonALevel
114119
};
115-
btn_a = iot_button_create(&btn_a_config);
116-
iot_button_register_cb(btn_a, BUTTON_SINGLE_CLICK, [](void* button_handle, void* usr_data) {
120+
ESP_ERROR_CHECK(iot_button_create(&btn_a_config, &btn_a_driver, &btn_a));
121+
iot_button_register_cb(btn_a, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
117122
auto self = static_cast<Df_K10Board*>(usr_data);
118123
auto& app = Application::GetInstance();
119124
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
120125
self->ResetWifiConfiguration();
121126
}
122127
app.ToggleChatState();
123128
}, this);
124-
iot_button_register_cb(btn_a, BUTTON_LONG_PRESS_START, [](void* button_handle, void* usr_data) {
129+
iot_button_register_cb(btn_a, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
125130
auto self = static_cast<Df_K10Board*>(usr_data);
126131
auto codec = self->GetAudioCodec();
127132
auto volume = codec->output_volume() - 10;
@@ -134,30 +139,23 @@ class Df_K10Board : public WifiBoard {
134139

135140
// Button B
136141
button_config_t btn_b_config = {
137-
.type = BUTTON_TYPE_CUSTOM,
138142
.long_press_time = 1000,
139-
.short_press_time = 50,
140-
.custom_button_config = {
141-
.active_level = 0,
142-
.button_custom_init =nullptr,
143-
.button_custom_get_key_value = [](void *param) -> uint8_t {
144-
auto self = static_cast<Df_K10Board*>(param);
145-
return self->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_12);
146-
},
147-
.button_custom_deinit = nullptr,
148-
.priv = this,
149-
},
143+
.short_press_time = 50
150144
};
151-
btn_b = iot_button_create(&btn_b_config);
152-
iot_button_register_cb(btn_b, BUTTON_SINGLE_CLICK, [](void* button_handle, void* usr_data) {
145+
button_driver_t btn_b_driver = {
146+
.enable_power_save = false,
147+
.get_key_level = GetButtonBLevel
148+
};
149+
ESP_ERROR_CHECK(iot_button_create(&btn_b_config, &btn_b_driver, &btn_b));
150+
iot_button_register_cb(btn_b, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
153151
auto self = static_cast<Df_K10Board*>(usr_data);
154152
auto& app = Application::GetInstance();
155153
if (app.GetDeviceState() == kDeviceStateStarting && !WifiStation::GetInstance().IsConnected()) {
156154
self->ResetWifiConfiguration();
157155
}
158156
app.ToggleChatState();
159157
}, this);
160-
iot_button_register_cb(btn_b, BUTTON_LONG_PRESS_START, [](void* button_handle, void* usr_data) {
158+
iot_button_register_cb(btn_b, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
161159
auto self = static_cast<Df_K10Board*>(usr_data);
162160
auto codec = self->GetAudioCodec();
163161
auto volume = codec->output_volume() + 10;
@@ -253,3 +251,5 @@ class Df_K10Board : public WifiBoard {
253251
};
254252

255253
DECLARE_BOARD(Df_K10Board);
254+
255+
Df_K10Board* Df_K10Board::instance_ = nullptr;

main/boards/esp-box-lite/esp_box_lite_board.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include "config.h"
99
#include "iot/thing_manager.h"
1010
#include "assets/lang_config.h"
11+
1112
#include <esp_log.h>
13+
#include <button_adc.h>
1214
#include <esp_lcd_panel_vendor.h>
1315
#include <driver/i2c_master.h>
1416
#include <driver/spi_common.h>
@@ -86,7 +88,7 @@ class EspBoxBoardLite : public WifiBoard {
8688
ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, SPI_DMA_CH_AUTO));
8789
}
8890

89-
void changeVol(int val) {
91+
void ChangeVol(int val) {
9092
auto codec = GetAudioCodec();
9193
auto volume = codec->output_volume() + val;
9294
if (volume > 100) {
@@ -109,7 +111,11 @@ class EspBoxBoardLite : public WifiBoard {
109111

110112
void InitializeButtons() {
111113
/* Initialize ADC esp-box lite的前三个按钮采用是的adc按钮,而非gpio */
112-
button_adc_config_t adc_cfg;
114+
button_config_t btn_config = {
115+
.long_press_time = 2000,
116+
.short_press_time = 50,
117+
};
118+
button_adc_config_t adc_cfg = {};
113119
adc_cfg.adc_channel = ADC_CHANNEL_0; // ADC1 channel 0 is GPIO1
114120
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
115121
const adc_oneshot_unit_init_cfg_t init_config1 = {
@@ -121,27 +127,31 @@ class EspBoxBoardLite : public WifiBoard {
121127
adc_cfg.button_index = BSP_ADC_BUTTON_PREV;
122128
adc_cfg.min = 2310; // middle is 2410mV
123129
adc_cfg.max = 2510;
124-
adc_button_[0] = new Button(adc_cfg);
130+
ESP_ERROR_CHECK(iot_button_new_adc_device(&btn_config, &adc_cfg, &adc_button_[0]));
131+
adc_button_[0] = new Button(adc_button_[0]);
125132

126133
adc_cfg.button_index = BSP_ADC_BUTTON_ENTER;
127134
adc_cfg.min = 1880; // middle is 1980mV
128135
adc_cfg.max = 2080;
129-
adc_button_[1] = new Button(adc_cfg);
136+
ESP_ERROR_CHECK(iot_button_new_adc_device(&btn_config, &adc_cfg, &adc_button_[1]));
137+
adc_button_[1] = new Button(adc_button_[1]);
130138

131139
adc_cfg.button_index = BSP_ADC_BUTTON_NEXT;
132140
adc_cfg.min = 720; // middle is 820mV
133141
adc_cfg.max = 920;
134-
adc_button_[2] = new Button(adc_cfg);
142+
143+
ESP_ERROR_CHECK(iot_button_new_adc_device(&btn_config, &adc_cfg, &adc_button_[2]));
144+
adc_button_[2] = new Button(adc_button_[2]);
135145

136146
auto volume_up_button = adc_button_[BSP_ADC_BUTTON_NEXT];
137-
volume_up_button->OnClick([this]() {changeVol(10);});
147+
volume_up_button->OnClick([this]() {ChangeVol(10);});
138148
volume_up_button->OnLongPress([this]() {
139149
GetAudioCodec()->SetOutputVolume(100);
140150
GetDisplay()->ShowNotification(Lang::Strings::MAX_VOLUME);
141151
});
142152

143153
auto volume_down_button = adc_button_[BSP_ADC_BUTTON_PREV];
144-
volume_down_button->OnClick([this]() {changeVol(-10);});
154+
volume_down_button->OnClick([this]() {ChangeVol(-10);});
145155
volume_down_button->OnLongPress([this]() {
146156
GetAudioCodec()->SetOutputVolume(0);
147157
GetDisplay()->ShowNotification(Lang::Strings::MUTED);

0 commit comments

Comments
 (0)