https://github.com/WebBluetoothCG/demos/pull/42

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:18:14 2016 +0100
Revision:
35:8ce23bc1af38
Parent:
32:ece16b5987dd
Child:
37:b624ae5e94a5
Synchronized with git rev 732971e7
Author: James Devine
microbit-dal: Added events to MicroBitPin

Added rise, fall, pulse HI and LO events.

The pulse Hi and LO event timestamp given in the MicroBitEvent is the
duration for which the input was HI or LO for.

eventOn(int eventType) is used to configure the events generated
from the pin instance.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 1:8aa5cdb4ab67 1 /*
Jonathan Austin 1:8aa5cdb4ab67 2 The MIT License (MIT)
Jonathan Austin 1:8aa5cdb4ab67 3
Jonathan Austin 1:8aa5cdb4ab67 4 Copyright (c) 2016 British Broadcasting Corporation.
Jonathan Austin 1:8aa5cdb4ab67 5 This software is provided by Lancaster University by arrangement with the BBC.
Jonathan Austin 1:8aa5cdb4ab67 6
Jonathan Austin 1:8aa5cdb4ab67 7 Permission is hereby granted, free of charge, to any person obtaining a
Jonathan Austin 1:8aa5cdb4ab67 8 copy of this software and associated documentation files (the "Software"),
Jonathan Austin 1:8aa5cdb4ab67 9 to deal in the Software without restriction, including without limitation
Jonathan Austin 1:8aa5cdb4ab67 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
Jonathan Austin 1:8aa5cdb4ab67 11 and/or sell copies of the Software, and to permit persons to whom the
Jonathan Austin 1:8aa5cdb4ab67 12 Software is furnished to do so, subject to the following conditions:
Jonathan Austin 1:8aa5cdb4ab67 13
Jonathan Austin 1:8aa5cdb4ab67 14 The above copyright notice and this permission notice shall be included in
Jonathan Austin 1:8aa5cdb4ab67 15 all copies or substantial portions of the Software.
Jonathan Austin 1:8aa5cdb4ab67 16
Jonathan Austin 1:8aa5cdb4ab67 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jonathan Austin 1:8aa5cdb4ab67 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jonathan Austin 1:8aa5cdb4ab67 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Jonathan Austin 1:8aa5cdb4ab67 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Jonathan Austin 1:8aa5cdb4ab67 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 23 DEALINGS IN THE SOFTWARE.
Jonathan Austin 1:8aa5cdb4ab67 24 */
Jonathan Austin 1:8aa5cdb4ab67 25
Jonathan Austin 1:8aa5cdb4ab67 26 /**
Jonathan Austin 1:8aa5cdb4ab67 27 * Class definition for a MicroBitEvent
Jonathan Austin 1:8aa5cdb4ab67 28 *
Jonathan Austin 1:8aa5cdb4ab67 29 * It represents a common event that is generated by the various components on the micro:bit.
Jonathan Austin 1:8aa5cdb4ab67 30 */
Jonathan Austin 1:8aa5cdb4ab67 31 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 32 #include "MicroBitEvent.h"
Jonathan Austin 1:8aa5cdb4ab67 33 #include "MicroBitSystemTimer.h"
Jonathan Austin 1:8aa5cdb4ab67 34 #include "EventModel.h"
Jonathan Austin 1:8aa5cdb4ab67 35
Jonathan Austin 1:8aa5cdb4ab67 36 EventModel* EventModel::defaultEventBus = NULL;
Jonathan Austin 1:8aa5cdb4ab67 37
Jonathan Austin 1:8aa5cdb4ab67 38 /**
Jonathan Austin 1:8aa5cdb4ab67 39 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 40 *
Jonathan Austin 1:8aa5cdb4ab67 41 * @param src The id of the MicroBit Component that generated the event e.g. MICROBIT_ID_BUTTON_A.
Jonathan Austin 1:8aa5cdb4ab67 42 *
Jonathan Austin 1:8aa5cdb4ab67 43 * @param value A component specific code indicating the cause of the event.
Jonathan Austin 1:8aa5cdb4ab67 44 *
Jonathan Austin 1:8aa5cdb4ab67 45 * @param mode Optional definition of how the event should be processed after construction (if at all):
Jonathan Austin 1:8aa5cdb4ab67 46 * CREATE_ONLY: MicroBitEvent is initialised, and no further processing takes place.
Jonathan Austin 1:8aa5cdb4ab67 47 * CREATE_AND_FIRE: MicroBitEvent is initialised, and its event handlers are immediately fired (not suitable for use in interrupts!).
Jonathan Austin 1:8aa5cdb4ab67 48 *
Jonathan Austin 1:8aa5cdb4ab67 49 * @code
Jonathan Austin 1:8aa5cdb4ab67 50 * // Create and launch an event using the default configuration
Jonathan Austin 1:8aa5cdb4ab67 51 * MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK);
Jonathan Austin 1:8aa5cdb4ab67 52 *
Jonathan Austin 1:8aa5cdb4ab67 53 * // Create an event only, do not fire onto an EventModel.
Jonathan Austin 1:8aa5cdb4ab67 54 * MicrobitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK,CREATE_AND_FIRE);
Jonathan Austin 1:8aa5cdb4ab67 55 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 56 */
Jonathan Austin 1:8aa5cdb4ab67 57 MicroBitEvent::MicroBitEvent(uint16_t source, uint16_t value, MicroBitEventLaunchMode mode)
Jonathan Austin 1:8aa5cdb4ab67 58 {
Jonathan Austin 1:8aa5cdb4ab67 59 this->source = source;
Jonathan Austin 1:8aa5cdb4ab67 60 this->value = value;
LancasterUniversity 35:8ce23bc1af38 61 this->timestamp = system_timer_current_time_us();
Jonathan Austin 1:8aa5cdb4ab67 62
Jonathan Austin 1:8aa5cdb4ab67 63 if(mode != CREATE_ONLY)
Jonathan Austin 1:8aa5cdb4ab67 64 this->fire();
Jonathan Austin 1:8aa5cdb4ab67 65 }
Jonathan Austin 1:8aa5cdb4ab67 66
Jonathan Austin 1:8aa5cdb4ab67 67 /**
Jonathan Austin 1:8aa5cdb4ab67 68 * Default constructor - initialises all values, and sets timestamp to the current time.
Jonathan Austin 1:8aa5cdb4ab67 69 */
Jonathan Austin 1:8aa5cdb4ab67 70 MicroBitEvent::MicroBitEvent()
Jonathan Austin 1:8aa5cdb4ab67 71 {
Jonathan Austin 1:8aa5cdb4ab67 72 this->source = 0;
Jonathan Austin 1:8aa5cdb4ab67 73 this->value = 0;
LancasterUniversity 35:8ce23bc1af38 74 this->timestamp = system_timer_current_time_us();
Jonathan Austin 1:8aa5cdb4ab67 75 }
Jonathan Austin 1:8aa5cdb4ab67 76
Jonathan Austin 1:8aa5cdb4ab67 77 /**
Jonathan Austin 1:8aa5cdb4ab67 78 * Fires this MicroBitEvent onto the Default EventModel, or a custom one!
Jonathan Austin 1:8aa5cdb4ab67 79 */
Jonathan Austin 1:8aa5cdb4ab67 80 void MicroBitEvent::fire()
Jonathan Austin 1:8aa5cdb4ab67 81 {
Jonathan Austin 1:8aa5cdb4ab67 82 if(EventModel::defaultEventBus)
Jonathan Austin 1:8aa5cdb4ab67 83 EventModel::defaultEventBus->send(*this);
Jonathan Austin 1:8aa5cdb4ab67 84 }
Jonathan Austin 1:8aa5cdb4ab67 85
Jonathan Austin 1:8aa5cdb4ab67 86
Jonathan Austin 1:8aa5cdb4ab67 87 /**
Jonathan Austin 1:8aa5cdb4ab67 88 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 89 * Create a new MicroBitEventQueueItem.
Jonathan Austin 1:8aa5cdb4ab67 90 *
Jonathan Austin 1:8aa5cdb4ab67 91 * @param evt The event to be queued.
Jonathan Austin 1:8aa5cdb4ab67 92 */
Jonathan Austin 1:8aa5cdb4ab67 93 MicroBitEventQueueItem::MicroBitEventQueueItem(MicroBitEvent evt)
Jonathan Austin 1:8aa5cdb4ab67 94 {
Jonathan Austin 1:8aa5cdb4ab67 95 this->evt = evt;
Jonathan Austin 1:8aa5cdb4ab67 96 this->next = NULL;
LancasterUniversity 31:87789e55bac7 97 }