Final Commit

Committer:
jbeason3
Date:
Fri Oct 04 21:27:24 2019 +0000
Revision:
0:81e1a090126c
Final Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jbeason3 0:81e1a090126c 1 /* A digital input with switch debouncing
jbeason3 0:81e1a090126c 2 * Copyright 2015, Takuo Watanabe
jbeason3 0:81e1a090126c 3 *
jbeason3 0:81e1a090126c 4 * Licensed under the Apache License, Version 2.0 (the "License");
jbeason3 0:81e1a090126c 5 * you may not use this file except in compliance with the License.
jbeason3 0:81e1a090126c 6 * You may obtain a copy of the License at
jbeason3 0:81e1a090126c 7 *
jbeason3 0:81e1a090126c 8 * http://www.apache.org/licenses/LICENSE-2.0
jbeason3 0:81e1a090126c 9 *
jbeason3 0:81e1a090126c 10 * Unless required by applicable law or agreed to in writing, software
jbeason3 0:81e1a090126c 11 * distributed under the License is distributed on an "AS IS" BASIS,
jbeason3 0:81e1a090126c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jbeason3 0:81e1a090126c 13 * See the License for the specific language governing permissions and
jbeason3 0:81e1a090126c 14 * limitations under the License.
jbeason3 0:81e1a090126c 15 */
jbeason3 0:81e1a090126c 16
jbeason3 0:81e1a090126c 17 #ifndef DEBOUNCEIN_H_
jbeason3 0:81e1a090126c 18 #define DEBOUNCEIN_H_
jbeason3 0:81e1a090126c 19
jbeason3 0:81e1a090126c 20 #include "mbed.h"
jbeason3 0:81e1a090126c 21
jbeason3 0:81e1a090126c 22 /** A digital input with switch debouncing
jbeason3 0:81e1a090126c 23 *
jbeason3 0:81e1a090126c 24 * Example:
jbeason3 0:81e1a090126c 25 * @code
jbeason3 0:81e1a090126c 26 * #include "mbed.h"
jbeason3 0:81e1a090126c 27 * #include "DebounceIn.h"
jbeason3 0:81e1a090126c 28 *
jbeason3 0:81e1a090126c 29 * int nrise = 0, nfall = 0;
jbeason3 0:81e1a090126c 30 * void rise() { nrise++; }
jbeason3 0:81e1a090126c 31 * void fall() { nfall++; }
jbeason3 0:81e1a090126c 32 *
jbeason3 0:81e1a090126c 33 * int main() {
jbeason3 0:81e1a090126c 34 * DebounceIn button(p14);
jbeason3 0:81e1a090126c 35 * button.rise(rise);
jbeason3 0:81e1a090126c 36 * button.fall(fall);
jbeason3 0:81e1a090126c 37 * while (true) {
jbeason3 0:81e1a090126c 38 * printf("nrise=%d, nfall=%d\n", nrise, nfall);
jbeason3 0:81e1a090126c 39 * wait(1);
jbeason3 0:81e1a090126c 40 * }
jbeason3 0:81e1a090126c 41 * }
jbeason3 0:81e1a090126c 42 * @endcode
jbeason3 0:81e1a090126c 43 */
jbeason3 0:81e1a090126c 44 class DebounceIn: public mbed::DigitalIn {
jbeason3 0:81e1a090126c 45 public:
jbeason3 0:81e1a090126c 46 /** Create a DebounceIn object connected to the specified pin
jbeason3 0:81e1a090126c 47 *
jbeason3 0:81e1a090126c 48 * @param pin DebounceIn pin to connect to
jbeason3 0:81e1a090126c 49 * @param us Input sampling rate (default 1000 microseconds)
jbeason3 0:81e1a090126c 50 */
jbeason3 0:81e1a090126c 51 DebounceIn(PinName pin, timestamp_t us = 1000);
jbeason3 0:81e1a090126c 52
jbeason3 0:81e1a090126c 53 virtual ~DebounceIn();
jbeason3 0:81e1a090126c 54
jbeason3 0:81e1a090126c 55 /** Set the input sampling rate
jbeason3 0:81e1a090126c 56 *
jbeason3 0:81e1a090126c 57 * @param us Input sampling rate
jbeason3 0:81e1a090126c 58 */
jbeason3 0:81e1a090126c 59 void set_sample_rate(timestamp_t us);
jbeason3 0:81e1a090126c 60
jbeason3 0:81e1a090126c 61 /** Read the input, represented as 0 or 1 (int)
jbeason3 0:81e1a090126c 62 *
jbeason3 0:81e1a090126c 63 * @returns An integer representing the state of the input pin
jbeason3 0:81e1a090126c 64 */
jbeason3 0:81e1a090126c 65 int read(void);
jbeason3 0:81e1a090126c 66
jbeason3 0:81e1a090126c 67 #ifdef MBED_OPERATORS
jbeason3 0:81e1a090126c 68 /** An operator shorthand for read()
jbeason3 0:81e1a090126c 69 */
jbeason3 0:81e1a090126c 70 operator int();
jbeason3 0:81e1a090126c 71 #endif
jbeason3 0:81e1a090126c 72
jbeason3 0:81e1a090126c 73 /** Attach a function to call when a rising edge occurs on the input
jbeason3 0:81e1a090126c 74 *
jbeason3 0:81e1a090126c 75 * @param fptr pointer to a void function, or 0 to set as none
jbeason3 0:81e1a090126c 76 */
jbeason3 0:81e1a090126c 77 void rise(void (*fptr)(void));
jbeason3 0:81e1a090126c 78
jbeason3 0:81e1a090126c 79 /** Attach a member function to call when a rising edge occurs on the input
jbeason3 0:81e1a090126c 80 *
jbeason3 0:81e1a090126c 81 * @param tptr pointer to the object to call the member function on
jbeason3 0:81e1a090126c 82 * @param mptr pointer to the member function to be called
jbeason3 0:81e1a090126c 83 */
jbeason3 0:81e1a090126c 84 template<typename T>
jbeason3 0:81e1a090126c 85 void rise(T* tptr, void (T::*mptr)(void)) {
jbeason3 0:81e1a090126c 86 _rise.attach(tptr, mptr);
jbeason3 0:81e1a090126c 87 }
jbeason3 0:81e1a090126c 88
jbeason3 0:81e1a090126c 89 /** Attach a function to call when a falling edge occurs on the input
jbeason3 0:81e1a090126c 90 *
jbeason3 0:81e1a090126c 91 * @param fptr pointer to a void function, or 0 to set as none
jbeason3 0:81e1a090126c 92 */
jbeason3 0:81e1a090126c 93 void fall(void (*fptr)(void));
jbeason3 0:81e1a090126c 94
jbeason3 0:81e1a090126c 95 /** Attach a member function to call when a falling edge occurs on the input
jbeason3 0:81e1a090126c 96 *
jbeason3 0:81e1a090126c 97 * @param tptr pointer to the object to call the member function on
jbeason3 0:81e1a090126c 98 * @param mptr pointer to the member function to be called
jbeason3 0:81e1a090126c 99 */
jbeason3 0:81e1a090126c 100 template<typename T>
jbeason3 0:81e1a090126c 101 void fall(T* tptr, void (T::*mptr)(void)) {
jbeason3 0:81e1a090126c 102 _fall.attach(tptr, mptr);
jbeason3 0:81e1a090126c 103 }
jbeason3 0:81e1a090126c 104
jbeason3 0:81e1a090126c 105 private:
jbeason3 0:81e1a090126c 106 Ticker _ticker;
jbeason3 0:81e1a090126c 107 int _prev, _curr;
jbeason3 0:81e1a090126c 108 FunctionPointer _rise, _fall;
jbeason3 0:81e1a090126c 109 void sample(void);
jbeason3 0:81e1a090126c 110 };
jbeason3 0:81e1a090126c 111
jbeason3 0:81e1a090126c 112 #endif /* DEBOUNCEIN_H_ */