Extends DigitalIn to DebounceIn to provide mechanical switch debouncing.

Dependents:   AVC_20110423 Pushbutton_NoBounce_Demo FinalTime AVC_2012 ... more

Committer:
Sissors
Date:
Fri May 31 17:31:17 2013 +0000
Revision:
2:31ae5cfb44a4
Parent:
1:91a2e988ba9d
Removed 'name' argument from constructor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:38fc7a626a26 1 /*
AjK 0:38fc7a626a26 2 Copyright (c) 2010 Andy Kirkham
AjK 0:38fc7a626a26 3
AjK 0:38fc7a626a26 4 Permission is hereby granted, free of charge, to any person obtaining a copy
AjK 0:38fc7a626a26 5 of this software and associated documentation files (the "Software"), to deal
AjK 0:38fc7a626a26 6 in the Software without restriction, including without limitation the rights
AjK 0:38fc7a626a26 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
AjK 0:38fc7a626a26 8 copies of the Software, and to permit persons to whom the Software is
AjK 0:38fc7a626a26 9 furnished to do so, subject to the following conditions:
AjK 0:38fc7a626a26 10
AjK 0:38fc7a626a26 11 The above copyright notice and this permission notice shall be included in
AjK 0:38fc7a626a26 12 all copies or substantial portions of the Software.
AjK 0:38fc7a626a26 13
AjK 0:38fc7a626a26 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
AjK 0:38fc7a626a26 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
AjK 0:38fc7a626a26 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AjK 0:38fc7a626a26 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
AjK 0:38fc7a626a26 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AjK 0:38fc7a626a26 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
AjK 0:38fc7a626a26 20 THE SOFTWARE.
AjK 0:38fc7a626a26 21 */
AjK 0:38fc7a626a26 22
AjK 0:38fc7a626a26 23 #ifndef DEBOUNCEIN_H
AjK 0:38fc7a626a26 24 #define DEBOUNCEIN_H
AjK 0:38fc7a626a26 25
AjK 0:38fc7a626a26 26 #include "mbed.h"
AjK 0:38fc7a626a26 27
AjK 0:38fc7a626a26 28 /** DebounceIn adds mechanical switch debouncing to DigitialIn.
AjK 0:38fc7a626a26 29 *
AjK 0:38fc7a626a26 30 * Example:
AjK 0:38fc7a626a26 31 * @code
AjK 0:38fc7a626a26 32 * #include "mbed.h"
AjK 0:38fc7a626a26 33 * #include "DebounceIn.h"
AjK 0:38fc7a626a26 34 *
AjK 0:38fc7a626a26 35 * DebounceIn d(p5);
AjK 0:38fc7a626a26 36 * DigitialOut led1(LED1);
AjK 0:38fc7a626a26 37 * DigitialOut led2(LED2);
AjK 0:38fc7a626a26 38 *
AjK 0:38fc7a626a26 39 * int main() {
AjK 0:38fc7a626a26 40 * while(1) {
AjK 0:38fc7a626a26 41 * led1 = d;
AjK 0:38fc7a626a26 42 * led2 = d.read();
AjK 0:38fc7a626a26 43 * }
AjK 0:38fc7a626a26 44 * }
AjK 0:38fc7a626a26 45 * @endcode
AjK 0:38fc7a626a26 46 *
AjK 0:38fc7a626a26 47 * @see set_debounce_us() To change the sampling frequency.
AjK 0:38fc7a626a26 48 * @see set_samples() To alter the number of samples.
AjK 0:38fc7a626a26 49 *
AjK 1:91a2e988ba9d 50 * Users of this library may also be interested in PinDetect library:-
AjK 1:91a2e988ba9d 51 * @see http://mbed.org/users/AjK/libraries/PinDetect/latest
AjK 1:91a2e988ba9d 52 *
AjK 0:38fc7a626a26 53 * This example shows one input displayed by two outputs. The input
AjK 0:38fc7a626a26 54 * is debounced by the default 10ms.
AjK 0:38fc7a626a26 55 */
AjK 0:38fc7a626a26 56
AjK 0:38fc7a626a26 57 class DebounceIn : public DigitalIn {
AjK 0:38fc7a626a26 58 public:
AjK 0:38fc7a626a26 59
AjK 0:38fc7a626a26 60 /** set_debounce_us
AjK 0:38fc7a626a26 61 *
AjK 0:38fc7a626a26 62 * Sets the debounce sample period time in microseconds, default is 1000 (1ms)
AjK 0:38fc7a626a26 63 *
AjK 0:38fc7a626a26 64 * @param int i The debounce sample period time to set.
AjK 0:38fc7a626a26 65 */
AjK 0:38fc7a626a26 66 void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); }
AjK 0:38fc7a626a26 67
AjK 0:38fc7a626a26 68 /** set_samples
AjK 0:38fc7a626a26 69 *
AjK 0:38fc7a626a26 70 * Defines the number of samples before switching the shadow
AjK 0:38fc7a626a26 71 * definition of the pin.
AjK 0:38fc7a626a26 72 *
AjK 0:38fc7a626a26 73 * @param int i The number of samples.
AjK 0:38fc7a626a26 74 */
AjK 0:38fc7a626a26 75 void set_samples(int i) { _samples = i; }
AjK 0:38fc7a626a26 76
AjK 0:38fc7a626a26 77 /** read
AjK 0:38fc7a626a26 78 *
AjK 0:38fc7a626a26 79 * Read the value of the debounced pin.
AjK 0:38fc7a626a26 80 */
AjK 0:38fc7a626a26 81 int read(void) { return _shadow; }
AjK 0:38fc7a626a26 82
AjK 0:38fc7a626a26 83 #ifdef MBED_OPERATORS
AjK 0:38fc7a626a26 84 /** operator int()
AjK 0:38fc7a626a26 85 *
AjK 0:38fc7a626a26 86 * Read the value of the debounced pin.
AjK 0:38fc7a626a26 87 */
AjK 0:38fc7a626a26 88 operator int() { return read(); }
AjK 0:38fc7a626a26 89 #endif
AjK 0:38fc7a626a26 90
AjK 0:38fc7a626a26 91 /** Constructor
AjK 0:38fc7a626a26 92 *
AjK 0:38fc7a626a26 93 * @param PinName pin The pin to assign as an input.
AjK 0:38fc7a626a26 94 */
Sissors 2:31ae5cfb44a4 95 DebounceIn(PinName pin) : DigitalIn(pin) { _counter = 0; _samples = 10; set_debounce_us(1000); };
AjK 0:38fc7a626a26 96
AjK 0:38fc7a626a26 97 protected:
AjK 0:38fc7a626a26 98 void _callback(void) {
AjK 0:38fc7a626a26 99 if (DigitalIn::read()) {
AjK 0:38fc7a626a26 100 if (_counter < _samples) _counter++;
AjK 0:38fc7a626a26 101 if (_counter == _samples) _shadow = 1;
AjK 0:38fc7a626a26 102 }
AjK 0:38fc7a626a26 103 else {
AjK 0:38fc7a626a26 104 if (_counter > 0) _counter--;
AjK 0:38fc7a626a26 105 if (_counter == 0) _shadow = 0;
AjK 0:38fc7a626a26 106 }
AjK 0:38fc7a626a26 107 }
AjK 0:38fc7a626a26 108
AjK 0:38fc7a626a26 109 Ticker _ticker;
AjK 0:38fc7a626a26 110 int _shadow;
AjK 0:38fc7a626a26 111 int _counter;
AjK 0:38fc7a626a26 112 int _samples;
AjK 0:38fc7a626a26 113 };
AjK 0:38fc7a626a26 114
AjK 0:38fc7a626a26 115 #endif
AjK 0:38fc7a626a26 116