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

button long-press & short press implement on nRF52

I am very new begginer and dont know where to start about nordic nrf52832 chip. Started a week ago with downloading SDK and playing with examples in it.

I in specific need some guidance to operate the long press and short press on same button and thereby make different functions in response to long or short press of button. Do you have any examples or guidlines for this?

In detail, i Want to make long press of button to perform one task, and short press of button to perform other task. I just want to get to know how the buttons are coded for detecting and performing the task. i Saw many posts in this website but couldnot find any coding example.

  • Hi,

    This is expected. The APP_BUTTON_PUSH from the app_button library is on a different "level" (app button library) then BSP_BUTTON_ACTION_LONG_PUSH (BSP library). app_button itself only support button press and release events, and has no concept of the duration of the pushes. 

    If you want t use BSP_BUTTON_ACTION_LONG_PUSH to get long pushes, then it makes sense to use the BSP_BUTTON_ACTION_PUSH event for the short/normal push, ignoring the app_button events.

  • Hi Einar,

    I am also trying to do the same feature.

    static void bsp_button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        bsp_event_t        event  = BSP_EVENT_NOTHING;
        uint32_t           button = 0;
        uint32_t           err_code;
        static uint8_t     current_long_push_pin_no;              /**< Pin number of a currently pushed button, that could become a long push if held long enough. */
        static bsp_event_t release_event_at_push[BUTTONS_NUMBER]; /**< Array of what the release event of each button was last time it was pushed, so that no release event is sent if the event was bound after the push of the button. */
    
        button = bsp_board_pin_to_button_idx(pin_no);
    
        if (button < BUTTONS_NUMBER)
        {
            switch (button_action)
            {
            		case BSP_BUTTON_ACTION_PUSH:
                    event = m_events_list[button].push_event;
                    if (m_events_list[button].long_push_event != BSP_EVENT_NOTHING)
                    {
                        //11/27 Wedy add the note: here can set the timer for long press button
                        err_code = app_timer_start(m_bsp_button_tmr, APP_TIMER_TICKS(BSP_LONG_PUSH_TIMEOUT_MS), (void*)&current_long_push_pin_no); //0716 Wedy add for marked to change the time condition "timer start"
                        if (err_code == NRF_SUCCESS)
                        {
                            current_long_push_pin_no = pin_no;
                        }
                    }
                    release_event_at_push[button] = m_events_list[button].release_event;
                    break;
                case APP_BUTTON_RELEASE:
                    (void)app_timer_stop(m_bsp_button_tmr); //0716 Wedy note for timer stop
                    if (release_event_at_push[button] == m_events_list[button].release_event)
                    {
                        event = m_events_list[button].release_event;
                    }
                    //11/27 Wedy add to set the long press release function
                    /*if (release_event_at_push [button] == m_events_list[button].long_push_event)
                    {
                        event=m_events_list[button].release_event;
                    }*/
                    break;
                case BSP_BUTTON_ACTION_LONG_PUSH:
                    event = m_events_list[button].long_push_event;
                    //11/27 Wedy add for the long press release Setting
                    release_event_at_push[button]=m_events_list[button].long_push_event;
            }
        }

    As you mentioned, is it can be achieved by modified the app_button_push to bsp_button_action_push?

    I just modified my code, but it still trigger the short press when I was trying to trigger the long press event.

    Besides, do you have any idea of double click based on this structure can be shared?

    Thanks.

    BR,

    Wedy

Related