Fan Controller - PWM HF (25kHz) Type, w/ a Two Button Escalator & a 4-LED Binary State-Display for UI.
The Circuit, as Built on an Universal PCB, Ready For Installation (in a '3.5 Drive-Slot) - Using a Thermoplastic Carrier :
DebounceIn.h@1:0a2f89da0616, 2018-11-04 (annotated)
- Committer:
- mzcs
- Date:
- Sun Nov 04 17:00:46 2018 +0000
- Revision:
- 1:0a2f89da0616
- Parent:
- 0:437bb8e2f8a7
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzcs | 0:437bb8e2f8a7 | 1 | /* |
mzcs | 0:437bb8e2f8a7 | 2 | Copyright (c) 2010 Andy Kirkham |
mzcs | 0:437bb8e2f8a7 | 3 | |
mzcs | 0:437bb8e2f8a7 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mzcs | 0:437bb8e2f8a7 | 5 | of this software and associated documentation files (the "Software"), to deal |
mzcs | 0:437bb8e2f8a7 | 6 | in the Software without restriction, including without limitation the rights |
mzcs | 0:437bb8e2f8a7 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mzcs | 0:437bb8e2f8a7 | 8 | copies of the Software, and to permit persons to whom the Software is |
mzcs | 0:437bb8e2f8a7 | 9 | furnished to do so, subject to the following conditions: |
mzcs | 0:437bb8e2f8a7 | 10 | |
mzcs | 0:437bb8e2f8a7 | 11 | The above copyright notice and this permission notice shall be included in |
mzcs | 0:437bb8e2f8a7 | 12 | all copies or substantial portions of the Software. |
mzcs | 0:437bb8e2f8a7 | 13 | |
mzcs | 0:437bb8e2f8a7 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mzcs | 0:437bb8e2f8a7 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mzcs | 0:437bb8e2f8a7 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mzcs | 0:437bb8e2f8a7 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mzcs | 0:437bb8e2f8a7 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mzcs | 0:437bb8e2f8a7 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mzcs | 0:437bb8e2f8a7 | 20 | THE SOFTWARE. |
mzcs | 0:437bb8e2f8a7 | 21 | */ |
mzcs | 0:437bb8e2f8a7 | 22 | |
mzcs | 0:437bb8e2f8a7 | 23 | #ifndef AJK_DEBOUNCEIN_H |
mzcs | 0:437bb8e2f8a7 | 24 | #define AJK_DEBOUNCEIN_H |
mzcs | 0:437bb8e2f8a7 | 25 | |
mzcs | 0:437bb8e2f8a7 | 26 | #include "mbed.h" |
mzcs | 0:437bb8e2f8a7 | 27 | |
mzcs | 0:437bb8e2f8a7 | 28 | /** DebounceIn adds mechanical switch debouncing to DigitialIn. |
mzcs | 0:437bb8e2f8a7 | 29 | * |
mzcs | 0:437bb8e2f8a7 | 30 | * Example: |
mzcs | 0:437bb8e2f8a7 | 31 | * @code |
mzcs | 0:437bb8e2f8a7 | 32 | * #include "mbed.h" |
mzcs | 0:437bb8e2f8a7 | 33 | * #include "DebounceIn.h" |
mzcs | 0:437bb8e2f8a7 | 34 | * |
mzcs | 0:437bb8e2f8a7 | 35 | * DebounceIn d(p5); |
mzcs | 0:437bb8e2f8a7 | 36 | * DigitialOut led1(LED1); |
mzcs | 0:437bb8e2f8a7 | 37 | * DigitialOut led2(LED2); |
mzcs | 0:437bb8e2f8a7 | 38 | * |
mzcs | 0:437bb8e2f8a7 | 39 | * int main() { |
mzcs | 0:437bb8e2f8a7 | 40 | * while(1) { |
mzcs | 0:437bb8e2f8a7 | 41 | * led1 = d; |
mzcs | 0:437bb8e2f8a7 | 42 | * led2 = d.read(); |
mzcs | 0:437bb8e2f8a7 | 43 | * } |
mzcs | 0:437bb8e2f8a7 | 44 | * } |
mzcs | 0:437bb8e2f8a7 | 45 | * @endcode |
mzcs | 0:437bb8e2f8a7 | 46 | * |
mzcs | 0:437bb8e2f8a7 | 47 | * @see set_debounce_us() To change the sampling frequency. |
mzcs | 0:437bb8e2f8a7 | 48 | * @see set_samples() To alter the number of samples. |
mzcs | 0:437bb8e2f8a7 | 49 | * |
mzcs | 0:437bb8e2f8a7 | 50 | * Users of this library may also be interested in PinDetect library:- |
mzcs | 0:437bb8e2f8a7 | 51 | * @see http://mbed.org/users/AjK/libraries/PinDetect/latest |
mzcs | 0:437bb8e2f8a7 | 52 | * |
mzcs | 0:437bb8e2f8a7 | 53 | * This example shows one input displayed by two outputs. The input |
mzcs | 0:437bb8e2f8a7 | 54 | * is debounced by the default 10ms. |
mzcs | 0:437bb8e2f8a7 | 55 | */ |
mzcs | 0:437bb8e2f8a7 | 56 | |
mzcs | 0:437bb8e2f8a7 | 57 | namespace AjK { |
mzcs | 0:437bb8e2f8a7 | 58 | |
mzcs | 0:437bb8e2f8a7 | 59 | class DebounceIn { |
mzcs | 0:437bb8e2f8a7 | 60 | public: |
mzcs | 0:437bb8e2f8a7 | 61 | |
mzcs | 0:437bb8e2f8a7 | 62 | friend class DigitalIn; |
mzcs | 0:437bb8e2f8a7 | 63 | friend class Ticker; |
mzcs | 0:437bb8e2f8a7 | 64 | |
mzcs | 0:437bb8e2f8a7 | 65 | /** set_debounce_us |
mzcs | 0:437bb8e2f8a7 | 66 | * |
mzcs | 0:437bb8e2f8a7 | 67 | * Sets the debounce sample period time in microseconds, default is 25000 (25ms) |
mzcs | 0:437bb8e2f8a7 | 68 | * |
mzcs | 0:437bb8e2f8a7 | 69 | * @param uint32_t i The debounce sample period time to set. |
mzcs | 0:437bb8e2f8a7 | 70 | */ |
mzcs | 0:437bb8e2f8a7 | 71 | // void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); } |
mzcs | 0:437bb8e2f8a7 | 72 | void set_debounce_us(uint32_t i) { _debounce_us = i; _ticker->attach_us(callback(this, &DebounceIn::_callback), i); } |
mzcs | 0:437bb8e2f8a7 | 73 | |
mzcs | 0:437bb8e2f8a7 | 74 | /** get_debounce_us |
mzcs | 0:437bb8e2f8a7 | 75 | * |
mzcs | 0:437bb8e2f8a7 | 76 | * Gets the debounce sample period time in microseconds |
mzcs | 0:437bb8e2f8a7 | 77 | * |
mzcs | 0:437bb8e2f8a7 | 78 | */ |
mzcs | 0:437bb8e2f8a7 | 79 | uint32_t get_debounce_us(void) { return _debounce_us; } // // // |
mzcs | 0:437bb8e2f8a7 | 80 | |
mzcs | 0:437bb8e2f8a7 | 81 | /** set_samples |
mzcs | 0:437bb8e2f8a7 | 82 | * |
mzcs | 0:437bb8e2f8a7 | 83 | * Defines the number of samples before switching the shadow |
mzcs | 0:437bb8e2f8a7 | 84 | * definition of the pin. |
mzcs | 0:437bb8e2f8a7 | 85 | * |
mzcs | 0:437bb8e2f8a7 | 86 | * @param int i The number of samples. |
mzcs | 0:437bb8e2f8a7 | 87 | */ |
mzcs | 0:437bb8e2f8a7 | 88 | void set_samples(uint8_t i) { _samples = i; } |
mzcs | 0:437bb8e2f8a7 | 89 | |
mzcs | 0:437bb8e2f8a7 | 90 | /** get_samples |
mzcs | 0:437bb8e2f8a7 | 91 | * |
mzcs | 0:437bb8e2f8a7 | 92 | * Gets the number of samples before switching the shadow |
mzcs | 0:437bb8e2f8a7 | 93 | * definition of the pin. |
mzcs | 0:437bb8e2f8a7 | 94 | * |
mzcs | 0:437bb8e2f8a7 | 95 | */ |
mzcs | 0:437bb8e2f8a7 | 96 | uint8_t get_samples(void) { return _samples; } |
mzcs | 0:437bb8e2f8a7 | 97 | |
mzcs | 0:437bb8e2f8a7 | 98 | /** read |
mzcs | 0:437bb8e2f8a7 | 99 | * |
mzcs | 0:437bb8e2f8a7 | 100 | * Read the value of the debounced pin. |
mzcs | 0:437bb8e2f8a7 | 101 | */ |
mzcs | 0:437bb8e2f8a7 | 102 | int8_t read(void) { return this->_shadow; } |
mzcs | 0:437bb8e2f8a7 | 103 | |
mzcs | 0:437bb8e2f8a7 | 104 | #ifdef MBED_OPERATORS |
mzcs | 0:437bb8e2f8a7 | 105 | /** operator int() |
mzcs | 0:437bb8e2f8a7 | 106 | * |
mzcs | 0:437bb8e2f8a7 | 107 | * Read the value of the debounced pin. |
mzcs | 0:437bb8e2f8a7 | 108 | */ |
mzcs | 0:437bb8e2f8a7 | 109 | operator int() { return read(); } |
mzcs | 0:437bb8e2f8a7 | 110 | #endif |
mzcs | 0:437bb8e2f8a7 | 111 | |
mzcs | 0:437bb8e2f8a7 | 112 | /** Set the pin mode. |
mzcs | 0:437bb8e2f8a7 | 113 | * |
mzcs | 0:437bb8e2f8a7 | 114 | * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode |
mzcs | 0:437bb8e2f8a7 | 115 | * @param PinMode m The mode to pass on to the DigitalIn |
mzcs | 0:437bb8e2f8a7 | 116 | */ |
mzcs | 0:437bb8e2f8a7 | 117 | void mode(PinMode m) { _digital_in->mode( m ); } |
mzcs | 0:437bb8e2f8a7 | 118 | |
mzcs | 0:437bb8e2f8a7 | 119 | /** get_edge_direction |
mzcs | 0:437bb8e2f8a7 | 120 | * |
mzcs | 0:437bb8e2f8a7 | 121 | * Gets the value of the debounced pin' edge direction (rising/falling). |
mzcs | 0:437bb8e2f8a7 | 122 | */ |
mzcs | 0:437bb8e2f8a7 | 123 | int8_t get_edge_direction(void) { return _edge_direction; } // // // |
mzcs | 0:437bb8e2f8a7 | 124 | |
mzcs | 0:437bb8e2f8a7 | 125 | /** get_edge_direction_acted_upon |
mzcs | 0:437bb8e2f8a7 | 126 | * |
mzcs | 0:437bb8e2f8a7 | 127 | * Gets the debounced pin edge direction' usage status (used/unused) |
mzcs | 0:437bb8e2f8a7 | 128 | */ |
mzcs | 0:437bb8e2f8a7 | 129 | bool get_edge_direction_acted_upon(void) { return _edge_direction_acted_upon; } // // // |
mzcs | 0:437bb8e2f8a7 | 130 | |
mzcs | 0:437bb8e2f8a7 | 131 | /** set_edge_direction_acted_upon |
mzcs | 0:437bb8e2f8a7 | 132 | * |
mzcs | 0:437bb8e2f8a7 | 133 | * Sets the status of the debounced pin edge direction' as used |
mzcs | 0:437bb8e2f8a7 | 134 | */ |
mzcs | 0:437bb8e2f8a7 | 135 | void set_edge_direction_acted_upon(void) { _edge_direction_acted_upon = 1; } // // // |
mzcs | 0:437bb8e2f8a7 | 136 | |
mzcs | 0:437bb8e2f8a7 | 137 | /** Constructor |
mzcs | 0:437bb8e2f8a7 | 138 | * |
mzcs | 0:437bb8e2f8a7 | 139 | * @param PinName pin The pin to assign as an input. |
mzcs | 0:437bb8e2f8a7 | 140 | */ |
mzcs | 0:437bb8e2f8a7 | 141 | DebounceIn() { error("DebounceIn: You must supply a PinName"); } |
mzcs | 0:437bb8e2f8a7 | 142 | |
mzcs | 0:437bb8e2f8a7 | 143 | DebounceIn(PinName pin) { _init(pin, PullNone); }; |
mzcs | 0:437bb8e2f8a7 | 144 | |
mzcs | 0:437bb8e2f8a7 | 145 | DebounceIn(PinName pin, PinMode mode) { _init(pin, mode); }; |
mzcs | 0:437bb8e2f8a7 | 146 | |
mzcs | 0:437bb8e2f8a7 | 147 | /** DebounceIn destructor |
mzcs | 0:437bb8e2f8a7 | 148 | */ |
mzcs | 0:437bb8e2f8a7 | 149 | ~DebounceIn() { |
mzcs | 0:437bb8e2f8a7 | 150 | if ( _ticker ) { delete( _ticker ); } |
mzcs | 0:437bb8e2f8a7 | 151 | if ( _digital_in ) { delete( _digital_in ); } |
mzcs | 0:437bb8e2f8a7 | 152 | } |
mzcs | 0:437bb8e2f8a7 | 153 | |
mzcs | 0:437bb8e2f8a7 | 154 | protected: |
mzcs | 0:437bb8e2f8a7 | 155 | |
mzcs | 0:437bb8e2f8a7 | 156 | /** initialise class |
mzcs | 0:437bb8e2f8a7 | 157 | * |
mzcs | 0:437bb8e2f8a7 | 158 | * @param PinName p is a valid pin that supports DigitalIn |
mzcs | 0:437bb8e2f8a7 | 159 | * @param PinMode m The mode the DigitalIn should use. |
mzcs | 0:437bb8e2f8a7 | 160 | */ |
mzcs | 0:437bb8e2f8a7 | 161 | void _init(PinName p, PinMode m) { |
mzcs | 0:437bb8e2f8a7 | 162 | |
mzcs | 0:437bb8e2f8a7 | 163 | _counter = 0; |
mzcs | 0:437bb8e2f8a7 | 164 | _samples = 10; |
mzcs | 0:437bb8e2f8a7 | 165 | _shadow = -1; |
mzcs | 0:437bb8e2f8a7 | 166 | _edge_direction = -1; |
mzcs | 0:437bb8e2f8a7 | 167 | _edge_direction_acted_upon = 1; |
mzcs | 0:437bb8e2f8a7 | 168 | |
mzcs | 0:437bb8e2f8a7 | 169 | _digital_in = new DigitalIn( p, m ); |
mzcs | 0:437bb8e2f8a7 | 170 | _ticker = new Ticker; |
mzcs | 0:437bb8e2f8a7 | 171 | |
mzcs | 0:437bb8e2f8a7 | 172 | set_debounce_us(25000); |
mzcs | 0:437bb8e2f8a7 | 173 | } |
mzcs | 0:437bb8e2f8a7 | 174 | |
mzcs | 0:437bb8e2f8a7 | 175 | void _callback(void) { |
mzcs | 0:437bb8e2f8a7 | 176 | |
mzcs | 0:437bb8e2f8a7 | 177 | if (_digital_in->read() == 1) { |
mzcs | 0:437bb8e2f8a7 | 178 | if (_counter < _samples) _counter++; |
mzcs | 0:437bb8e2f8a7 | 179 | if (_counter == _samples) { |
mzcs | 0:437bb8e2f8a7 | 180 | if (_shadow == 0) { _edge_direction = 1; _edge_direction_acted_upon = 0; } // // // |
mzcs | 0:437bb8e2f8a7 | 181 | _shadow = 1; |
mzcs | 0:437bb8e2f8a7 | 182 | } |
mzcs | 0:437bb8e2f8a7 | 183 | } |
mzcs | 0:437bb8e2f8a7 | 184 | else { |
mzcs | 0:437bb8e2f8a7 | 185 | if (_counter > 0) _counter--; |
mzcs | 0:437bb8e2f8a7 | 186 | if (_counter == 0) { |
mzcs | 0:437bb8e2f8a7 | 187 | if (_shadow == 1) { _edge_direction = 0; _edge_direction_acted_upon = 0; } // // // |
mzcs | 0:437bb8e2f8a7 | 188 | _shadow = 0; |
mzcs | 0:437bb8e2f8a7 | 189 | } |
mzcs | 0:437bb8e2f8a7 | 190 | } |
mzcs | 0:437bb8e2f8a7 | 191 | } |
mzcs | 0:437bb8e2f8a7 | 192 | |
mzcs | 0:437bb8e2f8a7 | 193 | DigitalIn *_digital_in; |
mzcs | 0:437bb8e2f8a7 | 194 | Ticker *_ticker; |
mzcs | 0:437bb8e2f8a7 | 195 | int8_t _shadow; |
mzcs | 0:437bb8e2f8a7 | 196 | uint32_t _debounce_us; // // // |
mzcs | 0:437bb8e2f8a7 | 197 | int8_t _edge_direction; // // // |
mzcs | 0:437bb8e2f8a7 | 198 | bool _edge_direction_acted_upon; // // // |
mzcs | 0:437bb8e2f8a7 | 199 | uint8_t _counter; |
mzcs | 0:437bb8e2f8a7 | 200 | uint8_t _samples; |
mzcs | 0:437bb8e2f8a7 | 201 | }; |
mzcs | 0:437bb8e2f8a7 | 202 | |
mzcs | 0:437bb8e2f8a7 | 203 | }; // namespace AjK ends. |
mzcs | 0:437bb8e2f8a7 | 204 | |
mzcs | 0:437bb8e2f8a7 | 205 | using namespace AjK; |
mzcs | 0:437bb8e2f8a7 | 206 | |
mzcs | 0:437bb8e2f8a7 | 207 | #endif |
mzcs | 0:437bb8e2f8a7 | 208 |