From 4ddbac3be37a84cc75c2775ba80d4e1e1245cae4 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Thu, 3 Jun 2021 17:53:44 +0200 Subject: [PATCH] sys/event: add periodic event --- sys/Makefile.dep | 5 ++ sys/event/periodic.c | 42 ++++++++++++++ sys/include/event/periodic.h | 105 +++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 sys/event/periodic.c create mode 100644 sys/include/event/periodic.h diff --git a/sys/Makefile.dep b/sys/Makefile.dep index f6de237113..e08fbb6499 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -478,6 +478,11 @@ ifneq (,$(filter event_timeout_ztimer,$(USEMODULE))) USEMODULE += event_timeout endif +ifneq (,$(filter event_periodic_timeout,$(USEMODULE))) + USEMODULE += event_timeout + USEMODULE += ztimer_periodic +endif + ifneq (,$(filter event_timeout,$(USEMODULE))) ifneq (,$(filter event_timeout_ztimer,$(USEMODULE))) USEMODULE += ztimer_usec diff --git a/sys/event/periodic.c b/sys/event/periodic.c new file mode 100644 index 0000000000..e493b0ee33 --- /dev/null +++ b/sys/event/periodic.c @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2021 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_event + * @{ + * + * @file + * @brief Periodic Event Implementation + * + * @author Francisco Molina + * + * @} + */ +#include "kernel_defines.h" +#include "ztimer.h" +#include "ztimer/periodic.h" +#include "event/periodic.h" + +static int _event_periodic_callback(void *arg) +{ + event_periodic_t *event_periodic = (event_periodic_t *)arg; + + event_post(event_periodic->queue, event_periodic->event); + + return 0; +} + +void event_periodic_init(event_periodic_t *event_periodic, + ztimer_clock_t *clock, + event_queue_t *queue, event_t *event) +{ + ztimer_periodic_init(clock, &event_periodic->timer, _event_periodic_callback, + event_periodic, 0); + event_periodic->queue = queue; + event_periodic->event = event; +} diff --git a/sys/include/event/periodic.h b/sys/include/event/periodic.h new file mode 100644 index 0000000000..408e8f0eef --- /dev/null +++ b/sys/include/event/periodic.h @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2021 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_event + * @brief Provides functionality to trigger periodic events + * + * event_periodic intentionally doesn't extend event structures in order to + * support events that are integrated in larger structs intrusively. + * + * Example: + * + * ~~~~~~~~~~~~~~~~~~~~~~~~ {.c} + * event_periodic_t event_periodic; + * + * printf("posting timed callback every 1sec\n"); + * event_periodic_init(&event_periodic, ZTIMER_USEC, &queue, (event_t*)&event); + * event_periodic_start(&event_periodic, 1000000); + * [...] + * ~~~~~~~~~~~~~~~~~~~~~~~~ + * + * @{ + * + * @file + * @brief Event Periodic API + * + * @author Francisco Molina + * + */ + +#ifndef EVENT_PERIODIC_H +#define EVENT_PERIODIC_H + +#include "event.h" +#include "ztimer/periodic.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Timeout Event structure + */ +typedef struct { + ztimer_periodic_t timer; /**< ztimer object used for timeout */ + event_queue_t *queue; /**< event queue to post event to */ + event_t *event; /**< event to post after timeout */ +} event_periodic_t; + +/** + * @brief Initialize a periodic event timeout + * + * @param[in] event_periodic event_periodic object to initialize + * @param[in] clock the clock to configure this timer on + * @param[in] queue queue that the timed-out event will be + * added to + * @param[in] event event to add to queue after timeout + */ +void event_periodic_init(event_periodic_t *event_periodic, ztimer_clock_t *clock, + event_queue_t *queue, event_t *event); + +/** + * @brief Starts a periodic timeout + * + * This will make the event as configured in @p event_periodic be triggered + * at every interval ticks (based on event_periodic->clock). + * + * @note: the used event_periodic struct must stay valid until after the timeout + * event has been processed! + * + * @param[in] event_periodic event_timout context object to use + * @param[in] interval period length for the event + */ +static inline void event_periodic_start(event_periodic_t *event_periodic, + uint32_t interval) +{ + event_periodic->timer.interval = interval; + ztimer_periodic_start(&event_periodic->timer); +} + +/** + * @brief Stop a periodic timeout event + * + * Calling this function will cancel the timeout by removing its underlying + * timer. If the timer has already fired before calling this function, the + * connected event will be put already into the given event queue and this + * function does not have any effect. + * + * @param[in] event_periodic event_periodic_timeout context object to use + */ +static inline void event_periodic_stop(event_periodic_t *event_periodic) +{ + ztimer_periodic_stop(&event_periodic->timer); +} + +#ifdef __cplusplus +} +#endif +#endif /* EVENT_PERIODIC_H */ +/** @} */