Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ExioBufferedPinDetect Class Reference
PinDetect adds mechanical switch debouncing to DigitialIn and interrupt callbacks. More...
#include <ExioBufferedPinDetect.h>
Public Member Functions | |
| ExioBufferedPinDetect (ExioInBuffer *buffer, int pin) | |
| PinDetect constructor. | |
| ExioBufferedPinDetect (ExioInBuffer *buffer, int pin, PinMode m) | |
| PinDetect constructor. | |
| ~ExioBufferedPinDetect () | |
| 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 (ExioInBuffer *buffer, int pin, 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 136 of file ExioBufferedPinDetect.h.
Constructor & Destructor Documentation
| ExioBufferedPinDetect | ( | ExioInBuffer * | buffer, |
| int | pin | ||
| ) |
PinDetect constructor.
By default the PinMode is set to PullDown.
- See also:
- http://mbed.org/handbook/DigitalIn
- Parameters:
-
p PinName is a valid pin that supports DigitalIn
Definition at line 207 of file ExioBufferedPinDetect.h.
| ExioBufferedPinDetect | ( | ExioInBuffer * | buffer, |
| int | pin, | ||
| PinMode | m | ||
| ) |
PinDetect constructor.
- See also:
- http://mbed.org/handbook/DigitalIn
- Parameters:
-
PinName p is a valid pin that supports DigitalIn PinMode m The mode the DigitalIn should use.
Definition at line 222 of file ExioBufferedPinDetect.h.
PinDetect destructor.
Definition at line 228 of file ExioBufferedPinDetect.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:
-
function A C function pointer
Definition at line 294 of file ExioBufferedPinDetect.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:
-
object An object that conatins the callback method. method The method within the object to call.
Definition at line 322 of file ExioBufferedPinDetect.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:
-
function A C function pointer
Definition at line 398 of file ExioBufferedPinDetect.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:
-
object An object that conatins the callback method. method The method within the object to call.
Definition at line 426 of file ExioBufferedPinDetect.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:
-
function A C function pointer
Definition at line 346 of file ExioBufferedPinDetect.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:
-
object An object that conatins the callback method. method The method within the object to call.
Definition at line 374 of file ExioBufferedPinDetect.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:
-
function A C function pointer
Definition at line 450 of file ExioBufferedPinDetect.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:
-
object An object that conatins the callback method. method The method within the object to call.
Definition at line 478 of file ExioBufferedPinDetect.h.
| void init | ( | ExioInBuffer * | buffer, |
| int | pin, | ||
| PinMode | m | ||
| ) | [protected] |
initialise class
- Parameters:
-
PinName p is a valid pin that supports DigitalIn PinMode m The mode the DigitalIn should use.
Definition at line 175 of file ExioBufferedPinDetect.h.
| void isr | ( | void | ) | [protected] |
The Ticker periodic callback function.
Definition at line 491 of file ExioBufferedPinDetect.h.
| void mode | ( | PinMode | m ) |
Set the pin mode.
- Parameters:
-
PinMode m The mode to pass on to the DigitalIn
Definition at line 272 of file ExioBufferedPinDetect.h.
| operator int | ( | ) |
operator int()
Read the value of the pin being sampled.
Definition at line 486 of file ExioBufferedPinDetect.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:
-
int New assert value (1 or 0)
Definition at line 249 of file ExioBufferedPinDetect.h.
| void setSampleFrequency | ( | int | i = PINDETECT_SAMPLE_PERIOD ) |
Set the sampling time in microseconds.
- Parameters:
-
int The time between pin samples in microseconds.
Definition at line 237 of file ExioBufferedPinDetect.h.
| void setSamplesTillAssert | ( | int | i ) |
Set the number of continuous samples until assert assumed.
Defaults to 1 (1 * sample frequency).
- Parameters:
-
int The number of continuous samples until assert assumed.
Definition at line 257 of file ExioBufferedPinDetect.h.
| void setSamplesTillHeld | ( | int | i ) |
Set the number of continuous samples until held assumed.
Defaults to 50 * sample frequency.
- Parameters:
-
int The number of continuous samples until held assumed.
Definition at line 265 of file ExioBufferedPinDetect.h.
Generated on Sat Jul 16 2022 02:09:56 by
1.7.2