1
1
/*
2
- * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2
+ * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
17
17
#include "esp_vfs_fat.h"
18
18
19
19
#include "iot_button.h"
20
+ #include "button_gpio.h"
20
21
#include "bsp/esp-box-3.h"
21
22
#include "bsp/display.h"
22
23
#include "bsp/touch.h"
@@ -71,27 +72,44 @@ static i2c_master_bus_handle_t i2c_handle = NULL;
71
72
static bool i2c_initialized = false;
72
73
73
74
// This is just a wrapper to get function signature for espressif/button API callback
74
- static uint8_t bsp_get_main_button (void * param );
75
- static esp_err_t bsp_init_main_button (void * param );
76
-
77
- static const button_config_t bsp_button_config [BSP_BUTTON_NUM ] = {
75
+ static uint8_t bsp_get_main_button (button_driver_t * button_driver );
76
+
77
+ typedef enum {
78
+ BSP_BUTTON_TYPE_GPIO ,
79
+ BSP_BUTTON_TYPE_CUSTOM
80
+ } bsp_button_type_t ;
81
+
82
+ typedef struct {
83
+ bsp_button_type_t type ;
84
+ union {
85
+ button_gpio_config_t gpio ;
86
+ button_driver_t custom ;
87
+ } cfg ;
88
+ } bsp_button_config_t ;
89
+
90
+ static const bsp_button_config_t bsp_button_config [BSP_BUTTON_NUM ] = {
78
91
{
79
- .type = BUTTON_TYPE_GPIO ,
80
- .gpio_button_config .gpio_num = BSP_BUTTON_CONFIG_IO ,
81
- .gpio_button_config .active_level = 0 ,
92
+ .type = BSP_BUTTON_TYPE_GPIO ,
93
+ .cfg .gpio = {
94
+ .gpio_num = BSP_BUTTON_CONFIG_IO ,
95
+ .active_level = 0 ,
96
+ }
82
97
},
83
98
{
84
- .type = BUTTON_TYPE_GPIO ,
85
- .gpio_button_config .gpio_num = BSP_BUTTON_MUTE_IO ,
86
- .gpio_button_config .active_level = 0 ,
99
+ .type = BSP_BUTTON_TYPE_GPIO ,
100
+ .cfg .gpio = {
101
+ .gpio_num = BSP_BUTTON_MUTE_IO ,
102
+ .active_level = 0 ,
103
+ }
87
104
},
88
105
{
89
- .type = BUTTON_TYPE_CUSTOM ,
90
- .custom_button_config .button_custom_init = bsp_init_main_button ,
91
- .custom_button_config .button_custom_get_key_value = bsp_get_main_button ,
92
- .custom_button_config .button_custom_deinit = NULL ,
93
- .custom_button_config .active_level = 1 ,
94
- .custom_button_config .priv = (void * ) BSP_BUTTON_MAIN ,
106
+ .type = BSP_BUTTON_TYPE_CUSTOM ,
107
+ .cfg .custom = {
108
+ .enable_power_save = false,
109
+ .get_key_level = bsp_get_main_button ,
110
+ .enter_power_save = NULL ,
111
+ .del = NULL
112
+ }
95
113
}
96
114
};
97
115
@@ -624,7 +642,7 @@ esp_err_t bsp_display_exit_sleep(void)
624
642
return ESP_OK ;
625
643
}
626
644
627
- static uint8_t bsp_get_main_button (void * param )
645
+ static uint8_t bsp_get_main_button (button_driver_t * button_driver )
628
646
{
629
647
assert (tp );
630
648
#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0 )
@@ -637,17 +655,10 @@ static uint8_t bsp_get_main_button(void *param)
637
655
#endif
638
656
}
639
657
640
- static esp_err_t bsp_init_main_button (void * param )
641
- {
642
- if (tp == NULL ) {
643
- BSP_ERROR_CHECK_RETURN_ERR (bsp_touch_new (NULL , & tp ));
644
- }
645
- return ESP_OK ;
646
- }
647
-
648
658
esp_err_t bsp_iot_button_create (button_handle_t btn_array [], int * btn_cnt , int btn_array_size )
649
659
{
650
660
esp_err_t ret = ESP_OK ;
661
+ const button_config_t btn_config = {0 };
651
662
if ((btn_array_size < BSP_BUTTON_NUM ) ||
652
663
(btn_array == NULL )) {
653
664
return ESP_ERR_INVALID_ARG ;
@@ -657,10 +668,15 @@ esp_err_t bsp_iot_button_create(button_handle_t btn_array[], int *btn_cnt, int b
657
668
* btn_cnt = 0 ;
658
669
}
659
670
for (int i = 0 ; i < BSP_BUTTON_NUM ; i ++ ) {
660
- btn_array [i ] = iot_button_create (& bsp_button_config [i ]);
661
- if (btn_array [i ] == NULL ) {
662
- ret = ESP_FAIL ;
663
- break ;
671
+ if (bsp_button_config [i ].type == BSP_BUTTON_TYPE_CUSTOM ) {
672
+ if (tp == NULL ) {
673
+ BSP_ERROR_CHECK_RETURN_ERR (bsp_touch_new (NULL , & tp ));
674
+ }
675
+ ret |= iot_button_create (& btn_config , & bsp_button_config [i ].cfg .custom , & btn_array [i ]);
676
+ } else if (bsp_button_config [i ].type == BSP_BUTTON_TYPE_GPIO ) {
677
+ ret |= iot_button_new_gpio_device (& btn_config , & bsp_button_config [i ].cfg .gpio , & btn_array [i ]);
678
+ } else {
679
+ ESP_LOGW (TAG , "Unsupported button type!" );
664
680
}
665
681
if (btn_cnt ) {
666
682
(* btn_cnt )++ ;
0 commit comments