InterruptIn style DigitalIn debounced with callbacks for pin state change and pin state hold.

Dependents:   AVC_20110423 SimplePIDBot Pushbutton_Debounce_Interrupt FinalProgram ... more

Embed: (wiki syntax)

« Back to documentation index

PinDetect Class Reference

PinDetect adds mechanical switch debouncing to DigitialIn and interrupt callbacks. More...

#include <PinDetect.h>

Public Member Functions

 PinDetect (PinName p)
 PinDetect constructor.
 PinDetect (PinName p, PinMode m)
 PinDetect constructor.
 ~PinDetect ()
 PinDetect destructor.
void setSampleFrequency (int i=PINDETECT_SAMPLE_PERIOD)
 Set the sampling time in microseconds.
void setAssertValue (int i=PINDETECT_PIN_ASSTERED)
 Set the value used as assert.
void setSamplesTillAssert (int i)
 Set the number of continuous samples until assert assumed.
void setSamplesTillHeld (int i)
 Set the number of continuous samples until held assumed.
void mode (PinMode m)
 Set the pin mode.
void attach_asserted (void(*function)(void))
 Attach a callback function.
template<typename T >
void attach_asserted (T *object, void(T::*member)(void))
 Attach a callback object/method.
void attach_deasserted (void(*function)(void))
 Attach a callback function.
template<typename T >
void attach_deasserted (T *object, void(T::*member)(void))
 Attach a callback object/method.
void attach_asserted_held (void(*function)(void))
 Attach a callback function.
template<typename T >
void attach_asserted_held (T *object, void(T::*member)(void))
 Attach a callback object/method.
void attach_deasserted_held (void(*function)(void))
 Attach a callback function.
template<typename T >
void attach_deasserted_held (T *object, void(T::*member)(void))
 Attach a callback object/method.
 operator int ()
 operator int()

Protected Member Functions

void init (PinName p, PinMode m)
 initialise class
void isr (void)
 The Ticker periodic callback function.

Detailed Description

PinDetect adds mechanical switch debouncing to DigitialIn and interrupt callbacks.

This is done by sampling the specified pin at regular intervals and detecting any change of state ( 0 -> 1 or 1 -> 0 ). When a state change is detected the attached callback handler is called. Additionally, if the pin stays in the same state after a state change for a defined period of time, an extra callback is made allowing a program to detect when a "key is pressed and held down" rather than a momentary key/switch press.

All parameters are customisable which include:-

  • The sampling frequency.
  • The number of continuous samples until a state change is detected.
  • The number of continuous samples until a key is assumed held after a state change.
  • The logic level which is assumed to be asserted (0volts or +volts).

Only callbacks that have been attached will be called by the library.

Example:

 #include "mbed.h"
 #include "PinDetect.h"

 PinDetect  pin( p30 );
 DigitialOut led1( LED1 );
 DigitialOut led2( LED2 );
 DigitialOut led3( LED3 );
 DigitialOut led4( LED4 );

 void keyPressed( void ) {
     led2 = 1;
     led3 = 0;
     led4 = 0;
 }

 void keyReleased( void ) {
     led2 = 0;
     led3 = 0;
     led4 = 0;
 }

 void keyPressedHeld( void ) {
     led3 = 1;
 }

 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 );

     // 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(); // Defaults to 20ms.

     while( 1 ) {
         led1 = !led1;
         wait( 0.2 );
     }
 }

This example will flash led1 in a similar to a standard starting program.

Applying a "1" (switch on) to pin 30 will switch on led2, removing the "1" to "0" (switch off) led2 goes out. Holding the "switch" at one for one second will switch on led3. An unasserted P30 (switched off) will, after one second illuminate led4 when the deasserted calledback is called.

The above is a very basic introduction. For more details:-

See also:
example.h

Definition at line 130 of file PinDetect.h.


Constructor & Destructor Documentation

PinDetect ( PinName  p )

PinDetect constructor.

By default the PinMode is set to PullDown.

See also:
http://mbed.org/handbook/DigitalIn
Parameters:
pPinName is a valid pin that supports DigitalIn

Definition at line 180 of file PinDetect.h.

PinDetect ( PinName  p,
PinMode  m 
)

PinDetect constructor.

See also:
http://mbed.org/handbook/DigitalIn
Parameters:
PinNamep is a valid pin that supports DigitalIn
PinModem The mode the DigitalIn should use.

Definition at line 190 of file PinDetect.h.

~PinDetect (  )

PinDetect destructor.

Definition at line 196 of file PinDetect.h.


Member Function Documentation

void attach_asserted ( void(*)(void)  function )

Attach a callback function.

 DigitalOut led1( LED1 );
 PinDetect pin( p30 );

 void myCallback( void ) {
   led1 = 1;
 };
 
 main() {
     pin.attach_asserted( &myCallback );
 }

Call this function when a pin is asserted.

Parameters:
functionA C function pointer

Definition at line 262 of file PinDetect.h.

void attach_asserted ( T *  object,
void(T::*)(void)  member 
)

Attach a callback object/method.

 class Bar {
   public:
     void myCallback( void ) { led1 = 1; }
 };

 DigitalOut led1( LED1 );
 PinDetect pin( p30 );
 Bar bar;

 main() {
     pin.attach_asserted( &bar, &Bar::myCallback );
 }

Call this function when a pin is asserted.

Parameters:
objectAn object that conatins the callback method.
methodThe method within the object to call.

Definition at line 290 of file PinDetect.h.

void attach_asserted_held ( void(*)(void)  function )

Attach a callback function.

 DigitalOut led2( LED2 );
 PinDetect pin( p30 );

 void myCallback( void ) {
   led2 = 1;
 };

 main() {
     pin.attach_asserted_held( &myCallback );
 }

Call this function when a pin is asserted and held.

Parameters:
functionA C function pointer

Definition at line 366 of file PinDetect.h.

void attach_asserted_held ( T *  object,
void(T::*)(void)  member 
)

Attach a callback object/method.

 class Bar {
   public:
     void myCallback( void ) { led2 = 0; }
 };

 DigitalOut led2( LED2 );
 PinDetect pin( p30 );
 Bar bar;
 
 main() {
     pin.attach_asserted_held( &bar, &Bar::myCallback );
 }

Call this function when a pin is asserted and held.

Parameters:
objectAn object that conatins the callback method.
methodThe method within the object to call.

Definition at line 394 of file PinDetect.h.

void attach_deasserted ( void(*)(void)  function )

Attach a callback function.

 DigitalOut led1( LED1 );
 PinDetect pin( p30 );

 void myCallback( void ) {
   led1 = 0;
 };

 main() {
     pin.attach_deasserted( &myCallback );
 }

Call this function when a pin is deasserted.

Parameters:
functionA C function pointer

Definition at line 314 of file PinDetect.h.

void attach_deasserted ( T *  object,
void(T::*)(void)  member 
)

Attach a callback object/method.

 class Bar {
   public:
     void myCallback( void ) { led1 = 0; }
 };

 DigitalOut led1( LED1 );
 PinDetect pin( p30 );
 Bar bar;
 
 main() {
     pin.attach_deasserted( &bar, &Bar::myCallback );
 }

Call this function when a pin is deasserted.

Parameters:
objectAn object that conatins the callback method.
methodThe method within the object to call.

Definition at line 342 of file PinDetect.h.

void attach_deasserted_held ( void(*)(void)  function )

Attach a callback function.

 DigitalOut led3( LED3 );
 PinDetect pin( p30 );

 void myCallback( void ) {
   led3 = 1;
 };

 main() {
     pin.attach_deasserted_held( &myCallback );
 }

Call this function when a pin is deasserted and held.

Parameters:
functionA C function pointer

Definition at line 418 of file PinDetect.h.

void attach_deasserted_held ( T *  object,
void(T::*)(void)  member 
)

Attach a callback object/method.

 class Bar {
   public:
     void myCallback( void ) { led3 = 0; }
 };

 DigitalOut led3( LED3 );
 PinDetect pin( p30 );
 Bar bar;
 
 main() {
     pin.attach_deasserted_held( &bar, &Bar::myCallback );
 }

Call this function when a pin is deasserted and held.

Parameters:
objectAn object that conatins the callback method.
methodThe method within the object to call.

Definition at line 446 of file PinDetect.h.

void init ( PinName  p,
PinMode  m 
) [protected]

initialise class

Parameters:
PinNamep is a valid pin that supports DigitalIn
PinModem The mode the DigitalIn should use.

Definition at line 153 of file PinDetect.h.

void isr ( void   ) [protected]

The Ticker periodic callback function.

Definition at line 459 of file PinDetect.h.

void mode ( PinMode  m )

Set the pin mode.

See also:
http://mbed.org/projects/libraries/api/mbed/trunk/DigitalInOut#DigitalInOut.mode
Parameters:
PinModem The mode to pass on to the DigitalIn

Definition at line 240 of file PinDetect.h.

operator int (  )

operator int()

Read the value of the pin being sampled.

Definition at line 454 of file PinDetect.h.

void setAssertValue ( int  i = PINDETECT_PIN_ASSTERED )

Set the value used as assert.

Defaults to 1 (ie if pin == 1 then pin asserted).

Parameters:
intNew assert value (1 or 0)

Definition at line 217 of file PinDetect.h.

void setSampleFrequency ( int  i = PINDETECT_SAMPLE_PERIOD )

Set the sampling time in microseconds.

Parameters:
intThe time between pin samples in microseconds.

Definition at line 205 of file PinDetect.h.

void setSamplesTillAssert ( int  i )

Set the number of continuous samples until assert assumed.

Defaults to 1 (1 * sample frequency).

Parameters:
intThe number of continuous samples until assert assumed.

Definition at line 225 of file PinDetect.h.

void setSamplesTillHeld ( int  i )

Set the number of continuous samples until held assumed.

Defaults to 50 * sample frequency.

Parameters:
intThe number of continuous samples until held assumed.

Definition at line 233 of file PinDetect.h.