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

Button long press functionality using nrf52840

I'm using nrf52840  and SDK VR 15.0.0

I want to apply the long press button functionality and short press button functionality to change the screens in OLED SSD1306.

How can I perform that?

Thank you!

  • can you explain the line 7 in your example code?

    why the BTN_ACTION_DISCONNECT is given instead of that we can give this one "BSP_BUTTON_ACTION_LONG_PUSH" will it work for long press?

  • #define OLED_BTN 2
    
    static void bsp_event_handler(bsp_event_t event)
    {
        ret_code_t err_code;
    
        switch (event)
        {
            case BSP_EVENT_SLEEP:
                sleep_mode_enter();
                break;
    
            case BSP_EVENT_DISCONNECT:
                err_code = sd_ble_gap_disconnect(m_conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }
                break;
    
            case BSP_EVENT_WHITELIST_OFF:
                if (m_conn_handle == BLE_CONN_HANDLE_INVALID)
                {
                    err_code = ble_advertising_restart_without_whitelist(&m_advertising);
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                }
                break;
            case BSP_EVENT_KEY_1:
                 screen_4(); //Which is to be displayed after long press release of button 2.
               break;
    
            default:
                break;
        }
    }
    
    static void buttons_leds_init(bool * p_erase_bonds)
    {
        ret_code_t err_code;
        bsp_event_t startup_event;
    
        err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_btn_ble_init(NULL, &startup_event);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_event_to_button_action_assign(OLED_BTN,   //button 2
                                                 BSP_BUTTON_ACTION_LONG_PUSH,// given 5
                                                 BSP_EVENT_KEY_1);
    
        *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
    }
    

    The above code is from main.c 

    Correct me where i have gone wrong?

  • Hello again,

    manoj97 said:
    can you explain the line 7 in your example code?

    That was an oversight by me, sorry. I have updated the code now to the correct parameter.

    manoj97 said:
    why the BTN_ACTION_DISCONNECT is given instead of that we can give this one "BSP_BUTTON_ACTION_LONG_PUSH" will it work for long press?

    You are correct that you may instead just supply BSP_BUTTON_ACTION_LONG_PUSH as the argument directly - but I would recommend that you create a forwarding macro that matches the naming of the action you are connecting the action to. This way, your code readability will increase.

    manoj97 said:
    Correct me where i have gone wrong?

    Could you elaborate why you are wrong? Are you getting an error - if so, which error are you getting, and which function call returns the error?
    If you are not getting an error, what is the behavior you are seeing - and how does it differ from what you were expecting?

    Best regards,
    Karl

  • According to my code which I have shared with you ,

    Is when I press and hold button 2  for  5 sec it has to trigger the screen_9().

    But it is not happing. That is my point so I asked where I have mistaken

  • This is understood from your previous reply, but in order to be able to help you I will need to know more about the behavior you are seeing.

    What tests have you conducted to identify what is happening? Have you used the debugger to step through your code, to see where it might go wrong? Or, have you tested if the button press causes the bsp_event_handler to be called at all?
    In the case that the bsp_event_handler is called, which event is it called with?

    The more info you provide about what you have done to identify your issue, the easier it will be to help you resolve the issue.
    It is very hard to help you when you only provide a code snippet and "it is not happening", because I have no way to run the little code you have provided and you may have programmed something else somewhere else in your code that will prevent the execution of the code you have provided. I have no way of knowing, and thus I rely on your accurate description of the behavior you are seeing.

    Please check the things I mention above - is the bsp_event_handler called at all when you press your button, and what event is it called with - and get back to me with the result.

    Best regards,
    Karl

Related