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