Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/inc/drivers/MicroBitMultiButton.h@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 | #ifndef MICROBIT_MULTI_BUTTON_H |
MrBedfordVan | 0:b9164b348919 | 27 | #define MICROBIT_MULTI_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 "MicroBitButton.h" |
MrBedfordVan | 0:b9164b348919 | 32 | #include "EventModel.h" |
MrBedfordVan | 0:b9164b348919 | 33 | |
MrBedfordVan | 0:b9164b348919 | 34 | #define MICROBIT_MULTI_BUTTON_STATE_1 0x01 |
MrBedfordVan | 0:b9164b348919 | 35 | #define MICROBIT_MULTI_BUTTON_STATE_2 0x02 |
MrBedfordVan | 0:b9164b348919 | 36 | #define MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_1 0x04 |
MrBedfordVan | 0:b9164b348919 | 37 | #define MICROBIT_MULTI_BUTTON_HOLD_TRIGGERED_2 0x08 |
MrBedfordVan | 0:b9164b348919 | 38 | #define MICROBIT_MULTI_BUTTON_SUPRESSED_1 0X10 |
MrBedfordVan | 0:b9164b348919 | 39 | #define MICROBIT_MULTI_BUTTON_SUPRESSED_2 0x20 |
MrBedfordVan | 0:b9164b348919 | 40 | #define MICROBIT_MULTI_BUTTON_ATTACHED 0x40 |
MrBedfordVan | 0:b9164b348919 | 41 | |
MrBedfordVan | 0:b9164b348919 | 42 | /** |
MrBedfordVan | 0:b9164b348919 | 43 | * Class definition for MicroBitMultiButton. |
MrBedfordVan | 0:b9164b348919 | 44 | * |
MrBedfordVan | 0:b9164b348919 | 45 | * Represents a virtual button, capable of reacting to simultaneous presses of two |
MrBedfordVan | 0:b9164b348919 | 46 | * other buttons. |
MrBedfordVan | 0:b9164b348919 | 47 | */ |
MrBedfordVan | 0:b9164b348919 | 48 | class MicroBitMultiButton : public MicroBitComponent |
MrBedfordVan | 0:b9164b348919 | 49 | { |
MrBedfordVan | 0:b9164b348919 | 50 | uint16_t button1; // ID of the first button we're monitoring |
MrBedfordVan | 0:b9164b348919 | 51 | uint16_t button2; // ID of the second button we're monitoring |
MrBedfordVan | 0:b9164b348919 | 52 | MicroBitButtonEventConfiguration eventConfiguration; // Do we want to generate high level event (clicks), or defer this to another service. |
MrBedfordVan | 0:b9164b348919 | 53 | |
MrBedfordVan | 0:b9164b348919 | 54 | /** |
MrBedfordVan | 0:b9164b348919 | 55 | * Retrieves the button id for the alternate button id given. |
MrBedfordVan | 0:b9164b348919 | 56 | * |
MrBedfordVan | 0:b9164b348919 | 57 | * @param b the id of the button whose state we would like to retrieve. |
MrBedfordVan | 0:b9164b348919 | 58 | * |
MrBedfordVan | 0:b9164b348919 | 59 | * @return the other sub button id. |
MrBedfordVan | 0:b9164b348919 | 60 | */ |
MrBedfordVan | 0:b9164b348919 | 61 | uint16_t otherSubButton(uint16_t b); |
MrBedfordVan | 0:b9164b348919 | 62 | |
MrBedfordVan | 0:b9164b348919 | 63 | /** |
MrBedfordVan | 0:b9164b348919 | 64 | * Determines if the given button id is marked as pressed. |
MrBedfordVan | 0:b9164b348919 | 65 | * |
MrBedfordVan | 0:b9164b348919 | 66 | * @param button the id of the button whose state we would like to retrieve. |
MrBedfordVan | 0:b9164b348919 | 67 | * |
MrBedfordVan | 0:b9164b348919 | 68 | * @return 1 if pressed, 0 if not. |
MrBedfordVan | 0:b9164b348919 | 69 | */ |
MrBedfordVan | 0:b9164b348919 | 70 | int isSubButtonPressed(uint16_t button); |
MrBedfordVan | 0:b9164b348919 | 71 | |
MrBedfordVan | 0:b9164b348919 | 72 | /** |
MrBedfordVan | 0:b9164b348919 | 73 | * Determines if the given button id is marked as held. |
MrBedfordVan | 0:b9164b348919 | 74 | * |
MrBedfordVan | 0:b9164b348919 | 75 | * @param button the id of the button whose state we would like to retrieve. |
MrBedfordVan | 0:b9164b348919 | 76 | * |
MrBedfordVan | 0:b9164b348919 | 77 | * @return 1 if held, 0 if not. |
MrBedfordVan | 0:b9164b348919 | 78 | */ |
MrBedfordVan | 0:b9164b348919 | 79 | int isSubButtonHeld(uint16_t button); |
MrBedfordVan | 0:b9164b348919 | 80 | |
MrBedfordVan | 0:b9164b348919 | 81 | /** |
MrBedfordVan | 0:b9164b348919 | 82 | * Determines if the given button id is marked as supressed. |
MrBedfordVan | 0:b9164b348919 | 83 | * |
MrBedfordVan | 0:b9164b348919 | 84 | * @param button the id of the button whose state we would like to retrieve. |
MrBedfordVan | 0:b9164b348919 | 85 | * |
MrBedfordVan | 0:b9164b348919 | 86 | * @return 1 if supressed, 0 if not. |
MrBedfordVan | 0:b9164b348919 | 87 | */ |
MrBedfordVan | 0:b9164b348919 | 88 | int isSubButtonSupressed(uint16_t button); |
MrBedfordVan | 0:b9164b348919 | 89 | |
MrBedfordVan | 0:b9164b348919 | 90 | /** |
MrBedfordVan | 0:b9164b348919 | 91 | * Configures the button pressed state for the given button id. |
MrBedfordVan | 0:b9164b348919 | 92 | * |
MrBedfordVan | 0:b9164b348919 | 93 | * @param button the id of the button whose state requires updating. |
MrBedfordVan | 0:b9164b348919 | 94 | * |
MrBedfordVan | 0:b9164b348919 | 95 | * @param value the value to set for this buttons state. (Transformed into a logical 0 or 1). |
MrBedfordVan | 0:b9164b348919 | 96 | */ |
MrBedfordVan | 0:b9164b348919 | 97 | void setButtonState(uint16_t button, int value); |
MrBedfordVan | 0:b9164b348919 | 98 | |
MrBedfordVan | 0:b9164b348919 | 99 | /** |
MrBedfordVan | 0:b9164b348919 | 100 | * Configures the button held state for the given button id. |
MrBedfordVan | 0:b9164b348919 | 101 | * |
MrBedfordVan | 0:b9164b348919 | 102 | * @param button the id of the button whose state requires updating. |
MrBedfordVan | 0:b9164b348919 | 103 | * |
MrBedfordVan | 0:b9164b348919 | 104 | * @param value the value to set for this buttons state. (Transformed into a logical 0 or 1). |
MrBedfordVan | 0:b9164b348919 | 105 | */ |
MrBedfordVan | 0:b9164b348919 | 106 | void setHoldState(uint16_t button, int value); |
MrBedfordVan | 0:b9164b348919 | 107 | |
MrBedfordVan | 0:b9164b348919 | 108 | /** |
MrBedfordVan | 0:b9164b348919 | 109 | * Configures the button suppressed state for the given button id. |
MrBedfordVan | 0:b9164b348919 | 110 | * |
MrBedfordVan | 0:b9164b348919 | 111 | * @param button the id of the button whose state requires updating. |
MrBedfordVan | 0:b9164b348919 | 112 | * |
MrBedfordVan | 0:b9164b348919 | 113 | * @param value the value to set for this buttons state. (Transformed into a logical 0 or 1). |
MrBedfordVan | 0:b9164b348919 | 114 | */ |
MrBedfordVan | 0:b9164b348919 | 115 | void setSupressedState(uint16_t button, int value); |
MrBedfordVan | 0:b9164b348919 | 116 | |
MrBedfordVan | 0:b9164b348919 | 117 | public: |
MrBedfordVan | 0:b9164b348919 | 118 | |
MrBedfordVan | 0:b9164b348919 | 119 | /** |
MrBedfordVan | 0:b9164b348919 | 120 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 121 | * |
MrBedfordVan | 0:b9164b348919 | 122 | * Create a representation of a virtual button, that generates events based upon the combination |
MrBedfordVan | 0:b9164b348919 | 123 | * of two given buttons. |
MrBedfordVan | 0:b9164b348919 | 124 | * |
MrBedfordVan | 0:b9164b348919 | 125 | * @param button1 the unique ID of the first button to watch. |
MrBedfordVan | 0:b9164b348919 | 126 | * |
MrBedfordVan | 0:b9164b348919 | 127 | * @param button2 the unique ID of the second button to watch. |
MrBedfordVan | 0:b9164b348919 | 128 | * |
MrBedfordVan | 0:b9164b348919 | 129 | * @param id the unique EventModel id of this MicroBitMultiButton instance. |
MrBedfordVan | 0:b9164b348919 | 130 | * |
MrBedfordVan | 0:b9164b348919 | 131 | * @code |
MrBedfordVan | 0:b9164b348919 | 132 | * multiButton(MICROBIT_ID_BUTTON_A, MICROBIT_ID_BUTTON_B, MICROBIT_ID_BUTTON_AB); |
MrBedfordVan | 0:b9164b348919 | 133 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 134 | */ |
MrBedfordVan | 0:b9164b348919 | 135 | MicroBitMultiButton(uint16_t button1, uint16_t button2, uint16_t id); |
MrBedfordVan | 0:b9164b348919 | 136 | |
MrBedfordVan | 0:b9164b348919 | 137 | /** |
MrBedfordVan | 0:b9164b348919 | 138 | * Tests if this MicroBitMultiButton instance is virtually pressed. |
MrBedfordVan | 0:b9164b348919 | 139 | * |
MrBedfordVan | 0:b9164b348919 | 140 | * @return 1 if both physical buttons are pressed simultaneously. |
MrBedfordVan | 0:b9164b348919 | 141 | * |
MrBedfordVan | 0:b9164b348919 | 142 | * @code |
MrBedfordVan | 0:b9164b348919 | 143 | * if(buttonAB.isPressed()) |
MrBedfordVan | 0:b9164b348919 | 144 | * display.scroll("Pressed!"); |
MrBedfordVan | 0:b9164b348919 | 145 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 146 | */ |
MrBedfordVan | 0:b9164b348919 | 147 | int isPressed(); |
MrBedfordVan | 0:b9164b348919 | 148 | |
MrBedfordVan | 0:b9164b348919 | 149 | /** |
MrBedfordVan | 0:b9164b348919 | 150 | * Changes the event configuration of this button to the given MicroBitButtonEventConfiguration. |
MrBedfordVan | 0:b9164b348919 | 151 | * All subsequent events generated by this button will then be informed by this configuration. |
MrBedfordVan | 0:b9164b348919 | 152 | * |
MrBedfordVan | 0:b9164b348919 | 153 | * @param config The new configuration for this button. Legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS. |
MrBedfordVan | 0:b9164b348919 | 154 | * |
MrBedfordVan | 0:b9164b348919 | 155 | * @code |
MrBedfordVan | 0:b9164b348919 | 156 | * // Configure a button to generate all possible events. |
MrBedfordVan | 0:b9164b348919 | 157 | * buttonAB.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS); |
MrBedfordVan | 0:b9164b348919 | 158 | * |
MrBedfordVan | 0:b9164b348919 | 159 | * // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events. |
MrBedfordVan | 0:b9164b348919 | 160 | * buttonAB.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); |
MrBedfordVan | 0:b9164b348919 | 161 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 162 | */ |
MrBedfordVan | 0:b9164b348919 | 163 | void setEventConfiguration(MicroBitButtonEventConfiguration config); |
MrBedfordVan | 0:b9164b348919 | 164 | |
MrBedfordVan | 0:b9164b348919 | 165 | private: |
MrBedfordVan | 0:b9164b348919 | 166 | |
MrBedfordVan | 0:b9164b348919 | 167 | /** |
MrBedfordVan | 0:b9164b348919 | 168 | * A member function that is invoked when any event is detected from the two |
MrBedfordVan | 0:b9164b348919 | 169 | * button IDs this MicrobitMultiButton instance was constructed with. |
MrBedfordVan | 0:b9164b348919 | 170 | * |
MrBedfordVan | 0:b9164b348919 | 171 | * @param evt the event received from the default EventModel. |
MrBedfordVan | 0:b9164b348919 | 172 | */ |
MrBedfordVan | 0:b9164b348919 | 173 | void onButtonEvent(MicroBitEvent evt); |
MrBedfordVan | 0:b9164b348919 | 174 | }; |
MrBedfordVan | 0:b9164b348919 | 175 | |
MrBedfordVan | 0:b9164b348919 | 176 | #endif |