This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Using app_buttons with BSP buttons PLUS own buttons?

I am writing an application for the nRF52DK which uses two additional push buttons besides the 4 already present on the board.

Using GPIOTE the detection of the button press for the 2 new buttons (connected to pin 22 and 23) works as desired, however the buttons bounce a lot so I would need to write a debounce routine.

Browsing the devzone I saw that there is a library called app_buttons which is based on GPIOTE and seems to handle all of this automatically. However I could not clearly see if this library was ONLY for the existing buttons or whether I could just add some more pins/buttons to it.

As I didn't want to change SDK files, and especially not pca10040.h, bps.c, boards.c, etc. I copied the whole stuff in a new "bsp_and_custom_buttons.h/.c" and made the following dirty hack changes:

#ifdef BUTTONS_NUMBER
#define BUTTONS_NUMBER 6

#define BUTTON_5       22
#define BUTTON_6       23
#define TRAINER_BUTTON_INCLINE_UP BUTTON_5
#define TRAINER_BUTTON_INCLINE_DOWN BUTTON_6
#define BSP_BUTTON_5  BUTTON_5
#define BSP_BUTTON_6  BUTTON_6
#define BUTTON_STOP 23
#endif
..
static void incline_button_event_handler(uint8_t pin_no, uint8_t button_action);
..
#ifndef BSP_SIMPLE
static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
{
    #ifdef BSP_BUTTON_0
    {BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},
    #endif // BUTTON_0

    #ifdef BSP_BUTTON_1
    {BSP_BUTTON_1, false, BUTTON_PULL, bsp_button_event_handler},
    #endif // BUTTON_1

    #ifdef BSP_BUTTON_2
    {BSP_BUTTON_2, false, BUTTON_PULL, bsp_button_event_handler},
    #endif // BUTTON_2

    #ifdef BSP_BUTTON_3
    {BSP_BUTTON_3, false, BUTTON_PULL, bsp_button_event_handler},
    #endif // BUTTON_3

    #ifdef TRAINER_BUTTON_INCLINE_UP
    {BSP_BUTTON_5, false, BUTTON_PULL, incline_button_event_handler},
    #endif

    #ifdef TRAINER_BUTTON_INCLINE_DOWN  
    {BSP_BUTTON_6, false, BUTTON_PULL, incline_button_event_handler},
    #endif
...
static void incline_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
	
	if(app_button_is_pushed(TRAINER_BUTTON_INCLINE_UP) == true)
	{
                        if (incline_level < 20)
                        {
                            incline_level++;
                        }
			NRF_LOG_INFO("Button TRAINER_BUTTON_INCLINE_UP got pressed! New incline: %d", incline_level);
			//nrf_delay_ms(500);
			
	}
  //code run on button state change
        if(app_button_is_pushed(TRAINER_BUTTON_INCLINE_DOWN) == true)
        {
                if (incline_level > 10)
                        {
                            incline_level--;
                        }
                NRF_LOG_INFO("Button TRAINER_BUTTON_INCLINE_DOWN got pressed! New incline: %d", incline_level);

        }
	
}
...

My button definition:

#ifdef BSP_BUTTON_5
{BSP_BUTTON_5, false, BUTTON_PULL, incline_button_event_handler},
#endif // BUTTON_5

#ifdef BSP_BUTTON_6
{BSP_BUTTON_6, false, BUTTON_PULL, incline_button_event_handler},
#endif // BUTTON_6

My event handler:

static void incline_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
	
	if(app_button_is_pushed(4) == true)
	{
                        if (incline_level < 20)
                        {
                            incline_level++;
                        }
			NRF_LOG_INFO("Button TRAINER_BUTTON_INCLINE_UP got pressed! New incline: %d", incline_level);
			//nrf_delay_ms(500);
			
	}
  //code run on button state change
        if(app_button_is_pushed(5) == true)
        {
                if (incline_level > 10)
                        {
                            incline_level--;
                        }
                NRF_LOG_INFO("Button TRAINER_BUTTON_INCLINE_DOWN got pressed! New incline: %d", incline_level);

        }
	
}

In the end it actually WORKED but this feels so ugly as I basically have to overwrite #defines from the SDK BSP in order to get my extra buttons "into the game". 

However I didn't see any other chance in order to keep the existing BSP code for the buttons, LEDs, etc and getting my stuff in.

I'm pretty sure this is not the way it is meant to be?

Am I supposed to use the app_button library for arbitrary pins besides the existing 4 buttons on the DK52?

Is there a best practice how to do it?

Or should I better keep my hands of here and use the GPIOTE stuff directly but need to implement an debounce handler then myself?

Related