Mistake on this page?
Report an issue in GitHub or email us

Event

The Event class provides APIs to configure events delay and period timings. The timings use the std::chrono::duration class provided by the <utility> library. You can use post API to post an event to the underlying EventQueue, and you can use cancel to cancel the most recently posted event.

The Event class is thread safe. The post and cancel APIs are IRQ safe.

Event class reference

Public Member Functions
template<typename F >
 Event (EventQueue *q, F f)
 Create an event. More...
 Event (const Event &e)
 Copy constructor for events. More...
Eventoperator= (const Event &that)
 Assignment operator for events. More...
 ~Event ()
 Destructor for events. More...
void delay (duration d)
 Configure the delay of an event. More...
void delay (int d)
 Configure the delay of an event. More...
void period (duration p)
 Configure the period of an event. More...
void period (int p)
 Configure the period of an event. More...
int post (ArgTs...args) const
 Posts an event onto the underlying event queue. More...
void call (ArgTs...args) const
 Posts an event onto the underlying event queue, returning void. More...
void operator() (ArgTs...args) const
 Posts an event onto the underlying event queue, returning void. More...
void cancel () const
 Cancels the most recently posted event. More...
template<typename F , typename... ContextArgTs>
 Event (EventQueue *q, F f, ContextArgTs...context_args)
 Create an event. More...
template<typename T , typename R , typename B0 >
 Event (EventQueue *q, T *obj, R(T::*method)(B0, ArgTs...), B0 b0)
 Create an event. More...
template<typename T , typename R , typename B0 >
 Event (EventQueue *q, const T *obj, R(T::*method)(B0, ArgTs...) const, B0 b0)
 Create an event. More...
template<typename T , typename R , typename B0 >
 Event (EventQueue *q, volatile T *obj, R(T::*method)(B0, ArgTs...) volatile, B0 b0)
 Create an event. More...
template<typename T , typename R , typename B0 >
 Event (EventQueue *q, const volatile T *obj, R(T::*method)(B0, ArgTs...) const volatile, B0 b0)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 >
 Event (EventQueue *q, T *obj, R(T::*method)(B0, B1, ArgTs...), B0 b0, B1 b1)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 >
 Event (EventQueue *q, const T *obj, R(T::*method)(B0, B1, ArgTs...) const, B0 b0, B1 b1)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 >
 Event (EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, ArgTs...) volatile, B0 b0, B1 b1)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 >
 Event (EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, ArgTs...) const volatile, B0 b0, B1 b1)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 >
 Event (EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, ArgTs...), B0 b0, B1 b1, B2 b2)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 >
 Event (EventQueue *q, const T *obj, R(T::*method)(B0, B1, B2, ArgTs...) const, B0 b0, B1 b1, B2 b2)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 >
 Event (EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...) volatile, B0 b0, B1 b1, B2 b2)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 >
 Event (EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, B2, ArgTs...) const volatile, B0 b0, B1 b1, B2 b2)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 >
 Event (EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 >
 Event (EventQueue *q, const T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...) const, B0 b0, B1 b1, B2 b2, B3 b3)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 >
 Event (EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...) volatile, B0 b0, B1 b1, B2 b2, B3 b3)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 >
 Event (EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, B2, B3, ArgTs...) const volatile, B0 b0, B1 b1, B2 b2, B3 b3)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 , typename B4 >
 Event (EventQueue *q, T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...), B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 , typename B4 >
 Event (EventQueue *q, const T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...) const, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 , typename B4 >
 Event (EventQueue *q, volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...) volatile, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
 Create an event. More...
template<typename T , typename R , typename B0 , typename B1 , typename B2 , typename B3 , typename B4 >
 Event (EventQueue *q, const volatile T *obj, R(T::*method)(B0, B1, B2, B3, B4, ArgTs...) const volatile, B0 b0, B1 b1, B2 b2, B3 b3, B4 b4)
 Create an event. More...
Static Public Member Functions
static void thunk (void *func, ArgTs...args)
 Static thunk for passing as C-style function. More...

EventQueue example: posting events to the queue

The code below demonstrates how you can instantiate, configure and post events.

/*
 * Copyright (c) 2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"

// Creates an event bound to the specified event queue
EventQueue queue;
void handler(int count);
Event<void(int)> event(&queue, handler);

void handler(int count)
{
    printf("Event = %d \n", count);
    return;
}

void post_events(void)
{

    // Events can be posted multiple times and enqueue gracefully until
    // the dispatch function is called.
    event.post(1);
    event.post(2);
    event.post(3);
}

int main()
{

    Thread event_thread;

    // The event can be manually configured for special timing requirements
    // specified in milliseconds
    event.delay(100);       // Starting delay - 100 msec
    event.period(200);      // Delay between each evet - 200msec

    event_thread.start(callback(post_events));

    // Posted events are dispatched in the context of the queue's
    // dispatch function
    queue.dispatch(400);        // Dispatch time - 400msec
    // 400 msec - Only 2 set of events will be dispatched as period is 200 msec

    event_thread.join();
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.