Remarks
PushButton | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
The PushButton class represents a simple push button, such as a tactile momentary button. To get notified when it’s clicked, subscribe to the Clicked event. If you need to know when the button is held down, subscribe to the PressStarted and PressEnded events.
Code Example
public class MeadowApp : App<F7Micro, MeadowApp>
{
public MeadowApp()
{
PushButton pushButton;
// Initialize by passing a IDigitalInputPort
//IDigitalInputPort digitalInputPort = Device.CreateDigitalInputPort(
// Device.Pins.D08,
// InterruptMode.EdgeBoth,
// ResistorMode.PullUp, 20);
//pushButton = new PushButton(digitalInputPort);
// Initialize by sending Device and Pins
pushButton = new PushButton(
Device,
Device.Pins.D08,
ResistorMode.PullUp
);
pushButton.PressStarted += PushButtonPressStarted;
pushButton.PressEnded += PushButtonPressEnded;
}
void PushButtonPressStarted(object sender, EventArgs e)
{
Console.WriteLine("Press Started...");
}
void PushButtonPressEnded(object sender, EventArgs e)
{
Console.WriteLine("Press Ended...");
}
}
Sample projects available on GitHub
Wiring Example
Characteristic | Locus |
---|---|
Inheritance | System.Object > PushButton |
Implements | System.IDisposable |
Namespace | Meadow.Foundation.Sensors.Buttons |
Assembly | Meadow.Foundation.dll |
Syntax
public class PushButton : IButton, IDisposable
Constructors
PushButton(IDigitalInputPort)
Creates PushButton with a digital input port, especifying if its using an Internal or External PullUp/PullDown resistor.
Declaration
public PushButton(IDigitalInputPort interruptPort)
Parameters
Type | Name | Description |
---|---|---|
IDigitalInputPort | interruptPort |
PushButton(IIODevice, IPin, ResistorMode)
Creates PushButton with a digital input pin connected on a IIOdevice, especifying if its using an Internal or External PullUp/PullDown resistor.
Declaration
public PushButton(IIODevice device, IPin inputPin, ResistorMode resistorMode = null)
Parameters
Type | Name | Description |
---|---|---|
IIODevice | device | |
IPin | inputPin | |
ResistorMode | resistorMode |
Fields
buttonPressStart
Maximum DateTime value when the button was just pushed
Declaration
protected DateTime buttonPressStart
Field Value
Type | Description |
---|---|
System.DateTime |
resistorMode
Resistor Type to indicate if
Declaration
protected ResistorMode resistorMode
Field Value
Type | Description |
---|---|
ResistorMode |
Properties
DebounceDuration
This duration controls the debounce filter. It also has the effect of rate limiting clicks. Decrease this time to allow users to click more quickly.
Declaration
public TimeSpan DebounceDuration { get; set; }
Property Value
Type | Description |
---|---|
System.TimeSpan |
DigitalIn
Returns digital input port.
Declaration
public IDigitalInputPort DigitalIn { get; }
Property Value
Type | Description |
---|---|
IDigitalInputPort |
LongPressThreshold
The minimum duration for a long press.
Declaration
public TimeSpan LongPressThreshold { get; set; }
Property Value
Type | Description |
---|---|
System.TimeSpan |
state
Returns the current raw state of the switch.
Declaration
protected bool state { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
State
Returns the sanitized state of the switch. If the switch is pressed, returns true, otherwise false.
Declaration
public bool State { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Methods
Dispose()
Declaration
public void Dispose()
RaiseClicked()
Raised when the button circuit is re-opened after it has been closed (at the end of a �press�).
Declaration
protected virtual void RaiseClicked()
RaiseLongPress()
Raised when the button circuit is pressed for at least 500ms.
Declaration
protected virtual void RaiseLongPress()
RaisePressEnded()
Raised when a press ends (the button is released; circuit is opened).
Declaration
protected virtual void RaisePressEnded()
RaisePressStarted()
Raised when a press starts (the button is pushed down; circuit is closed).
Declaration
protected virtual void RaisePressStarted()
Events
Clicked
Raised when the button circuit is re-opened after it has been closed (at the end of a �press�.
Declaration
public event EventHandler Clicked
Event Type
Type | Description |
---|---|
System.EventHandler |
LongPressClicked
Raised when the button circuit is pressed for at least 500ms.
Declaration
public event EventHandler LongPressClicked
Event Type
Type | Description |
---|---|
System.EventHandler |
PressEnded
Raised when a press ends (the button is released; circuit is opened).
Declaration
public event EventHandler PressEnded
Event Type
Type | Description |
---|---|
System.EventHandler |
PressStarted
Raised when a press starts (the button is pushed down; circuit is closed).
Declaration
public event EventHandler PressStarted
Event Type
Type | Description |
---|---|
System.EventHandler |