This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

same timer for 2 activities

i'm looking to use only one timer for two independent activities:

  • battery service (as in Nordic examples) and
  • a routine to detect a certain button press pattern (measure the on time and off time). The button is used only after wake-on, if the expected pattern is recognized the button is disabled and battery service initialized. I've created a timer, using app_timer_create; I will be started from button press event, or from battery event, as required.

Q1 : is it a good idea, or is better to define 2 define 2 timers, with two timeout handlers ?

Q2: how can i use the p_context pointer, to detect in the timer's timeout_handler what event started the timer ? is there any example of how this (void *p_context) should be used ?

Update 5 april. How to use p_context

  1. Allocate a variable, which is not deleted when changing scope. (Either a global or static variable)
  2. Pass a pointer to this variable as p_context in app_timer_start, after casting it to void*
  3. In the timer event handler, cast this pointer to the correct datatype, and use it.

This is for example done in bsp.c in SDK 11:

1:

static uint8_t     current_long_push_pin_no; 

2:

err_code = app_timer_start(m_button_timer_id, BSP_MS_TO_TICK(BSP_LONG_PUSH_TIMEOUT_MS), (void*)&current_long_push_pin_no);

3:

static void button_timer_handler(void * p_context)
{
    bsp_button_event_handler(*(uint8_t *)p_context, BSP_BUTTON_ACTION_LONG_PUSH);
}
Related