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

nRF52840 DK, implementing Board Button event handler, but no response on button press.

Hi, 

I am trying to do some tasks based on a particular button press, below attached is the buttons_init() function and the button_evt_handler() function. Code compiles and runs successfully, but nothing happens on button press. Please let me know if I am missing out on something. 

Thanks in advance.

static void buttons_init(void)
{
    uint32_t err_code;

    static app_button_cfg_t buttons[] = 
    { 
        {BSP_BUTTON_0, false, BUTTON_PULL, button_evt_handler},
        {BSP_BUTTON_1, false, BUTTON_PULL, button_evt_handler}
    };

    err_code = app_button_init(buttons, ARRAY_SIZE(buttons), BUTTON_DEBOUNCE_DELAY);
    APP_ERROR_CHECK(err_code);

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
}    

static void button_evt_handler(uint8_t pin_no, uint8_t button_action)
{
    if(button_action == APP_BUTTON_PUSH)
    {
        switch(pin_no)
        {
            case BSP_BUTTON_0:
                NRF_LOG_INFO("Sending blood pressure data\r\n");
                break;
            case BSP_BUTTON_1:
                NRF_LOG_INFO("Sending temperature data\r\n");
                break;
            default:
                NRF_LOG_INFO("Default case\r\n");
                break;
        }
        
    }
}

  • Hi ,

    Here is a better way to do it!

    #define UART_PRINTING_ENABLED   // comment it to disable the uart logging
    
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
      ret_code_t err_code;
    
      switch(pin_no)
      {
       case BUTTON_1:
         if(button_action == APP_BUTTON_PUSH) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button1 pressed.");
            #endif
         }
         else if(button_action == APP_BUTTON_RELEASE) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button1 released.");
            #endif
         }
         break;
    
       case BUTTON_2:
         if(button_action == APP_BUTTON_PUSH) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button2 pressed.");
            #endif
         }
         else if(button_action == APP_BUTTON_RELEASE) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button2 released.");
            #endif
         }
         break;
    
       case BUTTON_3:
         if(button_action == APP_BUTTON_PUSH) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button3 pressed.");
            #endif
         }
         else if(button_action == APP_BUTTON_RELEASE) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button3 released.");
            #endif
         }
         break;
    
       case BUTTON_4:
         if(button_action == APP_BUTTON_PUSH) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button4 pressed.");
            #endif
         }
         else if(button_action == APP_BUTTON_RELEASE) {
            #ifdef UART_PRINTING_ENABLED
            NRF_LOG_INFO("button4 released.");
            #endif
         }
         break;
         default:
            return; // no implementation needed
      }
    
    }
    
    /**@brief Function for buttons configuration.
     *
     */
    static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] = 
    {
    // BUTTONS_NUMBER,
    // BUTTON_PIN,
    // BUTTONS_ACTIVE_STATE,
    // BUTTON_PULL,
    // are all declared in your components/boards/Ppca10056.h file
    
        {BUTTON_1, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler},
        {BUTTON_2, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler},
        {BUTTON_3, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler},
        {BUTTON_4, BUTTONS_ACTIVE_STATE, BUTTON_PULL, button_event_handler}
    }; 
    
    /**@brief Function for initializing the buttons with the app_button library.
     *
     */
     void buttons_init(){
    
        ret_code_t  err_code;                        
    // BUTTONS_NUMBER  is declared in your components/boards/Ppca10056.h file
        err_code = app_button_init((app_button_cfg_t *)app_buttons,
                                                    BUTTONS_NUMBER,
                                             BUTTON_DETECTION_TIME);
        APP_ERROR_CHECK(err_code);
    
        err_code = app_button_enable();
        APP_ERROR_CHECK(err_code);
    
    }

    Make sure you initialize the timer before calling the buttons_init() in the main.

    /**
     *@brief Function for initialising the timers. 
     * It is necessary for the buttons to work when using the app_button library. 
     */
    static void timers_init(void)
    {
        // Initialize timer module.
        ret_code_t err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    }

        timers_init();
        leds_init();
        buttons_init();

    Also make sure to enable the app button module in the sdk_config.h file.

    // <q> BUTTON_ENABLED  - Enables Button module
     
    
    #ifndef BUTTON_ENABLED
    #define BUTTON_ENABLED 1
    #endif

    And here is 4 key keyboard based off the nRF52840-DK. You can test it, or just re-use the button handling part for your case.

    The github repos

    Best regards,

    rmptxf

  • Hi, 

    Thanks for your response, I tried to implement it using the code snippets you have provided. But still no response on button press. I have tried the following:

    1. Changed the button_event_handler as per the above.

    2. Changed the app_button_cfg_t structure to as defined above

    3. Called timer_init() before buttons_init()

    4. BUTTON_ENABLED was already set. 

    Also, as a footnote, I am implementing this button handler along with a bsp_event_handler already defined, would it be affecting this handler or are they completely independent? Please let me know.

    Thanks again,

    Paarth

  • Hi,

    How do you check whether a button is responding or not ? I mean, in the code snippet you provided, you're using the nRF logging. Is the uart logging already working ?

    What bsp events the bsp_event_handler is used for ?  for handling the buttons events too ?

    regards,

    rmptxf

  • Hi, 

    The bsp_event_handler is handling BSP_EVENT_SLEEP,  BSP_EVENT_DISCONNECT and BSP_EVENT_WHITELIST_OFF.

    ou're using the nRF logging. Is the uart logging already working ?

    I am only using the nRF logs for debugging. 

    Thanks again, 

    Paarth

  • Hi,

    Would you mind sending your project files here, or email them. To look into the issue ?

    Best regards,

Related