|
| 1 | +/* The MIT License (MIT) |
| 2 | + * |
| 3 | + * Copyright (c) 2014 - 2015, Andreas Merkle |
| 4 | + * http://www.blue-andi.de |
| 5 | + * vscp@blue-andi.de |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all |
| 15 | + * copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + * SOFTWARE. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +/******************************************************************************* |
| 28 | + DESCRIPTION |
| 29 | +*******************************************************************************/ |
| 30 | +/** |
| 31 | +@brief Digital input debounce class |
| 32 | +@file DigInDebounce.h |
| 33 | +@author Andreas Merkle, http://www.blue-andi.de |
| 34 | +
|
| 35 | +@section desc Description |
| 36 | +This module provides a class for debouncing ditial in. |
| 37 | +
|
| 38 | +*******************************************************************************/ |
| 39 | +/** @defgroup DigInDebounce Digital input debounce class |
| 40 | + * This module provides a class for debouncing ditial in. |
| 41 | + * @{ |
| 42 | + */ |
| 43 | + |
| 44 | +/* |
| 45 | + * Don't forget to set JAVADOC_AUTOBRIEF to YES in the doxygen file to generate |
| 46 | + * a correct module description. |
| 47 | + */ |
| 48 | + |
| 49 | +#ifndef __DIGINDEBOUNCE_H__ |
| 50 | +#define __DIGINDEBOUNCE_H__ |
| 51 | + |
| 52 | +/******************************************************************************* |
| 53 | + INCLUDES |
| 54 | +*******************************************************************************/ |
| 55 | +#if (100 <= ARDUINO) |
| 56 | +#include "Arduino.h" |
| 57 | +#else /* (100 > ARDUINO) */ |
| 58 | +#include "WProgram.h" |
| 59 | +#endif /* (100 > ARDUINO) */ |
| 60 | + |
| 61 | +#ifdef __cplusplus |
| 62 | +extern "C" |
| 63 | +{ |
| 64 | +#endif |
| 65 | + |
| 66 | +/******************************************************************************* |
| 67 | + COMPILER SWITCHES |
| 68 | +*******************************************************************************/ |
| 69 | + |
| 70 | +/******************************************************************************* |
| 71 | + CONSTANTS |
| 72 | +*******************************************************************************/ |
| 73 | + |
| 74 | +/******************************************************************************* |
| 75 | + MACROS |
| 76 | +*******************************************************************************/ |
| 77 | + |
| 78 | +/******************************************************************************* |
| 79 | + CLASSES, TYPES AND STRUCTURES |
| 80 | +*******************************************************************************/ |
| 81 | + |
| 82 | +/** |
| 83 | + * Digital in debouncing |
| 84 | + */ |
| 85 | +class DigInDebounce |
| 86 | +{ |
| 87 | +public: |
| 88 | + |
| 89 | + /** Initialize and instantiate a digital in debouncer. |
| 90 | + * |
| 91 | + * @param[in] pin Digital input pin |
| 92 | + */ |
| 93 | + DigInDebounce(int pin = 0) : |
| 94 | + mPin(pin), |
| 95 | + mState(HIGH), |
| 96 | + mLastReadState(HIGH), |
| 97 | + mcDebounceDelay(50) |
| 98 | + { |
| 99 | + } |
| 100 | + |
| 101 | + /** Destroy a digital in debouncer instance. */ |
| 102 | + ~DigInDebounce() |
| 103 | + { |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Setup digital input pin. |
| 108 | + */ |
| 109 | + void setup(int pin) |
| 110 | + { |
| 111 | + mPin = pin; |
| 112 | + |
| 113 | + mState = digitalRead(mPin); |
| 114 | + mLastReadState = mState; |
| 115 | + |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the digital input state. |
| 121 | + */ |
| 122 | + int read(void) |
| 123 | + { |
| 124 | + int readState = digitalRead(mPin); |
| 125 | + |
| 126 | + if (readState != mLastReadState) |
| 127 | + { |
| 128 | + mLastDebounceTime = millis(); |
| 129 | + } |
| 130 | + else if ((millis() - mLastDebounceTime) > mcDebounceDelay) |
| 131 | + { |
| 132 | + if (readState != mState) |
| 133 | + { |
| 134 | + mState = readState; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + mLastReadState = readState; |
| 139 | + |
| 140 | + return mState; |
| 141 | + } |
| 142 | + |
| 143 | +private: |
| 144 | + |
| 145 | + int mPin; /**< Pin number */ |
| 146 | + int mState; /**< Current digital in state */ |
| 147 | + int mLastReadState; /**< Last read digital in state */ |
| 148 | + unsigned long mLastDebounceTime; /**< Last time the digital in state changed */ |
| 149 | + |
| 150 | + const unsigned long mcDebounceDelay; /**< Debounce delay */ |
| 151 | +}; |
| 152 | + |
| 153 | +/******************************************************************************* |
| 154 | + VARIABLES |
| 155 | +*******************************************************************************/ |
| 156 | + |
| 157 | +/******************************************************************************* |
| 158 | + FUNCTIONS |
| 159 | +*******************************************************************************/ |
| 160 | + |
| 161 | +#ifdef __cplusplus |
| 162 | +} |
| 163 | +#endif |
| 164 | + |
| 165 | +#endif /* __DIGINDEBOUNCE_H__ */ |
| 166 | + |
| 167 | +/** @} */ |
0 commit comments