Pavel S / PinDetect

Fork of PinDetect by Arturo Alvarado

Committer:
pilotak
Date:
Wed Nov 16 23:20:29 2016 +0000
Revision:
4:bacc386fe2ad
Child:
5:2b0afd293d1d
rewritten to Mbed OS 5.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pilotak 4:bacc386fe2ad 1 /*
pilotak 4:bacc386fe2ad 2 Copyright (c) 2010 Andy Kirkham
pilotak 4:bacc386fe2ad 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 4:bacc386fe2ad 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 4:bacc386fe2ad 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 4:bacc386fe2ad 27 static void donothing() {}
pilotak 4:bacc386fe2ad 28
pilotak 4:bacc386fe2ad 29 PinDetect::PinDetect(PinName p) : _callbackAsserted(),
pilotak 4:bacc386fe2ad 30 _callbackDeasserted(),
pilotak 4:bacc386fe2ad 31 _callbackAssertedHeld(),
pilotak 4:bacc386fe2ad 32 _callbackDeassertedHeld() {
pilotak 4:bacc386fe2ad 33 init( p, PullDefault );
pilotak 4:bacc386fe2ad 34 _callbackAsserted.attach(donothing);
pilotak 4:bacc386fe2ad 35 _callbackDeasserted.attach(donothing);
pilotak 4:bacc386fe2ad 36 _callbackAssertedHeld.attach(donothing);
pilotak 4:bacc386fe2ad 37 _callbackDeassertedHeld.attach(donothing);
pilotak 4:bacc386fe2ad 38 }
pilotak 4:bacc386fe2ad 39
pilotak 4:bacc386fe2ad 40 PinDetect::PinDetect(PinName p, PinMode m) : _callbackAsserted(),
pilotak 4:bacc386fe2ad 41 _callbackDeasserted(),
pilotak 4:bacc386fe2ad 42 _callbackAssertedHeld(),
pilotak 4:bacc386fe2ad 43 _callbackDeassertedHeld() {
pilotak 4:bacc386fe2ad 44 init( p, m );
pilotak 4:bacc386fe2ad 45 _callbackAsserted.attach(donothing);
pilotak 4:bacc386fe2ad 46 _callbackDeasserted.attach(donothing);
pilotak 4:bacc386fe2ad 47 _callbackAssertedHeld.attach(donothing);
pilotak 4:bacc386fe2ad 48 _callbackDeassertedHeld.attach(donothing);
pilotak 4:bacc386fe2ad 49 }
pilotak 4:bacc386fe2ad 50
pilotak 4:bacc386fe2ad 51
pilotak 4:bacc386fe2ad 52 PinDetect::~PinDetect(void){
pilotak 4:bacc386fe2ad 53 if ( _ticker ) delete( _ticker );
pilotak 4:bacc386fe2ad 54 if ( _in ) delete( _in );
pilotak 4:bacc386fe2ad 55 }
pilotak 4:bacc386fe2ad 56
pilotak 4:bacc386fe2ad 57 void PinDetect::init(PinName p, PinMode m) {
pilotak 4:bacc386fe2ad 58 _sampleTime = PINDETECT_SAMPLE_PERIOD;
pilotak 4:bacc386fe2ad 59 _samplesTillAssert = PINDETECT_ASSERT_COUNT;
pilotak 4:bacc386fe2ad 60 _samplesTillHeld = 0;
pilotak 4:bacc386fe2ad 61 _samplesTillAssertReload = PINDETECT_ASSERT_COUNT;
pilotak 4:bacc386fe2ad 62 _samplesTillHeldReload = PINDETECT_HOLD_COUNT;
pilotak 4:bacc386fe2ad 63 _assertValue = PINDETECT_PIN_ASSTERED;
pilotak 4:bacc386fe2ad 64
pilotak 4:bacc386fe2ad 65 _in = new DigitalIn( p );
pilotak 4:bacc386fe2ad 66 _in->mode( m );
pilotak 4:bacc386fe2ad 67 _prevState = _in->read();
pilotak 4:bacc386fe2ad 68 _ticker = new Ticker;
pilotak 4:bacc386fe2ad 69 }
pilotak 4:bacc386fe2ad 70
pilotak 4:bacc386fe2ad 71 void PinDetect::setSampleFrequency(int i) {
pilotak 4:bacc386fe2ad 72 _sampleTime = i;
pilotak 4:bacc386fe2ad 73 _prevState = _in->read();
pilotak 4:bacc386fe2ad 74 _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime );
pilotak 4:bacc386fe2ad 75 }
pilotak 4:bacc386fe2ad 76
pilotak 4:bacc386fe2ad 77 void PinDetect::setAssertValue (int i) {
pilotak 4:bacc386fe2ad 78 _assertValue = i & 1;
pilotak 4:bacc386fe2ad 79 }
pilotak 4:bacc386fe2ad 80
pilotak 4:bacc386fe2ad 81 void PinDetect::setSamplesTillAssert(int i) {
pilotak 4:bacc386fe2ad 82 _samplesTillAssertReload = i;
pilotak 4:bacc386fe2ad 83 }
pilotak 4:bacc386fe2ad 84
pilotak 4:bacc386fe2ad 85 void PinDetect::setSamplesTillHeld(int i) {
pilotak 4:bacc386fe2ad 86 _samplesTillHeldReload = i;
pilotak 4:bacc386fe2ad 87 }
pilotak 4:bacc386fe2ad 88
pilotak 4:bacc386fe2ad 89 void PinDetect::mode(PinMode m) {
pilotak 4:bacc386fe2ad 90 _in->mode( m );
pilotak 4:bacc386fe2ad 91 }
pilotak 4:bacc386fe2ad 92
pilotak 4:bacc386fe2ad 93 void PinDetect::attach_asserted(Callback<void()> function) {
pilotak 4:bacc386fe2ad 94 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 95 _callbackAsserted.attach( function );
pilotak 4:bacc386fe2ad 96 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 97 }
pilotak 4:bacc386fe2ad 98
pilotak 4:bacc386fe2ad 99 void PinDetect::attach_deasserted(Callback<void()> function) {
pilotak 4:bacc386fe2ad 100 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 101 _callbackDeasserted.attach( function );
pilotak 4:bacc386fe2ad 102 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 103 }
pilotak 4:bacc386fe2ad 104
pilotak 4:bacc386fe2ad 105 void PinDetect::attach_asserted_held(Callback<void()> function) {
pilotak 4:bacc386fe2ad 106 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 107 _callbackAssertedHeld.attach( function );
pilotak 4:bacc386fe2ad 108 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 109 }
pilotak 4:bacc386fe2ad 110
pilotak 4:bacc386fe2ad 111 void PinDetect::attach_deasserted_held(Callback<void()> function) {
pilotak 4:bacc386fe2ad 112 core_util_critical_section_enter();
pilotak 4:bacc386fe2ad 113 _callbackDeassertedHeld.attach( function );
pilotak 4:bacc386fe2ad 114 core_util_critical_section_exit();
pilotak 4:bacc386fe2ad 115 }
pilotak 4:bacc386fe2ad 116
pilotak 4:bacc386fe2ad 117 void PinDetect::isr(void) {
pilotak 4:bacc386fe2ad 118 int currentState = _in->read();
pilotak 4:bacc386fe2ad 119
pilotak 4:bacc386fe2ad 120 if ( currentState != _prevState ) {
pilotak 4:bacc386fe2ad 121 if ( _samplesTillAssert == 0 ) {
pilotak 4:bacc386fe2ad 122 _prevState = currentState;
pilotak 4:bacc386fe2ad 123 _samplesTillHeld = _samplesTillHeldReload;
pilotak 4:bacc386fe2ad 124 if ( currentState == _assertValue )
pilotak 4:bacc386fe2ad 125 _callbackAsserted.call();
pilotak 4:bacc386fe2ad 126 else
pilotak 4:bacc386fe2ad 127 _callbackDeasserted.call();
pilotak 4:bacc386fe2ad 128 }
pilotak 4:bacc386fe2ad 129 else {
pilotak 4:bacc386fe2ad 130 _samplesTillAssert--;
pilotak 4:bacc386fe2ad 131 }
pilotak 4:bacc386fe2ad 132 }
pilotak 4:bacc386fe2ad 133 else {
pilotak 4:bacc386fe2ad 134 _samplesTillAssert = _samplesTillAssertReload;
pilotak 4:bacc386fe2ad 135 }
pilotak 4:bacc386fe2ad 136
pilotak 4:bacc386fe2ad 137 if ( _samplesTillHeld ) {
pilotak 4:bacc386fe2ad 138 if ( _prevState == currentState ) {
pilotak 4:bacc386fe2ad 139 _samplesTillHeld--;
pilotak 4:bacc386fe2ad 140 if ( _samplesTillHeld == 0 ) {
pilotak 4:bacc386fe2ad 141 if ( currentState == _assertValue )
pilotak 4:bacc386fe2ad 142 _callbackAssertedHeld.call();
pilotak 4:bacc386fe2ad 143 else
pilotak 4:bacc386fe2ad 144 _callbackDeassertedHeld.call();
pilotak 4:bacc386fe2ad 145 }
pilotak 4:bacc386fe2ad 146 }
pilotak 4:bacc386fe2ad 147 else {
pilotak 4:bacc386fe2ad 148 _samplesTillHeld = 0;
pilotak 4:bacc386fe2ad 149 }
pilotak 4:bacc386fe2ad 150 }
pilotak 4:bacc386fe2ad 151 }
pilotak 4:bacc386fe2ad 152 }; // namespace AjK ends.
pilotak 4:bacc386fe2ad 153
pilotak 4:bacc386fe2ad 154 using namespace AjK;
pilotak 4:bacc386fe2ad 155