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 Reply Children
  • The easiest is probably to strip down your application so it can just reproduce your issue without having any sensitive information, and you can upload it here. So e.g. if you are able to reproduce the issue without the eeprom parts, then perhaps you can strip down that part, basically remaining only the components for the button, the timer, and see if it still fails. If so, you can upload it here. It shouldn't hold any sensitive information.

    If you need to create a private ticket, that is pretty much the same way that you created this ticket, but I believe you need to fill your profile with a bit more information first. It should say what you need when you try to create a private ticket. 

    Best regards,

    Edvin

  • long_press.zip

    Hi I have uploaded my application here, let me know if you faced any issue in test. it is without any sensitive information.

Related