PinDetect rework, removed the continuous sampling, using just irq, timers and pin state sequence.
Fork of PinDetect by
Diff: PinDetect.h
- Revision:
- 1:611a8f5ac65c
- Parent:
- 0:4f4ccb203a70
- Child:
- 2:cb3afc45028b
diff -r 4f4ccb203a70 -r 611a8f5ac65c PinDetect.h --- a/PinDetect.h Thu Jan 13 01:47:31 2011 +0000 +++ b/PinDetect.h Thu Jan 13 09:53:26 2011 +0000 @@ -31,8 +31,6 @@ #define PINDETECT_PIN_ASSTERED 1 #endif -#define PINDETECT_PIN_DEASSTERED !PINDETECT_PIN_ASSTERED - #ifndef PINDETECT_SAMPLE_PERIOD #define PINDETECT_SAMPLE_PERIOD 20000 #endif @@ -130,32 +128,72 @@ * @see example.h */ class PinDetect { + +protected: + DigitalIn *_in; + Ticker *_ticker; + int _prevState; + int _currentStateCounter; + int _sampleTime; + int _assertValue; + int _samplesTillAssertReload; + int _samplesTillAssert; + int _samplesTillHeldReload; + int _samplesTillHeld; + FunctionPointer _callbackAsserted; + FunctionPointer _callbackDeasserted; + FunctionPointer _callbackAssertedHeld; + FunctionPointer _callbackDeassertedHeld; + + /** initialise class + * + * @param PinName p is a valid pin that supports DigitalIn + * @param PinMode m The mode the DigitalIn should use. + */ + void 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; + } + public: PinDetect() { error("You must supply a PinName"); } /** PinDetect constructor * + * By default the PinMode is set to PullDown. + * * @see http://mbed.org/handbook/DigitalIn * @param p PinName is a valid pin that supports DigitalIn */ PinDetect(PinName p) { - _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); - _prevState = _in->read(); - _ticker = new Ticker; + init( p, PullDown ); + } + + /** PinDetect constructor + * + * @see http://mbed.org/handbook/DigitalIn + * @param PinName p is a valid pin that supports DigitalIn + * @param PinMode m The mode the DigitalIn should use. + */ + PinDetect(PinName p, PinMode m) { + init( p, m ); } /** PinDetect destructor */ ~PinDetect() { - if (_ticker) delete(_ticker); - if (_in) delete(_in); + if ( _ticker ) delete( _ticker ); + if ( _in ) delete( _in ); } /** Set the sampling time in microseconds. @@ -164,7 +202,7 @@ */ void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD) { _sampleTime = i; - _ticker->attach_us(this, &PinDetect::isr, _sampleTime); + _ticker->attach_us( this, &PinDetect::isr, _sampleTime ); } /** Set the value used as assert. @@ -196,7 +234,7 @@ * @see http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode * @param PinMode m The mode to pass on to the DigitalIn */ - void mode(PinMode m) { _in->mode(m); } + void mode(PinMode m) { _in->mode( m ); } /** Attach a callback function * @@ -219,7 +257,7 @@ * @param function A C function pointer */ void attach_asserted(void (*function)(void)) { - _callbackAsserted.attach(function); + _callbackAsserted.attach( function ); } /** Attach a callback object/method @@ -247,7 +285,7 @@ */ template<typename T> void attach_asserted(T *object, void (T::*member)(void)) { - _callbackAsserted.attach(object, member); + _callbackAsserted.attach( object, member ); } /** Attach a callback function @@ -271,7 +309,7 @@ * @param function A C function pointer */ void attach_deasserted(void (*function)(void)) { - _callbackDeasserted.attach(function); + _callbackDeasserted.attach( function ); } /** Attach a callback object/method @@ -299,7 +337,7 @@ */ template<typename T> void attach_deasserted(T *object, void (T::*member)(void)) { - _callbackDeasserted.attach(object, member); + _callbackDeasserted.attach( object, member ); } /** Attach a callback function @@ -323,7 +361,7 @@ * @param function A C function pointer */ void attach_asserted_held(void (*function)(void)) { - _callbackAssertedHeld.attach(function); + _callbackAssertedHeld.attach( function ); } /** Attach a callback object/method @@ -351,7 +389,7 @@ */ template<typename T> void attach_asserted_held(T *object, void (T::*member)(void)) { - _callbackAssertedHeld.attach(object, member); + _callbackAssertedHeld.attach( object, member ); } /** Attach a callback function @@ -375,7 +413,7 @@ * @param function A C function pointer */ void attach_deasserted_held(void (*function)(void)) { - _callbackDeassertedHeld.attach(function); + _callbackDeassertedHeld.attach( function ); } /** Attach a callback object/method @@ -403,7 +441,7 @@ */ template<typename T> void attach_deasserted_held(T *object, void (T::*member)(void)) { - _callbackDeassertedHeld.attach(object, member); + _callbackDeassertedHeld.attach( object, member ); } /** operator int() @@ -417,11 +455,11 @@ void isr(void) { int currentState = _in->read(); - if (currentState != _prevState) { - if (_samplesTillAssert == 0) { + if ( currentState != _prevState ) { + if ( _samplesTillAssert == 0 ) { _prevState = currentState; _samplesTillHeld = _samplesTillHeldReload; - if (currentState == _assertValue) + if ( currentState == _assertValue ) _callbackAsserted.call(); else _callbackDeasserted.call(); @@ -434,11 +472,11 @@ _samplesTillAssert = _samplesTillAssertReload; } - if (_samplesTillHeld) { - if (_prevState == currentState) { + if ( _samplesTillHeld ) { + if ( _prevState == currentState ) { _samplesTillHeld--; - if (_samplesTillHeld == 0) { - if (currentState == _assertValue) + if ( _samplesTillHeld == 0 ) { + if ( currentState == _assertValue ) _callbackAssertedHeld.call(); else _callbackDeassertedHeld.call(); @@ -450,21 +488,6 @@ } } -protected: - DigitalIn *_in; - Ticker *_ticker; - int _prevState; - int _currentStateCounter; - int _sampleTime; - int _assertValue; - int _samplesTillAssertReload; - int _samplesTillAssert; - int _samplesTillHeldReload; - int _samplesTillHeld; - FunctionPointer _callbackAsserted; - FunctionPointer _callbackDeasserted; - FunctionPointer _callbackAssertedHeld; - FunctionPointer _callbackDeassertedHeld; }; }; // namespace AjK ends.