Remarks

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

DipSwitch represents a DIP-switch wired in a bus configuration, in which all switches are terminated to the same ground/common or high pin.

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected DipSwitch dipSwitch;

    public MeadowApp()
    {
        Console.WriteLine("Initializing...");

        IDigitalInputPort[] ports =
        {
            Device.CreateDigitalInputPort(Device.Pins.D06, InterruptMode.EdgeRising, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D07, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D08, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D09, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D10, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D11, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D12, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
            Device.CreateDigitalInputPort(Device.Pins.D13, InterruptMode.EdgeFalling, ResistorMode.InternalPullDown),
        };

        dipSwitch = new DipSwitch(ports);
        dipSwitch.Changed += (s,e) =>
        {
            Console.WriteLine("Switch " + e.ItemIndex + " changed to " + (((ISwitch)e.Item).IsOn ? "on" : "off"));
        };

        Console.WriteLine("DipSwitch...");
    }
}

Sample projects available on GitHub

Code Example

protected DipSwitch dipSwitch;

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

    IDigitalInputPort[] ports =
    {
        Device.CreateDigitalInputPort(Device.Pins.D06, InterruptMode.EdgeRising, ResistorMode.InternalPullDown),
    };

    dipSwitch = new DipSwitch(ports);
    dipSwitch.Changed += (s,e) =>
    {
        Console.WriteLine("Switch " + e.ItemIndex + " changed to " + (((ISwitch)e.Item).IsOn ? "on" : "off"));
    };

    Console.WriteLine("DipSwitch...");

    return Task.CompletedTask;
}

Sample project(s) available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Switches.DipSwitch/DipSwitch_Fritzing.svg"

Characteristic Locus
Inheritance System.Object > DipSwitch
Namespace Meadow.Foundation.Sensors.Switches
Assembly Meadow.Foundation.dll

Syntax

public class DipSwitch : object

Constructors

DipSwitch(IDigitalInputController, IPin[], InterruptMode, ResistorMode)

Creates a new DipSwitch connected to the specified switchPins, with the InterruptMode and ResisterMode specified by the type parameters.

Declaration
public DipSwitch(IDigitalInputController device, IPin[] switchPins, InterruptMode interruptMode, ResistorMode resistorMode)

Parameters

Type Name Description
IDigitalInputController device
IPin[] switchPins
InterruptMode interruptMode
ResistorMode resistorMode

DipSwitch(IDigitalInputController, IPin[], InterruptMode, ResistorMode, TimeSpan, TimeSpan)

Creates a new DipSwitch connected to the specified switchPins, with the InterruptMode and ResisterMode specified by the type parameters.

Declaration
public DipSwitch(IDigitalInputController device, IPin[] switchPins, InterruptMode interruptMode, ResistorMode resistorMode, TimeSpan debounceDuration, TimeSpan glitchFilterCycleCount)

Parameters

Type Name Description
IDigitalInputController device
IPin[] switchPins
InterruptMode interruptMode
ResistorMode resistorMode
TimeSpan debounceDuration
TimeSpan glitchFilterCycleCount

DipSwitch(IDigitalInputPort[])

Creates a new DipSwitch connected to an array of Interrupt Ports

Declaration
public DipSwitch(IDigitalInputPort[] interruptPorts)

Parameters

Type Name Description
IDigitalInputPort[] interruptPorts

Properties

Item[Int32]

Returns the ISwitch at the specified index.

Declaration
public ISwitch this[int i] { get; }

Parameters

Type Name Description
System.Int32 i

Property Value

Type Description
ISwitch

Switches

Returns the switch array.

Declaration
public ISwitch[] Switches { get; }

Property Value

Type Description
ISwitch[]

Methods

HandleSwitchChanged(Int32)

Event handler when switch value has been changed

Declaration
protected void HandleSwitchChanged(int switchNumber)

Parameters

Type Name Description
System.Int32 switchNumber

Events

Changed

Raised when one of the switches is switched on or off.

Declaration
public event ArrayEventHandler Changed

Event Type

Type Description
ArrayEventHandler