Skip to main content

Meadow.Foundation.Sensors.Switches.SpstSwitch

SpstSwitch
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet 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()
{
Resolver.Log.Info("Initializing...");

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

Resolver.Log.Info("SpstSwitch ready...");

return Task.CompletedTask;
}

Sample project(s) available on GitHub

Wiring Example

Class SpstSwitch

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

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

Assembly: Meadow.Foundation.dll
View Source
Declaration
public class SpstSwitch : ISwitch, ISensor<bool>, ISensor

Implements:
Meadow.Peripherals.Switches.ISwitch, Meadow.Peripherals.Sensors.ISensor<System.Boolean>, Meadow.Peripherals.Sensors.ISensor

Properties

IsOn

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

View Source
Declaration
public bool IsOn { get; protected set; }

DigitalInputPort

Returns the DigitalInputPort.

View Source
Declaration
protected IDigitalInterruptPort DigitalInputPort { get; set; }

IsDisposed

Is the object disposed

View Source
Declaration
public bool IsDisposed { get; }

Methods

DigitalInChanged(object, DigitalPortResult)

Event handler when switch value has been changed

View Source
Declaration
protected void DigitalInChanged(object sender, DigitalPortResult e)
Parameters
TypeName
System.Objectsender
Meadow.Hardware.DigitalPortResulte

Read()

Convenience method to get the current sensor reading

View Source
Declaration
public Task<bool> Read()
Returns

System.Threading.Tasks.Task<System.Boolean>

Dispose()

View Source
Declaration
public void Dispose()

Dispose(bool)

Dispose of the object

View Source
Declaration
protected virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Events

Changed

Raised when the switch circuit is opened or closed.

View Source
Declaration
public event EventHandler Changed
Event Type

System.EventHandler

Implements

  • Meadow.Peripherals.Switches.ISwitch
  • Meadow.Peripherals.Sensors.ISensor<System.Boolean>
  • Meadow.Peripherals.Sensors.ISensor