Remarks

SpstSwitch
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

SpstSwitch represents a simple, on/off, Single-Pole-Single-Throw (SPST) switch that closes a circuit to either ground/common or high:

Use the CircuitTerminationType to specify whether the other side of the switch terminates to ground or high.

The following example shows how to use a SPST switch:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    DigitalOutputPort _blueLED;
    SpstSwitch _spstSwitch;

    public MeadowApp()
    {
        _blueLED = new DigitalOutputPort(Device.Pins.OnboardLEDBlue, true);

        _spstSwitch = new SpstSwitch(Device.Pins.D13, CircuitTerminationType.High);
        _spstSwitch.Changed += (s, e) =>
        {
            Console.WriteLine("Switch Changed");
            Console.WriteLine("Switch on: " + _spstSwitch.IsOn.ToString());
        };

        Console.WriteLine("Initial switch state, isOn: " + _spstSwitch.IsOn.ToString());
    }
}

Sample projects available on GitHub

Code Example

protected SpstSwitch spstSwitch;

public override Task Initialize()
{
    Console.WriteLine("Initializing...");

    spstSwitch = new SpstSwitch(Device.CreateDigitalInputPort(Device.Pins.D02, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown));
    spstSwitch.Changed += (s,e) => 
    {
        Console.WriteLine("Switch Changed");
        Console.WriteLine($"Switch on: {spstSwitch.IsOn}");
    };

    Console.WriteLine("SpstSwitch ready...");

    return Task.CompletedTask;
}

Sample project(s) available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Switches.SpstSwitch/SpstSwitch_Fritzing.svg"

Characteristic Locus
Inheritance System.Object > SpstSwitch
Implements ISensor
Namespace Meadow.Foundation.Sensors.Switches
Assembly Meadow.Foundation.dll

Syntax

public class SpstSwitch : ISwitch, ISensor

Constructors

SpstSwitch(IDigitalInputController, IPin, InterruptMode, ResistorMode)

Instantiates a new SpstSwitch object connected to the specified digital pin, and with the specified CircuitTerminationType in the type parameter.

Declaration
public SpstSwitch(IDigitalInputController device, IPin pin, InterruptMode interruptMode, ResistorMode resistorMode)

Parameters

Type Name Description
IDigitalInputController device
IPin pin
InterruptMode interruptMode
ResistorMode resistorMode

SpstSwitch(IDigitalInputController, IPin, InterruptMode, ResistorMode, TimeSpan, TimeSpan)

Instantiates a new SpstSwitch object connected to the specified digital pin, and with the specified CircuitTerminationType in the type parameter.

Declaration
public SpstSwitch(IDigitalInputController device, IPin pin, InterruptMode interruptMode, ResistorMode resistorMode, TimeSpan debounceDuration, TimeSpan glitchFilterCycleCount)

Parameters

Type Name Description
IDigitalInputController device
IPin pin
InterruptMode interruptMode
ResistorMode resistorMode
TimeSpan debounceDuration
TimeSpan glitchFilterCycleCount

SpstSwitch(IDigitalInputPort)

Creates a SpstSwitch on a especified interrupt port

Declaration
public SpstSwitch(IDigitalInputPort interruptPort)

Parameters

Type Name Description
IDigitalInputPort interruptPort

Properties

DigitalIn

Returns the DigitalInputPort.

Declaration
protected IDigitalInputPort DigitalIn { get; set; }

Property Value

Type Description
IDigitalInputPort

IsOn

Describes whether or not the switch circuit is closed/connected (IsOn = true), or open (IsOn = false).

Declaration
public bool IsOn { get; protected set; }

Property Value

Type Description
System.Boolean

Methods

DigitalInChanged(Object, DigitalPortResult)

Event handler when switch value has been changed

Declaration
protected void DigitalInChanged(object sender, DigitalPortResult e)

Parameters

Type Name Description
System.Object sender
DigitalPortResult e

Events

Changed

Raised when the switch circuit is opened or closed.

Declaration
public event EventHandler Changed

Event Type

Type Description
EventHandler