mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitListener.h Source File

MicroBitListener.h

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 #ifndef MICROBIT_LISTENER_H
00027 #define MICROBIT_LISTENER_H
00028 
00029 #include "mbed.h"
00030 #include "MicroBitConfig.h"
00031 #include "MicroBitEvent.h"
00032 #include "MemberFunctionCallback.h"
00033 #include "MicroBitConfig.h"
00034 
00035 // MicroBitListener flags...
00036 #define MESSAGE_BUS_LISTENER_PARAMETERISED          0x0001
00037 #define MESSAGE_BUS_LISTENER_METHOD                 0x0002
00038 #define MESSAGE_BUS_LISTENER_BUSY                   0x0004
00039 #define MESSAGE_BUS_LISTENER_REENTRANT              0x0008
00040 #define MESSAGE_BUS_LISTENER_QUEUE_IF_BUSY          0x0010
00041 #define MESSAGE_BUS_LISTENER_DROP_IF_BUSY           0x0020
00042 #define MESSAGE_BUS_LISTENER_NONBLOCKING            0x0040
00043 #define MESSAGE_BUS_LISTENER_URGENT                 0x0080
00044 #define MESSAGE_BUS_LISTENER_DELETING               0x8000
00045 
00046 #define MESSAGE_BUS_LISTENER_IMMEDIATE              (MESSAGE_BUS_LISTENER_NONBLOCKING |  MESSAGE_BUS_LISTENER_URGENT)
00047 
00048 /**
00049   * This structure defines a MicroBitListener used to invoke functions, or member
00050   * functions if an instance of EventModel receives an event whose id and value
00051   * match this MicroBitListener's id and value.
00052   */
00053 struct MicroBitListener
00054 {
00055     uint16_t        id;             // The ID of the component that this listener is interested in.
00056     uint16_t        value;          // Value this listener is interested in receiving.
00057     uint16_t        flags;          // Status and configuration options codes for this listener.
00058 
00059     union
00060     {
00061         void (*cb)(MicroBitEvent);
00062         void (*cb_param)(MicroBitEvent, void *);
00063         MemberFunctionCallback *cb_method;
00064     };
00065 
00066     void*           cb_arg;         // Optional argument to be passed to the caller.
00067 
00068     MicroBitEvent               evt;
00069     MicroBitEventQueueItem      *evt_queue;
00070 
00071     MicroBitListener *next;
00072 
00073     /**
00074       * Constructor.
00075       *
00076       * Create a new Message Bus Listener.
00077       *
00078       * @param id The ID of the component you want to listen to.
00079       *
00080       * @param value The event value you would like to listen to from that component
00081       *
00082       * @param handler A function pointer to call when the event is detected.
00083       *
00084       * @param flags User specified, implementation specific flags, that allow behaviour of this events listener
00085       * to be tuned.
00086       */
00087     MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent), uint16_t flags = EVENT_LISTENER_DEFAULT_FLAGS);
00088 
00089     /**
00090       * Constructor.
00091       *
00092       * Create a new Message Bus Listener, this constructor accepts an additional
00093       * parameter "arg", which is passed to the handler.
00094       *
00095       * @param id The ID of the component you want to listen to.
00096       *
00097       * @param value The event value you would like to listen to from that component
00098       *
00099       * @param handler A function pointer to call when the event is detected.
00100       *
00101       * @param arg A pointer to some data that will be given to the handler.
00102       *
00103       * @param flags User specified, implementation specific flags, that allow behaviour of this events listener
00104       * to be tuned.
00105       */
00106     MicroBitListener(uint16_t id, uint16_t value, void (*handler)(MicroBitEvent, void *), void* arg, uint16_t flags = EVENT_LISTENER_DEFAULT_FLAGS);
00107 
00108 
00109     /**
00110       * Constructor.
00111       *
00112       * Create a new Message Bus Listener, with a callback to a C++ member function.
00113       *
00114       * @param id The ID of the component you want to listen to.
00115       *
00116       * @param value The event value you would like to listen to from that component
00117       *
00118       * @param object The C++ object on which to call the event handler.
00119       *
00120       * @param method The method within the C++ object to call.
00121       *
00122       * @param flags User specified, implementation specific flags, that allow behaviour of this events listener
00123       * to be tuned.
00124       */
00125     template <typename T>
00126     MicroBitListener(uint16_t id, uint16_t value, T* object, void (T::*method)(MicroBitEvent), uint16_t flags = EVENT_LISTENER_DEFAULT_FLAGS);
00127 
00128     /**
00129       * Destructor. Ensures all resources used by this listener are freed.
00130       */
00131     ~MicroBitListener();
00132 
00133     /**
00134       * Queues and event up to be processed.
00135       *
00136       * @param e The event to queue
00137       */
00138     void queue(MicroBitEvent e);
00139 };
00140 
00141 /**
00142   * Constructor.
00143   *
00144   * Create a new Message Bus Listener, with a callback to a C++ member function.
00145   *
00146   * @param id The ID of the component you want to listen to.
00147   *
00148   * @param value The event value you would like to listen to from that component
00149   *
00150   * @param object The C++ object on which to call the event handler.
00151   *
00152   * @param method The method within the C++ object to call.
00153   *
00154   * @param flags User specified, implementation specific flags, that allow behaviour of this events listener
00155   * to be tuned.
00156   */
00157 template <typename T>
00158 MicroBitListener::MicroBitListener(uint16_t id, uint16_t value, T* object, void (T::*method)(MicroBitEvent), uint16_t flags)
00159 {
00160     this->id = id;
00161     this->value = value;
00162     this->cb_method = new MemberFunctionCallback(object, method);
00163     this->cb_arg = NULL;
00164     this->flags = flags | MESSAGE_BUS_LISTENER_METHOD;
00165     this->evt_queue = NULL;
00166     this->next = NULL;
00167 }
00168 
00169 #endif