This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

BSP_BUTTON_ACTION_LONG_PUSH equivalent in nrf_connect_sdk

In nRF_SDK_For_Thread_and_Zigbee, on the nordic BSP package following three-button functionalities are available:

#define BSP_BUTTON_ACTION_PUSH      (APP_BUTTON_PUSH)    /**< Represents pushing a button. See @ref bsp_button_action_t. */
#define BSP_BUTTON_ACTION_RELEASE (APP_BUTTON_RELEASE) /**< Represents releasing a button. See @ref bsp_button_action_t. */
#define BSP_BUTTON_ACTION_LONG_PUSH (3) /**< Represents pushing and holding a button for @ref BSP_LONG_PUSH_TIMEOUT_MS milliseconds. See also @ref bsp_button_action_t. */

In nRF_Connect_SDK, how can I implement
BSP_BUTTON_ACTION_LONG_PUSH functionality?



Parents
  • I could not find any libraries in NCS that does this. However, I modified the button sample (NCS v1.9.0-rc1) to support long button presses, check it out here:

    8105.button_long_push.zip

    I tested the sample with an nRF52840 DK

    Here is the changes I applied to NCS v1.9.0-rc1:

    diff --git a/samples/basic/button/prj.conf b/samples/basic/button/prj.conf
    index 91c3c15b37..48962a0db7 100644
    --- a/samples/basic/button/prj.conf
    +++ b/samples/basic/button/prj.conf
    @@ -1 +1,2 @@
     CONFIG_GPIO=y
    +CONFIG_CBPRINTF_FP_SUPPORT=y
    \ No newline at end of file
    diff --git a/samples/basic/button/src/main.c b/samples/basic/button/src/main.c
    index 6c367e74be..a952d6d116 100644
    --- a/samples/basic/button/src/main.c
    +++ b/samples/basic/button/src/main.c
    @@ -24,6 +24,8 @@
     static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios,
     							      {0});
     static struct gpio_callback button_cb_data;
    +uint32_t timestamp;
    +#define BUTTON_PUSH_TIME_MS 1000
     
     /*
      * The led0 devicetree alias is optional. If present, we'll use it
    @@ -35,7 +37,18 @@ static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios,
     void button_pressed(const struct device *dev, struct gpio_callback *cb,
     		    uint32_t pins)
     {
    -	printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
    +	//printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
    +	int level = gpio_pin_get(dev, button.pin);
    +	if(level){
    +		//printk("Button pushed\n");
    +		timestamp = k_uptime_get_32();
    +	}else{
    +		uint32_t button_push_time = k_uptime_get_32() - timestamp;
    +		//printk("Button push lasted for %d seconds\n",button_push_time);
    +		if(button_push_time > BUTTON_PUSH_TIME_MS){
    +			printk("Long button push\n");
    +		}
    +	}
     }
     
     void main(void)
    @@ -56,7 +69,7 @@ void main(void)
     	}
     
     	ret = gpio_pin_interrupt_configure_dt(&button,
    -					      GPIO_INT_EDGE_TO_ACTIVE);
    +					      GPIO_INT_EDGE_BOTH);
     	if (ret != 0) {
     		printk("Error %d: failed to configure interrupt on %s pin %d\n",
     			ret, button.port->name, button.pin);
    

    Best regards,

    Simon

  • Hi Simon,

    This seems to work out for me.

    I just have one question, what is the role of  "CONFIG_CBPRINTF_FP_SUPPORT=y" this config in prj.conf?

Reply Children
Related