1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

ztimer_periodic: make callback function return bool

The callback function of `ztimer_periodic` is only expected to have
two states.
So let it return `true` if the timer should keep repeating, `false`
otherwise.
This commit is contained in:
Benjamin Valentin 2021-12-07 17:14:51 +01:00
parent 536f7e23c8
commit 64bd80d389
4 changed files with 9 additions and 7 deletions

View File

@ -22,7 +22,7 @@
#include "ztimer/periodic.h" #include "ztimer/periodic.h"
#include "event/periodic.h" #include "event/periodic.h"
static int _event_periodic_callback(void *arg) static bool _event_periodic_callback(void *arg)
{ {
event_periodic_t *event_periodic = (event_periodic_t *)arg; event_periodic_t *event_periodic = (event_periodic_t *)arg;

View File

@ -66,6 +66,7 @@
#ifndef ZTIMER_PERIODIC_H #ifndef ZTIMER_PERIODIC_H
#define ZTIMER_PERIODIC_H #define ZTIMER_PERIODIC_H
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "ztimer.h" #include "ztimer.h"
@ -77,7 +78,7 @@ extern "C" {
/** /**
* @brief Periodic timer stop unless it returns this value * @brief Periodic timer stop unless it returns this value
*/ */
#define ZTIMER_PERIODIC_KEEP_GOING 0 #define ZTIMER_PERIODIC_KEEP_GOING true
/** /**
* @brief ztimer periodic structure * @brief ztimer periodic structure
@ -87,7 +88,7 @@ typedef struct {
ztimer_clock_t *clock; /**< clock for this timer */ ztimer_clock_t *clock; /**< clock for this timer */
uint32_t interval; /**< interval of this timer */ uint32_t interval; /**< interval of this timer */
ztimer_now_t last; /**< last trigger time */ ztimer_now_t last; /**< last trigger time */
int (*callback)(void *); /**< called on each trigger */ bool (*callback)(void *); /**< called on each trigger */
void *arg; /**< argument for callback */ void *arg; /**< argument for callback */
} ztimer_periodic_t; } ztimer_periodic_t;
@ -100,11 +101,12 @@ typedef struct {
* @param[in] clock the clock to configure this timer on * @param[in] clock the clock to configure this timer on
* @param[inout] timer periodic timer object to initialize * @param[inout] timer periodic timer object to initialize
* @param[in] callback function to call on each trigger * @param[in] callback function to call on each trigger
* returns `true` if the timer should keep going
* @param[in] arg argument to pass to callback function * @param[in] arg argument to pass to callback function
* @param[in] interval period length of this timer instance * @param[in] interval period length of this timer instance
*/ */
void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer, void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer,
int (*callback)(void *), bool (*callback)(void *),
void *arg, uint32_t interval); void *arg, uint32_t interval);
/** /**

View File

@ -54,7 +54,7 @@ static void _ztimer_periodic_callback(void *arg)
} }
void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer, void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer,
int (*callback)( bool (*callback)(
void *), void *arg, uint32_t interval) void *), void *arg, uint32_t interval)
{ {
*timer = *timer =

View File

@ -41,7 +41,7 @@ static const char *_names[] = { "ZTIMER_MSEC", "ZTIMER_USEC" };
static uint32_t _intervals[] = { 100, 10000 }; static uint32_t _intervals[] = { 100, 10000 };
static uint32_t _max_offsets[] = { 2, 100 }; static uint32_t _max_offsets[] = { 2, 100 };
static int callback(void *arg) static bool callback(void *arg)
{ {
_times[_count] = ztimer_now(arg); _times[_count] = ztimer_now(arg);
@ -58,7 +58,7 @@ static int callback(void *arg)
mutex_unlock(&_mutex); mutex_unlock(&_mutex);
} }
return (_count == REPEAT); return _count < REPEAT;
} }
int main(void) int main(void)