Code for autonomous rover for Sparkfun AVC. DataBus won 3rd in 2012 and the same code was used on Troubled Child, a 1986 Jeep Grand Wagoneer to win 1st in 2014.

Dependencies:   mbed Watchdog SDFileSystem DigoleSerialDisp

UI/Buttons/PinDetect.cpp

Committer:
shimniok
Date:
2018-11-30
Revision:
25:bb5356402687
Parent:
23:a34af501ea89

File content as of revision 25:bb5356402687:

/*
    by Michael Shimniok
    
    based on PinDetect Copyright (c) 2010 Andy Kirkham

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

#if 0

#include "PinDetect.h"

/** initialise class */
void PinDetect::init(PinName p, PinMode m) {
    _sampleTime              = PINDETECT_SAMPLE_PERIOD;
    _samplesTillAssert       = PINDETECT_ASSERT_COUNT;
    _samplesTillHeld         = 0;
    _samplesTillAssertReload = PINDETECT_ASSERT_COUNT;
    _samplesTillHeldReload   = PINDETECT_HOLD_COUNT;
    _assertValue             = PINDETECT_PIN_ASSTERED;

    _in = new DigitalIn( p );
    _in->mode( m );
    _prevState = _in->read();
    _ticker = new Ticker;
}


void PinDetect::isr(void) {
/*
    int currentState = _in->read();

    if ( currentState != _prevState ) {
        if ( _samplesTillAssert == 0 ) {
            _prevState = currentState;
            _samplesTillHeld = _samplesTillHeldReload;
            if ( currentState == _assertValue )
                _callbackAsserted.call();
            else
                _callbackDeasserted.call();
        }
        else {
            _samplesTillAssert--;
        }
    }
    else {
        _samplesTillAssert = _samplesTillAssertReload;
    }

    if ( _samplesTillHeld ) {
        if ( _prevState == currentState ) {
            _samplesTillHeld--;
            if ( _samplesTillHeld == 0 ) {
                if ( currentState == _assertValue )
                    _callbackAssertedHeld.call();
                else
                    _callbackDeassertedHeld.call();
            }
        }
        else {
            _samplesTillHeld = 0;
        }
    }
    */
}


PinDetect::PinDetect(PinName p) {
    init( p, PullDown );
}

PinDetect::PinDetect(PinName p, PinMode m) {
    init( p, m );
}

PinDetect::~PinDetect() {
    if ( _ticker )  delete( _ticker );
    if ( _in )      delete( _in );
}

void PinDetect::setSampleFrequency(int i) {
    _sampleTime = i;
    _prevState  = _in->read();
    //_ticker->attach_us( this, &PinDetect::isr, _sampleTime );
}

void PinDetect::setAssertValue (int i) {
     _assertValue = i & 1; 
}

void PinDetect::setSamplesTillAssert(int i) {
    _samplesTillAssertReload = i; 
}

void PinDetect::setSamplesTillHeld(int i) { 
    _samplesTillHeldReload = i; 
}

void PinDetect::mode(PinMode m) { 
    _in->mode( m ); 
}

void PinDetect::attach_asserted(Callback function) {
  _callbackAsserted = function;
}

template<typename T>
void PinDetect::attach_asserted(T *object, void (T::*member)(void)) {
//    _callbackAsserted.attach( object, member );
}

void PinDetect::attach_deasserted(void (*function)(void)) {
//    _callbackDeasserted.attach( function );
}

template<typename T>
void PinDetect::attach_deasserted(T *object, void (T::*member)(void)) {
//    _callbackDeasserted.attach( object, member );
}

void PinDetect::attach_asserted_held(void (*function)(void)) {
//    _callbackAssertedHeld.attach( function );
}
template<typename T>
void PinDetect::attach_asserted_held(T *object, void (T::*member)(void)) {
//    _callbackAssertedHeld.attach( object, member );
}

void PinDetect::attach_deasserted_held(void (*function)(void)) {
//    _callbackDeassertedHeld.attach( function );
}

template<typename T>
void PinDetect::attach_deasserted_held(T *object, void (T::*member)(void)) {
//    _callbackDeassertedHeld.attach( object, member );
}

#endif