Debounce a mechanical switch by periodic sampling.
Dependents: RedButton WS_7_Seg_mit_LM1635 WS_7_Seg_mit_LM1635 Lichtschalter_M0 ... more
Debouncer.h@2:7f2f00805d41, 2015-05-18 (annotated)
- Committer:
- huliyang
- Date:
- Mon May 18 13:25:07 2015 +0000
- Revision:
- 2:7f2f00805d41
- Parent:
- 0:2359961af424
Debouncer: re-order fields for better alignment & packing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
huliyang | 0:2359961af424 | 1 | /* Copyright (c) 2015 Liyang HU, MIT License |
huliyang | 0:2359961af424 | 2 | * |
huliyang | 0:2359961af424 | 3 | * Permission is hereby granted, free of charge, to any person obtaining |
huliyang | 0:2359961af424 | 4 | * a copy of this software and associated documentation files (the |
huliyang | 0:2359961af424 | 5 | * "Software"), to deal in the Software without restriction, including |
huliyang | 0:2359961af424 | 6 | * without limitation the rights to use, copy, modify, merge, publish, |
huliyang | 0:2359961af424 | 7 | * distribute, sublicense, and/or sell copies of the Software, and to |
huliyang | 0:2359961af424 | 8 | * permit persons to whom the Software is furnished to do so, subject to |
huliyang | 0:2359961af424 | 9 | * the following conditions: |
huliyang | 0:2359961af424 | 10 | * |
huliyang | 0:2359961af424 | 11 | * The above copyright notice and this permission notice shall be |
huliyang | 0:2359961af424 | 12 | * included in all copies or substantial portions of the Software. |
huliyang | 0:2359961af424 | 13 | * |
huliyang | 0:2359961af424 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
huliyang | 0:2359961af424 | 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
huliyang | 0:2359961af424 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
huliyang | 0:2359961af424 | 17 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
huliyang | 0:2359961af424 | 18 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
huliyang | 0:2359961af424 | 19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
huliyang | 0:2359961af424 | 20 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
huliyang | 0:2359961af424 | 21 | */ |
huliyang | 0:2359961af424 | 22 | |
huliyang | 0:2359961af424 | 23 | #include <mbed.h> |
huliyang | 0:2359961af424 | 24 | #include <DigitalIn.h> |
huliyang | 0:2359961af424 | 25 | #include <Ticker.h> |
huliyang | 0:2359961af424 | 26 | |
huliyang | 0:2359961af424 | 27 | class Debouncer : private Ticker |
huliyang | 0:2359961af424 | 28 | { |
huliyang | 0:2359961af424 | 29 | private: |
huliyang | 0:2359961af424 | 30 | FunctionPointer fun_fall, fun_rise; |
huliyang | 0:2359961af424 | 31 | DigitalIn in; |
huliyang | 2:7f2f00805d41 | 32 | uint32_t previous; |
huliyang | 2:7f2f00805d41 | 33 | uint8_t _samples; |
huliyang | 0:2359961af424 | 34 | bool now; |
huliyang | 0:2359961af424 | 35 | void tick(void); |
huliyang | 0:2359961af424 | 36 | |
huliyang | 0:2359961af424 | 37 | public: |
huliyang | 0:2359961af424 | 38 | Debouncer(PinName pin, PinMode mode = PullUp); |
huliyang | 2:7f2f00805d41 | 39 | Debouncer &samples(uint8_t n = 8); |
huliyang | 0:2359961af424 | 40 | Debouncer &period(float seconds = 3.90625e-3); |
huliyang | 0:2359961af424 | 41 | operator bool(void); |
huliyang | 0:2359961af424 | 42 | |
huliyang | 0:2359961af424 | 43 | Debouncer &attach_fall(void (*fun)(void) = NULL); |
huliyang | 0:2359961af424 | 44 | Debouncer &attach_rise(void (*fun)(void) = NULL); |
huliyang | 0:2359961af424 | 45 | template <typename T> |
huliyang | 0:2359961af424 | 46 | Debouncer &attach_fall(T *obj, void (T::*fun)(void) = NULL); |
huliyang | 0:2359961af424 | 47 | template <typename T> |
huliyang | 0:2359961af424 | 48 | Debouncer &attach_rise(T *obj, void (T::*fun)(void) = NULL); |
huliyang | 0:2359961af424 | 49 | }; |