InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold. Updated callback procedures for mbed 5.xx by spotlightkid
Dependents: city1082-capsense-sw2-tft-leds CITY1082-TFT-basic_sw2_ctr 4180_final_project_github mRotaryEncoder_HelloWorld-os ... more
PinDetect.h@3:d552d2899765, 2019-10-17 (annotated)
- Committer:
- reedas
- Date:
- Thu Oct 17 15:52:19 2019 +0000
- Revision:
- 3:d552d2899765
- Parent:
- 2:cb3afc45028b
Updated to MBed 5 callback function set ups
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:4f4ccb203a70 | 1 | /* |
reedas | 3:d552d2899765 | 2 | Copyright (c) 2010 Andy Kirkham, 2018 Christopher Arndt |
AjK | 0:4f4ccb203a70 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy |
AjK | 0:4f4ccb203a70 | 4 | of this software and associated documentation files (the "Software"), to deal |
AjK | 0:4f4ccb203a70 | 5 | in the Software without restriction, including without limitation the rights |
AjK | 0:4f4ccb203a70 | 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AjK | 0:4f4ccb203a70 | 7 | copies of the Software, and to permit persons to whom the Software is |
AjK | 0:4f4ccb203a70 | 8 | furnished to do so, subject to the following conditions: |
AjK | 0:4f4ccb203a70 | 9 | The above copyright notice and this permission notice shall be included in |
AjK | 0:4f4ccb203a70 | 10 | all copies or substantial portions of the Software. |
AjK | 0:4f4ccb203a70 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AjK | 0:4f4ccb203a70 | 12 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AjK | 0:4f4ccb203a70 | 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AjK | 0:4f4ccb203a70 | 14 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AjK | 0:4f4ccb203a70 | 15 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AjK | 0:4f4ccb203a70 | 16 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
AjK | 0:4f4ccb203a70 | 17 | THE SOFTWARE. |
AjK | 0:4f4ccb203a70 | 18 | */ |
AjK | 0:4f4ccb203a70 | 19 | |
reedas | 3:d552d2899765 | 20 | #ifndef PIN_DETECT_H |
reedas | 3:d552d2899765 | 21 | #define PIN_DETECT_H |
AjK | 0:4f4ccb203a70 | 22 | |
AjK | 0:4f4ccb203a70 | 23 | #include "mbed.h" |
AjK | 0:4f4ccb203a70 | 24 | |
reedas | 3:d552d2899765 | 25 | #ifndef PINDETECT_PIN_ASSERTED |
reedas | 3:d552d2899765 | 26 | #define PINDETECT_PIN_ASSERTED 1 |
AjK | 0:4f4ccb203a70 | 27 | #endif |
AjK | 0:4f4ccb203a70 | 28 | |
AjK | 0:4f4ccb203a70 | 29 | #ifndef PINDETECT_SAMPLE_PERIOD |
AjK | 0:4f4ccb203a70 | 30 | #define PINDETECT_SAMPLE_PERIOD 20000 |
AjK | 0:4f4ccb203a70 | 31 | #endif |
AjK | 0:4f4ccb203a70 | 32 | |
reedas | 3:d552d2899765 | 33 | #ifndef PINDETECT_ASSERT_COUNT |
AjK | 0:4f4ccb203a70 | 34 | #define PINDETECT_ASSERT_COUNT 1 |
AjK | 0:4f4ccb203a70 | 35 | #endif |
AjK | 0:4f4ccb203a70 | 36 | |
AjK | 0:4f4ccb203a70 | 37 | #ifndef PINDETECT_HOLD_COUNT |
AjK | 0:4f4ccb203a70 | 38 | #define PINDETECT_HOLD_COUNT 50 |
AjK | 0:4f4ccb203a70 | 39 | #endif |
AjK | 0:4f4ccb203a70 | 40 | |
AjK | 0:4f4ccb203a70 | 41 | namespace AjK { |
AjK | 0:4f4ccb203a70 | 42 | |
AjK | 0:4f4ccb203a70 | 43 | /** PinDetect adds mechanical switch debouncing to DigitialIn and interrupt callbacks. |
AjK | 0:4f4ccb203a70 | 44 | * |
AjK | 0:4f4ccb203a70 | 45 | * This is done by sampling the specified pin at regular intervals and detecting any |
AjK | 0:4f4ccb203a70 | 46 | * change of state ( 0 -> 1 or 1 -> 0 ). When a state change is detected the attached |
AjK | 0:4f4ccb203a70 | 47 | * callback handler is called. Additionally, if the pin stays in the same state after |
AjK | 0:4f4ccb203a70 | 48 | * a state change for a defined period of time, an extra callback is made allowing a |
AjK | 0:4f4ccb203a70 | 49 | * program to detect when a "key is pressed and held down" rather than a momentary |
AjK | 0:4f4ccb203a70 | 50 | * key/switch press. |
AjK | 0:4f4ccb203a70 | 51 | * |
AjK | 0:4f4ccb203a70 | 52 | * All parameters are customisable which include:- |
AjK | 0:4f4ccb203a70 | 53 | * <ul> |
AjK | 0:4f4ccb203a70 | 54 | * <li> The sampling frequency. </li> |
reedas | 3:d552d2899765 | 55 | * <li> The number of continuous samples until a state change is detected. </li> |
AjK | 0:4f4ccb203a70 | 56 | * <li> The number of continuous samples until a key is assumed held after a state change. </li> |
AjK | 0:4f4ccb203a70 | 57 | * <li> The logic level which is assumed to be asserted (0volts or +volts). </li> |
AjK | 0:4f4ccb203a70 | 58 | * </ul> |
AjK | 0:4f4ccb203a70 | 59 | * |
AjK | 0:4f4ccb203a70 | 60 | * Only callbacks that have been attached will be called by the library. |
AjK | 0:4f4ccb203a70 | 61 | * |
AjK | 0:4f4ccb203a70 | 62 | * Example: |
AjK | 0:4f4ccb203a70 | 63 | * @code |
AjK | 0:4f4ccb203a70 | 64 | * #include "mbed.h" |
AjK | 0:4f4ccb203a70 | 65 | * #include "PinDetect.h" |
AjK | 0:4f4ccb203a70 | 66 | * |
reedas | 3:d552d2899765 | 67 | * PinDetect pin(p30); |
reedas | 3:d552d2899765 | 68 | * DigitialOut led1(LED1); |
reedas | 3:d552d2899765 | 69 | * DigitialOut led2(LED2); |
reedas | 3:d552d2899765 | 70 | * DigitialOut led3(LED3); |
reedas | 3:d552d2899765 | 71 | * DigitialOut led4(LED4); |
AjK | 0:4f4ccb203a70 | 72 | * |
reedas | 3:d552d2899765 | 73 | * void keyPressed(void) { |
AjK | 0:4f4ccb203a70 | 74 | * led2 = 1; |
AjK | 0:4f4ccb203a70 | 75 | * led3 = 0; |
AjK | 0:4f4ccb203a70 | 76 | * led4 = 0; |
AjK | 0:4f4ccb203a70 | 77 | * } |
AjK | 0:4f4ccb203a70 | 78 | * |
reedas | 3:d552d2899765 | 79 | * void keyReleased(void) { |
AjK | 0:4f4ccb203a70 | 80 | * led2 = 0; |
AjK | 0:4f4ccb203a70 | 81 | * led3 = 0; |
AjK | 0:4f4ccb203a70 | 82 | * led4 = 0; |
AjK | 0:4f4ccb203a70 | 83 | * } |
AjK | 0:4f4ccb203a70 | 84 | * |
reedas | 3:d552d2899765 | 85 | * void keyPressedHeld(void) { |
AjK | 0:4f4ccb203a70 | 86 | * led3 = 1; |
AjK | 0:4f4ccb203a70 | 87 | * } |
AjK | 0:4f4ccb203a70 | 88 | * |
reedas | 3:d552d2899765 | 89 | * void keyReleasedHeld(void) { |
AjK | 0:4f4ccb203a70 | 90 | * led4 = 1; |
AjK | 0:4f4ccb203a70 | 91 | * } |
AjK | 0:4f4ccb203a70 | 92 | * |
AjK | 0:4f4ccb203a70 | 93 | * int main() { |
reedas | 3:d552d2899765 | 94 | * pin.mode(PullDown); |
reedas | 3:d552d2899765 | 95 | * pin.attach_asserted(keyPressed); |
reedas | 3:d552d2899765 | 96 | * pin.attach_deasserted(keyReleased); |
reedas | 3:d552d2899765 | 97 | * pin.attach_asserted_held(keyPressedHeld); |
reedas | 3:d552d2899765 | 98 | * pin.attach_deasserted_held(keyReleasedHeld); |
AjK | 0:4f4ccb203a70 | 99 | * |
AjK | 0:4f4ccb203a70 | 100 | * // Sampling does not begin until you set a frequency. |
AjK | 0:4f4ccb203a70 | 101 | * // The default is 20ms. If you want a different frequency |
AjK | 0:4f4ccb203a70 | 102 | * // then pass the period in microseconds for example, for 10ms :- |
reedas | 3:d552d2899765 | 103 | * // pin.setSampleFrequency(10000); |
AjK | 0:4f4ccb203a70 | 104 | * // |
AjK | 0:4f4ccb203a70 | 105 | * pin.setSampleFrequency(); // Defaults to 20ms. |
AjK | 0:4f4ccb203a70 | 106 | * |
reedas | 3:d552d2899765 | 107 | * while (1) { |
AjK | 0:4f4ccb203a70 | 108 | * led1 = !led1; |
reedas | 3:d552d2899765 | 109 | * wait(0.2); |
AjK | 0:4f4ccb203a70 | 110 | * } |
AjK | 0:4f4ccb203a70 | 111 | * } |
AjK | 0:4f4ccb203a70 | 112 | * @endcode |
AjK | 0:4f4ccb203a70 | 113 | * |
AjK | 0:4f4ccb203a70 | 114 | * This example will flash led1 in a similar to a standard starting program. |
AjK | 0:4f4ccb203a70 | 115 | * |
AjK | 0:4f4ccb203a70 | 116 | * Applying a "1" (switch on) to pin 30 will switch on led2, removing the "1" to "0" |
AjK | 0:4f4ccb203a70 | 117 | * (switch off) led2 goes out. Holding the "switch" at one for one second will switch |
AjK | 0:4f4ccb203a70 | 118 | * on led3. An unasserted P30 (switched off) will, after one second illuminate led4 |
AjK | 0:4f4ccb203a70 | 119 | * when the deasserted calledback is called. |
AjK | 0:4f4ccb203a70 | 120 | * |
AjK | 0:4f4ccb203a70 | 121 | * The above is a very basic introduction. For more details:- |
AjK | 0:4f4ccb203a70 | 122 | * @see example.h |
AjK | 0:4f4ccb203a70 | 123 | */ |
AjK | 0:4f4ccb203a70 | 124 | class PinDetect { |
AjK | 1:611a8f5ac65c | 125 | |
AjK | 1:611a8f5ac65c | 126 | protected: |
AjK | 1:611a8f5ac65c | 127 | DigitalIn *_in; |
AjK | 1:611a8f5ac65c | 128 | Ticker *_ticker; |
AjK | 1:611a8f5ac65c | 129 | int _prevState; |
AjK | 1:611a8f5ac65c | 130 | int _sampleTime; |
AjK | 1:611a8f5ac65c | 131 | int _assertValue; |
AjK | 1:611a8f5ac65c | 132 | int _samplesTillAssertReload; |
AjK | 1:611a8f5ac65c | 133 | int _samplesTillAssert; |
AjK | 1:611a8f5ac65c | 134 | int _samplesTillHeldReload; |
AjK | 1:611a8f5ac65c | 135 | int _samplesTillHeld; |
reedas | 3:d552d2899765 | 136 | Callback<void()> _callbackAsserted; |
reedas | 3:d552d2899765 | 137 | Callback<void()> _callbackDeasserted; |
reedas | 3:d552d2899765 | 138 | Callback<void()> _callbackAssertedHeld; |
reedas | 3:d552d2899765 | 139 | Callback<void()> _callbackDeassertedHeld; |
reedas | 3:d552d2899765 | 140 | |
AjK | 1:611a8f5ac65c | 141 | /** initialise class |
AjK | 1:611a8f5ac65c | 142 | * |
AjK | 1:611a8f5ac65c | 143 | * @param PinName p is a valid pin that supports DigitalIn |
AjK | 1:611a8f5ac65c | 144 | * @param PinMode m The mode the DigitalIn should use. |
AjK | 1:611a8f5ac65c | 145 | */ |
AjK | 1:611a8f5ac65c | 146 | void init(PinName p, PinMode m) { |
AjK | 1:611a8f5ac65c | 147 | _sampleTime = PINDETECT_SAMPLE_PERIOD; |
AjK | 1:611a8f5ac65c | 148 | _samplesTillAssert = PINDETECT_ASSERT_COUNT; |
AjK | 1:611a8f5ac65c | 149 | _samplesTillHeld = 0; |
AjK | 1:611a8f5ac65c | 150 | _samplesTillAssertReload = PINDETECT_ASSERT_COUNT; |
AjK | 1:611a8f5ac65c | 151 | _samplesTillHeldReload = PINDETECT_HOLD_COUNT; |
reedas | 3:d552d2899765 | 152 | _assertValue = PINDETECT_PIN_ASSERTED; |
reedas | 3:d552d2899765 | 153 | |
reedas | 3:d552d2899765 | 154 | _in = new DigitalIn(p); |
reedas | 3:d552d2899765 | 155 | _in->mode(m); |
reedas | 3:d552d2899765 | 156 | _prevState = _in->read(); |
AjK | 1:611a8f5ac65c | 157 | _ticker = new Ticker; |
AjK | 1:611a8f5ac65c | 158 | } |
reedas | 3:d552d2899765 | 159 | |
AjK | 0:4f4ccb203a70 | 160 | public: |
reedas | 3:d552d2899765 | 161 | friend class Ticker; |
AjK | 0:4f4ccb203a70 | 162 | |
reedas | 3:d552d2899765 | 163 | PinDetect() { |
reedas | 3:d552d2899765 | 164 | error("You must supply a PinName"); |
reedas | 3:d552d2899765 | 165 | } |
AjK | 0:4f4ccb203a70 | 166 | |
AjK | 0:4f4ccb203a70 | 167 | /** PinDetect constructor |
AjK | 0:4f4ccb203a70 | 168 | * |
AjK | 1:611a8f5ac65c | 169 | * By default the PinMode is set to PullDown. |
AjK | 1:611a8f5ac65c | 170 | * |
AjK | 0:4f4ccb203a70 | 171 | * @see http://mbed.org/handbook/DigitalIn |
AjK | 0:4f4ccb203a70 | 172 | * @param p PinName is a valid pin that supports DigitalIn |
reedas | 3:d552d2899765 | 173 | */ |
AjK | 0:4f4ccb203a70 | 174 | PinDetect(PinName p) { |
reedas | 3:d552d2899765 | 175 | init(p, PullDown); |
AjK | 1:611a8f5ac65c | 176 | } |
AjK | 1:611a8f5ac65c | 177 | |
AjK | 1:611a8f5ac65c | 178 | /** PinDetect constructor |
AjK | 1:611a8f5ac65c | 179 | * |
AjK | 1:611a8f5ac65c | 180 | * @see http://mbed.org/handbook/DigitalIn |
AjK | 1:611a8f5ac65c | 181 | * @param PinName p is a valid pin that supports DigitalIn |
AjK | 1:611a8f5ac65c | 182 | * @param PinMode m The mode the DigitalIn should use. |
reedas | 3:d552d2899765 | 183 | */ |
AjK | 1:611a8f5ac65c | 184 | PinDetect(PinName p, PinMode m) { |
reedas | 3:d552d2899765 | 185 | init(p, m); |
AjK | 0:4f4ccb203a70 | 186 | } |
reedas | 3:d552d2899765 | 187 | |
AjK | 0:4f4ccb203a70 | 188 | /** PinDetect destructor |
reedas | 3:d552d2899765 | 189 | */ |
AjK | 0:4f4ccb203a70 | 190 | ~PinDetect() { |
reedas | 3:d552d2899765 | 191 | if (_ticker) delete(_ticker); |
reedas | 3:d552d2899765 | 192 | if (_in) delete(_in); |
AjK | 0:4f4ccb203a70 | 193 | } |
reedas | 3:d552d2899765 | 194 | |
AjK | 0:4f4ccb203a70 | 195 | /** Set the sampling time in microseconds. |
AjK | 0:4f4ccb203a70 | 196 | * |
AjK | 0:4f4ccb203a70 | 197 | * @param int The time between pin samples in microseconds. |
AjK | 0:4f4ccb203a70 | 198 | */ |
reedas | 3:d552d2899765 | 199 | void setSampleFrequency(int i=PINDETECT_SAMPLE_PERIOD) { |
reedas | 3:d552d2899765 | 200 | _sampleTime = i; |
reedas | 3:d552d2899765 | 201 | _prevState = _in->read(); |
reedas | 3:d552d2899765 | 202 | _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime); |
AjK | 0:4f4ccb203a70 | 203 | } |
reedas | 3:d552d2899765 | 204 | |
AjK | 0:4f4ccb203a70 | 205 | /** Set the value used as assert. |
AjK | 0:4f4ccb203a70 | 206 | * |
reedas | 3:d552d2899765 | 207 | * Defaults to 1 (i.e. if pin == 1 then pin asserted). |
AjK | 0:4f4ccb203a70 | 208 | * |
AjK | 0:4f4ccb203a70 | 209 | * @param int New assert value (1 or 0) |
AjK | 0:4f4ccb203a70 | 210 | */ |
reedas | 3:d552d2899765 | 211 | void setAssertValue(int i=PINDETECT_PIN_ASSERTED) { |
reedas | 3:d552d2899765 | 212 | _assertValue = i & 1; |
reedas | 3:d552d2899765 | 213 | } |
reedas | 3:d552d2899765 | 214 | |
AjK | 0:4f4ccb203a70 | 215 | /** Set the number of continuous samples until assert assumed. |
AjK | 0:4f4ccb203a70 | 216 | * |
AjK | 0:4f4ccb203a70 | 217 | * Defaults to 1 (1 * sample frequency). |
AjK | 0:4f4ccb203a70 | 218 | * |
AjK | 0:4f4ccb203a70 | 219 | * @param int The number of continuous samples until assert assumed. |
reedas | 3:d552d2899765 | 220 | */ |
reedas | 3:d552d2899765 | 221 | void setSamplesTillAssert(int i) { |
reedas | 3:d552d2899765 | 222 | _samplesTillAssertReload = i; |
reedas | 3:d552d2899765 | 223 | } |
reedas | 3:d552d2899765 | 224 | |
AjK | 0:4f4ccb203a70 | 225 | /** Set the number of continuous samples until held assumed. |
AjK | 0:4f4ccb203a70 | 226 | * |
AjK | 0:4f4ccb203a70 | 227 | * Defaults to 50 * sample frequency. |
AjK | 0:4f4ccb203a70 | 228 | * |
AjK | 0:4f4ccb203a70 | 229 | * @param int The number of continuous samples until held assumed. |
reedas | 3:d552d2899765 | 230 | */ |
reedas | 3:d552d2899765 | 231 | void setSamplesTillHeld(int i) { |
reedas | 3:d552d2899765 | 232 | _samplesTillHeldReload = i; |
reedas | 3:d552d2899765 | 233 | } |
reedas | 3:d552d2899765 | 234 | |
AjK | 0:4f4ccb203a70 | 235 | /** Set the pin mode. |
AjK | 0:4f4ccb203a70 | 236 | * |
AjK | 0:4f4ccb203a70 | 237 | * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode |
AjK | 0:4f4ccb203a70 | 238 | * @param PinMode m The mode to pass on to the DigitalIn |
AjK | 0:4f4ccb203a70 | 239 | */ |
reedas | 3:d552d2899765 | 240 | void mode(PinMode m) { |
reedas | 3:d552d2899765 | 241 | _in->mode(m); |
reedas | 3:d552d2899765 | 242 | } |
reedas | 3:d552d2899765 | 243 | |
reedas | 3:d552d2899765 | 244 | /** Attach a callback function |
AjK | 0:4f4ccb203a70 | 245 | * |
AjK | 0:4f4ccb203a70 | 246 | * @code |
AjK | 0:4f4ccb203a70 | 247 | * |
reedas | 3:d552d2899765 | 248 | * DigitalOut led1(LED1); |
reedas | 3:d552d2899765 | 249 | * PinDetect pin(p30); |
AjK | 0:4f4ccb203a70 | 250 | * |
reedas | 3:d552d2899765 | 251 | * void myCallback(void) { |
AjK | 0:4f4ccb203a70 | 252 | * led1 = 1; |
AjK | 0:4f4ccb203a70 | 253 | * }; |
reedas | 3:d552d2899765 | 254 | * |
AjK | 0:4f4ccb203a70 | 255 | * main() { |
reedas | 3:d552d2899765 | 256 | * pin.attach_asserted(myCallback); |
AjK | 0:4f4ccb203a70 | 257 | * } |
AjK | 0:4f4ccb203a70 | 258 | * |
AjK | 0:4f4ccb203a70 | 259 | * @endcode |
AjK | 0:4f4ccb203a70 | 260 | * |
AjK | 0:4f4ccb203a70 | 261 | * Call this function when a pin is asserted. |
AjK | 0:4f4ccb203a70 | 262 | * @param function A C function pointer |
AjK | 0:4f4ccb203a70 | 263 | */ |
reedas | 3:d552d2899765 | 264 | void attach_asserted(Callback<void()> cb) { |
reedas | 3:d552d2899765 | 265 | _callbackAsserted = cb; |
AjK | 0:4f4ccb203a70 | 266 | } |
reedas | 3:d552d2899765 | 267 | |
reedas | 3:d552d2899765 | 268 | /** Attach a callback function |
AjK | 0:4f4ccb203a70 | 269 | * |
AjK | 0:4f4ccb203a70 | 270 | * @code |
AjK | 0:4f4ccb203a70 | 271 | * |
reedas | 3:d552d2899765 | 272 | * DigitalOut led1(LED1); |
reedas | 3:d552d2899765 | 273 | * PinDetect pin(p30); |
AjK | 0:4f4ccb203a70 | 274 | * |
reedas | 3:d552d2899765 | 275 | * void myCallback(void) { |
AjK | 0:4f4ccb203a70 | 276 | * led1 = 0; |
AjK | 0:4f4ccb203a70 | 277 | * }; |
AjK | 0:4f4ccb203a70 | 278 | * |
AjK | 0:4f4ccb203a70 | 279 | * main() { |
reedas | 3:d552d2899765 | 280 | * pin.attach_deasserted(myCallback); |
AjK | 0:4f4ccb203a70 | 281 | * } |
AjK | 0:4f4ccb203a70 | 282 | * |
AjK | 0:4f4ccb203a70 | 283 | * @endcode |
AjK | 0:4f4ccb203a70 | 284 | * |
AjK | 0:4f4ccb203a70 | 285 | * Call this function when a pin is deasserted. |
AjK | 0:4f4ccb203a70 | 286 | * @param function A C function pointer |
AjK | 0:4f4ccb203a70 | 287 | */ |
reedas | 3:d552d2899765 | 288 | void attach_deasserted(Callback<void()> cb) { |
reedas | 3:d552d2899765 | 289 | _callbackDeasserted = cb; |
AjK | 0:4f4ccb203a70 | 290 | } |
reedas | 3:d552d2899765 | 291 | |
reedas | 3:d552d2899765 | 292 | /** Attach a callback function |
AjK | 0:4f4ccb203a70 | 293 | * |
AjK | 0:4f4ccb203a70 | 294 | * @code |
AjK | 0:4f4ccb203a70 | 295 | * |
reedas | 3:d552d2899765 | 296 | * DigitalOut led2(LED2); |
reedas | 3:d552d2899765 | 297 | * PinDetect pin(p30); |
AjK | 0:4f4ccb203a70 | 298 | * |
reedas | 3:d552d2899765 | 299 | * void myCallback(void) { |
AjK | 0:4f4ccb203a70 | 300 | * led2 = 1; |
AjK | 0:4f4ccb203a70 | 301 | * }; |
AjK | 0:4f4ccb203a70 | 302 | * |
AjK | 0:4f4ccb203a70 | 303 | * main() { |
reedas | 3:d552d2899765 | 304 | * pin.attach_asserted_held(myCallback); |
AjK | 0:4f4ccb203a70 | 305 | * } |
AjK | 0:4f4ccb203a70 | 306 | * |
AjK | 0:4f4ccb203a70 | 307 | * @endcode |
AjK | 0:4f4ccb203a70 | 308 | * |
AjK | 0:4f4ccb203a70 | 309 | * Call this function when a pin is asserted and held. |
AjK | 0:4f4ccb203a70 | 310 | * @param function A C function pointer |
AjK | 0:4f4ccb203a70 | 311 | */ |
reedas | 3:d552d2899765 | 312 | void attach_asserted_held(Callback<void()> cb) { |
reedas | 3:d552d2899765 | 313 | _callbackAssertedHeld = cb; |
AjK | 0:4f4ccb203a70 | 314 | } |
reedas | 3:d552d2899765 | 315 | |
reedas | 3:d552d2899765 | 316 | /** Attach a callback function |
AjK | 0:4f4ccb203a70 | 317 | * |
AjK | 0:4f4ccb203a70 | 318 | * @code |
AjK | 0:4f4ccb203a70 | 319 | * |
reedas | 3:d552d2899765 | 320 | * DigitalOut led3(LED3); |
reedas | 3:d552d2899765 | 321 | * PinDetect pin(p30); |
AjK | 0:4f4ccb203a70 | 322 | * |
reedas | 3:d552d2899765 | 323 | * void myCallback(void) { |
AjK | 0:4f4ccb203a70 | 324 | * led3 = 1; |
AjK | 0:4f4ccb203a70 | 325 | * }; |
AjK | 0:4f4ccb203a70 | 326 | * |
AjK | 0:4f4ccb203a70 | 327 | * main() { |
reedas | 3:d552d2899765 | 328 | * pin.attach_deasserted_held(myCallback); |
AjK | 0:4f4ccb203a70 | 329 | * } |
AjK | 0:4f4ccb203a70 | 330 | * |
AjK | 0:4f4ccb203a70 | 331 | * @endcode |
AjK | 0:4f4ccb203a70 | 332 | * |
AjK | 0:4f4ccb203a70 | 333 | * Call this function when a pin is deasserted and held. |
AjK | 0:4f4ccb203a70 | 334 | * @param function A C function pointer |
AjK | 0:4f4ccb203a70 | 335 | */ |
reedas | 3:d552d2899765 | 336 | void attach_deasserted_held(Callback<void()> cb) { |
reedas | 3:d552d2899765 | 337 | _callbackDeassertedHeld = cb; |
AjK | 0:4f4ccb203a70 | 338 | } |
reedas | 3:d552d2899765 | 339 | |
AjK | 0:4f4ccb203a70 | 340 | /** operator int() |
AjK | 0:4f4ccb203a70 | 341 | * |
AjK | 0:4f4ccb203a70 | 342 | * Read the value of the pin being sampled. |
AjK | 0:4f4ccb203a70 | 343 | */ |
reedas | 3:d552d2899765 | 344 | operator int() { |
reedas | 3:d552d2899765 | 345 | return _in->read(); |
reedas | 3:d552d2899765 | 346 | } |
AjK | 2:cb3afc45028b | 347 | |
reedas | 3:d552d2899765 | 348 | protected: |
AjK | 0:4f4ccb203a70 | 349 | /** The Ticker periodic callback function |
AjK | 0:4f4ccb203a70 | 350 | */ |
AjK | 0:4f4ccb203a70 | 351 | void isr(void) { |
AjK | 0:4f4ccb203a70 | 352 | int currentState = _in->read(); |
reedas | 3:d552d2899765 | 353 | |
reedas | 3:d552d2899765 | 354 | if (currentState != _prevState) { |
reedas | 3:d552d2899765 | 355 | if (_samplesTillAssert == 0) { |
AjK | 0:4f4ccb203a70 | 356 | _prevState = currentState; |
AjK | 0:4f4ccb203a70 | 357 | _samplesTillHeld = _samplesTillHeldReload; |
reedas | 3:d552d2899765 | 358 | if (currentState == _assertValue) { |
reedas | 3:d552d2899765 | 359 | if (_callbackAsserted) |
reedas | 3:d552d2899765 | 360 | _callbackAsserted(); |
reedas | 3:d552d2899765 | 361 | } else if (_callbackDeasserted) |
reedas | 3:d552d2899765 | 362 | _callbackDeasserted(); |
AjK | 0:4f4ccb203a70 | 363 | } |
AjK | 0:4f4ccb203a70 | 364 | else { |
AjK | 0:4f4ccb203a70 | 365 | _samplesTillAssert--; |
AjK | 0:4f4ccb203a70 | 366 | } |
AjK | 0:4f4ccb203a70 | 367 | } |
AjK | 0:4f4ccb203a70 | 368 | else { |
AjK | 0:4f4ccb203a70 | 369 | _samplesTillAssert = _samplesTillAssertReload; |
AjK | 0:4f4ccb203a70 | 370 | } |
reedas | 3:d552d2899765 | 371 | |
reedas | 3:d552d2899765 | 372 | if (_samplesTillHeld) { |
reedas | 3:d552d2899765 | 373 | if (_prevState == currentState) { |
AjK | 0:4f4ccb203a70 | 374 | _samplesTillHeld--; |
reedas | 3:d552d2899765 | 375 | if (_samplesTillHeld == 0) { |
reedas | 3:d552d2899765 | 376 | if (currentState == _assertValue) { |
reedas | 3:d552d2899765 | 377 | if (_callbackAssertedHeld) |
reedas | 3:d552d2899765 | 378 | _callbackAssertedHeld(); |
reedas | 3:d552d2899765 | 379 | } else if (_callbackDeassertedHeld) |
reedas | 3:d552d2899765 | 380 | _callbackDeassertedHeld(); |
AjK | 0:4f4ccb203a70 | 381 | } |
AjK | 0:4f4ccb203a70 | 382 | } |
AjK | 0:4f4ccb203a70 | 383 | else { |
AjK | 0:4f4ccb203a70 | 384 | _samplesTillHeld = 0; |
AjK | 0:4f4ccb203a70 | 385 | } |
AjK | 0:4f4ccb203a70 | 386 | } |
AjK | 0:4f4ccb203a70 | 387 | } |
reedas | 3:d552d2899765 | 388 | |
AjK | 0:4f4ccb203a70 | 389 | }; |
AjK | 0:4f4ccb203a70 | 390 | |
AjK | 0:4f4ccb203a70 | 391 | }; // namespace AjK ends. |
AjK | 0:4f4ccb203a70 | 392 | |
AjK | 0:4f4ccb203a70 | 393 | using namespace AjK; |
AjK | 0:4f4ccb203a70 | 394 | |
reedas | 3:d552d2899765 | 395 | #endif |