Skip to content

Commit

Permalink
Merge pull request #18656 from benpicco/event_periodic_callback_get_arg
Browse files Browse the repository at this point in the history
event_periodic_callback: add getter for user context, one-shot event
  • Loading branch information
benpicco authored Oct 4, 2022
2 parents 429ee83 + b2edca1 commit 161172c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions sys/include/event/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ typedef struct {
*/
void event_callback_init(event_callback_t *event_callback, void (*callback)(void *), void *arg);

/**
* @brief Generate a one-shot callback event on @p queue
*
* This will initialize @p event and post it immediately
*
* @param[in] event event_callback object to initialize
* @param[in] queue queue that the event will be added to
* @param[in] callback callback to set up
* @param[in] arg callback argument to set up
*/
static inline void event_callback_oneshot(event_callback_t *event,
event_queue_t *queue,
void (*callback)(void *), void *arg)
{
event_callback_init(event, callback, arg);
event_post(queue, &event->super);
}

/**
* @brief event callback handler function (used internally)
*
Expand Down
11 changes: 11 additions & 0 deletions sys/include/event/periodic_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ typedef struct {
event_callback_t event; /**< callback event portion */
} event_periodic_callback_t;

/**
* @brief Get user context from Periodic Callback Event
*
* @param[in] event event_periodic_callback object to initialize
* @return User supplied argument to the event
*/
static inline void *event_periodic_callback_get_arg(event_periodic_callback_t *event)
{
return event->event.arg;
}

/**
* @brief Initialize a periodic callback event
*
Expand Down
3 changes: 3 additions & 0 deletions tests/event_periodic_callback/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ int main(void)
{
event_periodic_callback_t a, b, c;

event_callback_t oneshot;
event_callback_oneshot(&oneshot, EVENT_PRIO_MEDIUM, _event_cb, "event 0");

event_periodic_callback_init(&a, ZTIMER_MSEC, EVENT_PRIO_MEDIUM, _event_cb, "event A");
event_periodic_callback_init(&b, ZTIMER_MSEC, EVENT_PRIO_MEDIUM, _event_cb, "event B");
event_periodic_callback_init(&c, ZTIMER_MSEC, EVENT_PRIO_MEDIUM, _event_cb, "event C");
Expand Down
1 change: 1 addition & 0 deletions tests/event_periodic_callback/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


def testfunc(child):
child.expect_exact("event 0")
child.expect_exact("event A")
child.expect_exact("event B")
child.expect_exact("event A")
Expand Down

0 comments on commit 161172c

Please sign in to comment.