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