microbit-dal/inc/MicroBitEvent.h

88 lines
2.7 KiB
C
Raw Normal View History

#ifndef MICROBIT_EVENT_H
#define MICROBIT_EVENT_H
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
#include "mbed.h"
enum MicroBitEventLaunchMode
{
CREATE_ONLY,
CREATE_AND_QUEUE,
CREATE_AND_FIRE
};
#define MICROBIT_EVENT_DEFAULT_LAUNCH_MODE CREATE_AND_QUEUE
/**
* Class definition for a MicrobitEvent
* It represents a common event that is generated by the various components on the MB.
*/
class MicroBitEvent
{
public:
//These are public at the moment for backwards compatability with old code
//will be refactored in the future!
uint16_t source; // ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
uint16_t value; // Component specific code indicating the cause of the event.
uint32_t timestamp; // Time at which the event was generated. ms since power on.
/**
* Constructor.
* @param src ID of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
* @param value Component specific code indicating the cause of the event.
* @param mode optional definition of how the event should be processed after construction (if at all):
*
* CREATE_ONLY: MicroBitEvent is initialised, and no further processing takes place.
* CREATE_AND_QUEUE: MicroBitEvent is initialised, and queued on the MicroBitMessageBus.
* CREATE_AND_FIRE: MicroBitEvent is initialised, and its event handlers are immediately fired (not suitable for use in interrupts!).
*
* Example: Create and launch an event using the default configuration
* @code
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK);
* @endcode
*
* Example: Create and launch an event and process all registered event handlers immediately.
* @code
* MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK,CREATE_AND_FIRE);
* @endcode
*/
MicroBitEvent(uint16_t source, uint16_t value, MicroBitEventLaunchMode mode = MICROBIT_EVENT_DEFAULT_LAUNCH_MODE);
/**
* Default constructor - initialises all values, and sets timestamp to the current time.
*/
MicroBitEvent();
/**
* Fires the represented event onto the message bus using the default configuration.
*/
void fire();
/**
* Fires the represented event onto the message bus.
* @param mode Configuration of how the event is processed.
*/
void fire(MicroBitEventLaunchMode mode);
};
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
/**
* Enclosing class to hold a chain of events.
*/
struct MicroBitEventQueueItem
{
MicroBitEvent evt;
MicroBitEventQueueItem *next;
microbit: Added configurable concurrency modes for MicroBitMessageBus handlers. MessageBus handlers can now have one of four concurrency modes for the eventuality of an event being raised whilst a previous event is still being processed. An additional (optional) parameter is provided to the listen() functions to allow this to be selected on a per event handler basis. The permissable options are: MESSAGE_BUS_LISTENER_REENTRANT: The event handler is fired with the new event, regardless of whether or not a previous event is still be processed by that handler. MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY: The new event is queued until such a time as the previous event has completed execution. The new event is then processed. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_DROP_IF_BUSY: The new event is dropped, and will never be processed the the event handler. This option does not preclude the processing of the new event by other event handlers. MESSAGE_BUS_LISTENER_NONBLOCKING: The event handler is self-declaring that it never blocks. This flag is used purely for optimisation, as it permits direct execution of the event hadnelr without inducing any overhead from the scheduler. In addition, the following minor revisions were made in this release: * Cleanup of the #include dependencies contained in the microbit-dal .h files * Bugfix to the scheduler block on event matching code. * Introduced a MICROBIT_ID_ALERT MessageBus channel, for general purpose eventing using nonces.
2015-09-11 15:39:38 +00:00
/**
* Constructor.
* Creates a new MicroBitEventQueueItem.
* @param evt The event that is to be queued.
*/
MicroBitEventQueueItem(MicroBitEvent evt);
};
#endif