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