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

Syntax

public class PushButton : IButton

Remarks

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.

Examples

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

using System.Threading;
using Meadow;

namespace PushButtonSample
{
    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...");
        }
    }
}
Example Circuit

Constructors

View Source

PushButton(IDigitalInputPort, Int32)

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

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

Parameters

Type Name Description
IDigitalInputPort interruptPort
System.Int32 debounceDuration
View Source

PushButton(IIODevice, IPin, 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, int debounceDuration = 20)

Parameters

Type Name Description
IIODevice device
IPin inputPin
System.Int32 debounceDuration

Fields

View Source

_buttonPressStart

Maximum DateTime value when the button was just pushed

Declaration
protected DateTime _buttonPressStart

Field Value

Type Description
System.DateTime
View Source

_circuitType

Circuit Termination Type (CommonGround, High or Floating)

Declaration
protected CircuitTerminationType _circuitType

Field Value

Type Description
CircuitTerminationType
View Source

_lastClicked

Minimum DateTime value when the button was pushed

Declaration
protected DateTime _lastClicked

Field Value

Type Description
System.DateTime

Properties

View Source

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

DigitalIn

Returns digital input port.

Declaration
public IDigitalInputPort DigitalIn { get; }

Property Value

Type Description
IDigitalInputPort
View Source

LongPressThreshold

The minimum duration for a long press.

Declaration
public TimeSpan LongPressThreshold { get; set; }

Property Value

Type Description
System.TimeSpan
View Source

State

Returns the current raw state of the switch. If the switch is pressed (connected), returns true, otherwise false.

Declaration
public bool State { get; }

Property Value

Type Description
System.Boolean

Methods

View Source

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()
View Source

RaiseLongPress()

Raised when the button circuit is pressed for at least 500ms.

Declaration
protected virtual void RaiseLongPress()
View Source

RaisePressEnded()

Raised when a press ends (the button is released; circuit is opened).

Declaration
protected virtual void RaisePressEnded()
View Source

RaisePressStarted()

Raised when a press starts (the button is pushed down; circuit is closed).

Declaration
protected virtual void RaisePressStarted()

Events

View Source

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

LongPressClicked

Raised when the button circuit is pressed for at least 500ms.

Declaration
public event EventHandler LongPressClicked

Event Type

Type Description
System.EventHandler
View Source

PressEnded

Raised when a press ends (the button is released; circuit is opened).

Declaration
public event EventHandler PressEnded

Event Type

Type Description
System.EventHandler
View Source

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