Forked to avoid changing the publioc repo
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
inc/drivers/MicroBitButton.h@74:4c7db2d5ca9a, 2017-04-20 (annotated)
- Committer:
- bluetooth_mdw
- Date:
- Thu Apr 20 13:42:44 2017 +0000
- Revision:
- 74:4c7db2d5ca9a
- Parent:
- 1:8aa5cdb4ab67
Example app showing how to include the Bluetooth accelerometer service in your micro:bit application code.
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 | #ifndef MICROBIT_BUTTON_H |
Jonathan Austin |
1:8aa5cdb4ab67 | 27 | #define MICROBIT_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 "MicroBitComponent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 32 | #include "MicroBitEvent.h" |
Jonathan Austin |
1:8aa5cdb4ab67 | 33 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 34 | #define MICROBIT_PIN_BUTTON_A P0_17 |
Jonathan Austin |
1:8aa5cdb4ab67 | 35 | #define MICROBIT_PIN_BUTTON_B P0_26 |
Jonathan Austin |
1:8aa5cdb4ab67 | 36 | #define MICROBIT_PIN_BUTTON_RESET P0_19 |
Jonathan Austin |
1:8aa5cdb4ab67 | 37 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 38 | #define MICROBIT_BUTTON_EVT_DOWN 1 |
Jonathan Austin |
1:8aa5cdb4ab67 | 39 | #define MICROBIT_BUTTON_EVT_UP 2 |
Jonathan Austin |
1:8aa5cdb4ab67 | 40 | #define MICROBIT_BUTTON_EVT_CLICK 3 |
Jonathan Austin |
1:8aa5cdb4ab67 | 41 | #define MICROBIT_BUTTON_EVT_LONG_CLICK 4 |
Jonathan Austin |
1:8aa5cdb4ab67 | 42 | #define MICROBIT_BUTTON_EVT_HOLD 5 |
Jonathan Austin |
1:8aa5cdb4ab67 | 43 | #define MICROBIT_BUTTON_EVT_DOUBLE_CLICK 6 |
Jonathan Austin |
1:8aa5cdb4ab67 | 44 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 45 | #define MICROBIT_BUTTON_LONG_CLICK_TIME 1000 |
Jonathan Austin |
1:8aa5cdb4ab67 | 46 | #define MICROBIT_BUTTON_HOLD_TIME 1500 |
Jonathan Austin |
1:8aa5cdb4ab67 | 47 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 48 | #define MICROBIT_BUTTON_STATE 1 |
Jonathan Austin |
1:8aa5cdb4ab67 | 49 | #define MICROBIT_BUTTON_STATE_HOLD_TRIGGERED 2 |
Jonathan Austin |
1:8aa5cdb4ab67 | 50 | #define MICROBIT_BUTTON_STATE_CLICK 4 |
Jonathan Austin |
1:8aa5cdb4ab67 | 51 | #define MICROBIT_BUTTON_STATE_LONG_CLICK 8 |
Jonathan Austin |
1:8aa5cdb4ab67 | 52 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 53 | #define MICROBIT_BUTTON_SIGMA_MIN 0 |
Jonathan Austin |
1:8aa5cdb4ab67 | 54 | #define MICROBIT_BUTTON_SIGMA_MAX 12 |
Jonathan Austin |
1:8aa5cdb4ab67 | 55 | #define MICROBIT_BUTTON_SIGMA_THRESH_HI 8 |
Jonathan Austin |
1:8aa5cdb4ab67 | 56 | #define MICROBIT_BUTTON_SIGMA_THRESH_LO 2 |
Jonathan Austin |
1:8aa5cdb4ab67 | 57 | #define MICROBIT_BUTTON_DOUBLE_CLICK_THRESH 50 |
Jonathan Austin |
1:8aa5cdb4ab67 | 58 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 59 | enum MicroBitButtonEventConfiguration |
Jonathan Austin |
1:8aa5cdb4ab67 | 60 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 61 | MICROBIT_BUTTON_SIMPLE_EVENTS, |
Jonathan Austin |
1:8aa5cdb4ab67 | 62 | MICROBIT_BUTTON_ALL_EVENTS |
Jonathan Austin |
1:8aa5cdb4ab67 | 63 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 64 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 65 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 66 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 67 | * Class definition for MicroBit Button. |
Jonathan Austin |
1:8aa5cdb4ab67 | 68 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 69 | * Represents a single, generic button on the device. |
Jonathan Austin |
1:8aa5cdb4ab67 | 70 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 71 | class MicroBitButton : public MicroBitComponent |
Jonathan Austin |
1:8aa5cdb4ab67 | 72 | { |
Jonathan Austin |
1:8aa5cdb4ab67 | 73 | PinName name; // mbed pin name for this button. |
Jonathan Austin |
1:8aa5cdb4ab67 | 74 | DigitalIn pin; // The mbed object looking after this pin at any point in time (may change!). |
Jonathan Austin |
1:8aa5cdb4ab67 | 75 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 76 | unsigned long downStartTime; // used to store the current system clock when a button down event occurs |
Jonathan Austin |
1:8aa5cdb4ab67 | 77 | uint8_t sigma; // integration of samples over time. We use this for debouncing, and noise tolerance for touch sensing |
Jonathan Austin |
1:8aa5cdb4ab67 | 78 | MicroBitButtonEventConfiguration eventConfiguration; // Do we want to generate high level event (clicks), or defer this to another service. |
Jonathan Austin |
1:8aa5cdb4ab67 | 79 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 80 | public: |
Jonathan Austin |
1:8aa5cdb4ab67 | 81 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 82 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 83 | * Constructor. |
Jonathan Austin |
1:8aa5cdb4ab67 | 84 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 85 | * Create a software representation of a button. |
Jonathan Austin |
1:8aa5cdb4ab67 | 86 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 87 | * @param name the physical pin on the processor that should be used as input. |
Jonathan Austin |
1:8aa5cdb4ab67 | 88 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 89 | * @param id the ID of the new MicroBitButton object. |
Jonathan Austin |
1:8aa5cdb4ab67 | 90 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 91 | * @param eventConfiguration Configures the events that will be generated by this MicroBitButton instance. |
Jonathan Austin |
1:8aa5cdb4ab67 | 92 | * Defaults to MICROBIT_BUTTON_ALL_EVENTS. |
Jonathan Austin |
1:8aa5cdb4ab67 | 93 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 94 | * @param mode the configuration of internal pullups/pulldowns, as defined in the mbed PinMode class. PullNone by default. |
Jonathan Austin |
1:8aa5cdb4ab67 | 95 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 96 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 97 | * buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A); |
Jonathan Austin |
1:8aa5cdb4ab67 | 98 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 99 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 100 | MicroBitButton(PinName name, uint16_t id, MicroBitButtonEventConfiguration eventConfiguration = MICROBIT_BUTTON_ALL_EVENTS, PinMode mode = PullNone); |
Jonathan Austin |
1:8aa5cdb4ab67 | 101 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 102 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 103 | * Tests if this Button is currently pressed. |
Jonathan Austin |
1:8aa5cdb4ab67 | 104 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 105 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 106 | * if(buttonA.isPressed()) |
Jonathan Austin |
1:8aa5cdb4ab67 | 107 | * display.scroll("Pressed!"); |
Jonathan Austin |
1:8aa5cdb4ab67 | 108 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 109 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 110 | * @return 1 if this button is pressed, 0 otherwise. |
Jonathan Austin |
1:8aa5cdb4ab67 | 111 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 112 | int isPressed(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 113 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 114 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 115 | * Changes the event configuration used by this button to the given MicroBitButtonEventConfiguration. |
Jonathan Austin |
1:8aa5cdb4ab67 | 116 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 117 | * All subsequent events generated by this button will then be informed by this configuraiton. |
Jonathan Austin |
1:8aa5cdb4ab67 | 118 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 119 | * @param config The new configuration for this button. Legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS. |
Jonathan Austin |
1:8aa5cdb4ab67 | 120 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 121 | * Example: |
Jonathan Austin |
1:8aa5cdb4ab67 | 122 | * @code |
Jonathan Austin |
1:8aa5cdb4ab67 | 123 | * // Configure a button to generate all possible events. |
Jonathan Austin |
1:8aa5cdb4ab67 | 124 | * buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS); |
Jonathan Austin |
1:8aa5cdb4ab67 | 125 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 126 | * // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events. |
Jonathan Austin |
1:8aa5cdb4ab67 | 127 | * buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); |
Jonathan Austin |
1:8aa5cdb4ab67 | 128 | * @endcode |
Jonathan Austin |
1:8aa5cdb4ab67 | 129 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 130 | void setEventConfiguration(MicroBitButtonEventConfiguration config); |
Jonathan Austin |
1:8aa5cdb4ab67 | 131 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 132 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 133 | * periodic callback from MicroBit system timer. |
Jonathan Austin |
1:8aa5cdb4ab67 | 134 | * |
Jonathan Austin |
1:8aa5cdb4ab67 | 135 | * Check for state change for this button, and fires various events on a state change. |
Jonathan Austin |
1:8aa5cdb4ab67 | 136 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 137 | virtual void systemTick(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 138 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 139 | /** |
Jonathan Austin |
1:8aa5cdb4ab67 | 140 | * Destructor for MicroBitButton, where we deregister this instance from the array of fiber components. |
Jonathan Austin |
1:8aa5cdb4ab67 | 141 | */ |
Jonathan Austin |
1:8aa5cdb4ab67 | 142 | ~MicroBitButton(); |
Jonathan Austin |
1:8aa5cdb4ab67 | 143 | }; |
Jonathan Austin |
1:8aa5cdb4ab67 | 144 | |
Jonathan Austin |
1:8aa5cdb4ab67 | 145 | #endif |