1717#include "esp_vfs_fat.h"
1818
1919#include "iot_button.h"
20+ #include "button_gpio.h"
2021#include "bsp/esp-box-3.h"
2122#include "bsp/display.h"
2223#include "bsp/touch.h"
@@ -73,27 +74,44 @@ static i2c_master_bus_handle_t i2c_handle = NULL;
7374static bool i2c_initialized = false;
7475
7576// This is just a wrapper to get function signature for espressif/button API callback
76- static uint8_t bsp_get_main_button (void * param );
77- static esp_err_t bsp_init_main_button (void * param );
78-
79- static const button_config_t bsp_button_config [BSP_BUTTON_NUM ] = {
77+ static uint8_t bsp_get_main_button (button_driver_t * button_driver );
78+
79+ typedef enum {
80+ BSP_BUTTON_TYPE_GPIO ,
81+ BSP_BUTTON_TYPE_CUSTOM
82+ } bsp_button_type_t ;
83+
84+ typedef struct {
85+ bsp_button_type_t type ;
86+ union {
87+ button_gpio_config_t gpio ;
88+ button_driver_t custom ;
89+ } cfg ;
90+ } bsp_button_config_t ;
91+
92+ static const bsp_button_config_t bsp_button_config [BSP_BUTTON_NUM ] = {
8093 {
81- .type = BUTTON_TYPE_GPIO ,
82- .gpio_button_config .gpio_num = BSP_BUTTON_CONFIG_IO ,
83- .gpio_button_config .active_level = 0 ,
94+ .type = BSP_BUTTON_TYPE_GPIO ,
95+ .cfg .gpio = {
96+ .gpio_num = BSP_BUTTON_CONFIG_IO ,
97+ .active_level = 0 ,
98+ }
8499 },
85100 {
86- .type = BUTTON_TYPE_GPIO ,
87- .gpio_button_config .gpio_num = BSP_BUTTON_MUTE_IO ,
88- .gpio_button_config .active_level = 0 ,
101+ .type = BSP_BUTTON_TYPE_GPIO ,
102+ .cfg .gpio = {
103+ .gpio_num = BSP_BUTTON_MUTE_IO ,
104+ .active_level = 0 ,
105+ }
89106 },
90107 {
91- .type = BUTTON_TYPE_CUSTOM ,
92- .custom_button_config .button_custom_init = bsp_init_main_button ,
93- .custom_button_config .button_custom_get_key_value = bsp_get_main_button ,
94- .custom_button_config .button_custom_deinit = NULL ,
95- .custom_button_config .active_level = 1 ,
96- .custom_button_config .priv = (void * ) BSP_BUTTON_MAIN ,
108+ .type = BSP_BUTTON_TYPE_CUSTOM ,
109+ .cfg .custom = {
110+ .enable_power_save = false,
111+ .get_key_level = bsp_get_main_button ,
112+ .enter_power_save = NULL ,
113+ .del = NULL
114+ }
97115 }
98116};
99117
@@ -630,7 +648,7 @@ esp_err_t bsp_display_exit_sleep(void)
630648}
631649#endif // (BSP_CONFIG_NO_GRAPHIC_LIB == 0)
632650
633- static uint8_t bsp_get_main_button (void * param )
651+ static uint8_t bsp_get_main_button (button_driver_t * button_driver )
634652{
635653 assert (tp );
636654#if (CONFIG_ESP_LCD_TOUCH_MAX_BUTTONS > 0 )
@@ -643,17 +661,10 @@ static uint8_t bsp_get_main_button(void *param)
643661#endif
644662}
645663
646- static esp_err_t bsp_init_main_button (void * param )
647- {
648- if (tp == NULL ) {
649- BSP_ERROR_CHECK_RETURN_ERR (bsp_touch_new (NULL , & tp ));
650- }
651- return ESP_OK ;
652- }
653-
654664esp_err_t bsp_iot_button_create (button_handle_t btn_array [], int * btn_cnt , int btn_array_size )
655665{
656666 esp_err_t ret = ESP_OK ;
667+ const button_config_t btn_config = {0 };
657668 if ((btn_array_size < BSP_BUTTON_NUM ) ||
658669 (btn_array == NULL )) {
659670 return ESP_ERR_INVALID_ARG ;
@@ -663,10 +674,15 @@ esp_err_t bsp_iot_button_create(button_handle_t btn_array[], int *btn_cnt, int b
663674 * btn_cnt = 0 ;
664675 }
665676 for (int i = 0 ; i < BSP_BUTTON_NUM ; i ++ ) {
666- btn_array [i ] = iot_button_create (& bsp_button_config [i ]);
667- if (btn_array [i ] == NULL ) {
668- ret = ESP_FAIL ;
669- break ;
677+ if (bsp_button_config [i ].type == BSP_BUTTON_TYPE_CUSTOM ) {
678+ if (tp == NULL ) {
679+ BSP_ERROR_CHECK_RETURN_ERR (bsp_touch_new (NULL , & tp ));
680+ }
681+ ret |= iot_button_create (& btn_config , & bsp_button_config [i ].cfg .custom , & btn_array [i ]);
682+ } else if (bsp_button_config [i ].type == BSP_BUTTON_TYPE_GPIO ) {
683+ ret |= iot_button_new_gpio_device (& btn_config , & bsp_button_config [i ].cfg .gpio , & btn_array [i ]);
684+ } else {
685+ ESP_LOGW (TAG , "Unsupported button type!" );
670686 }
671687 if (btn_cnt ) {
672688 (* btn_cnt )++ ;
0 commit comments