Attempting to publish a tree

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitListener.cpp Source File

MicroBitListener.cpp

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 /**
00027   * This structure defines a MicroBitListener used to invoke functions, or member
00028   * functions if an instance of EventModel receives an event whose id and value
00029   * match this MicroBitListener's id and value.
00030   */
00031 #include "MicroBitConfig.h"
00032 #include "MicroBitListener.h"
00033 
00034 /**
00035   * Constructor.
00036   *
00037   * Create a new Message Bus Listener.
00038   *
00039   * @param id The ID of the component you want to listen to.
00040   *
00041   * @param value The event value you would like to listen to from that component
00042   *
00043   * @param handler A function pointer to call when the event is detected.
00044   *
00045   * @param flags User specified, implementation specific flags, that allow behaviour of this events listener
00046   * to be tuned.
00047   */
00048 MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent), uint16_t flags)
00049 {
00050     this->id = id;
00051     this->value = value;
00052     this->cb = handler;
00053     this->cb_arg = NULL;
00054     this->flags = flags;
00055     this->next = NULL;
00056     this->evt_queue = NULL;
00057 }
00058 
00059 /**
00060   * Constructor.
00061   *
00062   * Create a new Message Bus Listener, this constructor accepts an additional
00063   * parameter "arg", which is passed to the handler.
00064   *
00065   * @param id The ID of the component you want to listen to.
00066   *
00067   * @param value The event value you would like to listen to from that component
00068   *
00069   * @param handler A function pointer to call when the event is detected.
00070   *
00071   * @param arg A pointer to some data that will be given to the handler.
00072   *
00073   * @param flags User specified, implementation specific flags, that allow behaviour of this events listener
00074   * to be tuned.
00075   */
00076 MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent, void *), void* arg, uint16_t flags)
00077 {
00078     this->id = id;
00079     this->value = value;
00080     this->cb_param = handler;
00081     this->cb_arg = arg;
00082     this->flags = flags | MESSAGE_BUS_LISTENER_PARAMETERISED;
00083     this->next = NULL;
00084     this->evt_queue = NULL;
00085 }
00086 
00087 /**
00088   * Destructor. Ensures all resources used by this listener are freed.
00089   */
00090 MicroBitListener::~MicroBitListener()
00091 {
00092     if(this->flags & MESSAGE_BUS_LISTENER_METHOD)
00093         delete cb_method;
00094 }
00095 
00096 /**
00097   * Queues and event up to be processed.
00098   *
00099   * @param e The event to queue
00100   */
00101 void MicroBitListener::queue(MicroBitEvent e)
00102 {
00103     int queueDepth;
00104 
00105     MicroBitEventQueueItem *p = evt_queue;
00106 
00107     if (evt_queue == NULL)
00108         evt_queue = new MicroBitEventQueueItem(e);
00109     else
00110     {
00111         queueDepth = 1;
00112 
00113         while (p->next != NULL)
00114         {
00115             p = p->next;
00116             queueDepth++;
00117         }
00118 
00119         if (queueDepth < MESSAGE_BUS_LISTENER_MAX_QUEUE_DEPTH)
00120             p->next = new MicroBitEventQueueItem(e);
00121     }
00122 }