Andrew Reed / PinDetect

Dependents:   city1082-capsense-sw2-tft-leds CITY1082-TFT-basic_sw2_ctr 4180_final_project_github mRotaryEncoder_HelloWorld-os ... 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_ASSERTED)
 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 (Callback< void()> cb)
 Attach a callback function.
void attach_deasserted (Callback< void()> cb)
 Attach a callback function.
void attach_asserted_held (Callback< void()> cb)
 Attach a callback function.
void attach_deasserted_held (Callback< void()> cb)
 Attach a callback function.
 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 124 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 174 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 184 of file PinDetect.h.

~PinDetect (  )

PinDetect destructor.

Definition at line 190 of file PinDetect.h.


Member Function Documentation

void attach_asserted ( Callback< void()>  cb )

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 264 of file PinDetect.h.

void attach_asserted_held ( Callback< void()>  cb )

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 312 of file PinDetect.h.

void attach_deasserted ( Callback< void()>  cb )

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 288 of file PinDetect.h.

void attach_deasserted_held ( Callback< void()>  cb )

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 336 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 146 of file PinDetect.h.

void isr ( void   ) [protected]

The Ticker periodic callback function.

Definition at line 351 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 344 of file PinDetect.h.

void setAssertValue ( int  i = PINDETECT_PIN_ASSERTED )

Set the value used as assert.

Defaults to 1 (i.e. if pin == 1 then pin asserted).

Parameters:
intNew assert value (1 or 0)

Definition at line 211 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 199 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 221 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 231 of file PinDetect.h.