InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold. Updated callback procedures for mbed 5.xx by spotlightkid
Dependents: city1082-capsense-sw2-tft-leds CITY1082-TFT-basic_sw2_ctr 4180_final_project_github mRotaryEncoder_HelloWorld-os ... more
Diff: PinDetect.h
- Revision:
- 3:d552d2899765
- Parent:
- 2:cb3afc45028b
--- a/PinDetect.h Thu Jan 13 11:09:22 2011 +0000 +++ b/PinDetect.h Thu Oct 17 15:52:19 2019 +0000 @@ -1,16 +1,13 @@ /* - Copyright (c) 2010 Andy Kirkham - + Copyright (c) 2010 Andy Kirkham, 2018 Christopher Arndt 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 @@ -20,22 +17,20 @@ THE SOFTWARE. */ -#ifndef AJK_PIN_DETECT_H -#define AJK_PIN_DETECT_H +#ifndef PIN_DETECT_H +#define PIN_DETECT_H -#ifndef MBED_H #include "mbed.h" -#endif -#ifndef PINDETECT_PIN_ASSTERED -#define PINDETECT_PIN_ASSTERED 1 +#ifndef PINDETECT_PIN_ASSERTED +#define PINDETECT_PIN_ASSERTED 1 #endif #ifndef PINDETECT_SAMPLE_PERIOD #define PINDETECT_SAMPLE_PERIOD 20000 #endif -#ifndef PINDETECT_ASSERT_COUNT +#ifndef PINDETECT_ASSERT_COUNT #define PINDETECT_ASSERT_COUNT 1 #endif @@ -57,7 +52,7 @@ * All parameters are customisable which include:- * <ul> * <li> The sampling frequency. </li> - * <li> The number of continuous samples until a state change is detected. </li> + * <li> The number of continuous samples until a state change is detected. </li> * <li> The number of continuous samples until a key is assumed held after a state change. </li> * <li> The logic level which is assumed to be asserted (0volts or +volts). </li> * </ul> @@ -69,50 +64,49 @@ * #include "mbed.h" * #include "PinDetect.h" * - * PinDetect pin( p30 ); - * DigitialOut led1( LED1 ); - * DigitialOut led2( LED2 ); - * DigitialOut led3( LED3 ); - * DigitialOut led4( LED4 ); + * PinDetect pin(p30); + * DigitialOut led1(LED1); + * DigitialOut led2(LED2); + * DigitialOut led3(LED3); + * DigitialOut led4(LED4); * - * void keyPressed( void ) { + * void keyPressed(void) { * led2 = 1; * led3 = 0; * led4 = 0; * } * - * void keyReleased( void ) { + * void keyReleased(void) { * led2 = 0; * led3 = 0; * led4 = 0; * } * - * void keyPressedHeld( void ) { + * void keyPressedHeld(void) { * led3 = 1; * } * - * void keyReleasedHeld( void ) { + * void keyReleasedHeld(void) { * led4 = 1; * } * * int main() { - * - * pin.mode( PullDown ); - * pin.attach_asserted( &keyPressed ); - * pin.attach_deasserted( &keyReleased ); - * pin.attach_asserted_held( &keyPressedHeld ); - * pin.attach_deasserted_held( &keyReleasedHeld ); + * pin.mode(PullDown); + * pin.attach_asserted(keyPressed); + * pin.attach_deasserted(keyReleased); + * pin.attach_asserted_held(keyPressedHeld); + * pin.attach_deasserted_held(keyReleasedHeld); * * // Sampling does not begin until you set a frequency. * // The default is 20ms. If you want a different frequency * // then pass the period in microseconds for example, for 10ms :- - * // pin.setSampleFrequency( 10000 ); + * // pin.setSampleFrequency(10000); * // * pin.setSampleFrequency(); // Defaults to 20ms. * - * while( 1 ) { + * while (1) { * led1 = !led1; - * wait( 0.2 ); + * wait(0.2); * } * } * @endcode @@ -133,18 +127,17 @@ 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; - + Callback<void()> _callbackAsserted; + Callback<void()> _callbackDeasserted; + Callback<void()> _callbackAssertedHeld; + Callback<void()> _callbackDeassertedHeld; + /** initialise class * * @param PinName p is a valid pin that supports DigitalIn @@ -156,19 +149,20 @@ _samplesTillHeld = 0; _samplesTillAssertReload = PINDETECT_ASSERT_COUNT; _samplesTillHeldReload = PINDETECT_HOLD_COUNT; - _assertValue = PINDETECT_PIN_ASSTERED; - - _in = new DigitalIn( p ); - _in->mode( m ); - _prevState = _in->read(); + _assertValue = PINDETECT_PIN_ASSERTED; + + _in = new DigitalIn(p); + _in->mode(m); + _prevState = _in->read(); _ticker = new Ticker; } - + public: + friend class Ticker; - friend class Ticker; - - PinDetect() { error("You must supply a PinName"); } + PinDetect() { + error("You must supply a PinName"); + } /** PinDetect constructor * @@ -176,9 +170,9 @@ * * @see http://mbed.org/handbook/DigitalIn * @param p PinName is a valid pin that supports DigitalIn - */ + */ PinDetect(PinName p) { - init( p, PullDown ); + init(p, PullDown); } /** PinDetect constructor @@ -186,72 +180,80 @@ * @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 ); + 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. * * @param int The time between pin samples in microseconds. */ - void setSampleFrequency(int i = PINDETECT_SAMPLE_PERIOD) { - _sampleTime = i; - _prevState = _in->read(); - _ticker->attach_us( this, &PinDetect::isr, _sampleTime ); + void setSampleFrequency(int i=PINDETECT_SAMPLE_PERIOD) { + _sampleTime = i; + _prevState = _in->read(); + _ticker->attach_us(callback(this, &PinDetect::isr), _sampleTime); } - + /** Set the value used as assert. * - * Defaults to 1 (ie if pin == 1 then pin asserted). + * Defaults to 1 (i.e. if pin == 1 then pin asserted). * * @param int New assert value (1 or 0) */ - void setAssertValue (int i = PINDETECT_PIN_ASSTERED) { _assertValue = i & 1; } - + void setAssertValue(int i=PINDETECT_PIN_ASSERTED) { + _assertValue = i & 1; + } + /** Set the number of continuous samples until assert assumed. * * Defaults to 1 (1 * sample frequency). * * @param int The number of continuous samples until assert assumed. - */ - void setSamplesTillAssert(int i) { _samplesTillAssertReload = i; } - + */ + void setSamplesTillAssert(int i) { + _samplesTillAssertReload = i; + } + /** Set the number of continuous samples until held assumed. * * Defaults to 50 * sample frequency. * * @param int The number of continuous samples until held assumed. - */ - void setSamplesTillHeld(int i) { _samplesTillHeldReload = i; } - + */ + void setSamplesTillHeld(int i) { + _samplesTillHeldReload = i; + } + /** Set the pin mode. * * @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 ); } - - /** Attach a callback function + void mode(PinMode m) { + _in->mode(m); + } + + /** Attach a callback function * * @code * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); + * DigitalOut led1(LED1); + * PinDetect pin(p30); * - * void myCallback( void ) { + * void myCallback(void) { * led1 = 1; * }; - * + * * main() { - * pin.attach_asserted( &myCallback ); + * pin.attach_asserted(myCallback); * } * * @endcode @@ -259,51 +261,23 @@ * Call this function when a pin is asserted. * @param function A C function pointer */ - void attach_asserted(void (*function)(void)) { - _callbackAsserted.attach( function ); + void attach_asserted(Callback<void()> cb) { + _callbackAsserted = cb; } - - /** Attach a callback object/method + + /** Attach a callback function * * @code * - * class Bar { - * public: - * void myCallback( void ) { led1 = 1; } - * }; - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_asserted( &bar, &Bar::myCallback ); - * } - * - * @endcode + * DigitalOut led1(LED1); + * PinDetect pin(p30); * - * Call this function when a pin is asserted. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_asserted(T *object, void (T::*member)(void)) { - _callbackAsserted.attach( object, member ); - } - - /** Attach a callback function - * - * @code - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { + * void myCallback(void) { * led1 = 0; * }; * * main() { - * pin.attach_deasserted( &myCallback ); + * pin.attach_deasserted(myCallback); * } * * @endcode @@ -311,51 +285,23 @@ * Call this function when a pin is deasserted. * @param function A C function pointer */ - void attach_deasserted(void (*function)(void)) { - _callbackDeasserted.attach( function ); + void attach_deasserted(Callback<void()> cb) { + _callbackDeasserted = cb; } - - /** Attach a callback object/method + + /** Attach a callback function * * @code * - * class Bar { - * public: - * void myCallback( void ) { led1 = 0; } - * }; - * - * DigitalOut led1( LED1 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_deasserted( &bar, &Bar::myCallback ); - * } - * - * @endcode + * DigitalOut led2(LED2); + * PinDetect pin(p30); * - * Call this function when a pin is deasserted. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_deasserted(T *object, void (T::*member)(void)) { - _callbackDeasserted.attach( object, member ); - } - - /** Attach a callback function - * - * @code - * - * DigitalOut led2( LED2 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { + * void myCallback(void) { * led2 = 1; * }; * * main() { - * pin.attach_asserted_held( &myCallback ); + * pin.attach_asserted_held(myCallback); * } * * @endcode @@ -363,51 +309,23 @@ * Call this function when a pin is asserted and held. * @param function A C function pointer */ - void attach_asserted_held(void (*function)(void)) { - _callbackAssertedHeld.attach( function ); + void attach_asserted_held(Callback<void()> cb) { + _callbackAssertedHeld = cb; } - - /** Attach a callback object/method + + /** Attach a callback function * * @code * - * class Bar { - * public: - * void myCallback( void ) { led2 = 0; } - * }; - * - * DigitalOut led2( LED2 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_asserted_held( &bar, &Bar::myCallback ); - * } - * - * @endcode + * DigitalOut led3(LED3); + * PinDetect pin(p30); * - * Call this function when a pin is asserted and held. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_asserted_held(T *object, void (T::*member)(void)) { - _callbackAssertedHeld.attach( object, member ); - } - - /** Attach a callback function - * - * @code - * - * DigitalOut led3( LED3 ); - * PinDetect pin( p30 ); - * - * void myCallback( void ) { + * void myCallback(void) { * led3 = 1; * }; * * main() { - * pin.attach_deasserted_held( &myCallback ); + * pin.attach_deasserted_held(myCallback); * } * * @endcode @@ -415,58 +333,33 @@ * Call this function when a pin is deasserted and held. * @param function A C function pointer */ - void attach_deasserted_held(void (*function)(void)) { - _callbackDeassertedHeld.attach( function ); + void attach_deasserted_held(Callback<void()> cb) { + _callbackDeassertedHeld = cb; } - - /** Attach a callback object/method - * - * @code - * - * class Bar { - * public: - * void myCallback( void ) { led3 = 0; } - * }; - * - * DigitalOut led3( LED3 ); - * PinDetect pin( p30 ); - * Bar bar; - * - * main() { - * pin.attach_deasserted_held( &bar, &Bar::myCallback ); - * } - * - * @endcode - * - * Call this function when a pin is deasserted and held. - * @param object An object that conatins the callback method. - * @param method The method within the object to call. - */ - template<typename T> - void attach_deasserted_held(T *object, void (T::*member)(void)) { - _callbackDeassertedHeld.attach( object, member ); - } - + /** operator int() * * Read the value of the pin being sampled. */ - operator int() { return _in->read(); } + operator int() { + return _in->read(); + } -protected: +protected: /** The Ticker periodic callback function */ 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 ) - _callbackAsserted.call(); - else - _callbackDeasserted.call(); + if (currentState == _assertValue) { + if (_callbackAsserted) + _callbackAsserted(); + } else if (_callbackDeasserted) + _callbackDeasserted(); } else { _samplesTillAssert--; @@ -475,15 +368,16 @@ else { _samplesTillAssert = _samplesTillAssertReload; } - - if ( _samplesTillHeld ) { - if ( _prevState == currentState ) { + + if (_samplesTillHeld) { + if (_prevState == currentState) { _samplesTillHeld--; - if ( _samplesTillHeld == 0 ) { - if ( currentState == _assertValue ) - _callbackAssertedHeld.call(); - else - _callbackDeassertedHeld.call(); + if (_samplesTillHeld == 0) { + if (currentState == _assertValue) { + if (_callbackAssertedHeld) + _callbackAssertedHeld(); + } else if (_callbackDeassertedHeld) + _callbackDeassertedHeld(); } } else { @@ -491,11 +385,11 @@ } } } - + }; }; // namespace AjK ends. using namespace AjK; -#endif +#endif \ No newline at end of file