2015-09-09 23:04:27 +00:00
|
|
|
/**
|
|
|
|
* Class definition for a MicroBitListener.
|
|
|
|
*
|
|
|
|
* MicroBitListener holds all the information related to a single event handler required
|
|
|
|
* to match and fire event handlers to incoming events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mbed.h"
|
|
|
|
#include "MicroBit.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* Create a new Message Bus Listener.
|
|
|
|
* @param id The ID of the component you want to listen to.
|
|
|
|
* @param value The event ID you would like to listen to from that component
|
|
|
|
* @param handler A function pointer to call when the event is detected.
|
|
|
|
*/
|
2015-09-11 15:39:38 +00:00
|
|
|
MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent), uint16_t flags)
|
2015-09-09 23:04:27 +00:00
|
|
|
{
|
|
|
|
this->id = id;
|
|
|
|
this->value = value;
|
|
|
|
this->cb = handler;
|
|
|
|
this->cb_arg = NULL;
|
2015-09-11 15:39:38 +00:00
|
|
|
this->flags = flags;
|
2015-09-09 23:04:27 +00:00
|
|
|
this->next = NULL;
|
2015-10-28 14:17:12 +00:00
|
|
|
this->evt_queue = NULL;
|
2015-09-09 23:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* Create a new parameterised Message Bus Listener.
|
|
|
|
* @param id The ID of the component you want to listen to.
|
|
|
|
* @param value The event ID you would like to listen to from that component.
|
|
|
|
* @param handler A function pointer to call when the event is detected.
|
|
|
|
* @param arg An additional argument to pass to the event handler function.
|
|
|
|
*/
|
2015-09-11 15:39:38 +00:00
|
|
|
MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent, void *), void* arg, uint16_t flags)
|
2015-09-09 23:04:27 +00:00
|
|
|
{
|
|
|
|
this->id = id;
|
|
|
|
this->value = value;
|
|
|
|
this->cb_param = handler;
|
|
|
|
this->cb_arg = arg;
|
2015-09-11 15:39:38 +00:00
|
|
|
this->flags = flags | MESSAGE_BUS_LISTENER_PARAMETERISED;
|
2015-09-09 23:04:27 +00:00
|
|
|
this->next = NULL;
|
2015-10-28 14:17:12 +00:00
|
|
|
this->evt_queue = NULL;
|
2015-09-09 23:04:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 11:53:39 +00:00
|
|
|
/**
|
|
|
|
* Destructor. Ensures all resources used by this listener are freed.
|
|
|
|
*/
|
|
|
|
MicroBitListener::~MicroBitListener()
|
|
|
|
{
|
|
|
|
if(this->flags & MESSAGE_BUS_LISTENER_METHOD)
|
|
|
|
delete cb_method;
|
|
|
|
}
|
2015-09-11 15:39:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Queues and event up to be processed.
|
|
|
|
* @param e The event to queue
|
|
|
|
*/
|
|
|
|
void MicroBitListener::queue(MicroBitEvent e)
|
|
|
|
{
|
2015-10-31 10:27:38 +00:00
|
|
|
int queueDepth;
|
|
|
|
|
2015-09-11 15:39:38 +00:00
|
|
|
MicroBitEventQueueItem *p = evt_queue;
|
|
|
|
|
|
|
|
if (evt_queue == NULL)
|
2015-10-31 10:27:38 +00:00
|
|
|
evt_queue = new MicroBitEventQueueItem(e);
|
2015-09-11 15:39:38 +00:00
|
|
|
else
|
|
|
|
{
|
2015-10-31 10:27:38 +00:00
|
|
|
queueDepth = 1;
|
|
|
|
|
2015-09-11 15:39:38 +00:00
|
|
|
while (p->next != NULL)
|
2015-10-31 10:27:38 +00:00
|
|
|
{
|
2015-09-11 15:39:38 +00:00
|
|
|
p = p->next;
|
2015-10-31 10:27:38 +00:00
|
|
|
queueDepth++;
|
|
|
|
}
|
2015-09-11 15:39:38 +00:00
|
|
|
|
2015-10-31 10:27:38 +00:00
|
|
|
if (queueDepth < MESSAGE_BUS_LISTENER_MAX_QUEUE_DEPTH)
|
|
|
|
p->next = new MicroBitEventQueueItem(e);
|
2015-09-11 15:39:38 +00:00
|
|
|
}
|
|
|
|
}
|