diff --git a/components/button/button_adc.c b/components/button/button_adc.c index 5bca066f5..b009761cf 100644 --- a/components/button/button_adc.c +++ b/components/button/button_adc.c @@ -252,6 +252,12 @@ uint8_t button_adc_get_key_level(button_driver_t *button_driver) return BUTTON_INACTIVE; } +static uint32_t button_adc_get_channel(button_driver_t *button_driver) +{ + button_adc_obj *adc_btn = __containerof(button_driver, button_adc_obj, base); + return adc_btn->ch; +} + esp_err_t iot_button_new_adc_device(const button_config_t *button_config, const button_adc_config_t *adc_config, button_handle_t *ret_button) { esp_err_t ret = ESP_OK; @@ -314,6 +320,7 @@ esp_err_t iot_button_new_adc_device(const button_config_t *button_config, const adc_btn->ch = adc_config->adc_channel; adc_btn->index = adc_config->button_index; adc_btn->base.get_key_level = button_adc_get_key_level; + adc_btn->base.get_hardware_data = button_adc_get_channel; adc_btn->base.del = button_adc_del; ret = iot_button_create(button_config, &adc_btn->base, ret_button); ESP_GOTO_ON_FALSE(ret == ESP_OK, ESP_FAIL, err, TAG, "Create button failed"); @@ -324,4 +331,4 @@ esp_err_t iot_button_new_adc_device(const button_config_t *button_config, const free(adc_btn); } return ret; -} +} \ No newline at end of file diff --git a/components/button/button_gpio.c b/components/button/button_gpio.c index 3f244d180..0462e5960 100644 --- a/components/button/button_gpio.c +++ b/components/button/button_gpio.c @@ -36,6 +36,12 @@ static uint8_t button_gpio_get_key_level(button_driver_t *button_driver) return level == gpio_btn->active_level ? 1 : 0; } +static int button_gpio_get_pin(button_driver_t *button_driver) +{ + button_gpio_obj *gpio_btn = __containerof(button_driver, button_gpio_obj, base); + return (int)gpio_btn->gpio_num; +} + static esp_err_t button_gpio_enable_gpio_wakeup(uint32_t gpio_num, uint8_t active_level, bool enable) { esp_err_t ret; @@ -135,6 +141,7 @@ esp_err_t iot_button_new_gpio_device(const button_config_t *button_config, const } gpio_btn->base.get_key_level = button_gpio_get_key_level; + gpio_btn->base.get_hardware_data = button_gpio_get_pin; gpio_btn->base.del = button_gpio_del; ret = iot_button_create(button_config, &gpio_btn->base, ret_button); diff --git a/components/button/include/iot_button.h b/components/button/include/iot_button.h index 96f81c5f6..e0d32af77 100644 --- a/components/button/include/iot_button.h +++ b/components/button/include/iot_button.h @@ -229,6 +229,16 @@ esp_err_t iot_button_set_param(button_handle_t btn_handle, button_param_t param, */ uint8_t iot_button_get_key_level(button_handle_t btn_handle); +/** @brief Get button hardware gpio pin or adc channel number. +* +* @param btn_handle Button handle +* +* @return Depends button type: +* - BUTTON_GPIO: pin number +* - BUTTON_ADC: channel number +*/ +int iot_button_get_hardware_data(button_handle_t btn_handle); + /** * @brief resume button timer, if button timer is stopped. Make sure iot_button_create() is called before calling this API. * diff --git a/components/button/interface/button_interface.h b/components/button/interface/button_interface.h index d61d3fd34..61630d12f 100644 --- a/components/button/interface/button_interface.h +++ b/components/button/interface/button_interface.h @@ -22,6 +22,9 @@ struct button_driver_t { /*!< (necessary) Get key level */ uint8_t (*get_key_level)(button_driver_t *button_driver); + /*!< (optional) Get hardware data */ + int (*get_hardware_data)(button_driver_t *button_driver); + /*!< (optional) Enter Power Save cb */ esp_err_t (*enter_power_save)(button_driver_t *button_driver); diff --git a/components/button/iot_button.c b/components/button/iot_button.c index 9175d3db3..e0517104e 100644 --- a/components/button/iot_button.c +++ b/components/button/iot_button.c @@ -591,6 +591,16 @@ uint8_t iot_button_get_key_level(button_handle_t btn_handle) return level; } +int iot_button_get_hardware_data(button_handle_t btn_handle) +{ + BTN_CHECK(NULL != btn_handle, "Pointer of handle is invalid", 0); + button_dev_t *btn = (button_dev_t *) btn_handle; + if (btn->driver->get_hardware_data) { + return btn->driver->get_hardware_data(btn->driver); + } + return -1; +} + esp_err_t iot_button_resume(void) { if (!g_button_timer_handle) {