;
Dependents: Audio_Player Wav_player_RPG_menu
Fork of DebounceIn by
DebounceIn.h@0:38fc7a626a26, 2010-10-27 (annotated)
- Committer:
- AjK
- Date:
- Wed Oct 27 01:16:02 2010 +0000
- Revision:
- 0:38fc7a626a26
- Child:
- 1:91a2e988ba9d
Add registered name to the constructor.
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 |
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 | 0:38fc7a626a26 | 50 | * This example shows one input displayed by two outputs. The input |
AjK | 0:38fc7a626a26 | 51 | * is debounced by the default 10ms. |
AjK | 0:38fc7a626a26 | 52 | */ |
AjK | 0:38fc7a626a26 | 53 | |
AjK | 0:38fc7a626a26 | 54 | class DebounceIn : public DigitalIn { |
AjK | 0:38fc7a626a26 | 55 | public: |
AjK | 0:38fc7a626a26 | 56 | |
AjK | 0:38fc7a626a26 | 57 | /** set_debounce_us |
AjK | 0:38fc7a626a26 | 58 | * |
AjK | 0:38fc7a626a26 | 59 | * Sets the debounce sample period time in microseconds, default is 1000 (1ms) |
AjK | 0:38fc7a626a26 | 60 | * |
AjK | 0:38fc7a626a26 | 61 | * @param int i The debounce sample period time to set. |
AjK | 0:38fc7a626a26 | 62 | */ |
AjK | 0:38fc7a626a26 | 63 | void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); } |
AjK | 0:38fc7a626a26 | 64 | |
AjK | 0:38fc7a626a26 | 65 | /** set_samples |
AjK | 0:38fc7a626a26 | 66 | * |
AjK | 0:38fc7a626a26 | 67 | * Defines the number of samples before switching the shadow |
AjK | 0:38fc7a626a26 | 68 | * definition of the pin. |
AjK | 0:38fc7a626a26 | 69 | * |
AjK | 0:38fc7a626a26 | 70 | * @param int i The number of samples. |
AjK | 0:38fc7a626a26 | 71 | */ |
AjK | 0:38fc7a626a26 | 72 | void set_samples(int i) { _samples = i; } |
AjK | 0:38fc7a626a26 | 73 | |
AjK | 0:38fc7a626a26 | 74 | /** read |
AjK | 0:38fc7a626a26 | 75 | * |
AjK | 0:38fc7a626a26 | 76 | * Read the value of the debounced pin. |
AjK | 0:38fc7a626a26 | 77 | */ |
AjK | 0:38fc7a626a26 | 78 | int read(void) { return _shadow; } |
AjK | 0:38fc7a626a26 | 79 | |
AjK | 0:38fc7a626a26 | 80 | #ifdef MBED_OPERATORS |
AjK | 0:38fc7a626a26 | 81 | /** operator int() |
AjK | 0:38fc7a626a26 | 82 | * |
AjK | 0:38fc7a626a26 | 83 | * Read the value of the debounced pin. |
AjK | 0:38fc7a626a26 | 84 | */ |
AjK | 0:38fc7a626a26 | 85 | operator int() { return read(); } |
AjK | 0:38fc7a626a26 | 86 | #endif |
AjK | 0:38fc7a626a26 | 87 | |
AjK | 0:38fc7a626a26 | 88 | /** Constructor |
AjK | 0:38fc7a626a26 | 89 | * |
AjK | 0:38fc7a626a26 | 90 | * @param PinName pin The pin to assign as an input. |
AjK | 0:38fc7a626a26 | 91 | */ |
AjK | 0:38fc7a626a26 | 92 | DebounceIn(PinName pin, const char *name = NULL) : DigitalIn(pin, name) { _counter = 0; _samples = 10; set_debounce_us(1000); }; |
AjK | 0:38fc7a626a26 | 93 | |
AjK | 0:38fc7a626a26 | 94 | protected: |
AjK | 0:38fc7a626a26 | 95 | void _callback(void) { |
AjK | 0:38fc7a626a26 | 96 | if (DigitalIn::read()) { |
AjK | 0:38fc7a626a26 | 97 | if (_counter < _samples) _counter++; |
AjK | 0:38fc7a626a26 | 98 | if (_counter == _samples) _shadow = 1; |
AjK | 0:38fc7a626a26 | 99 | } |
AjK | 0:38fc7a626a26 | 100 | else { |
AjK | 0:38fc7a626a26 | 101 | if (_counter > 0) _counter--; |
AjK | 0:38fc7a626a26 | 102 | if (_counter == 0) _shadow = 0; |
AjK | 0:38fc7a626a26 | 103 | } |
AjK | 0:38fc7a626a26 | 104 | } |
AjK | 0:38fc7a626a26 | 105 | |
AjK | 0:38fc7a626a26 | 106 | Ticker _ticker; |
AjK | 0:38fc7a626a26 | 107 | int _shadow; |
AjK | 0:38fc7a626a26 | 108 | int _counter; |
AjK | 0:38fc7a626a26 | 109 | int _samples; |
AjK | 0:38fc7a626a26 | 110 | }; |
AjK | 0:38fc7a626a26 | 111 | |
AjK | 0:38fc7a626a26 | 112 | #endif |
AjK | 0:38fc7a626a26 | 113 |