No Changes

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

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?

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