Solution for Bluetooth SIG hands-on training course
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw_starter by
MicroBitButton.h
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #ifndef MICROBIT_BUTTON_H 00027 #define MICROBIT_BUTTON_H 00028 00029 #include "mbed.h" 00030 #include "MicroBitConfig.h" 00031 #include "MicroBitComponent.h" 00032 #include "MicroBitEvent.h" 00033 00034 #define MICROBIT_PIN_BUTTON_A P0_17 00035 #define MICROBIT_PIN_BUTTON_B P0_26 00036 #define MICROBIT_PIN_BUTTON_RESET P0_19 00037 00038 #define MICROBIT_BUTTON_EVT_DOWN 1 00039 #define MICROBIT_BUTTON_EVT_UP 2 00040 #define MICROBIT_BUTTON_EVT_CLICK 3 00041 #define MICROBIT_BUTTON_EVT_LONG_CLICK 4 00042 #define MICROBIT_BUTTON_EVT_HOLD 5 00043 #define MICROBIT_BUTTON_EVT_DOUBLE_CLICK 6 00044 00045 #define MICROBIT_BUTTON_LONG_CLICK_TIME 1000 00046 #define MICROBIT_BUTTON_HOLD_TIME 1500 00047 00048 #define MICROBIT_BUTTON_STATE 1 00049 #define MICROBIT_BUTTON_STATE_HOLD_TRIGGERED 2 00050 #define MICROBIT_BUTTON_STATE_CLICK 4 00051 #define MICROBIT_BUTTON_STATE_LONG_CLICK 8 00052 00053 #define MICROBIT_BUTTON_SIGMA_MIN 0 00054 #define MICROBIT_BUTTON_SIGMA_MAX 12 00055 #define MICROBIT_BUTTON_SIGMA_THRESH_HI 8 00056 #define MICROBIT_BUTTON_SIGMA_THRESH_LO 2 00057 #define MICROBIT_BUTTON_DOUBLE_CLICK_THRESH 50 00058 00059 enum MicroBitButtonEventConfiguration 00060 { 00061 MICROBIT_BUTTON_SIMPLE_EVENTS, 00062 MICROBIT_BUTTON_ALL_EVENTS 00063 }; 00064 00065 00066 /** 00067 * Class definition for MicroBit Button. 00068 * 00069 * Represents a single, generic button on the device. 00070 */ 00071 class MicroBitButton : public MicroBitComponent 00072 { 00073 PinName name; // mbed pin name for this button. 00074 DigitalIn pin; // The mbed object looking after this pin at any point in time (may change!). 00075 00076 unsigned long downStartTime; // used to store the current system clock when a button down event occurs 00077 uint8_t sigma; // integration of samples over time. We use this for debouncing, and noise tolerance for touch sensing 00078 MicroBitButtonEventConfiguration eventConfiguration; // Do we want to generate high level event (clicks), or defer this to another service. 00079 00080 public: 00081 00082 /** 00083 * Constructor. 00084 * 00085 * Create a software representation of a button. 00086 * 00087 * @param name the physical pin on the processor that should be used as input. 00088 * 00089 * @param id the ID of the new MicroBitButton object. 00090 * 00091 * @param eventConfiguration Configures the events that will be generated by this MicroBitButton instance. 00092 * Defaults to MICROBIT_BUTTON_ALL_EVENTS. 00093 * 00094 * @param mode the configuration of internal pullups/pulldowns, as defined in the mbed PinMode class. PullNone by default. 00095 * 00096 * @code 00097 * buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A); 00098 * @endcode 00099 */ 00100 MicroBitButton(PinName name, uint16_t id, MicroBitButtonEventConfiguration eventConfiguration = MICROBIT_BUTTON_ALL_EVENTS, PinMode mode = PullNone); 00101 00102 /** 00103 * Tests if this Button is currently pressed. 00104 * 00105 * @code 00106 * if(buttonA.isPressed()) 00107 * display.scroll("Pressed!"); 00108 * @endcode 00109 * 00110 * @return 1 if this button is pressed, 0 otherwise. 00111 */ 00112 int isPressed(); 00113 00114 /** 00115 * Changes the event configuration used by this button to the given MicroBitButtonEventConfiguration. 00116 * 00117 * All subsequent events generated by this button will then be informed by this configuraiton. 00118 * 00119 * @param config The new configuration for this button. Legal values are MICROBIT_BUTTON_ALL_EVENTS or MICROBIT_BUTTON_SIMPLE_EVENTS. 00120 * 00121 * Example: 00122 * @code 00123 * // Configure a button to generate all possible events. 00124 * buttonA.setEventConfiguration(MICROBIT_BUTTON_ALL_EVENTS); 00125 * 00126 * // Configure a button to suppress MICROBIT_BUTTON_EVT_CLICK and MICROBIT_BUTTON_EVT_LONG_CLICK events. 00127 * buttonA.setEventConfiguration(MICROBIT_BUTTON_SIMPLE_EVENTS); 00128 * @endcode 00129 */ 00130 void setEventConfiguration(MicroBitButtonEventConfiguration config); 00131 00132 /** 00133 * periodic callback from MicroBit system timer. 00134 * 00135 * Check for state change for this button, and fires various events on a state change. 00136 */ 00137 virtual void systemTick(); 00138 00139 /** 00140 * Destructor for MicroBitButton, where we deregister this instance from the array of fiber components. 00141 */ 00142 ~MicroBitButton(); 00143 }; 00144 00145 #endif
Generated on Tue Jul 12 2022 20:39:13 by 1.7.2