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.
ExioBufferedDebounceIn.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 /* 00024 * 2016.11.5 Customized for MCP23S17 Buffered Input by ryood 00025 */ 00026 00027 #ifndef EXIOBUFFEREDDEBOUNCEIN_H 00028 #define EXIOBUFFEREDDEBOUNCEIN_H 00029 00030 #include "mbed.h" 00031 #include "ExioBufferedIn.h" 00032 00033 /** DebounceIn adds mechanical switch debouncing to DigitialIn. 00034 * 00035 * Example: 00036 * @code 00037 * #include "mbed.h" 00038 * #include "DebounceIn.h" 00039 * 00040 * DebounceIn d(p5); 00041 * DigitialOut led1(LED1); 00042 * DigitialOut led2(LED2); 00043 * 00044 * int main() { 00045 * while(1) { 00046 * led1 = d; 00047 * led2 = d.read(); 00048 * } 00049 * } 00050 * @endcode 00051 * 00052 * @see set_debounce_us() To change the sampling frequency. 00053 * @see set_samples() To alter the number of samples. 00054 * 00055 * Users of this library may also be interested in PinDetect library:- 00056 * @see http://mbed.org/users/AjK/libraries/PinDetect/latest 00057 * 00058 * This example shows one input displayed by two outputs. The input 00059 * is debounced by the default 10ms. 00060 */ 00061 00062 class ExioBufferedDebounceIn : public ExioBufferedIn { 00063 public: 00064 00065 /** set_debounce_us 00066 * 00067 * Sets the debounce sample period time in microseconds, default is 1000 (1ms) 00068 * 00069 * @param int i The debounce sample period time to set. 00070 */ 00071 void set_debounce_us(int i) { _ticker.attach_us(this, &ExioBufferedDebounceIn::_callback, i); } 00072 00073 /** set_samples 00074 * 00075 * Defines the number of samples before switching the shadow 00076 * definition of the pin. 00077 * 00078 * @param int i The number of samples. 00079 */ 00080 void set_samples(int i) { _samples = i; } 00081 00082 /** read 00083 * 00084 * Read the value of the debounced pin. 00085 */ 00086 int read(void) { return _shadow; } 00087 00088 #ifdef MBED_OPERATORS 00089 /** operator int() 00090 * 00091 * Read the value of the debounced pin. 00092 */ 00093 operator int() { return read(); } 00094 #endif 00095 00096 /** Constructor 00097 * 00098 * @param buffer The Exio Buffered object 00099 * @param pin The pin number 00100 */ 00101 ExioBufferedDebounceIn(ExioInBuffer* buffer, int pin) : 00102 ExioBufferedIn(buffer, pin) 00103 { 00104 _counter = 0; 00105 _samples = 10; 00106 // set_debounce_us(1000); 00107 }; 00108 00109 protected: 00110 void _callback(void) { 00111 if (ExioBufferedIn::read()) { 00112 if (_counter < _samples) _counter++; 00113 if (_counter == _samples) _shadow = 1; 00114 } 00115 else { 00116 if (_counter > 0) _counter--; 00117 if (_counter == 0) _shadow = 0; 00118 } 00119 } 00120 00121 Ticker _ticker; 00122 int _shadow; 00123 int _counter; 00124 int _samples; 00125 }; 00126 00127 #endif //EXIOBUFFEREDDEBOUNCEIN_H 00128
Generated on Sat Jul 16 2022 02:09:56 by
1.7.2