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