Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of PinDetect by
PinDetect.cpp@5:2b0afd293d1d, 2018-06-28 (annotated)
- Committer:
- pilotak
- Date:
- Thu Jun 28 14:02:22 2018 +0200
- Revision:
- 5:2b0afd293d1d
- Parent:
- 4:bacc386fe2ad
Callback fix for Mbed OS 5.9
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pilotak | 4:bacc386fe2ad | 1 | /* |
| pilotak | 4:bacc386fe2ad | 2 | Copyright (c) 2010 Andy Kirkham |
| pilotak | 5:2b0afd293d1d | 3 | |
| pilotak | 4:bacc386fe2ad | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| pilotak | 4:bacc386fe2ad | 5 | of this software and associated documentation files (the "Software"), to deal |
| pilotak | 4:bacc386fe2ad | 6 | in the Software without restriction, including without limitation the rights |
| pilotak | 4:bacc386fe2ad | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| pilotak | 4:bacc386fe2ad | 8 | copies of the Software, and to permit persons to whom the Software is |
| pilotak | 4:bacc386fe2ad | 9 | furnished to do so, subject to the following conditions: |
| pilotak | 5:2b0afd293d1d | 10 | |
| pilotak | 4:bacc386fe2ad | 11 | The above copyright notice and this permission notice shall be included in |
| pilotak | 4:bacc386fe2ad | 12 | all copies or substantial portions of the Software. |
| pilotak | 5:2b0afd293d1d | 13 | |
| pilotak | 4:bacc386fe2ad | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| pilotak | 4:bacc386fe2ad | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| pilotak | 4:bacc386fe2ad | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| pilotak | 4:bacc386fe2ad | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| pilotak | 4:bacc386fe2ad | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| pilotak | 4:bacc386fe2ad | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| pilotak | 4:bacc386fe2ad | 20 | THE SOFTWARE. |
| pilotak | 4:bacc386fe2ad | 21 | */ |
| pilotak | 4:bacc386fe2ad | 22 | |
| pilotak | 4:bacc386fe2ad | 23 | #include "PinDetect.h" |
| pilotak | 4:bacc386fe2ad | 24 | |
| pilotak | 4:bacc386fe2ad | 25 | namespace AjK { |
| pilotak | 4:bacc386fe2ad | 26 | |
| pilotak | 5:2b0afd293d1d | 27 | PinDetect::PinDetect(PinName p) { |
| pilotak | 5:2b0afd293d1d | 28 | init(p, PullDefault); |
| pilotak | 4:bacc386fe2ad | 29 | } |
| pilotak | 4:bacc386fe2ad | 30 | |
| pilotak | 5:2b0afd293d1d | 31 | PinDetect::PinDetect(PinName p, PinMode m) { |
| pilotak | 5:2b0afd293d1d | 32 | init(p, m); |
| pilotak | 4:bacc386fe2ad | 33 | } |
| pilotak | 4:bacc386fe2ad | 34 | |
| pilotak | 4:bacc386fe2ad | 35 | |
| pilotak | 5:2b0afd293d1d | 36 | PinDetect::~PinDetect(void) { |
| pilotak | 4:bacc386fe2ad | 37 | if ( _ticker ) delete( _ticker ); |
| pilotak | 5:2b0afd293d1d | 38 | |
| pilotak | 4:bacc386fe2ad | 39 | if ( _in ) delete( _in ); |
| pilotak | 4:bacc386fe2ad | 40 | } |
| pilotak | 4:bacc386fe2ad | 41 | |
| pilotak | 4:bacc386fe2ad | 42 | void PinDetect::init(PinName p, PinMode m) { |
| pilotak | 4:bacc386fe2ad | 43 | _sampleTime = PINDETECT_SAMPLE_PERIOD; |
| pilotak | 4:bacc386fe2ad | 44 | _samplesTillAssert = PINDETECT_ASSERT_COUNT; |
| pilotak | 4:bacc386fe2ad | 45 | _samplesTillHeld = 0; |
| pilotak | 4:bacc386fe2ad | 46 | _samplesTillAssertReload = PINDETECT_ASSERT_COUNT; |
| pilotak | 4:bacc386fe2ad | 47 | _samplesTillHeldReload = PINDETECT_HOLD_COUNT; |
| pilotak | 5:2b0afd293d1d | 48 | _assertValue = PINDETECT_PIN_ASSERTED; |
| pilotak | 5:2b0afd293d1d | 49 | |
| pilotak | 5:2b0afd293d1d | 50 | _in = new DigitalIn(p); |
| pilotak | 5:2b0afd293d1d | 51 | _in->mode(m); |
| pilotak | 5:2b0afd293d1d | 52 | _prevState = _in->read(); |
| pilotak | 4:bacc386fe2ad | 53 | _ticker = new Ticker; |
| pilotak | 5:2b0afd293d1d | 54 | |
| pilotak | 5:2b0afd293d1d | 55 | _callbackAsserted = NULL; |
| pilotak | 5:2b0afd293d1d | 56 | _callbackDeasserted = NULL; |
| pilotak | 5:2b0afd293d1d | 57 | _callbackAssertedHeld = NULL; |
| pilotak | 5:2b0afd293d1d | 58 | _callbackDeassertedHeld = NULL; |
| pilotak | 4:bacc386fe2ad | 59 | } |
| pilotak | 4:bacc386fe2ad | 60 | |
| pilotak | 5:2b0afd293d1d | 61 | void PinDetect::setSampleFrequency(int i) { |
| pilotak | 5:2b0afd293d1d | 62 | _sampleTime = i; |
| pilotak | 5:2b0afd293d1d | 63 | _prevState = _in->read(); |
| pilotak | 5:2b0afd293d1d | 64 | _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime); |
| pilotak | 4:bacc386fe2ad | 65 | } |
| pilotak | 4:bacc386fe2ad | 66 | |
| pilotak | 5:2b0afd293d1d | 67 | void PinDetect::setAssertValue(int i) { |
| pilotak | 4:bacc386fe2ad | 68 | _assertValue = i & 1; |
| pilotak | 4:bacc386fe2ad | 69 | } |
| pilotak | 4:bacc386fe2ad | 70 | |
| pilotak | 4:bacc386fe2ad | 71 | void PinDetect::setSamplesTillAssert(int i) { |
| pilotak | 4:bacc386fe2ad | 72 | _samplesTillAssertReload = i; |
| pilotak | 4:bacc386fe2ad | 73 | } |
| pilotak | 4:bacc386fe2ad | 74 | |
| pilotak | 4:bacc386fe2ad | 75 | void PinDetect::setSamplesTillHeld(int i) { |
| pilotak | 4:bacc386fe2ad | 76 | _samplesTillHeldReload = i; |
| pilotak | 4:bacc386fe2ad | 77 | } |
| pilotak | 4:bacc386fe2ad | 78 | |
| pilotak | 4:bacc386fe2ad | 79 | void PinDetect::mode(PinMode m) { |
| pilotak | 5:2b0afd293d1d | 80 | _in->mode(m); |
| pilotak | 4:bacc386fe2ad | 81 | } |
| pilotak | 4:bacc386fe2ad | 82 | |
| pilotak | 4:bacc386fe2ad | 83 | void PinDetect::attach_asserted(Callback<void()> function) { |
| pilotak | 4:bacc386fe2ad | 84 | core_util_critical_section_enter(); |
| pilotak | 5:2b0afd293d1d | 85 | _callbackAsserted = function; |
| pilotak | 4:bacc386fe2ad | 86 | core_util_critical_section_exit(); |
| pilotak | 4:bacc386fe2ad | 87 | } |
| pilotak | 4:bacc386fe2ad | 88 | |
| pilotak | 4:bacc386fe2ad | 89 | void PinDetect::attach_deasserted(Callback<void()> function) { |
| pilotak | 4:bacc386fe2ad | 90 | core_util_critical_section_enter(); |
| pilotak | 5:2b0afd293d1d | 91 | _callbackDeasserted = function; |
| pilotak | 4:bacc386fe2ad | 92 | core_util_critical_section_exit(); |
| pilotak | 4:bacc386fe2ad | 93 | } |
| pilotak | 4:bacc386fe2ad | 94 | |
| pilotak | 4:bacc386fe2ad | 95 | void PinDetect::attach_asserted_held(Callback<void()> function) { |
| pilotak | 4:bacc386fe2ad | 96 | core_util_critical_section_enter(); |
| pilotak | 5:2b0afd293d1d | 97 | _callbackAssertedHeld = function; |
| pilotak | 4:bacc386fe2ad | 98 | core_util_critical_section_exit(); |
| pilotak | 4:bacc386fe2ad | 99 | } |
| pilotak | 4:bacc386fe2ad | 100 | |
| pilotak | 4:bacc386fe2ad | 101 | void PinDetect::attach_deasserted_held(Callback<void()> function) { |
| pilotak | 4:bacc386fe2ad | 102 | core_util_critical_section_enter(); |
| pilotak | 5:2b0afd293d1d | 103 | _callbackDeassertedHeld = function; |
| pilotak | 4:bacc386fe2ad | 104 | core_util_critical_section_exit(); |
| pilotak | 4:bacc386fe2ad | 105 | } |
| pilotak | 4:bacc386fe2ad | 106 | |
| pilotak | 4:bacc386fe2ad | 107 | void PinDetect::isr(void) { |
| pilotak | 4:bacc386fe2ad | 108 | int currentState = _in->read(); |
| pilotak | 4:bacc386fe2ad | 109 | |
| pilotak | 4:bacc386fe2ad | 110 | if ( currentState != _prevState ) { |
| pilotak | 4:bacc386fe2ad | 111 | if ( _samplesTillAssert == 0 ) { |
| pilotak | 4:bacc386fe2ad | 112 | _prevState = currentState; |
| pilotak | 4:bacc386fe2ad | 113 | _samplesTillHeld = _samplesTillHeldReload; |
| pilotak | 5:2b0afd293d1d | 114 | |
| pilotak | 5:2b0afd293d1d | 115 | if ( currentState == _assertValue ) { |
| pilotak | 5:2b0afd293d1d | 116 | if (_callbackAsserted) _callbackAsserted.call(); |
| pilotak | 5:2b0afd293d1d | 117 | |
| pilotak | 5:2b0afd293d1d | 118 | } else { |
| pilotak | 5:2b0afd293d1d | 119 | if (_callbackDeasserted) _callbackDeasserted.call(); |
| pilotak | 5:2b0afd293d1d | 120 | } |
| pilotak | 5:2b0afd293d1d | 121 | |
| pilotak | 5:2b0afd293d1d | 122 | } else { |
| pilotak | 4:bacc386fe2ad | 123 | _samplesTillAssert--; |
| pilotak | 4:bacc386fe2ad | 124 | } |
| pilotak | 5:2b0afd293d1d | 125 | |
| pilotak | 5:2b0afd293d1d | 126 | } else { |
| pilotak | 4:bacc386fe2ad | 127 | _samplesTillAssert = _samplesTillAssertReload; |
| pilotak | 4:bacc386fe2ad | 128 | } |
| pilotak | 5:2b0afd293d1d | 129 | |
| pilotak | 4:bacc386fe2ad | 130 | if ( _samplesTillHeld ) { |
| pilotak | 4:bacc386fe2ad | 131 | if ( _prevState == currentState ) { |
| pilotak | 4:bacc386fe2ad | 132 | _samplesTillHeld--; |
| pilotak | 5:2b0afd293d1d | 133 | |
| pilotak | 4:bacc386fe2ad | 134 | if ( _samplesTillHeld == 0 ) { |
| pilotak | 5:2b0afd293d1d | 135 | if ( currentState == _assertValue ) { |
| pilotak | 5:2b0afd293d1d | 136 | if (_callbackAssertedHeld) _callbackAssertedHeld.call(); |
| pilotak | 5:2b0afd293d1d | 137 | |
| pilotak | 5:2b0afd293d1d | 138 | } else { |
| pilotak | 5:2b0afd293d1d | 139 | if (_callbackDeassertedHeld) _callbackDeassertedHeld.call(); |
| pilotak | 5:2b0afd293d1d | 140 | } |
| pilotak | 4:bacc386fe2ad | 141 | } |
| pilotak | 5:2b0afd293d1d | 142 | |
| pilotak | 5:2b0afd293d1d | 143 | } else { |
| pilotak | 4:bacc386fe2ad | 144 | _samplesTillHeld = 0; |
| pilotak | 4:bacc386fe2ad | 145 | } |
| pilotak | 4:bacc386fe2ad | 146 | } |
| pilotak | 4:bacc386fe2ad | 147 | } |
| pilotak | 4:bacc386fe2ad | 148 | }; // namespace AjK ends. |
| pilotak | 4:bacc386fe2ad | 149 | |
| pilotak | 4:bacc386fe2ad | 150 | using namespace AjK; |
| pilotak | 4:bacc386fe2ad | 151 |
