x
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
source/drivers/MicroBitMultiButton.cpp@1:8aa5cdb4ab67, 2016-04-07 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Thu Apr 07 01:33:22 2016 +0100
- Revision:
- 1:8aa5cdb4ab67
- Child:
- 69:b62f231e51ce
Synchronized with git rev 55cb9199
Who changed what in which revision?
User | Revision | Line number | New 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 MicroBitMultiButton. |
Jonathan Austin |
1:8aa5cdb4ab67 | 28 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 29 | * Represents a virtual button, capable of reacting to simultaneous presses of two |
Jonathan Austin |
1:8aa5cdb4ab67 | 30 | * other buttons. |
Jonathan Austin |
1:8aa5cdb4ab67 | 31 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitConfig.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | #include "MicroBitMultiButton.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | * Create a representation of a virtual button, that generates events based upon the combination |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | * of two given buttons. |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | * @param button1 the unique ID of the first button to watch. |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | * @param button2 the unique ID of the second button to watch. |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | * @param id the unique EventModel id of this MicroBitMultiButton instance. |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | * multiButton(MICROBIT_ID_BUTTON_A, MICROBIT_ID_BUTTON_B, MICROBIT_ID_BUTTON_AB); |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | MicroBitMultiButton::MicroBitMultiButton(uint16_t button1, uint16_t button2, uint16_t id) |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | this->id = id; |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | this->button1 = button1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | this->button2 = button2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | this->eventConfiguration = MICROBIT_BUTTON_SIMPLE_EVENTS; |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | if (EventModel::defaultEventBus) |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | EventModel::defaultEventBus->listen(button1, MICROBIT_EVT_ANY, this, &MicroBitMultiButton::onButtonEvent, MESSAGE_BUS_LISTENER_IMMEDIATE); |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | EventModel::defaultEventBus->listen(button2, MICROBIT_EVT_ANY, this, &MicroBitMultiButton::onButtonEvent, MESSAGE_BUS_LISTENER_IMMEDIATE); |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | * Retrieves the button id for the alternate button id given. |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | * @param b the id of the button whose state we would like to retrieve. |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | * @return the other sub button id. |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | uint16_t MicroBitMultiButton::otherSubButton(uint16_t b) |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | return (b == button1 ? button2 : button1); |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | * Determines if the given button id is marked as pressed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | * @param button the id of the button whose state we would like to retrieve. |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | * @return 1 if pressed, 0 if not. |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | int MicroBitMultiButton::isSubButtonPressed(uint16_t button) |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | if (button == button1) |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | return status & MICROBIT_MULTI_BUTTON_STATE_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | if (button == button2) |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | return status & MICROBIT_MULTI_BUTTON_STATE_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | return 0; |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | * Determines if the given button id is marked as held. |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | * @param button the id of the button whose state we would like to retrieve. |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | * @return 1 if held, 0 if not. |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | int MicroBitMultiButton::isSubButtonHeld(uint16_t button) |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | if (button == button1) |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | return status & MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | if (button == button2) |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | return status & MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | return 0; |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | * Determines if the given button id is marked as supressed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | * @param button the id of the button whose state we would like to retrieve. |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | * @return 1 if supressed, 0 if not. |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | int MicroBitMultiButton::isSubButtonSupressed(uint16_t button) |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | if (button == button1) |
Jonathan Austin |
1:8aa5cdb4ab67 | 123 | return status & MICROBIT_MULTI_BUTTON_SUPRESSED_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 124 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 125 | if (button == button2) |
Jonathan Austin |
1:8aa5cdb4ab67 | 126 | return status & MICROBIT_MULTI_BUTTON_SUPRESSED_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 127 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 128 | return 0; |
Jonathan Austin |
1:8aa5cdb4ab67 | 129 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 130 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 131 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 132 | * Configures the button pressed state for the given button id. |
Jonathan Austin |
1:8aa5cdb4ab67 | 133 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 134 | * @param button the id of the button whose state requires updating. |
Jonathan Austin |
1:8aa5cdb4ab67 | 135 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | * @param value the value to set for this buttons state. (Transformed into a logical 0 or 1). |
Jonathan Austin |
1:8aa5cdb4ab67 | 137 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | void MicroBitMultiButton::setButtonState(uint16_t button, int value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | if (button == button1) |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | if (value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | status |= MICROBIT_MULTI_BUTTON_STATE_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 144 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 145 | status &= ~MICROBIT_MULTI_BUTTON_STATE_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 146 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 147 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 148 | if (button == button2) |
Jonathan Austin |
1:8aa5cdb4ab67 | 149 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 150 | if (value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 151 | status |= MICROBIT_MULTI_BUTTON_STATE_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 152 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 153 | status &= ~MICROBIT_MULTI_BUTTON_STATE_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 154 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 155 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 156 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 157 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 158 | * Configures the button held state for the given button id. |
Jonathan Austin |
1:8aa5cdb4ab67 | 159 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 160 | * @param button the id of the button whose state requires updating. |
Jonathan Austin |
1:8aa5cdb4ab67 | 161 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 162 | * @param value the value to set for this buttons state. (Transformed into a logical 0 or 1). |
Jonathan Austin |
1:8aa5cdb4ab67 | 163 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 164 | void MicroBitMultiButton::setHoldState(uint16_t button, int value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 165 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 166 | if (button == button1) |
Jonathan Austin |
1:8aa5cdb4ab67 | 167 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 168 | if (value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 169 | status |= MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 170 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 171 | status &= ~MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 172 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 173 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 174 | if (button == button2) |
Jonathan Austin |
1:8aa5cdb4ab67 | 175 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 176 | if (value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 177 | status |= MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 178 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 179 | status &= ~MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 180 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 181 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 182 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 183 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 184 | * Configures the button suppressed state for the given button id. |
Jonathan Austin |
1:8aa5cdb4ab67 | 185 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 186 | * @param button the id of the button whose state requires updating. |
Jonathan Austin |
1:8aa5cdb4ab67 | 187 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 188 | * @param value the value to set for this buttons state. (Transformed into a logical 0 or 1). |
Jonathan Austin |
1:8aa5cdb4ab67 | 189 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 190 | void MicroBitMultiButton::setSupressedState(uint16_t button, int value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 191 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 192 | if (button == button1) |
Jonathan Austin |
1:8aa5cdb4ab67 | 193 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 194 | if (value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 195 | status |= MICROBIT_MULTI_BUTTON_SUPRESSED_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 196 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 197 | status &= ~MICROBIT_MULTI_BUTTON_SUPRESSED_1; |
Jonathan Austin |
1:8aa5cdb4ab67 | 198 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 199 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 200 | if (button == button2) |
Jonathan Austin |
1:8aa5cdb4ab67 | 201 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 202 | if (value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 203 | status |= MICROBIT_MULTI_BUTTON_SUPRESSED_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 204 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 205 | status &= ~MICROBIT_MULTI_BUTTON_SUPRESSED_2; |
Jonathan Austin |
1:8aa5cdb4ab67 | 206 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 207 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 208 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 209 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 210 | * Changes the event configuration of this button to the given MicroBitButtonEventConfiguration. |
Jonathan Austin |
1:8aa5cdb4ab67 | 211 | * All subsequent events generated by this button will then be informed by this configuraiton. |
Jonathan Austin |
1:8aa5cdb4ab67 | 212 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 213 | * @param config The new configuration for this button. Legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS. |
Jonathan Austin |
1:8aa5cdb4ab67 | 214 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 215 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 216 | * // Configure a button to generate all possible events. |
Jonathan Austin |
1:8aa5cdb4ab67 | 217 | * buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS); |
Jonathan Austin |
1:8aa5cdb4ab67 | 218 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 219 | * // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events. |
Jonathan Austin |
1:8aa5cdb4ab67 | 220 | * buttonAB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); |
Jonathan Austin |
1:8aa5cdb4ab67 | 221 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 222 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 223 | void MicroBitMultiButton::setEventConfiguration(MicroBitButtonEventConfiguration config) |
Jonathan Austin |
1:8aa5cdb4ab67 | 224 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 225 | this->eventConfiguration = config; |
Jonathan Austin |
1:8aa5cdb4ab67 | 226 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 227 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 228 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 229 | * A member function that is invoked when any event is detected from the two |
Jonathan Austin |
1:8aa5cdb4ab67 | 230 | * button IDs this MicrobitMultiButton instance was constructed with. |
Jonathan Austin |
1:8aa5cdb4ab67 | 231 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 232 | * @param evt the event received from the default EventModel. |
Jonathan Austin |
1:8aa5cdb4ab67 | 233 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 234 | void MicroBitMultiButton::onButtonEvent(MicroBitEvent evt) |
Jonathan Austin |
1:8aa5cdb4ab67 | 235 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 236 | int button = evt.source; |
Jonathan Austin |
1:8aa5cdb4ab67 | 237 | int otherButton = otherSubButton(button); |
Jonathan Austin |
1:8aa5cdb4ab67 | 238 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 239 | switch(evt.value) |
Jonathan Austin |
1:8aa5cdb4ab67 | 240 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 241 | case MICROBIT_BUTTON_EVT_DOWN: |
Jonathan Austin |
1:8aa5cdb4ab67 | 242 | setButtonState(button, 1); |
Jonathan Austin |
1:8aa5cdb4ab67 | 243 | if(isSubButtonPressed(otherButton)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 244 | MicroBitEvent e(id, MICROBIT_BUTTON_EVT_DOWN); |
Jonathan Austin |
1:8aa5cdb4ab67 | 245 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 246 | break; |
Jonathan Austin |
1:8aa5cdb4ab67 | 247 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 248 | case MICROBIT_BUTTON_EVT_HOLD: |
Jonathan Austin |
1:8aa5cdb4ab67 | 249 | setHoldState(button, 1); |
Jonathan Austin |
1:8aa5cdb4ab67 | 250 | if(isSubButtonHeld(otherButton)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 251 | MicroBitEvent e(id, MICROBIT_BUTTON_EVT_HOLD); |
Jonathan Austin |
1:8aa5cdb4ab67 | 252 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 253 | break; |
Jonathan Austin |
1:8aa5cdb4ab67 | 254 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 255 | case MICROBIT_BUTTON_EVT_UP: |
Jonathan Austin |
1:8aa5cdb4ab67 | 256 | if(isSubButtonPressed(otherButton)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 257 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 258 | MicroBitEvent e(id, MICROBIT_BUTTON_EVT_UP); |
Jonathan Austin |
1:8aa5cdb4ab67 | 259 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 260 | if (isSubButtonHeld(button) && isSubButtonHeld(otherButton)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 261 | MicroBitEvent e(id, MICROBIT_BUTTON_EVT_LONG_CLICK); |
Jonathan Austin |
1:8aa5cdb4ab67 | 262 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 263 | MicroBitEvent e(id, MICROBIT_BUTTON_EVT_CLICK); |
Jonathan Austin |
1:8aa5cdb4ab67 | 264 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 265 | setSupressedState(otherButton, 1); |
Jonathan Austin |
1:8aa5cdb4ab67 | 266 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 267 | else if (!isSubButtonSupressed(button) && eventConfiguration == MICROBIT_BUTTON_ALL_EVENTS) |
Jonathan Austin |
1:8aa5cdb4ab67 | 268 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 269 | if (isSubButtonHeld(button)) |
Jonathan Austin |
1:8aa5cdb4ab67 | 270 | MicroBitEvent e(button, MICROBIT_BUTTON_EVT_LONG_CLICK); |
Jonathan Austin |
1:8aa5cdb4ab67 | 271 | else |
Jonathan Austin |
1:8aa5cdb4ab67 | 272 | MicroBitEvent e(button, MICROBIT_BUTTON_EVT_CLICK); |
Jonathan Austin |
1:8aa5cdb4ab67 | 273 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 274 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 275 | setButtonState(button, 0); |
Jonathan Austin |
1:8aa5cdb4ab67 | 276 | setHoldState(button, 0); |
Jonathan Austin |
1:8aa5cdb4ab67 | 277 | setSupressedState(button, 0); |
Jonathan Austin |
1:8aa5cdb4ab67 | 278 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 279 | break; |
Jonathan Austin |
1:8aa5cdb4ab67 | 280 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 281 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 282 | } |
Jonathan Austin |
1:8aa5cdb4ab67 | 283 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 284 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 285 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 286 | * Tests if this MicroBitMultiButton instance is virtually pressed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 287 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 288 | * @return 1 if both physical buttons are pressed simultaneously. |
Jonathan Austin |
1:8aa5cdb4ab67 | 289 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 290 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 291 | * if(buttonAB.isPressed()) |
Jonathan Austin |
1:8aa5cdb4ab67 | 292 | * display.scroll("Pressed!"); |
Jonathan Austin |
1:8aa5cdb4ab67 | 293 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 294 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 295 | int MicroBitMultiButton::isPressed() |
Jonathan Austin |
1:8aa5cdb4ab67 | 296 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 297 | return ((status & MICROBIT_MULTI_BUTTON_STATE_1) && (status & MICROBIT_MULTI_BUTTON_STATE_2)); |
Jonathan Austin |
1:8aa5cdb4ab67 | 298 | } |