button: rename usr_data to hardware_data && allow user to pass arbitrary data through callback. Closes https://github.com/espressif/esp-iot-solution/pull/200
This commit is contained in:
parent
4e481f0ee6
commit
8aefe2dea3
3 changed files with 14 additions and 11 deletions
|
|
@ -1,4 +1,4 @@
|
|||
version: "2.0.1"
|
||||
version: "2.1.0"
|
||||
description: GPIO and ADC button driver
|
||||
url: https://github.com/espressif/esp-iot-solution/tree/master/components/button
|
||||
dependencies:
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void (* button_cb_t)(void *);
|
||||
typedef void (* button_cb_t)(void *, void *);
|
||||
typedef void *button_handle_t;
|
||||
|
||||
/**
|
||||
|
|
@ -87,12 +87,13 @@ esp_err_t iot_button_delete(button_handle_t btn_handle);
|
|||
* @param btn_handle A button handle to register
|
||||
* @param event Button event
|
||||
* @param cb Callback function.
|
||||
* @param usr_data user data
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_INVALID_ARG Arguments is invalid.
|
||||
*/
|
||||
esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb);
|
||||
esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb, void *usr_data);
|
||||
|
||||
/**
|
||||
* @brief Unregister the button event callback function.
|
||||
|
|
|
|||
|
|
@ -39,7 +39,8 @@ typedef struct Button {
|
|||
uint8_t debounce_cnt: 3;
|
||||
uint8_t active_level: 1;
|
||||
uint8_t button_level: 1;
|
||||
uint8_t (*hal_button_Level)(void *usr_data);
|
||||
uint8_t (*hal_button_Level)(void *hardware_data);
|
||||
void *hardware_data;
|
||||
void *usr_data;
|
||||
button_type_t type;
|
||||
button_cb_t cb[BUTTON_EVENT_MAX];
|
||||
|
|
@ -56,14 +57,14 @@ static bool g_is_timer_running = false;
|
|||
#define SHORT_TICKS (CONFIG_BUTTON_SHORT_PRESS_TIME_MS /TICKS_INTERVAL)
|
||||
#define LONG_TICKS (CONFIG_BUTTON_LONG_PRESS_TIME_MS /TICKS_INTERVAL)
|
||||
|
||||
#define CALL_EVENT_CB(ev) if(btn->cb[ev])btn->cb[ev](btn)
|
||||
#define CALL_EVENT_CB(ev) if(btn->cb[ev])btn->cb[ev](btn, btn->usr_data)
|
||||
|
||||
/**
|
||||
* @brief Button driver core function, driver state machine.
|
||||
*/
|
||||
static void button_handler(button_dev_t *btn)
|
||||
{
|
||||
uint8_t read_gpio_level = btn->hal_button_Level(btn->usr_data);
|
||||
uint8_t read_gpio_level = btn->hal_button_Level(btn->hardware_data);
|
||||
|
||||
/** ticks counter working.. */
|
||||
if ((btn->state) > 0) {
|
||||
|
|
@ -163,13 +164,13 @@ static void button_cb(void *args)
|
|||
}
|
||||
}
|
||||
|
||||
static button_dev_t *button_create_com(uint8_t active_level, uint8_t (*hal_get_key_state)(void *usr_data), void *usr_data)
|
||||
static button_dev_t *button_create_com(uint8_t active_level, uint8_t (*hal_get_key_state)(void *hardware_data), void *hardware_data)
|
||||
{
|
||||
BTN_CHECK(NULL != hal_get_key_state, "Function pointer is invalid", NULL);
|
||||
|
||||
button_dev_t *btn = (button_dev_t *) calloc(1, sizeof(button_dev_t));
|
||||
BTN_CHECK(NULL != btn, "Button memory alloc failed", NULL);
|
||||
btn->usr_data = usr_data;
|
||||
btn->hardware_data = hardware_data;
|
||||
btn->event = BUTTON_NONE_PRESS;
|
||||
btn->active_level = active_level;
|
||||
btn->hal_button_Level = hal_get_key_state;
|
||||
|
|
@ -259,10 +260,10 @@ esp_err_t iot_button_delete(button_handle_t btn_handle)
|
|||
button_dev_t *btn = (button_dev_t *)btn_handle;
|
||||
switch (btn->type) {
|
||||
case BUTTON_TYPE_GPIO:
|
||||
ret = button_gpio_deinit((int)(btn->usr_data));
|
||||
ret = button_gpio_deinit((int)(btn->hardware_data));
|
||||
break;
|
||||
case BUTTON_TYPE_ADC:
|
||||
ret = button_adc_deinit(ADC_BUTTON_SPLIT_CHANNEL(btn->usr_data), ADC_BUTTON_SPLIT_INDEX(btn->usr_data));
|
||||
ret = button_adc_deinit(ADC_BUTTON_SPLIT_CHANNEL(btn->hardware_data), ADC_BUTTON_SPLIT_INDEX(btn->hardware_data));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
@ -272,12 +273,13 @@ esp_err_t iot_button_delete(button_handle_t btn_handle)
|
|||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb)
|
||||
esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb, void *usr_data)
|
||||
{
|
||||
BTN_CHECK(NULL != btn_handle, "Pointer of handle is invalid", ESP_ERR_INVALID_ARG);
|
||||
BTN_CHECK(event < BUTTON_EVENT_MAX, "event is invalid", ESP_ERR_INVALID_ARG);
|
||||
button_dev_t *btn = (button_dev_t *) btn_handle;
|
||||
btn->cb[event] = cb;
|
||||
btn->usr_data = usr_data;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue