Skip to main content

Meadow.Foundation.Sensors.Switches.SpdtSwitch

SpdtSwitch
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet Gallery for Meadow.Foundation

SpdtSwitch represents a simple, two position, Single-Pole-Dual-Throw (SPDT) switch that closes a circuit to either ground/common or high depending on position.

The following example shows how to use a SPDT switch:

public class MeadowApp : App<F7Micro, MeadowApp>
{
DigitalOutputPort _blueLED;
SpdtSwitch _spdtSwitch;

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

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

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

Sample projects available on GitHub

Code Example

protected SpdtSwitch spdtSwitch;

public override Task Initialize()
{
Resolver.Log.Info("Initializing...");

spdtSwitch = new SpdtSwitch(Device.CreateDigitalInterruptPort(Device.Pins.D15, InterruptMode.EdgeBoth, ResistorMode.InternalPullDown));
spdtSwitch.Changed += (s, e) =>
{
Resolver.Log.Info(spdtSwitch.IsOn ? "Switch is on" : "Switch is off");
};

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

return Task.CompletedTask;
}

Sample project(s) available on GitHub

Wiring Example

Class SpdtSwitch

Represents a simple, two position, Single-Pole-Dual-Throw (SPDT) switch that closes a circuit to either ground/common or high depending on position

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

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

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

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

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
  • System.IDisposable