Remarks

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

An LED is a diode (a component that attempts to let current flow only one way through it) that emits light when voltage is applied to it. Because it is a diode, it only works when a voltage is applied in one direction.

LEDs commonly have one lead longer (the Cathode) than the other (the Anode). The longer cathode leg indicates that it should be connected to the positive (+) side of a circuit.

Code Example

LedBarGraph ledBarGraph;

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

    // Using an array of Pins 
    IPin[] pins =
    {
         Device.Pins.D11,
         Device.Pins.D10,
         Device.Pins.D09,
         Device.Pins.D08,
         Device.Pins.D07,
         Device.Pins.D06,
         Device.Pins.D05,
         Device.Pins.D04,
         Device.Pins.D03,
         Device.Pins.D02
    };

    ledBarGraph = new LedBarGraph(Device, pins);

    return Task.CompletedTask;
}

public override async Task Run()
{
    Console.WriteLine("TestLedBarGraph...");

    float percentage = 0;

    while (true)
    {
        Console.WriteLine("Turning them on and off for 200ms using SetLed...");
        for (int i = 0; i < ledBarGraph.Count; i++)
        {
            ledBarGraph.SetLed(i, true);
            await Task.Delay(100);
            ledBarGraph.SetLed(i, false);
        }

        await Task.Delay(1000);

        Console.WriteLine("Turning them on using Percentage...");
        while (percentage < 1)
        {
            percentage += 0.10f;
            Console.WriteLine($"{percentage}");
            ledBarGraph.Percentage = Math.Min(1.0f, percentage);
            await Task.Delay(100);
        }

        await Task.Delay(1000);

        Console.WriteLine("Turning them off using Percentage...");
        while (percentage > 0)
        {
            percentage -= 0.10f;
            Console.WriteLine($"{percentage}");
            ledBarGraph.Percentage = Math.Max(0.0f, percentage);
            await Task.Delay(100);
        }

        await Task.Delay(1000);

        Console.WriteLine("Charging animation...");
        while (percentage < 1)
        {
            percentage += 0.10f;
            Console.WriteLine($"{percentage}");
            ledBarGraph.Percentage = Math.Min(1.0f, percentage);
            ledBarGraph.StartBlink(ledBarGraph.GetTopLedForPercentage());
            await Task.Delay(2000);
        }

        await Task.Delay(1000);

        Console.WriteLine("Discharging animation...");
        while (percentage > 0)
        {
            percentage -= 0.10f;
            Console.WriteLine($"{percentage}");
            ledBarGraph.Percentage = Math.Max(0.0f, percentage);
            ledBarGraph.StartBlink(ledBarGraph.GetTopLedForPercentage());
            await Task.Delay(2000);
        }

        await Task.Delay(1000);

        Console.WriteLine("Blinking for 5 seconds at 500ms on/off...");
        ledBarGraph.StartBlink();
        await Task.Delay(5000);
        ledBarGraph.Stop();

        await Task.Delay(1000);

        Console.WriteLine("Blinking for 5 seconds at 200ms on/off...");
        ledBarGraph.StartBlink(TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(200));
        await Task.Delay(5000);
        ledBarGraph.Stop();

        await Task.Delay(1000);
    }
}

Sample project(s) available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Leds.Led/Led_Fritzing.svg"

Characteristic Locus
Inheritance System.Object > Led
Namespace Meadow.Foundation.Leds
Assembly Meadow.Foundation.dll

Syntax

public class Led : ILed

Constructors

Led(IDigitalOutputController, IPin)

Creates a LED through a pin directly from the Digital IO of the board

Declaration
public Led(IDigitalOutputController device, IPin pin)

Parameters

Type Name Description
IDigitalOutputController device

IDigitalOutputController to instantiate output port

IPin pin

Led(IDigitalOutputPort)

Creates a LED through a DigitalOutPutPort from an IO Expander

Declaration
public Led(IDigitalOutputPort port)

Parameters

Type Name Description
IDigitalOutputPort port

Properties

IsOn

Gets or sets a value indicating whether this Led is on.

Declaration
public bool IsOn { get; set; }

Property Value

Type Description
System.Boolean

true if is on; otherwise, false.

Port

Gets the port that is driving the LED

Declaration
protected IDigitalOutputPort Port { get; set; }

Property Value

Type Description
IDigitalOutputPort

The port

Methods

Blink animation that turns the LED on (500ms) and off (500ms)

Declaration
public void StartBlink()

Blink animation that turns the LED on and off based on the OnDuration and offDuration values in ms

Declaration
public void StartBlink(TimeSpan onDuration, TimeSpan offDuration)

Parameters

Type Name Description
TimeSpan onDuration
TimeSpan offDuration

StartBlinkAsync(TimeSpan, TimeSpan, CancellationToken)

Set LED to blink

Declaration
protected Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration, CancellationToken cancellationToken)

Parameters

Type Name Description
TimeSpan onDuration

on duration in ms

TimeSpan offDuration

off duration in ms

CancellationToken cancellationToken

cancellation token used to cancel blink

Returns

Type Description
Task

Stop()

Stops the LED when its blinking and/or turns it off.

Declaration
public void Stop()