Hello
I'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't work.
Code for entering button.
int click_count; //check click or double click
unsigned long timePress = 0;
unsigned long timePressLimit = 0;
static uint32_t pressed_time, released_time, pressed_duration = 0;
static bool pressed_cnt= false;
static uint32_t app_button_duration(void) //press time
{
pressed_duration = (released_time - pressed_time)*2; //ms
return pressed_duration;
}
static void app_button_init_time_variable(void)
{
pressed_duration = 0; //press time
}
static void app_button_event_generator(void) //button press time
{
ret_code_t err_code;
app_button_init_time_variable();
}
static void app_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
ret_code_t err_code;
static uint32_t first_pressed;
static uint32_t long_pressed;
switch (pin_no)
{
case BUTTON_1:
switch (button_action)
{
case APP_BUTTON_PUSH:
{
NRF_LOG_INFO("Button pressed...");
printf("Button pressed");
//get pressed time
pressed_time = app_timer_cnt_get()*1000/32768;//milli-sec
NRF_LOG_INFO("pressed_time: %d", (int)pressed_time);
printf("pressed_time: %d\n", (int)pressed_time);
pressed_cnt = true;
PowerOff_timers_start(); //press timer count
if(click_count == 0) //first click
{
printf("One click\n");
timePress = app_timer_cnt_get()*1000/32768;
timePressLimit = timePress + 500; //time press + 0.5sec
click_count = 1;
}
else if(click_count == 1 && app_timer_cnt_get()*1000/32768 < timePressLimit) // double click, click time < timePressLimit
{
printf("Double Click!\n");
//set variables back to 0
timePress = 0;
timePressLimit = 0;
click_count = 0;
}
if(click_count == 1 && timePressLimit != 0 && app_timer_cnt_get()*1000/32768 > timePressLimit) //second click but time's up
{
printf("One click\n");
//set variables back to 0
timePress = 0;
timePressLimit = 0;
click_count = 0;
}
} break;
case APP_BUTTON_RELEASE:
{
NRF_LOG_INFO("Button releaed...");
if (pressed_cnt)
{
released_time = app_timer_cnt_get()*1000/32768; //milli-sec
NRF_LOG_INFO("released_time: %d", (int)released_time);
printf("released_time: %d\n", (int)released_time);
}
pressed_cnt = false;
PowerOff_timers_stop();
} break;
}
break;
default:
APP_ERROR_HANDLER(pin_no);
break;
}
if(app_button_duration())
{
app_button_event_generator();
}
app_button_init_time_variable();
}
static void app_buttons_init(void)
{
uint32_t err_code;
static const app_button_cfg_t app_buttons[BUTTONS_NUMBER] =
{
{BUTTON_1, false, BUTTON_PULL, app_button_event_handler},
};
err_code = app_button_init((app_button_cfg_t *)app_buttons,
BUTTONS_NUMBER, // 1
APP_TIMER_TICKS(50)); //debounce
APP_ERROR_CHECK(err_code);
}
But there are two problems with this code.
1. Double-click will stop the board. (A single click or long push seems fine.)
2. When I click a button, I must be able to determine whether it is a single click or a double click. However, this code also executes a single click code when double-click.
(There should be time to wait for the next button after pressing the button to determine if there is a double click)
When debugging, double click to print the following error in the terminal.
<info> app: 13 <info> app: 14 <info> app: 12 <info> app: 12 <info> app: 12 <info> app: 13 <info> app: 14 <info> app: 12 <info> app: 12 <info> app: 12 <info> app: 13 <info> app: 14 <info> app: 12 <info> app: 12 <info> app: 12 <info> app: 13 <info> app: 14 <info> app: 12 <info> app: 12 <info> app: 12 <info> app: 13 <info> app: 14 <info> app: 12 <info> app: 12 <info> app: 12 <info> app: Button pressed... <info> app: pressed_time: 8572 <info> app: Button releaed... <info> app: released_time: 8598 <info> app: 11 <info> app: 9 <info> app: 10 <info> app: 10 <info> app: 12 <info> app: 11 <info> app: 9 [1;31mLogs dropped (1)[0m <info> app: 10 [1;31mLogs dropped (1)[0m <info> app: 10 [1;31mLogs dropped (1)[0m <info> app: 12 [1;31mLogs dropped (1)[0m <info> app: 11 [1;31mLogs dropped (1)[0m <info> app: 9 [1;31mLogs dropped (1)[0m <info> app: 10 [1;31mLogs dropped (1)[0m <info> app: 10 [1;31mLogs dropped (1)[0m <info> app: 12 [1;31mLogs dropped (1)[0m <info> app: 11
Can I know the cause of this error and what I can refer to about double-click?
Thank you.