Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 #ifndef MICROBIT_BUTTON_H
MrBedfordVan 0:b9164b348919 27 #define MICROBIT_BUTTON_H
MrBedfordVan 0:b9164b348919 28
MrBedfordVan 0:b9164b348919 29 #include "mbed.h"
MrBedfordVan 0:b9164b348919 30 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 31 #include "MicroBitComponent.h"
MrBedfordVan 0:b9164b348919 32 #include "MicroBitEvent.h"
MrBedfordVan 0:b9164b348919 33
MrBedfordVan 0:b9164b348919 34 #define MICROBIT_PIN_BUTTON_A P0_17
MrBedfordVan 0:b9164b348919 35 #define MICROBIT_PIN_BUTTON_B P0_26
MrBedfordVan 0:b9164b348919 36 #define MICROBIT_PIN_BUTTON_RESET P0_19
MrBedfordVan 0:b9164b348919 37
MrBedfordVan 0:b9164b348919 38 #define MICROBIT_BUTTON_EVT_DOWN 1
MrBedfordVan 0:b9164b348919 39 #define MICROBIT_BUTTON_EVT_UP 2
MrBedfordVan 0:b9164b348919 40 #define MICROBIT_BUTTON_EVT_CLICK 3
MrBedfordVan 0:b9164b348919 41 #define MICROBIT_BUTTON_EVT_LONG_CLICK 4
MrBedfordVan 0:b9164b348919 42 #define MICROBIT_BUTTON_EVT_HOLD 5
MrBedfordVan 0:b9164b348919 43 #define MICROBIT_BUTTON_EVT_DOUBLE_CLICK 6
MrBedfordVan 0:b9164b348919 44
MrBedfordVan 0:b9164b348919 45 #define MICROBIT_BUTTON_LONG_CLICK_TIME 1000
MrBedfordVan 0:b9164b348919 46 #define MICROBIT_BUTTON_HOLD_TIME 1500
MrBedfordVan 0:b9164b348919 47
MrBedfordVan 0:b9164b348919 48 #define MICROBIT_BUTTON_STATE 1
MrBedfordVan 0:b9164b348919 49 #define MICROBIT_BUTTON_STATE_HOLD_TRIGGERED 2
MrBedfordVan 0:b9164b348919 50 #define MICROBIT_BUTTON_STATE_CLICK 4
MrBedfordVan 0:b9164b348919 51 #define MICROBIT_BUTTON_STATE_LONG_CLICK 8
MrBedfordVan 0:b9164b348919 52
MrBedfordVan 0:b9164b348919 53 #define MICROBIT_BUTTON_SIGMA_MIN 0
MrBedfordVan 0:b9164b348919 54 #define MICROBIT_BUTTON_SIGMA_MAX 12
MrBedfordVan 0:b9164b348919 55 #define MICROBIT_BUTTON_SIGMA_THRESH_HI 8
MrBedfordVan 0:b9164b348919 56 #define MICROBIT_BUTTON_SIGMA_THRESH_LO 2
MrBedfordVan 0:b9164b348919 57 #define MICROBIT_BUTTON_DOUBLE_CLICK_THRESH 50
MrBedfordVan 0:b9164b348919 58
MrBedfordVan 0:b9164b348919 59 enum MicroBitButtonEventConfiguration
MrBedfordVan 0:b9164b348919 60 {
MrBedfordVan 0:b9164b348919 61 MICROBIT_BUTTON_SIMPLE_EVENTS,
MrBedfordVan 0:b9164b348919 62 MICROBIT_BUTTON_ALL_EVENTS
MrBedfordVan 0:b9164b348919 63 };
MrBedfordVan 0:b9164b348919 64
MrBedfordVan 0:b9164b348919 65
MrBedfordVan 0:b9164b348919 66 /**
MrBedfordVan 0:b9164b348919 67 * Class definition for MicroBit Button.
MrBedfordVan 0:b9164b348919 68 *
MrBedfordVan 0:b9164b348919 69 * Represents a single, generic button on the device.
MrBedfordVan 0:b9164b348919 70 */
MrBedfordVan 0:b9164b348919 71 class MicroBitButton : public MicroBitComponent
MrBedfordVan 0:b9164b348919 72 {
MrBedfordVan 0:b9164b348919 73 PinName name; // mbed pin name for this button.
MrBedfordVan 0:b9164b348919 74 DigitalIn pin; // The mbed object looking after this pin at any point in time (may change!).
MrBedfordVan 0:b9164b348919 75
MrBedfordVan 0:b9164b348919 76 unsigned long downStartTime; // used to store the current system clock when a button down event occurs
MrBedfordVan 0:b9164b348919 77 uint8_t sigma; // integration of samples over time. We use this for debouncing, and noise tolerance for touch sensing
MrBedfordVan 0:b9164b348919 78 MicroBitButtonEventConfiguration eventConfiguration; // Do we want to generate high level event (clicks), or defer this to another service.
MrBedfordVan 0:b9164b348919 79
MrBedfordVan 0:b9164b348919 80 public:
MrBedfordVan 0:b9164b348919 81
MrBedfordVan 0:b9164b348919 82 /**
MrBedfordVan 0:b9164b348919 83 * Constructor.
MrBedfordVan 0:b9164b348919 84 *
MrBedfordVan 0:b9164b348919 85 * Create a software representation of a button.
MrBedfordVan 0:b9164b348919 86 *
MrBedfordVan 0:b9164b348919 87 * @param name the physical pin on the processor that should be used as input.
MrBedfordVan 0:b9164b348919 88 *
MrBedfordVan 0:b9164b348919 89 * @param id the ID of the new MicroBitButton object.
MrBedfordVan 0:b9164b348919 90 *
MrBedfordVan 0:b9164b348919 91 * @param eventConfiguration Configures the events that will be generated by this MicroBitButton instance.
MrBedfordVan 0:b9164b348919 92 * Defaults to MICROBIT_BUTTON_ALL_EVENTS.
MrBedfordVan 0:b9164b348919 93 *
MrBedfordVan 0:b9164b348919 94 * @param mode the configuration of internal pullups/pulldowns, as defined in the mbed PinMode class. PullNone by default.
MrBedfordVan 0:b9164b348919 95 *
MrBedfordVan 0:b9164b348919 96 * @code
MrBedfordVan 0:b9164b348919 97 * buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
MrBedfordVan 0:b9164b348919 98 * @endcode
MrBedfordVan 0:b9164b348919 99 */
MrBedfordVan 0:b9164b348919 100 MicroBitButton(PinName name, uint16_t id, MicroBitButtonEventConfiguration eventConfiguration = MICROBIT_BUTTON_ALL_EVENTS, PinMode mode = PullNone);
MrBedfordVan 0:b9164b348919 101
MrBedfordVan 0:b9164b348919 102 /**
MrBedfordVan 0:b9164b348919 103 * Tests if this Button is currently pressed.
MrBedfordVan 0:b9164b348919 104 *
MrBedfordVan 0:b9164b348919 105 * @code
MrBedfordVan 0:b9164b348919 106 * if(buttonA.isPressed())
MrBedfordVan 0:b9164b348919 107 * display.scroll("Pressed!");
MrBedfordVan 0:b9164b348919 108 * @endcode
MrBedfordVan 0:b9164b348919 109 *
MrBedfordVan 0:b9164b348919 110 * @return 1 if this button is pressed, 0 otherwise.
MrBedfordVan 0:b9164b348919 111 */
MrBedfordVan 0:b9164b348919 112 int isPressed();
MrBedfordVan 0:b9164b348919 113
MrBedfordVan 0:b9164b348919 114 /**
MrBedfordVan 0:b9164b348919 115 * Changes the event configuration used by this button to the given MicroBitButtonEventConfiguration.
MrBedfordVan 0:b9164b348919 116 *
MrBedfordVan 0:b9164b348919 117 * All subsequent events generated by this button will then be informed by this configuraiton.
MrBedfordVan 0:b9164b348919 118 *
MrBedfordVan 0:b9164b348919 119 * @param config The new configuration for this button. Legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS.
MrBedfordVan 0:b9164b348919 120 *
MrBedfordVan 0:b9164b348919 121 * Example:
MrBedfordVan 0:b9164b348919 122 * @code
MrBedfordVan 0:b9164b348919 123 * // Configure a button to generate all possible events.
MrBedfordVan 0:b9164b348919 124 * buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS);
MrBedfordVan 0:b9164b348919 125 *
MrBedfordVan 0:b9164b348919 126 * // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events.
MrBedfordVan 0:b9164b348919 127 * buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS);
MrBedfordVan 0:b9164b348919 128 * @endcode
MrBedfordVan 0:b9164b348919 129 */
MrBedfordVan 0:b9164b348919 130 void setEventConfiguration(MicroBitButtonEventConfiguration config);
MrBedfordVan 0:b9164b348919 131
MrBedfordVan 0:b9164b348919 132 /**
MrBedfordVan 0:b9164b348919 133 * periodic callback from MicroBit system timer.
MrBedfordVan 0:b9164b348919 134 *
MrBedfordVan 0:b9164b348919 135 * Check for state change for this button, and fires various events on a state change.
MrBedfordVan 0:b9164b348919 136 */
MrBedfordVan 0:b9164b348919 137 virtual void systemTick();
MrBedfordVan 0:b9164b348919 138
MrBedfordVan 0:b9164b348919 139 /**
MrBedfordVan 0:b9164b348919 140 * Destructor for MicroBitButton, where we deregister this instance from the array of fiber components.
MrBedfordVan 0:b9164b348919 141 */
MrBedfordVan 0:b9164b348919 142 ~MicroBitButton();
MrBedfordVan 0:b9164b348919 143 };
MrBedfordVan 0:b9164b348919 144
MrBedfordVan 0:b9164b348919 145 #endif