Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/source/drivers/MicroBitButton.cpp@0:b9164b348919, 2016-10-17 (annotated)
- 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?
User | Revision | Line number | New 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 | #include "MicroBitConfig.h" |
MrBedfordVan | 0:b9164b348919 | 27 | #include "MicroBitButton.h" |
MrBedfordVan | 0:b9164b348919 | 28 | #include "MicroBitSystemTimer.h" |
MrBedfordVan | 0:b9164b348919 | 29 | |
MrBedfordVan | 0:b9164b348919 | 30 | /** |
MrBedfordVan | 0:b9164b348919 | 31 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 32 | * |
MrBedfordVan | 0:b9164b348919 | 33 | * Create a software representation of a button. |
MrBedfordVan | 0:b9164b348919 | 34 | * |
MrBedfordVan | 0:b9164b348919 | 35 | * @param name the physical pin on the processor that should be used as input. |
MrBedfordVan | 0:b9164b348919 | 36 | * |
MrBedfordVan | 0:b9164b348919 | 37 | * @param id the ID of the new MicroBitButton object. |
MrBedfordVan | 0:b9164b348919 | 38 | * |
MrBedfordVan | 0:b9164b348919 | 39 | * @param eventConfiguration Configures the events that will be generated by this MicroBitButton instance. |
MrBedfordVan | 0:b9164b348919 | 40 | * Defaults to MICROBIT_BUTTON_ALL_EVENTS. |
MrBedfordVan | 0:b9164b348919 | 41 | * |
MrBedfordVan | 0:b9164b348919 | 42 | * @param mode the configuration of internal pullups/pulldowns, as defined in the mbed PinMode class. PullNone by default. |
MrBedfordVan | 0:b9164b348919 | 43 | * |
MrBedfordVan | 0:b9164b348919 | 44 | * @code |
MrBedfordVan | 0:b9164b348919 | 45 | * buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A); |
MrBedfordVan | 0:b9164b348919 | 46 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 47 | */ |
MrBedfordVan | 0:b9164b348919 | 48 | MicroBitButton::MicroBitButton(PinName name, uint16_t id, MicroBitButtonEventConfiguration eventConfiguration, PinMode mode) : pin(name, mode) |
MrBedfordVan | 0:b9164b348919 | 49 | { |
MrBedfordVan | 0:b9164b348919 | 50 | this->id = id; |
MrBedfordVan | 0:b9164b348919 | 51 | this->name = name; |
MrBedfordVan | 0:b9164b348919 | 52 | this->eventConfiguration = eventConfiguration; |
MrBedfordVan | 0:b9164b348919 | 53 | this->downStartTime = 0; |
MrBedfordVan | 0:b9164b348919 | 54 | this->sigma = 0; |
MrBedfordVan | 0:b9164b348919 | 55 | system_timer_add_component(this); |
MrBedfordVan | 0:b9164b348919 | 56 | } |
MrBedfordVan | 0:b9164b348919 | 57 | |
MrBedfordVan | 0:b9164b348919 | 58 | /** |
MrBedfordVan | 0:b9164b348919 | 59 | * Changes the event configuration used by this button to the given MicroBitButtonEventConfiguration. |
MrBedfordVan | 0:b9164b348919 | 60 | * |
MrBedfordVan | 0:b9164b348919 | 61 | * All subsequent events generated by this button will then be informed by this configuraiton. |
MrBedfordVan | 0:b9164b348919 | 62 | * |
MrBedfordVan | 0:b9164b348919 | 63 | * @param config The new configuration for this button. Legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS. |
MrBedfordVan | 0:b9164b348919 | 64 | * |
MrBedfordVan | 0:b9164b348919 | 65 | * Example: |
MrBedfordVan | 0:b9164b348919 | 66 | * @code |
MrBedfordVan | 0:b9164b348919 | 67 | * // Configure a button to generate all possible events. |
MrBedfordVan | 0:b9164b348919 | 68 | * buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS); |
MrBedfordVan | 0:b9164b348919 | 69 | * |
MrBedfordVan | 0:b9164b348919 | 70 | * // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events. |
MrBedfordVan | 0:b9164b348919 | 71 | * buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); |
MrBedfordVan | 0:b9164b348919 | 72 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 73 | */ |
MrBedfordVan | 0:b9164b348919 | 74 | void MicroBitButton::setEventConfiguration(MicroBitButtonEventConfiguration config) |
MrBedfordVan | 0:b9164b348919 | 75 | { |
MrBedfordVan | 0:b9164b348919 | 76 | this->eventConfiguration = config; |
MrBedfordVan | 0:b9164b348919 | 77 | } |
MrBedfordVan | 0:b9164b348919 | 78 | |
MrBedfordVan | 0:b9164b348919 | 79 | /** |
MrBedfordVan | 0:b9164b348919 | 80 | * periodic callback from MicroBit system timer. |
MrBedfordVan | 0:b9164b348919 | 81 | * |
MrBedfordVan | 0:b9164b348919 | 82 | * Check for state change for this button, and fires various events on a state change. |
MrBedfordVan | 0:b9164b348919 | 83 | */ |
MrBedfordVan | 0:b9164b348919 | 84 | void MicroBitButton::systemTick() |
MrBedfordVan | 0:b9164b348919 | 85 | { |
MrBedfordVan | 0:b9164b348919 | 86 | // |
MrBedfordVan | 0:b9164b348919 | 87 | // If the pin is pulled low (touched), increment our culumative counter. |
MrBedfordVan | 0:b9164b348919 | 88 | // otherwise, decrement it. We're essentially building a lazy follower here. |
MrBedfordVan | 0:b9164b348919 | 89 | // This makes the output debounced for buttons, and desensitizes touch sensors |
MrBedfordVan | 0:b9164b348919 | 90 | // (particularly in environments where there is mains noise!) |
MrBedfordVan | 0:b9164b348919 | 91 | // |
MrBedfordVan | 0:b9164b348919 | 92 | if(!pin) |
MrBedfordVan | 0:b9164b348919 | 93 | { |
MrBedfordVan | 0:b9164b348919 | 94 | if (sigma < MICROBIT_BUTTON_SIGMA_MAX) |
MrBedfordVan | 0:b9164b348919 | 95 | sigma++; |
MrBedfordVan | 0:b9164b348919 | 96 | } |
MrBedfordVan | 0:b9164b348919 | 97 | else |
MrBedfordVan | 0:b9164b348919 | 98 | { |
MrBedfordVan | 0:b9164b348919 | 99 | if (sigma > MICROBIT_BUTTON_SIGMA_MIN) |
MrBedfordVan | 0:b9164b348919 | 100 | sigma--; |
MrBedfordVan | 0:b9164b348919 | 101 | } |
MrBedfordVan | 0:b9164b348919 | 102 | |
MrBedfordVan | 0:b9164b348919 | 103 | // Check to see if we have off->on state change. |
MrBedfordVan | 0:b9164b348919 | 104 | if(sigma > MICROBIT_BUTTON_SIGMA_THRESH_HI && !(status & MICROBIT_BUTTON_STATE)) |
MrBedfordVan | 0:b9164b348919 | 105 | { |
MrBedfordVan | 0:b9164b348919 | 106 | // Record we have a state change, and raise an event. |
MrBedfordVan | 0:b9164b348919 | 107 | status |= MICROBIT_BUTTON_STATE; |
MrBedfordVan | 0:b9164b348919 | 108 | MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_DOWN); |
MrBedfordVan | 0:b9164b348919 | 109 | |
MrBedfordVan | 0:b9164b348919 | 110 | //Record the time the button was pressed. |
MrBedfordVan | 0:b9164b348919 | 111 | downStartTime = system_timer_current_time(); |
MrBedfordVan | 0:b9164b348919 | 112 | } |
MrBedfordVan | 0:b9164b348919 | 113 | |
MrBedfordVan | 0:b9164b348919 | 114 | // Check to see if we have on->off state change. |
MrBedfordVan | 0:b9164b348919 | 115 | if(sigma < MICROBIT_BUTTON_SIGMA_THRESH_LO && (status & MICROBIT_BUTTON_STATE)) |
MrBedfordVan | 0:b9164b348919 | 116 | { |
MrBedfordVan | 0:b9164b348919 | 117 | status = 0; |
MrBedfordVan | 0:b9164b348919 | 118 | MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_UP); |
MrBedfordVan | 0:b9164b348919 | 119 | |
MrBedfordVan | 0:b9164b348919 | 120 | if (eventConfiguration == MICROBIT_BUTTON_ALL_EVENTS) |
MrBedfordVan | 0:b9164b348919 | 121 | { |
MrBedfordVan | 0:b9164b348919 | 122 | //determine if this is a long click or a normal click and send event |
MrBedfordVan | 0:b9164b348919 | 123 | if((system_timer_current_time() - downStartTime) >= MICROBIT_BUTTON_LONG_CLICK_TIME) |
MrBedfordVan | 0:b9164b348919 | 124 | MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_LONG_CLICK); |
MrBedfordVan | 0:b9164b348919 | 125 | else |
MrBedfordVan | 0:b9164b348919 | 126 | MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_CLICK); |
MrBedfordVan | 0:b9164b348919 | 127 | } |
MrBedfordVan | 0:b9164b348919 | 128 | } |
MrBedfordVan | 0:b9164b348919 | 129 | |
MrBedfordVan | 0:b9164b348919 | 130 | //if button is pressed and the hold triggered event state is not triggered AND we are greater than the button debounce value |
MrBedfordVan | 0:b9164b348919 | 131 | if((status & MICROBIT_BUTTON_STATE) && !(status & MICROBIT_BUTTON_STATE_HOLD_TRIGGERED) && (system_timer_current_time() - downStartTime) >= MICROBIT_BUTTON_HOLD_TIME) |
MrBedfordVan | 0:b9164b348919 | 132 | { |
MrBedfordVan | 0:b9164b348919 | 133 | //set the hold triggered event flag |
MrBedfordVan | 0:b9164b348919 | 134 | status |= MICROBIT_BUTTON_STATE_HOLD_TRIGGERED; |
MrBedfordVan | 0:b9164b348919 | 135 | |
MrBedfordVan | 0:b9164b348919 | 136 | //fire hold event |
MrBedfordVan | 0:b9164b348919 | 137 | MicroBitEvent evt(id,MICROBIT_BUTTON_EVT_HOLD); |
MrBedfordVan | 0:b9164b348919 | 138 | } |
MrBedfordVan | 0:b9164b348919 | 139 | } |
MrBedfordVan | 0:b9164b348919 | 140 | |
MrBedfordVan | 0:b9164b348919 | 141 | /** |
MrBedfordVan | 0:b9164b348919 | 142 | * Tests if this Button is currently pressed. |
MrBedfordVan | 0:b9164b348919 | 143 | * |
MrBedfordVan | 0:b9164b348919 | 144 | * @code |
MrBedfordVan | 0:b9164b348919 | 145 | * if(buttonA.isPressed()) |
MrBedfordVan | 0:b9164b348919 | 146 | * display.scroll("Pressed!"); |
MrBedfordVan | 0:b9164b348919 | 147 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 148 | * |
MrBedfordVan | 0:b9164b348919 | 149 | * @return 1 if this button is pressed, 0 otherwise. |
MrBedfordVan | 0:b9164b348919 | 150 | */ |
MrBedfordVan | 0:b9164b348919 | 151 | int MicroBitButton::isPressed() |
MrBedfordVan | 0:b9164b348919 | 152 | { |
MrBedfordVan | 0:b9164b348919 | 153 | return status & MICROBIT_BUTTON_STATE ? 1 : 0; |
MrBedfordVan | 0:b9164b348919 | 154 | } |
MrBedfordVan | 0:b9164b348919 | 155 | |
MrBedfordVan | 0:b9164b348919 | 156 | /** |
MrBedfordVan | 0:b9164b348919 | 157 | * Destructor for MicroBitButton, where we deregister this instance from the array of fiber components. |
MrBedfordVan | 0:b9164b348919 | 158 | */ |
MrBedfordVan | 0:b9164b348919 | 159 | MicroBitButton::~MicroBitButton() |
MrBedfordVan | 0:b9164b348919 | 160 | { |
MrBedfordVan | 0:b9164b348919 | 161 | system_timer_remove_component(this); |
MrBedfordVan | 0:b9164b348919 | 162 | } |