sys/event: add periodic event
This commit is contained in:
parent
fde0bb7819
commit
4ddbac3be3
@ -478,6 +478,11 @@ ifneq (,$(filter event_timeout_ztimer,$(USEMODULE)))
|
|||||||
USEMODULE += event_timeout
|
USEMODULE += event_timeout
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifneq (,$(filter event_periodic_timeout,$(USEMODULE)))
|
||||||
|
USEMODULE += event_timeout
|
||||||
|
USEMODULE += ztimer_periodic
|
||||||
|
endif
|
||||||
|
|
||||||
ifneq (,$(filter event_timeout,$(USEMODULE)))
|
ifneq (,$(filter event_timeout,$(USEMODULE)))
|
||||||
ifneq (,$(filter event_timeout_ztimer,$(USEMODULE)))
|
ifneq (,$(filter event_timeout_ztimer,$(USEMODULE)))
|
||||||
USEMODULE += ztimer_usec
|
USEMODULE += ztimer_usec
|
||||||
|
|||||||
42
sys/event/periodic.c
Normal file
42
sys/event/periodic.c
Normal file
@ -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 <francois-xavier.molina@inria.fr>
|
||||||
|
*
|
||||||
|
* @}
|
||||||
|
*/
|
||||||
|
#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;
|
||||||
|
}
|
||||||
105
sys/include/event/periodic.h
Normal file
105
sys/include/event/periodic.h
Normal file
@ -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 <francois-xavier.molina@inria.fr>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#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 */
|
||||||
|
/** @} */
|
||||||
Loading…
x
Reference in New Issue
Block a user