From a8c9931257dcb995960db6e0d815d50f367497d1 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 18 Mar 2018 12:39:19 +0000 Subject: [PATCH] Add a function to retrieve a button by index --- examples/dev/button-hal/button-hal-example.c | 2 +- os/dev/button-hal.c | 10 ++++++++++ os/dev/button-hal.h | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/examples/dev/button-hal/button-hal-example.c b/examples/dev/button-hal/button-hal-example.c index f4919f19e..b4d584e3e 100644 --- a/examples/dev/button-hal/button-hal-example.c +++ b/examples/dev/button-hal/button-hal-example.c @@ -43,7 +43,7 @@ PROCESS_THREAD(button_hal_example, ev, data) PROCESS_BEGIN(); - btn = button_hal_get_by_id(BUTTON_HAL_ID_BUTTON_ZERO); + btn = button_hal_get_by_index(0); printf("Button HAL example.\n"); printf("Device button count: %u.\n", button_hal_button_count); diff --git a/os/dev/button-hal.c b/os/dev/button-hal.c index 789100324..58cded790 100644 --- a/os/dev/button-hal.c +++ b/os/dev/button-hal.c @@ -141,6 +141,16 @@ button_hal_get_by_id(uint8_t unique_id) return NULL; } /*---------------------------------------------------------------------------*/ +button_hal_button_t * +button_hal_get_by_index(uint8_t index) +{ + if(index >= button_hal_button_count) { + return NULL; + } + + return button_hal_buttons[index]; +} +/*---------------------------------------------------------------------------*/ uint8_t button_hal_get_state(button_hal_button_t *button) { diff --git a/os/dev/button-hal.h b/os/dev/button-hal.h index 4f35437ed..c1822ff8e 100644 --- a/os/dev/button-hal.h +++ b/os/dev/button-hal.h @@ -247,6 +247,13 @@ void button_hal_init(void); */ button_hal_button_t *button_hal_get_by_id(uint8_t unique_id); +/** + * \brief Retrieve a button by its index + * \param index The button's index (0, 1, ... button_hal_button_count - 1) + * \return A pointer to the button or NULL if not found + */ +button_hal_button_t *button_hal_get_by_index(uint8_t index); + /** * \brief Get the state of a button (pressed / released) * \param button A pointer to the button