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.InternalPullUp, 20);
//pushButton = new PushButton(digitalInputPort);
// Initialize by sending Device and Pins
pushButton = new PushButton(
Device,
Device.Pins.D08,
ResistorMode.InternalPullUp
);
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
<img src="../../API_Assets/Meadow.Foundation.Sensors.Buttons.PushButton/PushButton_Fritzing.svg"
Characteristic | Locus |
---|---|
Inheritance | System.Object > PushButton |
Implements | IDisposable |
Namespace | Meadow.Foundation.Sensors.Buttons |
Assembly | Meadow.Foundation.dll |
Syntax
public class PushButton : IButton, IDisposable
Constructors
PushButton(IDigitalInputPort)
Creates PushButton with a pre-configured input port
Declaration
public PushButton(IDigitalInputPort inputPort)
Parameters
Type | Name | Description |
---|---|---|
IDigitalInputPort | inputPort |
PushButton(IPin, ResistorMode)
Creates PushButton with a digital input pin connected on a IIOdevice, specifying if its using an Internal or External PullUp/PullDown resistor.
Declaration
public PushButton(IPin inputPin, ResistorMode resistorMode = null)
Parameters
Type | Name | Description |
---|---|---|
IPin | inputPin | The pin used to create the button port |
ResistorMode | resistorMode | The resistor mode |
Fields
buttonPressStart
The date/time when the last button press occurred and the button hasn't been released
Declaration
protected DateTime buttonPressStart
Field Value
Type | Description |
---|---|
DateTime |
ctsPolling
Cancellation token source to disable button polling on dispose
Declaration
protected CancellationTokenSource? ctsPolling
Field Value
Type | Description |
---|---|
System.Nullable<CancellationTokenSource> |
DefaultDebounceDuration
Default Debounce used on the PushButton Input if an InputPort is auto-created
Declaration
public static readonly TimeSpan DefaultDebounceDuration
Field Value
Type | Description |
---|---|
TimeSpan |
DefaultGlitchDuration
Default Glitch Filter used on the PushButton Input if an InputPort is auto-created
Declaration
public static readonly TimeSpan DefaultGlitchDuration
Field Value
Type | Description |
---|---|
TimeSpan |
DefaultLongPressThreshold
Default threshold for LongPress events
Declaration
public static readonly TimeSpan DefaultLongPressThreshold
Field Value
Type | Description |
---|---|
TimeSpan |
shouldDisposeInput
Track if we created the input port in the PushButton instance (true) or was it passed in via the ctor (false)
Declaration
protected bool shouldDisposeInput
Field Value
Type | Description |
---|---|
System.Boolean |
Properties
ButtonPollingInterval
The button state polling interval for PushButton instances that are created from a port that doesn't have an tnterrupt mode of EdgeBoth - otherwise ignored
Declaration
public TimeSpan ButtonPollingInterval { get; set; }
Property Value
Type | Description |
---|---|
TimeSpan |
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 |
---|---|
TimeSpan |
DigitalIn
Returns digital input port
Declaration
protected IDigitalInputPort DigitalIn { get; set; }
Property Value
Type | Description |
---|---|
IDigitalInputPort |
IsPolling
The PushButton was created with an input port without interrupts If true, the object is polling to update state (may impact performance)
Declaration
public bool IsPolling { get; protected set; }
Property Value
Type | Description |
---|---|
System.Boolean |
LongClickedThreshold
The minimum duration for a long press
Declaration
public TimeSpan LongClickedThreshold { get; set; }
Property Value
Type | Description |
---|---|
TimeSpan |
resistorMode
The button port resistor mode
Declaration
protected ResistorMode resistorMode { get; }
Property Value
Type | Description |
---|---|
ResistorMode |
State
Returns the sanitized state of the button If pressed, return true, otherwise false
Declaration
public bool State { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Methods
CreateInputPort(IPin, ResistorMode)
Create a digital input port for a pin This will dynamically set the interupt mode based on the pin capabilities
Declaration
protected static IDigitalInputPort CreateInputPort(IPin inputPin, ResistorMode resistorMode = null)
Parameters
Type | Name | Description |
---|---|---|
IPin | inputPin | |
ResistorMode | resistorMode |
Returns
Type | Description |
---|---|
IDigitalInputPort |
Dispose()
Disposes the Digital Input resources
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()
RaiseLongClicked()
Raised when the button circuit is pressed for at least 500ms.
Declaration
protected virtual void RaiseLongClicked()
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()
Read()
Convenience method to get the current sensor reading
Declaration
public Task<bool> Read()
Returns
Type | Description |
---|---|
Task<System.Boolean> |
Events
Clicked
Raised when the button is released after a press
Declaration
public event EventHandler Clicked
Event Type
Type | Description |
---|---|
EventHandler |
LongClicked
Raised when the button is pressed for LongClickedThreshold or longer and then releases
Declaration
public event EventHandler LongClicked
Event Type
Type | Description |
---|---|
EventHandler |
PressEnded
Raised when a press ends
Declaration
public event EventHandler PressEnded
Event Type
Type | Description |
---|---|
EventHandler |
PressStarted
Raised when a press starts
Declaration
public event EventHandler PressStarted
Event Type
Type | Description |
---|---|
EventHandler |