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.
Dependencies: mbed Watchdog SDFileSystem DigoleSerialDisp
UI/Buttons/PinDetect.cpp@23:a34af501ea89, 2018-11-30 (annotated)
- Committer:
- shimniok
- Date:
- Fri Nov 30 15:41:05 2018 +0000
- Revision:
- 23:a34af501ea89
Disabled PinDetect code (for now); fixed attach() calls in various functions to use new callback class.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| shimniok | 23:a34af501ea89 | 1 | /* |
| shimniok | 23:a34af501ea89 | 2 | by Michael Shimniok |
| shimniok | 23:a34af501ea89 | 3 | |
| shimniok | 23:a34af501ea89 | 4 | based on PinDetect Copyright (c) 2010 Andy Kirkham |
| shimniok | 23:a34af501ea89 | 5 | |
| shimniok | 23:a34af501ea89 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| shimniok | 23:a34af501ea89 | 7 | of this software and associated documentation files (the "Software"), to deal |
| shimniok | 23:a34af501ea89 | 8 | in the Software without restriction, including without limitation the rights |
| shimniok | 23:a34af501ea89 | 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| shimniok | 23:a34af501ea89 | 10 | copies of the Software, and to permit persons to whom the Software is |
| shimniok | 23:a34af501ea89 | 11 | furnished to do so, subject to the following conditions: |
| shimniok | 23:a34af501ea89 | 12 | |
| shimniok | 23:a34af501ea89 | 13 | The above copyright notice and this permission notice shall be included in |
| shimniok | 23:a34af501ea89 | 14 | all copies or substantial portions of the Software. |
| shimniok | 23:a34af501ea89 | 15 | |
| shimniok | 23:a34af501ea89 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| shimniok | 23:a34af501ea89 | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| shimniok | 23:a34af501ea89 | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| shimniok | 23:a34af501ea89 | 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| shimniok | 23:a34af501ea89 | 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| shimniok | 23:a34af501ea89 | 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| shimniok | 23:a34af501ea89 | 22 | THE SOFTWARE. |
| shimniok | 23:a34af501ea89 | 23 | */ |
| shimniok | 23:a34af501ea89 | 24 | |
| shimniok | 23:a34af501ea89 | 25 | #if 0 |
| shimniok | 23:a34af501ea89 | 26 | |
| shimniok | 23:a34af501ea89 | 27 | #include "PinDetect.h" |
| shimniok | 23:a34af501ea89 | 28 | |
| shimniok | 23:a34af501ea89 | 29 | /** initialise class */ |
| shimniok | 23:a34af501ea89 | 30 | void PinDetect::init(PinName p, PinMode m) { |
| shimniok | 23:a34af501ea89 | 31 | _sampleTime = PINDETECT_SAMPLE_PERIOD; |
| shimniok | 23:a34af501ea89 | 32 | _samplesTillAssert = PINDETECT_ASSERT_COUNT; |
| shimniok | 23:a34af501ea89 | 33 | _samplesTillHeld = 0; |
| shimniok | 23:a34af501ea89 | 34 | _samplesTillAssertReload = PINDETECT_ASSERT_COUNT; |
| shimniok | 23:a34af501ea89 | 35 | _samplesTillHeldReload = PINDETECT_HOLD_COUNT; |
| shimniok | 23:a34af501ea89 | 36 | _assertValue = PINDETECT_PIN_ASSTERED; |
| shimniok | 23:a34af501ea89 | 37 | |
| shimniok | 23:a34af501ea89 | 38 | _in = new DigitalIn( p ); |
| shimniok | 23:a34af501ea89 | 39 | _in->mode( m ); |
| shimniok | 23:a34af501ea89 | 40 | _prevState = _in->read(); |
| shimniok | 23:a34af501ea89 | 41 | _ticker = new Ticker; |
| shimniok | 23:a34af501ea89 | 42 | } |
| shimniok | 23:a34af501ea89 | 43 | |
| shimniok | 23:a34af501ea89 | 44 | |
| shimniok | 23:a34af501ea89 | 45 | void PinDetect::isr(void) { |
| shimniok | 23:a34af501ea89 | 46 | /* |
| shimniok | 23:a34af501ea89 | 47 | int currentState = _in->read(); |
| shimniok | 23:a34af501ea89 | 48 | |
| shimniok | 23:a34af501ea89 | 49 | if ( currentState != _prevState ) { |
| shimniok | 23:a34af501ea89 | 50 | if ( _samplesTillAssert == 0 ) { |
| shimniok | 23:a34af501ea89 | 51 | _prevState = currentState; |
| shimniok | 23:a34af501ea89 | 52 | _samplesTillHeld = _samplesTillHeldReload; |
| shimniok | 23:a34af501ea89 | 53 | if ( currentState == _assertValue ) |
| shimniok | 23:a34af501ea89 | 54 | _callbackAsserted.call(); |
| shimniok | 23:a34af501ea89 | 55 | else |
| shimniok | 23:a34af501ea89 | 56 | _callbackDeasserted.call(); |
| shimniok | 23:a34af501ea89 | 57 | } |
| shimniok | 23:a34af501ea89 | 58 | else { |
| shimniok | 23:a34af501ea89 | 59 | _samplesTillAssert--; |
| shimniok | 23:a34af501ea89 | 60 | } |
| shimniok | 23:a34af501ea89 | 61 | } |
| shimniok | 23:a34af501ea89 | 62 | else { |
| shimniok | 23:a34af501ea89 | 63 | _samplesTillAssert = _samplesTillAssertReload; |
| shimniok | 23:a34af501ea89 | 64 | } |
| shimniok | 23:a34af501ea89 | 65 | |
| shimniok | 23:a34af501ea89 | 66 | if ( _samplesTillHeld ) { |
| shimniok | 23:a34af501ea89 | 67 | if ( _prevState == currentState ) { |
| shimniok | 23:a34af501ea89 | 68 | _samplesTillHeld--; |
| shimniok | 23:a34af501ea89 | 69 | if ( _samplesTillHeld == 0 ) { |
| shimniok | 23:a34af501ea89 | 70 | if ( currentState == _assertValue ) |
| shimniok | 23:a34af501ea89 | 71 | _callbackAssertedHeld.call(); |
| shimniok | 23:a34af501ea89 | 72 | else |
| shimniok | 23:a34af501ea89 | 73 | _callbackDeassertedHeld.call(); |
| shimniok | 23:a34af501ea89 | 74 | } |
| shimniok | 23:a34af501ea89 | 75 | } |
| shimniok | 23:a34af501ea89 | 76 | else { |
| shimniok | 23:a34af501ea89 | 77 | _samplesTillHeld = 0; |
| shimniok | 23:a34af501ea89 | 78 | } |
| shimniok | 23:a34af501ea89 | 79 | } |
| shimniok | 23:a34af501ea89 | 80 | */ |
| shimniok | 23:a34af501ea89 | 81 | } |
| shimniok | 23:a34af501ea89 | 82 | |
| shimniok | 23:a34af501ea89 | 83 | |
| shimniok | 23:a34af501ea89 | 84 | PinDetect::PinDetect(PinName p) { |
| shimniok | 23:a34af501ea89 | 85 | init( p, PullDown ); |
| shimniok | 23:a34af501ea89 | 86 | } |
| shimniok | 23:a34af501ea89 | 87 | |
| shimniok | 23:a34af501ea89 | 88 | PinDetect::PinDetect(PinName p, PinMode m) { |
| shimniok | 23:a34af501ea89 | 89 | init( p, m ); |
| shimniok | 23:a34af501ea89 | 90 | } |
| shimniok | 23:a34af501ea89 | 91 | |
| shimniok | 23:a34af501ea89 | 92 | PinDetect::~PinDetect() { |
| shimniok | 23:a34af501ea89 | 93 | if ( _ticker ) delete( _ticker ); |
| shimniok | 23:a34af501ea89 | 94 | if ( _in ) delete( _in ); |
| shimniok | 23:a34af501ea89 | 95 | } |
| shimniok | 23:a34af501ea89 | 96 | |
| shimniok | 23:a34af501ea89 | 97 | void PinDetect::setSampleFrequency(int i) { |
| shimniok | 23:a34af501ea89 | 98 | _sampleTime = i; |
| shimniok | 23:a34af501ea89 | 99 | _prevState = _in->read(); |
| shimniok | 23:a34af501ea89 | 100 | //_ticker->attach_us( this, &PinDetect::isr, _sampleTime ); |
| shimniok | 23:a34af501ea89 | 101 | } |
| shimniok | 23:a34af501ea89 | 102 | |
| shimniok | 23:a34af501ea89 | 103 | void PinDetect::setAssertValue (int i) { |
| shimniok | 23:a34af501ea89 | 104 | _assertValue = i & 1; |
| shimniok | 23:a34af501ea89 | 105 | } |
| shimniok | 23:a34af501ea89 | 106 | |
| shimniok | 23:a34af501ea89 | 107 | void PinDetect::setSamplesTillAssert(int i) { |
| shimniok | 23:a34af501ea89 | 108 | _samplesTillAssertReload = i; |
| shimniok | 23:a34af501ea89 | 109 | } |
| shimniok | 23:a34af501ea89 | 110 | |
| shimniok | 23:a34af501ea89 | 111 | void PinDetect::setSamplesTillHeld(int i) { |
| shimniok | 23:a34af501ea89 | 112 | _samplesTillHeldReload = i; |
| shimniok | 23:a34af501ea89 | 113 | } |
| shimniok | 23:a34af501ea89 | 114 | |
| shimniok | 23:a34af501ea89 | 115 | void PinDetect::mode(PinMode m) { |
| shimniok | 23:a34af501ea89 | 116 | _in->mode( m ); |
| shimniok | 23:a34af501ea89 | 117 | } |
| shimniok | 23:a34af501ea89 | 118 | |
| shimniok | 23:a34af501ea89 | 119 | void PinDetect::attach_asserted(Callback function) { |
| shimniok | 23:a34af501ea89 | 120 | _callbackAsserted = function; |
| shimniok | 23:a34af501ea89 | 121 | } |
| shimniok | 23:a34af501ea89 | 122 | |
| shimniok | 23:a34af501ea89 | 123 | template<typename T> |
| shimniok | 23:a34af501ea89 | 124 | void PinDetect::attach_asserted(T *object, void (T::*member)(void)) { |
| shimniok | 23:a34af501ea89 | 125 | // _callbackAsserted.attach( object, member ); |
| shimniok | 23:a34af501ea89 | 126 | } |
| shimniok | 23:a34af501ea89 | 127 | |
| shimniok | 23:a34af501ea89 | 128 | void PinDetect::attach_deasserted(void (*function)(void)) { |
| shimniok | 23:a34af501ea89 | 129 | // _callbackDeasserted.attach( function ); |
| shimniok | 23:a34af501ea89 | 130 | } |
| shimniok | 23:a34af501ea89 | 131 | |
| shimniok | 23:a34af501ea89 | 132 | template<typename T> |
| shimniok | 23:a34af501ea89 | 133 | void PinDetect::attach_deasserted(T *object, void (T::*member)(void)) { |
| shimniok | 23:a34af501ea89 | 134 | // _callbackDeasserted.attach( object, member ); |
| shimniok | 23:a34af501ea89 | 135 | } |
| shimniok | 23:a34af501ea89 | 136 | |
| shimniok | 23:a34af501ea89 | 137 | void PinDetect::attach_asserted_held(void (*function)(void)) { |
| shimniok | 23:a34af501ea89 | 138 | // _callbackAssertedHeld.attach( function ); |
| shimniok | 23:a34af501ea89 | 139 | } |
| shimniok | 23:a34af501ea89 | 140 | template<typename T> |
| shimniok | 23:a34af501ea89 | 141 | void PinDetect::attach_asserted_held(T *object, void (T::*member)(void)) { |
| shimniok | 23:a34af501ea89 | 142 | // _callbackAssertedHeld.attach( object, member ); |
| shimniok | 23:a34af501ea89 | 143 | } |
| shimniok | 23:a34af501ea89 | 144 | |
| shimniok | 23:a34af501ea89 | 145 | void PinDetect::attach_deasserted_held(void (*function)(void)) { |
| shimniok | 23:a34af501ea89 | 146 | // _callbackDeassertedHeld.attach( function ); |
| shimniok | 23:a34af501ea89 | 147 | } |
| shimniok | 23:a34af501ea89 | 148 | |
| shimniok | 23:a34af501ea89 | 149 | template<typename T> |
| shimniok | 23:a34af501ea89 | 150 | void PinDetect::attach_deasserted_held(T *object, void (T::*member)(void)) { |
| shimniok | 23:a34af501ea89 | 151 | // _callbackDeassertedHeld.attach( object, member ); |
| shimniok | 23:a34af501ea89 | 152 | } |
| shimniok | 23:a34af501ea89 | 153 | |
| shimniok | 23:a34af501ea89 | 154 | #endif |