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

APP_TIMER_DEF for several timers with same timer id. like C++ class member

hello

yes I have problems with my app_timers in SDK14.2 (migration from 12),

I well noticed that the prototypes of the functions create and start changed from sdk12.

// create timer

static app_timer_t timer_data = { {0} };
m_timer_id = &timer_data;
err_code = app_timer_create(&m_timer_id, APP_TIMER_MODE_REPEATED, cbk_measure);
APP_ERROR_CHECK(err_code);
-> this works well
ret_code_t err_code;
err_code = app_timer_start(m_timer_id, APP_TIMER_TICKS(m_probing_interval_ms), (void *) this);
NRF_LOG_DEBUG("FIXME: app_timer_start returns %d", err_code);
// APP_ERROR_CHECK(err_code);
-> but the start returns error code 8 (invalid param if i remember correctly) and I dont understand why. I tried APP_TIMER_DEF, but my timer_ids are C++ class members. I cannot simply use the APP_TIMER_DEF macro because I dont know if it ok if already declared. but it was already the case ion SDK12 and worked AFAIK

thx,

yacine

  • The APP_TIMER_DEF macro instantiates an app_timer_id_t with the name you provide and another variable of type app_timer_t with the name appended with _data. app_timer_id_t is just a pointer to the app_timer_t (app_timer_t *).

    For example:

    expands to

    So far so good, unless you want an app_timer to be a member of a struct or class, which is the problem I also ran into quickly. In that case, just do the same thing as the macro would, manually, and in your initialization code or constructor, set the app_timer_id_t member to the address of the timer.

    In C++:

    In C, you either have to do the initialization at the time of instantiation or not make the app_timer_id_t const:

1 2