Mbed OS 5

Fork of PinDetect by Arturo Alvarado

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PinDetect.cpp Source File

PinDetect.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003 
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010 
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013 
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "PinDetect.h"
00024 
00025 namespace AjK {
00026 
00027 PinDetect::PinDetect(PinName p) {
00028     init(p, PullDefault);
00029 }
00030 
00031 PinDetect::PinDetect(PinName p, PinMode m) {
00032     init(p, m);
00033 }
00034 
00035 
00036 PinDetect::~PinDetect(void) {
00037     if ( _ticker )  delete( _ticker );
00038 
00039     if ( _in )      delete( _in );
00040 }
00041 
00042 void PinDetect::init(PinName p, PinMode m) {
00043     _sampleTime              = PINDETECT_SAMPLE_PERIOD;
00044     _samplesTillAssert       = PINDETECT_ASSERT_COUNT;
00045     _samplesTillHeld         = 0;
00046     _samplesTillAssertReload = PINDETECT_ASSERT_COUNT;
00047     _samplesTillHeldReload   = PINDETECT_HOLD_COUNT;
00048     _assertValue             = PINDETECT_PIN_ASSERTED;
00049 
00050     _in = new DigitalIn(p);
00051     _in->mode(m);
00052     _prevState = _in->read();
00053     _ticker = new Ticker;
00054 
00055     _callbackAsserted = NULL;
00056     _callbackDeasserted = NULL;
00057     _callbackAssertedHeld = NULL;
00058     _callbackDeassertedHeld = NULL;
00059 }
00060 
00061 void PinDetect::setSampleFrequency(int i) {
00062     _sampleTime = i;
00063     _prevState  = _in->read();
00064     _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime);
00065 }
00066 
00067 void PinDetect::setAssertValue(int i) {
00068     _assertValue = i & 1;
00069 }
00070 
00071 void PinDetect::setSamplesTillAssert(int i) {
00072     _samplesTillAssertReload = i;
00073 }
00074 
00075 void PinDetect::setSamplesTillHeld(int i) {
00076     _samplesTillHeldReload = i;
00077 }
00078 
00079 void PinDetect::mode(PinMode m) {
00080     _in->mode(m);
00081 }
00082 
00083 void PinDetect::attach_asserted(Callback<void()> function) {
00084     core_util_critical_section_enter();
00085     _callbackAsserted = function;
00086     core_util_critical_section_exit();
00087 }
00088 
00089 void PinDetect::attach_deasserted(Callback<void()> function) {
00090     core_util_critical_section_enter();
00091     _callbackDeasserted = function;
00092     core_util_critical_section_exit();
00093 }
00094 
00095 void PinDetect::attach_asserted_held(Callback<void()> function) {
00096     core_util_critical_section_enter();
00097     _callbackAssertedHeld = function;
00098     core_util_critical_section_exit();
00099 }
00100 
00101 void PinDetect::attach_deasserted_held(Callback<void()> function) {
00102     core_util_critical_section_enter();
00103     _callbackDeassertedHeld = function;
00104     core_util_critical_section_exit();
00105 }
00106 
00107 void PinDetect::isr(void) {
00108     int currentState = _in->read();
00109 
00110     if ( currentState != _prevState ) {
00111         if ( _samplesTillAssert == 0 ) {
00112             _prevState = currentState;
00113             _samplesTillHeld = _samplesTillHeldReload;
00114 
00115             if ( currentState == _assertValue ) {
00116                 if (_callbackAsserted) _callbackAsserted.call();
00117 
00118             } else {
00119                 if (_callbackDeasserted) _callbackDeasserted.call();
00120             }
00121 
00122         } else {
00123             _samplesTillAssert--;
00124         }
00125 
00126     } else {
00127         _samplesTillAssert = _samplesTillAssertReload;
00128     }
00129 
00130     if ( _samplesTillHeld ) {
00131         if ( _prevState == currentState ) {
00132             _samplesTillHeld--;
00133 
00134             if ( _samplesTillHeld == 0 ) {
00135                 if ( currentState == _assertValue ) {
00136                     if (_callbackAssertedHeld) _callbackAssertedHeld.call();
00137 
00138                 } else {
00139                     if (_callbackDeassertedHeld) _callbackDeassertedHeld.call();
00140                 }
00141             }
00142 
00143         } else {
00144             _samplesTillHeld = 0;
00145         }
00146     }
00147 }
00148 }; // namespace AjK ends.
00149 
00150 using namespace AjK;
00151