;

Dependents:   Audio_Player Wav_player_RPG_menu

Fork of DebounceIn by Andy K

Committer:
rsartin3
Date:
Mon Mar 04 22:37:01 2013 +0000
Revision:
2:1f3e0982f89d
Parent:
1:91a2e988ba9d
;

Who changed what in which revision?

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