Merge pull request #17276 from fjmolinas/pr_event_periodic_count
sys/include/event/periodic: add count
This commit is contained in:
commit
536f7e23c8
@ -28,7 +28,11 @@ static int _event_periodic_callback(void *arg)
|
|||||||
|
|
||||||
event_post(event_periodic->queue, event_periodic->event);
|
event_post(event_periodic->queue, event_periodic->event);
|
||||||
|
|
||||||
return 0;
|
if (event_periodic->count && --event_periodic->count == 0) {
|
||||||
|
return !ZTIMER_PERIODIC_KEEP_GOING;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ZTIMER_PERIODIC_KEEP_GOING;
|
||||||
}
|
}
|
||||||
|
|
||||||
void event_periodic_init(event_periodic_t *event_periodic,
|
void event_periodic_init(event_periodic_t *event_periodic,
|
||||||
@ -37,6 +41,7 @@ void event_periodic_init(event_periodic_t *event_periodic,
|
|||||||
{
|
{
|
||||||
ztimer_periodic_init(clock, &event_periodic->timer, _event_periodic_callback,
|
ztimer_periodic_init(clock, &event_periodic->timer, _event_periodic_callback,
|
||||||
event_periodic, 0);
|
event_periodic, 0);
|
||||||
|
event_periodic->count = EVENT_PERIODIC_FOREVER;
|
||||||
event_periodic->queue = queue;
|
event_periodic->queue = queue;
|
||||||
event_periodic->event = event;
|
event_periodic->event = event;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,6 +43,11 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Run the periodic event forever
|
||||||
|
*/
|
||||||
|
#define EVENT_PERIODIC_FOREVER 0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Timeout Event structure
|
* @brief Timeout Event structure
|
||||||
*/
|
*/
|
||||||
@ -50,11 +55,14 @@ typedef struct {
|
|||||||
ztimer_periodic_t timer; /**< ztimer object used for timeout */
|
ztimer_periodic_t timer; /**< ztimer object used for timeout */
|
||||||
event_queue_t *queue; /**< event queue to post event to */
|
event_queue_t *queue; /**< event queue to post event to */
|
||||||
event_t *event; /**< event to post after timeout */
|
event_t *event; /**< event to post after timeout */
|
||||||
|
uint32_t count; /**< times the event should repeat itself */
|
||||||
} event_periodic_t;
|
} event_periodic_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize a periodic event timeout
|
* @brief Initialize a periodic event timeout
|
||||||
*
|
*
|
||||||
|
* @note: On init the periodic event is to to run forever.
|
||||||
|
*
|
||||||
* @param[in] event_periodic event_periodic object to initialize
|
* @param[in] event_periodic event_periodic object to initialize
|
||||||
* @param[in] clock the clock to configure this timer on
|
* @param[in] clock the clock to configure this timer on
|
||||||
* @param[in] queue queue that the timed-out event will be
|
* @param[in] queue queue that the timed-out event will be
|
||||||
@ -73,16 +81,32 @@ void event_periodic_init(event_periodic_t *event_periodic, ztimer_clock_t *clock
|
|||||||
* @note: the used event_periodic struct must stay valid until after the timeout
|
* @note: the used event_periodic struct must stay valid until after the timeout
|
||||||
* event has been processed!
|
* event has been processed!
|
||||||
*
|
*
|
||||||
|
* @note: this function does not touch the current count value.
|
||||||
|
*
|
||||||
* @param[in] event_periodic event_timout context object to use
|
* @param[in] event_periodic event_timout context object to use
|
||||||
* @param[in] interval period length for the event
|
* @param[in] interval period length for the event
|
||||||
*/
|
*/
|
||||||
static inline void event_periodic_start(event_periodic_t *event_periodic,
|
static inline void event_periodic_start(event_periodic_t *event_periodic, uint32_t interval)
|
||||||
uint32_t interval)
|
|
||||||
{
|
{
|
||||||
event_periodic->timer.interval = interval;
|
event_periodic->timer.interval = interval;
|
||||||
ztimer_periodic_start(&event_periodic->timer);
|
ztimer_periodic_start(&event_periodic->timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the amount of times the periodic event should repeat itself.
|
||||||
|
*
|
||||||
|
* @param[in] event_periodic event_timout context object to use
|
||||||
|
* @param[in] count times the event should repeat itself,
|
||||||
|
* EVENT_PERIODIC_FOREVER to run for ever.
|
||||||
|
*/
|
||||||
|
static inline void event_periodic_set_count(event_periodic_t *event_periodic, uint32_t count)
|
||||||
|
{
|
||||||
|
unsigned state = irq_disable();
|
||||||
|
|
||||||
|
event_periodic->count = count;
|
||||||
|
irq_restore(state);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Stop a periodic timeout event
|
* @brief Stop a periodic timeout event
|
||||||
*
|
*
|
||||||
@ -91,6 +115,10 @@ static inline void event_periodic_start(event_periodic_t *event_periodic,
|
|||||||
* connected event will be put already into the given event queue and this
|
* connected event will be put already into the given event queue and this
|
||||||
* function does not have any effect.
|
* function does not have any effect.
|
||||||
*
|
*
|
||||||
|
* @note Calling this function does not touch event_periodic->count, if the
|
||||||
|
* periodic event was not set to run for ever and did run until expiration,
|
||||||
|
* then count will be != 0 after this function is called.
|
||||||
|
*
|
||||||
* @param[in] event_periodic event_periodic_timeout context object to use
|
* @param[in] event_periodic event_periodic_timeout context object to use
|
||||||
*/
|
*/
|
||||||
static inline void event_periodic_stop(event_periodic_t *event_periodic)
|
static inline void event_periodic_stop(event_periodic_t *event_periodic)
|
||||||
|
|||||||
@ -43,11 +43,14 @@ static void _ztimer_periodic_reset(ztimer_periodic_t *timer, ztimer_now_t now)
|
|||||||
static void _ztimer_periodic_callback(void *arg)
|
static void _ztimer_periodic_callback(void *arg)
|
||||||
{
|
{
|
||||||
ztimer_periodic_t *timer = arg;
|
ztimer_periodic_t *timer = arg;
|
||||||
ztimer_now_t now = ztimer_now(timer->clock);
|
|
||||||
|
|
||||||
if (timer->callback(timer->arg) == ZTIMER_PERIODIC_KEEP_GOING) {
|
if (timer->callback(timer->arg) == ZTIMER_PERIODIC_KEEP_GOING) {
|
||||||
|
ztimer_now_t now = ztimer_now(timer->clock);
|
||||||
_ztimer_periodic_reset(timer, now);
|
_ztimer_periodic_reset(timer, now);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
timer->last = timer->last + timer->interval;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer,
|
void ztimer_periodic_init(ztimer_clock_t *clock, ztimer_periodic_t *timer,
|
||||||
|
|||||||
@ -72,8 +72,14 @@ static void callback_4times(void *arg)
|
|||||||
printf("trigger %d of periodic timeout, elapsed time: %" PRIu32 " us\n",
|
printf("trigger %d of periodic timeout, elapsed time: %" PRIu32 " us\n",
|
||||||
*count, elapsed);
|
*count, elapsed);
|
||||||
}
|
}
|
||||||
if (*count == 4) {
|
if (*count == 2) {
|
||||||
|
puts("stop periodic event");
|
||||||
event_periodic_stop(&event_periodic);
|
event_periodic_stop(&event_periodic);
|
||||||
|
puts("resume periodic event, 2 triggers remaining");
|
||||||
|
event_periodic_start(&event_periodic, EVENT_TIMEOUT_TIME);
|
||||||
|
before = event_periodic.timer.last;
|
||||||
|
}
|
||||||
|
if (*count == 4) {
|
||||||
mutex_unlock(&lock);
|
mutex_unlock(&lock);
|
||||||
}
|
}
|
||||||
else if (*count > 4) {
|
else if (*count > 4) {
|
||||||
@ -103,6 +109,7 @@ int main(void)
|
|||||||
puts("posting periodic timed callback with timeout 1sec");
|
puts("posting periodic timed callback with timeout 1sec");
|
||||||
event_periodic_init(&event_periodic, ZTIMER_USEC, EVENT_PRIO_MEDIUM,
|
event_periodic_init(&event_periodic, ZTIMER_USEC, EVENT_PRIO_MEDIUM,
|
||||||
&event_4times.super);
|
&event_4times.super);
|
||||||
|
event_periodic_set_count(&event_periodic, 4);
|
||||||
event_periodic_start(&event_periodic, EVENT_TIMEOUT_TIME);
|
event_periodic_start(&event_periodic, EVENT_TIMEOUT_TIME);
|
||||||
before = event_periodic.timer.last;
|
before = event_periodic.timer.last;
|
||||||
puts("waiting for periodic callback to be triggered 4 times");
|
puts("waiting for periodic callback to be triggered 4 times");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user