A digital input with switch debouncing
Dependents: DebounceIn_Demo RotaryEncoderWaterFlow
DebounceIn.cpp@0:e43521d55082, 2015-12-20 (annotated)
- Committer:
- takuo
- Date:
- Sun Dec 20 12:04:08 2015 +0000
- Revision:
- 0:e43521d55082
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuo | 0:e43521d55082 | 1 | /* A digital input with switch debouncing |
takuo | 0:e43521d55082 | 2 | * Copyright 2015, Takuo Watanabe |
takuo | 0:e43521d55082 | 3 | * |
takuo | 0:e43521d55082 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
takuo | 0:e43521d55082 | 5 | * you may not use this file except in compliance with the License. |
takuo | 0:e43521d55082 | 6 | * You may obtain a copy of the License at |
takuo | 0:e43521d55082 | 7 | * |
takuo | 0:e43521d55082 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
takuo | 0:e43521d55082 | 9 | * |
takuo | 0:e43521d55082 | 10 | * Unless required by applicable law or agreed to in writing, software |
takuo | 0:e43521d55082 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
takuo | 0:e43521d55082 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
takuo | 0:e43521d55082 | 13 | * See the License for the specific language governing permissions and |
takuo | 0:e43521d55082 | 14 | * limitations under the License. |
takuo | 0:e43521d55082 | 15 | */ |
takuo | 0:e43521d55082 | 16 | |
takuo | 0:e43521d55082 | 17 | #include "DebounceIn.h" |
takuo | 0:e43521d55082 | 18 | |
takuo | 0:e43521d55082 | 19 | DebounceIn::DebounceIn(PinName pin, timestamp_t us) : DigitalIn(pin) { |
takuo | 0:e43521d55082 | 20 | _prev = _curr = 0; |
takuo | 0:e43521d55082 | 21 | _rise = _fall = NULL; |
takuo | 0:e43521d55082 | 22 | set_sample_rate(us); |
takuo | 0:e43521d55082 | 23 | } |
takuo | 0:e43521d55082 | 24 | |
takuo | 0:e43521d55082 | 25 | DebounceIn::~DebounceIn() { |
takuo | 0:e43521d55082 | 26 | _ticker.detach(); |
takuo | 0:e43521d55082 | 27 | } |
takuo | 0:e43521d55082 | 28 | |
takuo | 0:e43521d55082 | 29 | int DebounceIn::read() { |
takuo | 0:e43521d55082 | 30 | return _curr; |
takuo | 0:e43521d55082 | 31 | } |
takuo | 0:e43521d55082 | 32 | |
takuo | 0:e43521d55082 | 33 | #ifdef MBED_OPERATORS |
takuo | 0:e43521d55082 | 34 | /** An operator shorthand for read() |
takuo | 0:e43521d55082 | 35 | */ |
takuo | 0:e43521d55082 | 36 | DebounceIn::operator int() { |
takuo | 0:e43521d55082 | 37 | return read(); |
takuo | 0:e43521d55082 | 38 | } |
takuo | 0:e43521d55082 | 39 | #endif |
takuo | 0:e43521d55082 | 40 | |
takuo | 0:e43521d55082 | 41 | void DebounceIn::set_sample_rate(timestamp_t us) { |
takuo | 0:e43521d55082 | 42 | _ticker.detach(); |
takuo | 0:e43521d55082 | 43 | _ticker.attach_us(this, &DebounceIn::sample, us); |
takuo | 0:e43521d55082 | 44 | } |
takuo | 0:e43521d55082 | 45 | |
takuo | 0:e43521d55082 | 46 | void DebounceIn::rise(void (*fptr)(void)) { |
takuo | 0:e43521d55082 | 47 | _rise.attach(fptr); |
takuo | 0:e43521d55082 | 48 | } |
takuo | 0:e43521d55082 | 49 | |
takuo | 0:e43521d55082 | 50 | void DebounceIn::fall(void (*fptr)(void)) { |
takuo | 0:e43521d55082 | 51 | _fall.attach(fptr); |
takuo | 0:e43521d55082 | 52 | } |
takuo | 0:e43521d55082 | 53 | |
takuo | 0:e43521d55082 | 54 | void DebounceIn::sample() { |
takuo | 0:e43521d55082 | 55 | int now = DigitalIn::read(); |
takuo | 0:e43521d55082 | 56 | if (now == _prev) { |
takuo | 0:e43521d55082 | 57 | if (!_curr && now) _rise.call(); |
takuo | 0:e43521d55082 | 58 | if (_curr && !now) _fall.call(); |
takuo | 0:e43521d55082 | 59 | _curr = now; |
takuo | 0:e43521d55082 | 60 | } |
takuo | 0:e43521d55082 | 61 | _prev = now; |
takuo | 0:e43521d55082 | 62 | } |