Long Press Button Interrupt in NCS

Hi, I am working on long press button interrupt in nrf52832 device & used NCS. I am unable to detect the long press from button.below is my functionality which is I am trying to implement.
 

press for 10 sec after that call a callback function.

void long_button_pressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
   // if (gpio_pin_get_dt(&button_0)) {
    if(pins & BIT(button_0.pin)){
        LOG_INF("button pin detected");
        k_timer_start(&button_timer, K_SECONDS(10), K_NO_WAIT);
    } else {
        LOG_INF("long_press not detected");
        k_timer_stop(&button_timer);
        long_press_detected = false;  // Reset flag if button is released
    }
}

void button_timer_expiry_handler(struct k_timer *timer_id)
{
    printk("Button long-pressed for 10 seconds\n");
    long_press_detected = true;
    Reset_Flag=1;
    eeprom_write(i2c_dev,EEPROM_RESET_FLAG_ADDRESS, &Reset_Flag, 1);
    eeprom_write(i2c_dev,EEPROM_RESET_TIME_FLAG, &current_epoch_time, 4);
    guide_flag=1;
    eeprom_write(i2c_dev,EEPROM_DEVICE_GUIDE_ADDRESS,&guide_flag,1);
    reset_hardware_locally();
    //reset_hardware();
    k_msleep(1000);
    NVIC_SystemReset();
    // Add your action here
}

void long_press_button(){
    if (!device_is_ready(button_0.port)) {
        printk("Button device not ready\n");
        return;
    }
    gpio_init_callback(&button_cb_data,long_button_pressed, BIT(button_0.pin));
    gpio_add_callback(button_0.port, &button_cb_data);

    k_timer_init(&button_timer, button_timer_expiry_handler, NULL);

}

above is the code of long press button.

Pin configuration :-

gpio_pin_configure_dt(&button_0, GPIO_INPUT);
gpio_pin_interrupt_configure_dt(&button_0, GPIO_INT_EDGE_BOTH);
configuration in overlay file:-
button0
{
gpios = <&gpio0 21 GPIO_ACTIVE_LOW>;
label = "SW1";
};
Suggest a solution how can implement long press button interrupt in zephyr ncs.
Parents
  • Hi,

    Are you able to enter in gpio interrupt callback?

    Regards

  • Yes, I am able to enter in gpio interrupt callback. I am got  LOG_INF("button pin detected"); from long_press_button callback.

  • Hello,

    Sorry for the late reply. Still working on the backlog from the christmas/new years break.

    It was not clear to me which callback does what, only based on your snippets. 

    It is still not clear to me which callback that is triggered and which one that is not. Which is the one that is triggered on a (normal) button press (that is not long)?

    bhargav_gondaliya said:
    I am got  LOG_INF("button pin detected"); from long_press_button callback.

    The "button pin detected" is from your long_button_pressed(), not long_press_button().

    But if you see "button pin detected" in your log, it suggests that the long_button_pressed() was triggered, at least. And that it calls k_timer_start(). Is button_timer_expiry_handler() triggered at any point? Or is "long_press not detected" printed before the button_timer_expiry_handler() is triggered?

    For debugging purposes, have you tried to comment out these two lines:

    k_timer_stop(&button_timer);
    long_press_detected = false; // Reset flag if button is released

    ?

    And finally, is long_press_button() called at some point before all this?

    Best regards,

    Edvin

  • Hi, when button pressed single time  long_button_pressed is called and got log of button pin detected.
    unable to triggered "button_timer_expiry_handler".

    neither "long_press not detected" printed nor button_timer_expiry_handler triggered.

    basically, I want to implement feature like when button is pressed for 10 sec my device should be reset (button_timer_expiry_handler should be triggered automatically after 10 sec ).

    I will try as you suggested

    For debugging purposes, have you tried to comment out these two lines:

    k_timer_stop(&button_timer);
    long_press_detected = false; // Reset flag if button is released

    Is pin configuration and button node is correct?

  • bhargav_gondaliya said:
    Is pin configuration and button node is correct?

    Looks ok, but I haven't tested it. If you want me to, please zip and upload the zipped application folder here. 

    But since you get the initial callback, I assume it is correct. 

    I would try to split up the application. I would say that you got the button handling working, so what remains is the timer and timeout handler. 

    What if you just start the timer from main(). Does the timeout handler get called then?

    Edvin said:

    And finally, is long_press_button() called at some point before all this?

    Still not sure where you are calling the long_press_button() from.

    Best regards,

    Edvin

Reply
  • bhargav_gondaliya said:
    Is pin configuration and button node is correct?

    Looks ok, but I haven't tested it. If you want me to, please zip and upload the zipped application folder here. 

    But since you get the initial callback, I assume it is correct. 

    I would try to split up the application. I would say that you got the button handling working, so what remains is the timer and timeout handler. 

    What if you just start the timer from main(). Does the timeout handler get called then?

    Edvin said:

    And finally, is long_press_button() called at some point before all this?

    Still not sure where you are calling the long_press_button() from.

    Best regards,

    Edvin

Children
Related