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 MicroBitEvent.cpp Source File

MicroBitEvent.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   * Class definition for a MicroBitEvent
00028   *
00029   * It represents a common event that is generated by the various components on the micro:bit.
00030   */
00031 #include "MicroBitConfig.h"
00032 #include "MicroBitEvent.h"
00033 #include "MicroBitSystemTimer.h"
00034 #include "EventModel.h"
00035 
00036 EventModel* EventModel::defaultEventBus = NULL;
00037 
00038 /**
00039   * Constructor.
00040   *
00041   * @param src The id of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
00042   *
00043   * @param value A component specific code indicating the cause of the event.
00044   *
00045   * @param mode Optional definition of how the event should be processed after construction (if at all):
00046   *                 CREATE_ONLY: MicroBitEvent is initialised, and no further processing takes place.
00047   *                 CREATE_AND_FIRE: MicroBitEvent is initialised, and its event handlers are immediately fired (not suitable for use in interrupts!).
00048   *
00049   * @code
00050   * // Create and launch an event using the default configuration
00051   * MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK);
00052   *
00053   * // Create an event only, do not fire onto an EventModel.
00054   * MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK,CREATE_AND_FIRE);
00055   * @endcode
00056   */
00057 MicroBitEvent::MicroBitEvent(uint16_t source, uint16_t value, MicroBitEventLaunchMode mode)
00058 {
00059     this->source = source;
00060     this->value = value;
00061     this->timestamp = system_timer_current_time_us();
00062 
00063     if(mode != CREATE_ONLY)
00064         this->fire();
00065 }
00066 
00067 /**
00068   * Default constructor - initialises all values, and sets timestamp to the current time.
00069   */
00070 MicroBitEvent::MicroBitEvent()
00071 {
00072     this->source = 0;
00073     this->value = 0;
00074     this->timestamp = system_timer_current_time_us();
00075 }
00076 
00077 /**
00078   * Fires this MicroBitEvent onto the Default EventModel, or a custom one!
00079   */
00080 void MicroBitEvent::fire()
00081 {
00082     if(EventModel::defaultEventBus)
00083         EventModel::defaultEventBus->send(*this);
00084 }
00085 
00086 
00087 /**
00088   * Constructor.
00089   * Create a new MicroBitEventQueueItem.
00090   *
00091   * @param evt The event to be queued.
00092   */
00093 MicroBitEventQueueItem::MicroBitEventQueueItem(MicroBitEvent evt)
00094 {
00095     this->evt = evt;
00096     this->next = NULL;
00097 }