A digital input with switch debouncing

Dependents:   DebounceIn_Demo RotaryEncoderWaterFlow

Committer:
takuo
Date:
Sun Dec 20 12:04:08 2015 +0000
Revision:
0:e43521d55082
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef DEBOUNCEIN_H_
takuo 0:e43521d55082 18 #define DEBOUNCEIN_H_
takuo 0:e43521d55082 19
takuo 0:e43521d55082 20 #include "mbed.h"
takuo 0:e43521d55082 21
takuo 0:e43521d55082 22 /** A digital input with switch debouncing
takuo 0:e43521d55082 23 *
takuo 0:e43521d55082 24 * Example:
takuo 0:e43521d55082 25 * @code
takuo 0:e43521d55082 26 * #include "mbed.h"
takuo 0:e43521d55082 27 * #include "DebounceIn.h"
takuo 0:e43521d55082 28 *
takuo 0:e43521d55082 29 * int nrise = 0, nfall = 0;
takuo 0:e43521d55082 30 * void rise() { nrise++; }
takuo 0:e43521d55082 31 * void fall() { nfall++; }
takuo 0:e43521d55082 32 *
takuo 0:e43521d55082 33 * int main() {
takuo 0:e43521d55082 34 * DebounceIn button(p14);
takuo 0:e43521d55082 35 * button.rise(rise);
takuo 0:e43521d55082 36 * button.fall(fall);
takuo 0:e43521d55082 37 * while (true) {
takuo 0:e43521d55082 38 * printf("nrise=%d, nfall=%d\n", nrise, nfall);
takuo 0:e43521d55082 39 * wait(1);
takuo 0:e43521d55082 40 * }
takuo 0:e43521d55082 41 * }
takuo 0:e43521d55082 42 * @endcode
takuo 0:e43521d55082 43 */
takuo 0:e43521d55082 44 class DebounceIn: public mbed::DigitalIn {
takuo 0:e43521d55082 45 public:
takuo 0:e43521d55082 46 /** Create a DebounceIn object connected to the specified pin
takuo 0:e43521d55082 47 *
takuo 0:e43521d55082 48 * @param pin DebounceIn pin to connect to
takuo 0:e43521d55082 49 * @param us Input sampling rate (default 1000 microseconds)
takuo 0:e43521d55082 50 */
takuo 0:e43521d55082 51 DebounceIn(PinName pin, timestamp_t us = 1000);
takuo 0:e43521d55082 52
takuo 0:e43521d55082 53 virtual ~DebounceIn();
takuo 0:e43521d55082 54
takuo 0:e43521d55082 55 /** Set the input sampling rate
takuo 0:e43521d55082 56 *
takuo 0:e43521d55082 57 * @param us Input sampling rate
takuo 0:e43521d55082 58 */
takuo 0:e43521d55082 59 void set_sample_rate(timestamp_t us);
takuo 0:e43521d55082 60
takuo 0:e43521d55082 61 /** Read the input, represented as 0 or 1 (int)
takuo 0:e43521d55082 62 *
takuo 0:e43521d55082 63 * @returns An integer representing the state of the input pin
takuo 0:e43521d55082 64 */
takuo 0:e43521d55082 65 int read(void);
takuo 0:e43521d55082 66
takuo 0:e43521d55082 67 #ifdef MBED_OPERATORS
takuo 0:e43521d55082 68 /** An operator shorthand for read()
takuo 0:e43521d55082 69 */
takuo 0:e43521d55082 70 operator int();
takuo 0:e43521d55082 71 #endif
takuo 0:e43521d55082 72
takuo 0:e43521d55082 73 /** Attach a function to call when a rising edge occurs on the input
takuo 0:e43521d55082 74 *
takuo 0:e43521d55082 75 * @param fptr pointer to a void function, or 0 to set as none
takuo 0:e43521d55082 76 */
takuo 0:e43521d55082 77 void rise(void (*fptr)(void));
takuo 0:e43521d55082 78
takuo 0:e43521d55082 79 /** Attach a member function to call when a rising edge occurs on the input
takuo 0:e43521d55082 80 *
takuo 0:e43521d55082 81 * @param tptr pointer to the object to call the member function on
takuo 0:e43521d55082 82 * @param mptr pointer to the member function to be called
takuo 0:e43521d55082 83 */
takuo 0:e43521d55082 84 template<typename T>
takuo 0:e43521d55082 85 void rise(T* tptr, void (T::*mptr)(void)) {
takuo 0:e43521d55082 86 _rise.attach(tptr, mptr);
takuo 0:e43521d55082 87 }
takuo 0:e43521d55082 88
takuo 0:e43521d55082 89 /** Attach a function to call when a falling edge occurs on the input
takuo 0:e43521d55082 90 *
takuo 0:e43521d55082 91 * @param fptr pointer to a void function, or 0 to set as none
takuo 0:e43521d55082 92 */
takuo 0:e43521d55082 93 void fall(void (*fptr)(void));
takuo 0:e43521d55082 94
takuo 0:e43521d55082 95 /** Attach a member function to call when a falling edge occurs on the input
takuo 0:e43521d55082 96 *
takuo 0:e43521d55082 97 * @param tptr pointer to the object to call the member function on
takuo 0:e43521d55082 98 * @param mptr pointer to the member function to be called
takuo 0:e43521d55082 99 */
takuo 0:e43521d55082 100 template<typename T>
takuo 0:e43521d55082 101 void fall(T* tptr, void (T::*mptr)(void)) {
takuo 0:e43521d55082 102 _fall.attach(tptr, mptr);
takuo 0:e43521d55082 103 }
takuo 0:e43521d55082 104
takuo 0:e43521d55082 105 private:
takuo 0:e43521d55082 106 Ticker _ticker;
takuo 0:e43521d55082 107 int _prev, _curr;
takuo 0:e43521d55082 108 FunctionPointer _rise, _fall;
takuo 0:e43521d55082 109 void sample(void);
takuo 0:e43521d55082 110 };
takuo 0:e43521d55082 111
takuo 0:e43521d55082 112 #endif /* DEBOUNCEIN_H_ */