<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69033/difficulty-implementing-switch-double-click</link><description>Hello 
 I&amp;#39;m trying to implement the click and double click and long click of a button in sdk v17. 
 Click and long click work fine. But double-click doesn&amp;#39;t work. 
 
 Code for entering button. 
 
 
 But there are two problems with this code. 1. Double</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 18 Dec 2020 14:57:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69033/difficulty-implementing-switch-double-click" /><item><title>RE: Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/thread/285876?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 14:57:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e0c3d3a9-7d6b-4dfc-89e7-d02bd194106a</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Sounds good.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you have more questions just let me know &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/thread/284673?ContentTypeID=1</link><pubDate>Mon, 14 Dec 2020 02:03:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4bc8bc3b-a2bb-44f7-967f-f1d575f93b70</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;Hellon, ovrebekk.&lt;br /&gt;I&amp;#39;m sorry for the late reply.&lt;/p&gt;
&lt;p&gt;I succeeded with the method you told me.&lt;br /&gt;But sometimes double-click doesn&amp;#39;t work, so I&amp;#39;ll try a little more.&lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/thread/283540?ContentTypeID=1</link><pubDate>Mon, 07 Dec 2020 12:43:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5a1f975-86ab-43cb-b28f-01f54ce45f0f</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Can you try the attached method to detect double click below?&lt;/p&gt;
&lt;p&gt;I tried it myself, and it seems to work fine, while requiring relatively little code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;int click_count; //check click or double click
bool buttonTimeout = false;


static void button_timer_timeout(void *p)
{
    NRF_LOG_INFO(&amp;quot;Timeout&amp;quot;);
    buttonTimeout = true;
    if(click_count == 1)
    {
        NRF_LOG_INFO(&amp;quot;Single click detected&amp;quot;);
    }
    click_count = 0;
}


static void app_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    ret_code_t err_code;

    switch (pin_no)
    {
        case BUTTON_1: 
            switch (button_action)
            {
                case APP_BUTTON_PUSH:
                    if(click_count == 0) //first click
                    {
                        buttonTimeout = false;
                        app_timer_start(m_button_click_timer, APP_TIMER_TICKS(400), 0);
                    }

                    click_count++;
                    break;

                case APP_BUTTON_RELEASE:
                    if(click_count == 2 &amp;amp;&amp;amp; !buttonTimeout)
                    {
                        app_timer_stop(m_button_click_timer);
                        click_count = 0;
                        NRF_LOG_INFO(&amp;quot;Double click detected&amp;quot;);
                    }
                    break;
            } 
            break;

        default:
            APP_ERROR_HANDLER(pin_no);
            break;
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Currently the double click timeout is set to 400ms, but you can change that in the call to app_timer_start.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/thread/283225?ContentTypeID=1</link><pubDate>Fri, 04 Dec 2020 02:30:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:da2bfa49-39ae-4e65-a513-e66c8841c5b2</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;I tried to implement double-click using a timer.&amp;nbsp; But it stops working when I double click. (The Bluetooth works without stopping.)&lt;/p&gt;
&lt;p&gt;The timer starts when I press the button. And I tried to stop the timer by double click or after a certain time.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;APP_TIMER_DEF(m_DoubleClick_timer_id); //button double click time, repeate timer

static void DoubleClick_timer_handler(void * p_context)
{
  press_time2++; //timer count
  printf(&amp;quot;Click : %d\n&amp;quot;, click_count);
  printf(&amp;quot;COUNT : %d\n&amp;quot;, press_time2);

  if(click_count &amp;gt;= 2) //double click
  {
    printf(&amp;quot;Double Click\n&amp;quot;);
    click_count = 0;
    press_time2 = 0; //count reset

    DoubleClick_timers_stop(); 
  }

  else if(press_time2 &amp;gt;= 2) //after click 0.5sec, no double click
  {
    printf(&amp;quot;One Click\n&amp;quot;);
    click_count = 0;
    press_time2 = 0; //count reset

    DoubleClick_timers_stop(); 
  }
}


static void timers_init(void)
{
    ret_code_t err_code;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

   //Create DOuble Click Time timers/
    err_code = app_timer_create(&amp;amp;m_DoubleClick_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                DoubleClick_timer_handler);
    APP_ERROR_CHECK(err_code);
}

static void DoubleClick_timers_start()
{
  ret_code_t err_code;

  err_code = app_timer_start(m_DoubleClick_timer_id, APP_TIMER_TICKS(250), NULL); 
  APP_ERROR_CHECK(err_code);
  //printf(&amp;quot;Wait next click\n&amp;quot;);
}


static void DoubleClick_timers_stop() //Stop Timer
{
    ret_code_t err_code;

    err_code = app_timer_stop(m_DoubleClick_timer_id); 
    APP_ERROR_CHECK(err_code);
}


static void app_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    ret_code_t err_code;

    switch (pin_no)
    {
      case BUTTON_1: 
        switch (button_action)
        {
          case APP_BUTTON_PUSH:
          {
            click_state = 1;
            click_count++;     
            
            if(click_count == 1) //first click
            {
              DoubleClick_timers_start();
            }  
        } break;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1607048698491v1.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;It seems to stop at timer_stop() after double click.&amp;nbsp;&amp;nbsp;&lt;span&gt;Is this problem related to the timer?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/thread/283216?ContentTypeID=1</link><pubDate>Fri, 04 Dec 2020 00:15:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:312086dc-c0b8-4951-aff6-f85347a876ff</guid><dc:creator>schosdas</dc:creator><description>&lt;p&gt;Hi, ovrebekk.&lt;/p&gt;
&lt;p&gt;Thank you for helping me.&lt;br /&gt;I&amp;#39;ve implemented the Button Long Press function before.&lt;/p&gt;
&lt;p&gt;This function was created by applying a timer. Should I apply a timer similar to this for double-click?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;APP_TIMER_DEF(m_PowerOff_timer_id); //button Long press time, repeate timer

static void PowerOff_timer_handler(void * p_context)
{
  press_time++;
  printf(&amp;quot;COUNT : %d\n&amp;quot;, press_time);

  if(press_time == 2) //press 2sec, Power Off
  {
    printf(&amp;quot;Power Off!\n&amp;quot;);
    //pwm_change_frequency(1500); 
    //do_play_buzzer();
    //nrf_gpio_pin_clear(POWER_SW); //LOW : power off
  }
}


/**@brief Function for the Timer initialization.
 *
 * @details Initializes the timer module. This creates and starts application timers.
 */
static void timers_init(void)
{
    ret_code_t err_code;

    // Initialize timer module.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

   //Create Power off Time timers/
    err_code = app_timer_create(&amp;amp;m_PowerOff_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                PowerOff_timer_handler);
    APP_ERROR_CHECK(err_code);
}


static void PowerOff_timers_start() //When push button
{
  ret_code_t err_code;

  err_code = app_timer_start(m_PowerOff_timer_id, APP_TIMER_TICKS(1000), NULL); //count 1sec
  APP_ERROR_CHECK(err_code);
}


static void PowerOff_timers_stop() //When release button
{
    ret_code_t err_code;

    //printf(&amp;quot;No Power Off\n&amp;quot;);
    press_time = 0; //count reset

    err_code = app_timer_stop(m_PowerOff_timer_id); 
    APP_ERROR_CHECK(err_code); 
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Difficulty implementing switch double-click.</title><link>https://devzone.nordicsemi.com/thread/283187?ContentTypeID=1</link><pubDate>Thu, 03 Dec 2020 17:06:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:61c24735-9575-4df1-94df-ca8105eee41c</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I believe the way to handle this is to start a timer on the first click, and then wait for either the timer to expire or another click to occur.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If the timer expires you count it as a single click, and if another click occurs you count it as a double click.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then after that you reset everything and go back to waiting for a click to happen.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t think you need to add code waiting for the next click after the time has expired.&lt;/p&gt;
&lt;p&gt;I have been able to run your code and see it in action, but I don&amp;#39;t have the implementation of the&amp;nbsp;PowerOff_timers_start() and&amp;nbsp;PowerOff_timers_stop() functions. Are you able to share those?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>