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

button push and release events

Hi,

to initialize  the button properly

to trigger the push event on button push and the release event on button release, do I just have to change the button active state, if not how can i control it !?

/**@brief Button configuration structure. */
typedef struct
{
uint8_t pin_no; /**< Pin to be used as a button. */
uint8_t active_state; /**< APP_BUTTON_ACTIVE_HIGH or APP_BUTTON_ACTIVE_LOW. */
#if defined(BUTTON_HIGH_ACCURACY_ENABLED) && (BUTTON_HIGH_ACCURACY_ENABLED == 1)
bool hi_accuracy; /**< True if GPIOTE high accuracy (IN_EVENT) is used. */
#endif
nrf_gpio_pin_pull_t pull_cfg; /**< Pull-up or -down configuration. */
app_button_handler_t button_handler; /**< Handler to be called when button is pushed. */
} app_button_cfg_t;

when the button push switch the analog input from 0 to 1 have to be initialized as follows : 

#ifdef BSP_BUTTON_0
{BSP_BUTTON_0, true, BUTTON_PULL, bsp_button_event_handler},
#endif // BUTTON_0

and when the button push switch the analog input from 1 to 0 have to be initialized as follows : 

#ifdef BSP_BUTTON_0
{BSP_BUTTON_0, false, BUTTON_PULL, bsp_button_event_handler},
#endif // BUTTON_0

  • Hi,

     

    BSP depends on a module named app_button, and BSP does not send the PUSH and RELEASE event, but app_button does.

    I would recommend that you, instead of using BSP, implement app_button directly in your application. Here's an example:

    static void button_evt_handler(uint8_t pin_no, uint8_t button_action)
    {
        switch (pin_no)
        {
            case MY_BUTTON:
            {
                if (button_action == APP_BUTTON_PUSH)
                {
                    /* Button pushed */
                    do_something();
                }
                else 
                {
                    /* APP_BUTTON_RELEASE */
                    do_something_else();
                }
            }
            break;
            default:
            break;
        }
    }
    
    static void buttons_init(void)
    {
       // The array must be static because a pointer to it will be saved in the button library.
        static app_button_cfg_t buttons[] =
        {
            {MY_BUTTON, true, BUTTON_PULL, button_evt_handler},        
        };
    
        ret_code_t err_code = app_button_init(buttons, ARRAY_SIZE(buttons), BUTTON_DETECTION_DELAY);
        APP_ERROR_CHECK(err_code);
        
        /* You can call this other places as well, for instance when you get BLE_GAP_EVT_CONNECTED */
        err_code = app_button_enable();
        APP_ERROR_CHECK(err_code);
    }

     

    Kind regards,

    Håkon

  • Hi Hakan,

    thanks for your reply.

    I will ask my question in other way

    when i configure my button as 

    {MY_BUTTON, true, BUTTON_PULL, button_evt_handler},  ?

    and when I configure it as :

    {MY_BUTTON, false, BUTTON_PULL, button_evt_handler}, ?

  • What you are setting there is the sense of the button:

    #define APP_BUTTON_ACTIVE_HIGH 1                               /**< Indicates that a button is active high. */
    #define APP_BUTTON_ACTIVE_LOW  0                               /**< Indicates that a button is active low. */

     

    If your button is active low, set it to "false". If active high, set it to "true".

     

    To address your question: The app_button library will provide an event when button is pushed, and when it is released. No need to do anything else than what is provided underneath.

     

    Kind regards,

    Håkon

Related