Skip to content

Commit 63dac97

Browse files
committed
button: add get_hardware_data() to iot_button to retrieve adc channel or gpio pin from object
1 parent 0b6ecf8 commit 63dac97

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

components/button/button_adc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ uint8_t button_adc_get_key_level(button_driver_t *button_driver)
252252
return BUTTON_INACTIVE;
253253
}
254254

255+
static uint32_t button_adc_get_channel(button_driver_t *button_driver)
256+
{
257+
button_adc_obj *adc_btn = __containerof(button_driver, button_adc_obj, base);
258+
return adc_btn->ch;
259+
}
260+
255261
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)
256262
{
257263
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
314320
adc_btn->ch = adc_config->adc_channel;
315321
adc_btn->index = adc_config->button_index;
316322
adc_btn->base.get_key_level = button_adc_get_key_level;
323+
adc_btn->base.get_hardware_data = button_adc_get_channel;
317324
adc_btn->base.del = button_adc_del;
318325
ret = iot_button_create(button_config, &adc_btn->base, ret_button);
319326
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
324331
free(adc_btn);
325332
}
326333
return ret;
327-
}
334+
}

components/button/button_gpio.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ static uint8_t button_gpio_get_key_level(button_driver_t *button_driver)
3636
return level == gpio_btn->active_level ? 1 : 0;
3737
}
3838

39+
static int button_gpio_get_pin(button_driver_t *button_driver)
40+
{
41+
button_gpio_obj *gpio_btn = __containerof(button_driver, button_gpio_obj, base);
42+
return (int)gpio_btn->gpio_num;
43+
}
44+
3945
static esp_err_t button_gpio_enable_gpio_wakeup(uint32_t gpio_num, uint8_t active_level, bool enable)
4046
{
4147
esp_err_t ret;
@@ -135,6 +141,7 @@ esp_err_t iot_button_new_gpio_device(const button_config_t *button_config, const
135141
}
136142

137143
gpio_btn->base.get_key_level = button_gpio_get_key_level;
144+
gpio_btn->base.get_hardware_data = button_gpio_get_pin;
138145
gpio_btn->base.del = button_gpio_del;
139146

140147
ret = iot_button_create(button_config, &gpio_btn->base, ret_button);

components/button/include/iot_button.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ esp_err_t iot_button_set_param(button_handle_t btn_handle, button_param_t param,
229229
*/
230230
uint8_t iot_button_get_key_level(button_handle_t btn_handle);
231231

232+
/** @brief Get button hardware gpio pin or adc channel number.
233+
*
234+
* @param btn_handle Button handle
235+
*
236+
* @return Depends button type:
237+
* - BUTTON_GPIO: pin number
238+
* - BUTTON_ADC: channel number
239+
*/
240+
int iot_button_get_hardware_data(button_handle_t btn_handle);
241+
232242
/**
233243
* @brief resume button timer, if button timer is stopped. Make sure iot_button_create() is called before calling this API.
234244
*

components/button/interface/button_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ struct button_driver_t {
2222
/*!< (necessary) Get key level */
2323
uint8_t (*get_key_level)(button_driver_t *button_driver);
2424

25+
/*!< (optional) Get hardware data */
26+
int (*get_hardware_data)(button_driver_t *button_driver);
27+
2528
/*!< (optional) Enter Power Save cb */
2629
esp_err_t (*enter_power_save)(button_driver_t *button_driver);
2730

components/button/iot_button.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,16 @@ uint8_t iot_button_get_key_level(button_handle_t btn_handle)
591591
return level;
592592
}
593593

594+
int iot_button_get_hardware_data(button_handle_t btn_handle)
595+
{
596+
BTN_CHECK(NULL != btn_handle, "Pointer of handle is invalid", 0);
597+
button_dev_t *btn = (button_dev_t *) btn_handle;
598+
if (btn->driver->get_hardware_data) {
599+
return btn->driver->get_hardware_data(btn->driver);
600+
}
601+
return -1;
602+
}
603+
594604
esp_err_t iot_button_resume(void)
595605
{
596606
if (!g_button_timer_handle) {

0 commit comments

Comments
 (0)