Hello
I am using sdk v17.0 and nrf52832.
I'm using two timers through a button.
1. Timer for click & double-click. (Single Timer)
2. Timer for Long Press. (Repeated Timer)
Using them separately is not a problem, but if I use them together, It stop when I double-click. I checked by debugging, but no error occurred. (I think it stopped at the RTC.)
static void PairLED_timer_handler(void * p_context) //when advertising toggle led
{
nrf_gpio_pin_toggle(Pairing_LED); //toggle led
}
uint32_t press_time; //press time count
uint32_t press_time2; //press time count
int click_count; //click count(use double click)
bool buttonTimeout = false; //use double click
static void PowerOff_timer_handler(void * p_context)
{
press_time++;
printf("COUNT : %d\n", press_time);
if(press_time == 2) //press 2sec, Power Off
{
printf("Power Off!\n");
//nrf_gpio_pin_clear(POWER_SW); //LOW : power off
}
}
static void DoubleClick_timer_handler(void * p_context)
{
//printf("Timeout\n");
printf("%d\n", click_count);
buttonTimeout = true;
if(click_count == 1)
{
printf("One Click\n");
}
click_count = 0;
}
static void timers_init(void)
{
ret_code_t err_code;
// Initialize timer module.
err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
//Create Pairing LED timers/
err_code = app_timer_create(&m_PairLED_timerid,
APP_TIMER_MODE_REPEATED,
PairLED_timer_handler);
APP_ERROR_CHECK(err_code);
//Create Power off Time timers/
err_code = app_timer_create(&m_PowerOff_timer_id,
APP_TIMER_MODE_REPEATED,
PowerOff_timer_handler);
APP_ERROR_CHECK(err_code);
//Create DOuble Click Time timers/
err_code = app_timer_create(&m_DoubleClick_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
DoubleClick_timer_handler);
APP_ERROR_CHECK(err_code);
}
static void PairLED_timers_start()
{
ret_code_t err_code;
err_code = app_timer_start(m_PairLED_timerid, APP_TIMER_TICKS(1000), NULL); //Toggle 1sec
APP_ERROR_CHECK(err_code);
printf("Scanning Start\n");
}
static void PairLED_timers_stop() //Stop Timer
{
ret_code_t err_code;
err_code = app_timer_stop(m_PairLED_timerid);
APP_ERROR_CHECK(err_code);
}
static void PowerOff_timers_start()
{
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()
{
ret_code_t err_code;
//printf("No Power Off\n");
press_time = 0; //count reset
err_code = app_timer_stop(m_PowerOff_timer_id);
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(500), NULL);
APP_ERROR_CHECK(err_code);
}
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);
//printf("Stop Timer\n");
}
//==========================================================
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;
switch (pin_no)
{
case BUTTON_1:
switch (button_action)
{
case APP_BUTTON_PUSH:
{
//printf("Button pressed\n");
//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; */
if(click_count == 0) //first click
{
buttonTimeout = false;
DoubleClick_timers_start(); //buttonTimeout = true, click_count = 0;
}
PowerOff_timers_start(); //press time count
}
click_count++;
break;
case APP_BUTTON_RELEASE:
{
//printf("Button releaed\n");
PowerOff_timers_stop();
/*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);
click_state = 0;
}
pressed_cnt = false; */
if(click_count == 2 && !buttonTimeout) //double click
{
DoubleClick_timers_stop();
click_count = 0;
printf("Double Click\n");
}
} 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);
}
Could you tell me the cause of this problem?
Thank you!