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 Program
{
    public static void Main()
    {
        var pushButton = new PushButton(
            Device.Pins.D08, 
            Meadow.Foundation.CircuitTerminationType.High
        );
        pushButton.PressStarted += PushButtonPressStarted;
        pushButton.PressEnded += PushButtonPressEnded;

        Thread.Sleep(Timeout.Infinite);
    }

    private void PushButtonPressStarted(object sender, EventArgs e)
    {
        Console.WriteLine("Press Started..."); 
    }

    private void PushButtonPressEnded(object sender, EventArgs e)
    {
        Console.WriteLine("Press Ended...");
    }

}

Sample projects available on GitHub

Wiring Example

Characteristic Locus
Inheritance System.Object > PushButton
Namespace Meadow.Foundation.Sensors.Buttons
Assembly Meadow.Foundation.dll

Syntax

public class PushButton : IButton

Constructors

PushButton(IDigitalInputPort, ResistorMode, Int32)

Creates a PushButton on a digital input portespecifying Interrupt Mode, Circuit Type and optionally Debounce filter duration.

Declaration
public PushButton(IDigitalInputPort interruptPort, ResistorMode resistor = null, int debounceDuration = 20)

Parameters

Type Name Description
IDigitalInputPort interruptPort
ResistorMode resistor
System.Int32 debounceDuration

PushButton(IIODevice, IPin, ResistorMode, Int32)

Creates PushButto a digital input port connected on a IIOdevice, especifying Interrupt Mode, Circuit Type and optionally Debounce filter duration.

Declaration
public PushButton(IIODevice device, IPin inputPin, ResistorMode resistor = null, int debounceDuration = 20)

Parameters

Type Name Description
IIODevice device
IPin inputPin
ResistorMode resistor
System.Int32 debounceDuration

Fields

_buttonPressStart

Maximum DateTime value when the button was just pushed

Declaration
protected DateTime _buttonPressStart

Field Value

Type Description
System.DateTime

_circuitType

Circuit Termination Type (CommonGround, High or Floating)

Declaration
protected CircuitTerminationType _circuitType

Field Value

Type Description
CircuitTerminationType

_lastClicked

Minimum DateTime value when the button was pushed

Declaration
protected DateTime _lastClicked

Field Value

Type Description
System.DateTime

Properties

_state

Returns the current raw state of the switch.

Declaration
protected bool _state { get; }

Property Value

Type Description
System.Boolean

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

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