Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
DebounceIn.h
00001 /* 00002 Copyright (c) 2010 Andy Kirkham 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #ifndef AJK_DEBOUNCEIN_H 00024 #define AJK_DEBOUNCEIN_H 00025 00026 #include "mbed.h" 00027 00028 /** DebounceIn adds mechanical switch debouncing to DigitialIn. 00029 * 00030 * Example: 00031 * @code 00032 * #include "mbed.h" 00033 * #include "DebounceIn.h" 00034 * 00035 * DebounceIn d(p5); 00036 * DigitialOut led1(LED1); 00037 * DigitialOut led2(LED2); 00038 * 00039 * int main() { 00040 * while(1) { 00041 * led1 = d; 00042 * led2 = d.read(); 00043 * } 00044 * } 00045 * @endcode 00046 * 00047 * @see set_debounce_us() To change the sampling frequency. 00048 * @see set_samples() To alter the number of samples. 00049 * 00050 * Users of this library may also be interested in PinDetect library:- 00051 * @see http://mbed.org/users/AjK/libraries/PinDetect/latest 00052 * 00053 * This example shows one input displayed by two outputs. The input 00054 * is debounced by the default 10ms. 00055 */ 00056 00057 namespace AjK { 00058 00059 class DebounceIn { 00060 public: 00061 00062 friend class DigitalIn; 00063 friend class Ticker; 00064 00065 /** set_debounce_us 00066 * 00067 * Sets the debounce sample period time in microseconds, default is 25000 (25ms) 00068 * 00069 * @param uint32_t i The debounce sample period time to set. 00070 */ 00071 // void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); } 00072 void set_debounce_us(uint32_t i) { _debounce_us = i; _ticker->attach_us(callback(this, &DebounceIn::_callback), i); } 00073 00074 /** get_debounce_us 00075 * 00076 * Gets the debounce sample period time in microseconds 00077 * 00078 */ 00079 uint32_t get_debounce_us(void) { return _debounce_us; } // // // 00080 00081 /** set_samples 00082 * 00083 * Defines the number of samples before switching the shadow 00084 * definition of the pin. 00085 * 00086 * @param int i The number of samples. 00087 */ 00088 void set_samples(uint8_t i) { _samples = i; } 00089 00090 /** get_samples 00091 * 00092 * Gets the number of samples before switching the shadow 00093 * definition of the pin. 00094 * 00095 */ 00096 uint8_t get_samples(void) { return _samples; } 00097 00098 /** read 00099 * 00100 * Read the value of the debounced pin. 00101 */ 00102 int8_t read(void) { return this->_shadow; } 00103 00104 #ifdef MBED_OPERATORS 00105 /** operator int() 00106 * 00107 * Read the value of the debounced pin. 00108 */ 00109 operator int() { return read(); } 00110 #endif 00111 00112 /** Set the pin mode. 00113 * 00114 * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode 00115 * @param PinMode m The mode to pass on to the DigitalIn 00116 */ 00117 void mode(PinMode m) { _digital_in->mode( m ); } 00118 00119 /** get_edge_direction 00120 * 00121 * Gets the value of the debounced pin' edge direction (rising/falling). 00122 */ 00123 int8_t get_edge_direction(void) { return _edge_direction; } // // // 00124 00125 /** get_edge_direction_acted_upon 00126 * 00127 * Gets the debounced pin edge direction' usage status (used/unused) 00128 */ 00129 bool get_edge_direction_acted_upon(void) { return _edge_direction_acted_upon; } // // // 00130 00131 /** set_edge_direction_acted_upon 00132 * 00133 * Sets the status of the debounced pin edge direction' as used 00134 */ 00135 void set_edge_direction_acted_upon(void) { _edge_direction_acted_upon = 1; } // // // 00136 00137 /** Constructor 00138 * 00139 * @param PinName pin The pin to assign as an input. 00140 */ 00141 DebounceIn() { error("DebounceIn: You must supply a PinName"); } 00142 00143 DebounceIn(PinName pin) { _init(pin, PullNone); }; 00144 00145 DebounceIn(PinName pin, PinMode mode) { _init(pin, mode); }; 00146 00147 /** DebounceIn destructor 00148 */ 00149 ~DebounceIn() { 00150 if ( _ticker ) { delete( _ticker ); } 00151 if ( _digital_in ) { delete( _digital_in ); } 00152 } 00153 00154 protected: 00155 00156 /** initialise class 00157 * 00158 * @param PinName p is a valid pin that supports DigitalIn 00159 * @param PinMode m The mode the DigitalIn should use. 00160 */ 00161 void _init(PinName p, PinMode m) { 00162 00163 _counter = 0; 00164 _samples = 10; 00165 _shadow = -1; 00166 _edge_direction = -1; 00167 _edge_direction_acted_upon = 1; 00168 00169 _digital_in = new DigitalIn( p, m ); 00170 _ticker = new Ticker; 00171 00172 set_debounce_us(25000); 00173 } 00174 00175 void _callback(void) { 00176 00177 if (_digital_in->read() == 1) { 00178 if (_counter < _samples) _counter++; 00179 if (_counter == _samples) { 00180 if (_shadow == 0) { _edge_direction = 1; _edge_direction_acted_upon = 0; } // // // 00181 _shadow = 1; 00182 } 00183 } 00184 else { 00185 if (_counter > 0) _counter--; 00186 if (_counter == 0) { 00187 if (_shadow == 1) { _edge_direction = 0; _edge_direction_acted_upon = 0; } // // // 00188 _shadow = 0; 00189 } 00190 } 00191 } 00192 00193 DigitalIn *_digital_in; 00194 Ticker *_ticker; 00195 int8_t _shadow; 00196 uint32_t _debounce_us; // // // 00197 int8_t _edge_direction; // // // 00198 bool _edge_direction_acted_upon; // // // 00199 uint8_t _counter; 00200 uint8_t _samples; 00201 }; 00202 00203 }; // namespace AjK ends. 00204 00205 using namespace AjK; 00206 00207 #endif 00208
Generated on Wed Jul 13 2022 08:13:48 by
1.7.2