I am trying to use an application timer along with some variables. I am using ble_app_template as base. To start of I've got the timer without any parameters being passed, which works just fine.
int counter = 0;
void valueIncrementer()
{
counter++;
NRF_LOG_INFO("counter = %d", counter);
}
static void timers_init(void)
{
// Initialize timer module.
ret_code_t err_code = app_timer_init(); //Initializes the library
APP_ERROR_CHECK(err_code);
/* YOUR_JOB: Create any timers to be used by the application.*/
err_code = app_timer_create(&valueIncrementer_timer_id,
APP_TIMER_MODE_REPEATED,
valueIncrementer);
APP_ERROR_CHECK(err_code);
/*^MY_JOB^*/
}
But I'm not managing to pass any variables. I have tried passing counter by value and by reference. I have also tried to declare
app_timer_timeout_handler_t valueIncrementer() {//same as before} which does not work either.
Originally I thought the way to go would be function pointers, which works if I use the global counter. (I've only included changes to the original here, duplicate lines have been removed)
void (*valueIncrementerFPtr)();
static void timers_init(void)
{
// Initialize timer module.
/* YOUR_JOB: Create any timers to be used by the application.*/
err_code = app_timer_create(&valueIncrementer_timer_id,
APP_TIMER_MODE_REPEATED,
*valueIncrementerFPtr);
APP_ERROR_CHECK(err_code);
/*^MY_JOB^*/
}
int main(void)
{
valueIncrementerFPtr = &valueIncrementer;
...//no more changes to main
}
As soon as I try to add a variable to valueIncrementer() (and *valueIncrementerFPtr)() ) I get warnings or errors. The best I've managed is use a void pointer and cast it back to an integer.
void valueIncrementer(void *countValue)
{
(int *)countValue ++;
NRF_LOG_INFO("counter = %d", (int)countValue);
}
void (*valueIncrementerFPtr)(void *);
static void timers_init(void)
{
// Initialize timer module.
/* YOUR_JOB: Create any timers to be used by the application.*/
err_code = app_timer_create(&valueIncrementer_timer_id,
APP_TIMER_MODE_REPEATED,
*valueIncrementerFPtr);
APP_ERROR_CHECK(err_code);
/*^MY_JOB^*/
}
int main(void)
{
valueIncrementerFPtr = &valueIncrementer;
...
} My output here is random as I am not passing anything to valueIncrementer.
I assume that I'm misunderstanding function pointers and/or the use of application timers in general (I have been using this tutorial on function pointers). Can someone explain what I'm doing wrong or point me in the right direction?
A working example of how to create a timer and passing a variable by reference to the time out handler would be much appreciated.