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

_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

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

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