Skip to content

Commit 724f440

Browse files
committed
fixbug: button driver cannot be in stack memory
1 parent a0ad3e6 commit 724f440

File tree

4 files changed

+42
-55
lines changed

4 files changed

+42
-55
lines changed

main/boards/df-k10/df_k10_board.cc

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,11 @@ class Df_K10Board : public WifiBoard {
3030
LcdDisplay *display_;
3131
button_handle_t btn_a;
3232
button_handle_t btn_b;
33+
button_driver_t* btn_a_driver_ = nullptr;
34+
button_driver_t* btn_b_driver_ = nullptr;
3335

3436
static Df_K10Board* instance_;
3537

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-
4438
void InitializeI2c() {
4539
// Initialize I2C peripheral
4640
i2c_master_bus_config_t i2c_bus_cfg = {
@@ -113,11 +107,12 @@ class Df_K10Board : public WifiBoard {
113107
.long_press_time = 1000,
114108
.short_press_time = 0
115109
};
116-
button_driver_t btn_a_driver = {
117-
.enable_power_save = false,
118-
.get_key_level = GetButtonALevel
110+
btn_a_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
111+
btn_a_driver_->enable_power_save = false;
112+
btn_a_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
113+
return instance_->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_2);
119114
};
120-
ESP_ERROR_CHECK(iot_button_create(&btn_a_config, &btn_a_driver, &btn_a));
115+
ESP_ERROR_CHECK(iot_button_create(&btn_a_config, btn_a_driver_, &btn_a));
121116
iot_button_register_cb(btn_a, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
122117
auto self = static_cast<Df_K10Board*>(usr_data);
123118
auto& app = Application::GetInstance();
@@ -142,11 +137,12 @@ class Df_K10Board : public WifiBoard {
142137
.long_press_time = 1000,
143138
.short_press_time = 0
144139
};
145-
button_driver_t btn_b_driver = {
146-
.enable_power_save = false,
147-
.get_key_level = GetButtonBLevel
140+
btn_b_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
141+
btn_b_driver_->enable_power_save = false;
142+
btn_b_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
143+
return instance_->IoExpanderGetLevel(IO_EXPANDER_PIN_NUM_12);
148144
};
149-
ESP_ERROR_CHECK(iot_button_create(&btn_b_config, &btn_b_driver, &btn_b));
145+
ESP_ERROR_CHECK(iot_button_create(&btn_b_config, btn_b_driver_, &btn_b));
150146
iot_button_register_cb(btn_b, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
151147
auto self = static_cast<Df_K10Board*>(usr_data);
152148
auto& app = Application::GetInstance();

main/boards/esp32-s3-touch-lcd-1.46/esp32-s3-touch-lcd-1.46.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,10 @@ class CustomBoard : public WifiBoard {
6565
esp_io_expander_handle_t io_expander = NULL;
6666
LcdDisplay* display_;
6767
button_handle_t boot_btn, pwr_btn;
68+
button_driver_t* boot_btn_driver_ = nullptr;
69+
button_driver_t* pwr_btn_driver_ = nullptr;
6870
static CustomBoard* instance_;
6971

70-
static uint8_t GetBootButtonLevel(button_driver_t *button_driver) {
71-
return gpio_get_level(BOOT_BUTTON_GPIO);
72-
}
73-
74-
static uint8_t GetPwrButtonLevel(button_driver_t *button_driver) {
75-
return gpio_get_level(PWR_BUTTON_GPIO);
76-
}
7772

7873
void InitializeI2c() {
7974
// Initialize I2C peripheral
@@ -175,11 +170,12 @@ class CustomBoard : public WifiBoard {
175170
.long_press_time = 2000,
176171
.short_press_time = 0
177172
};
178-
button_driver_t boot_btn_driver = {
179-
.enable_power_save = false,
180-
.get_key_level = GetBootButtonLevel
173+
boot_btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
174+
boot_btn_driver_->enable_power_save = false;
175+
boot_btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
176+
return gpio_get_level(BOOT_BUTTON_GPIO);
181177
};
182-
ESP_ERROR_CHECK(iot_button_create(&boot_btn_config, &boot_btn_driver, &boot_btn));
178+
ESP_ERROR_CHECK(iot_button_create(&boot_btn_config, boot_btn_driver_, &boot_btn));
183179
iot_button_register_cb(boot_btn, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
184180
auto self = static_cast<CustomBoard*>(usr_data);
185181
auto& app = Application::GetInstance();
@@ -197,11 +193,12 @@ class CustomBoard : public WifiBoard {
197193
.long_press_time = 5000,
198194
.short_press_time = 0
199195
};
200-
button_driver_t pwr_btn_driver = {
201-
.enable_power_save = false,
202-
.get_key_level = GetPwrButtonLevel
196+
pwr_btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
197+
pwr_btn_driver_->enable_power_save = false;
198+
pwr_btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
199+
return gpio_get_level(PWR_BUTTON_GPIO);
203200
};
204-
ESP_ERROR_CHECK(iot_button_create(&pwr_btn_config, &pwr_btn_driver, &pwr_btn));
201+
ESP_ERROR_CHECK(iot_button_create(&pwr_btn_config, pwr_btn_driver_, &pwr_btn));
205202
iot_button_register_cb(pwr_btn, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
206203
// 短按无处理
207204
}, this);

main/boards/esp32-s3-touch-lcd-1.85/esp32-s3-touch-lcd-1.85.cc

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,10 @@ class CustomBoard : public WifiBoard {
220220
esp_io_expander_handle_t io_expander = NULL;
221221
LcdDisplay* display_;
222222
button_handle_t boot_btn, pwr_btn;
223+
button_driver_t* boot_btn_driver_ = nullptr;
224+
button_driver_t* pwr_btn_driver_ = nullptr;
223225
static CustomBoard* instance_;
224226

225-
static uint8_t GetBootButtonLevel(button_driver_t *button_driver) {
226-
return gpio_get_level(BOOT_BUTTON_GPIO);
227-
}
228-
229-
static uint8_t GetPwrButtonLevel(button_driver_t *button_driver) {
230-
return gpio_get_level(PWR_BUTTON_GPIO);
231-
}
232227

233228
void InitializeI2c() {
234229
// Initialize I2C peripheral
@@ -391,11 +386,12 @@ class CustomBoard : public WifiBoard {
391386
.long_press_time = 2000,
392387
.short_press_time = 0
393388
};
394-
button_driver_t boot_btn_driver = {
395-
.enable_power_save = false,
396-
.get_key_level = GetBootButtonLevel
389+
boot_btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
390+
boot_btn_driver_->enable_power_save = false;
391+
boot_btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
392+
return gpio_get_level(BOOT_BUTTON_GPIO);
397393
};
398-
ESP_ERROR_CHECK(iot_button_create(&boot_btn_config, &boot_btn_driver, &boot_btn));
394+
ESP_ERROR_CHECK(iot_button_create(&boot_btn_config, boot_btn_driver_, &boot_btn));
399395
iot_button_register_cb(boot_btn, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
400396
auto self = static_cast<CustomBoard*>(usr_data);
401397
auto& app = Application::GetInstance();
@@ -410,11 +406,12 @@ class CustomBoard : public WifiBoard {
410406
.long_press_time = 5000,
411407
.short_press_time = 0
412408
};
413-
button_driver_t pwr_btn_driver = {
414-
.enable_power_save = false,
415-
.get_key_level = GetPwrButtonLevel
409+
pwr_btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
410+
pwr_btn_driver_->enable_power_save = false;
411+
pwr_btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
412+
return gpio_get_level(PWR_BUTTON_GPIO);
416413
};
417-
ESP_ERROR_CHECK(iot_button_create(&pwr_btn_config, &pwr_btn_driver, &pwr_btn));
414+
ESP_ERROR_CHECK(iot_button_create(&pwr_btn_config, pwr_btn_driver_, &pwr_btn));
418415
iot_button_register_cb(pwr_btn, BUTTON_LONG_PRESS_START, nullptr, [](void* button_handle, void* usr_data) {
419416
auto self = static_cast<CustomBoard*>(usr_data);
420417
if(self->GetBacklight()->brightness() > 0) {

main/boards/sensecap-watcher/sensecap_watcher.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class SensecapWatcher : public WifiBoard {
9595
esp_lcd_panel_io_handle_t panel_io_ = nullptr;
9696
esp_lcd_panel_handle_t panel_ = nullptr;
9797
uint32_t long_press_cnt_;
98+
button_driver_t* btn_driver_ = nullptr;
9899
static SensecapWatcher* instance_;
99100

100101
void InitializePowerSaveTimer() {
@@ -230,11 +231,6 @@ class SensecapWatcher : public WifiBoard {
230231
ESP_LOGI(TAG, "Knob initialized with pins A:%d B:%d", BSP_KNOB_A_PIN, BSP_KNOB_B_PIN);
231232
}
232233

233-
// 添加一个静态成员实例指针,用于按钮回调
234-
static uint8_t GetKnobButtonLevel(button_driver_t *button_driver) {
235-
return instance_->IoExpanderGetLevel(BSP_KNOB_BTN);
236-
}
237-
238234
void InitializeButton() {
239235
// 设置静态实例指针
240236
instance_ = this;
@@ -249,12 +245,13 @@ class SensecapWatcher : public WifiBoard {
249245
.long_press_time = 2000,
250246
.short_press_time = 0
251247
};
252-
button_driver_t btn_driver = {
253-
.enable_power_save = false,
254-
.get_key_level = GetKnobButtonLevel
248+
btn_driver_ = (button_driver_t*)calloc(1, sizeof(button_driver_t));
249+
btn_driver_->enable_power_save = false;
250+
btn_driver_->get_key_level = [](button_driver_t *button_driver) -> uint8_t {
251+
return !instance_->IoExpanderGetLevel(BSP_KNOB_BTN);
255252
};
256253

257-
ESP_ERROR_CHECK(iot_button_create(&btn_config, &btn_driver, &btns));
254+
ESP_ERROR_CHECK(iot_button_create(&btn_config, btn_driver_, &btns));
258255

259256
iot_button_register_cb(btns, BUTTON_SINGLE_CLICK, nullptr, [](void* button_handle, void* usr_data) {
260257
auto self = static_cast<SensecapWatcher*>(usr_data);

0 commit comments

Comments
 (0)