Remarks

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

RgbLed represents an RGB LED whose color is controlled by three digital output ports. These diodes consist of four legs - one for each of the colors mentioned and one for a common cathode (ground) or common anode (vcc), which is also the longest one.

To connect these deds to Meadow, it is recommended to use an external resistor of ~270 to 1K ohms to prevent too much current from flowing through the led and causing damage.

Circuit of a common anode RGB LED

Circuit of a common cathode RGB LED

Code Example

protected List<RgbLed> rgbLeds;

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

    var onRgbLed = new RgbLed(
        device: Device,
        redPin: Device.Pins.OnboardLedRed,
        greenPin: Device.Pins.OnboardLedGreen,
        bluePin: Device.Pins.OnboardLedBlue);
    onRgbLed.SetColor(RgbLedColors.Red);

    rgbLeds = new List<RgbLed>
    {
        new RgbLed(
            Device.CreateDigitalOutputPort(Device.Pins.D02),
            Device.CreateDigitalOutputPort(Device.Pins.D03),
            Device.CreateDigitalOutputPort(Device.Pins.D04)),
        new RgbLed(
            Device.CreateDigitalOutputPort(Device.Pins.D05),
            Device.CreateDigitalOutputPort(Device.Pins.D06),
            Device.CreateDigitalOutputPort(Device.Pins.D07)),
        new RgbLed(
            Device.CreateDigitalOutputPort(Device.Pins.D08),
            Device.CreateDigitalOutputPort(Device.Pins.D09),
            Device.CreateDigitalOutputPort(Device.Pins.D10)),
        new RgbLed(
            Device.CreateDigitalOutputPort(Device.Pins.D11),
            Device.CreateDigitalOutputPort(Device.Pins.D12),
            Device.CreateDigitalOutputPort(Device.Pins.D13))
    };

    onRgbLed.SetColor(RgbLedColors.Green);

    return Task.CompletedTask;
}

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

    while (true)
    {
        Console.WriteLine("Going through each color on each RGB LED...");
        foreach (var rgbLed in rgbLeds)
        {
            for (int i = 0; i < (int)RgbLedColors.count; i++)
            {
                rgbLed.SetColor((RgbLedColors)i);
                await Task.Delay(500);
            }
        }

        await Task.Delay(1000);

        Console.WriteLine("Blinking through each color on each RGB LED...");
        foreach (var rgbLed in rgbLeds)
        {
            for (int i = 0; i < (int)RgbLedColors.count; i++)
            {
                rgbLed.StartBlink((RgbLedColors)i);
                await Task.Delay(3000);
            }
        }

        await Task.Delay(1000);

        Console.WriteLine("Blinking through each color on each RGB LED...");
        foreach (var rgbLed in rgbLeds)
        {
            for (int i = 0; i < (int)RgbLedColors.count; i++)
            {
                rgbLed.StartBlink((RgbLedColors)i, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
                await Task.Delay(3000);
            }
        }

        await Task.Delay(1000);
    }
}

Sample project(s) available on GitHub

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

Syntax

public class RgbLed : IRgbLed

Constructors

RgbLed(IDigitalOutputController, IPin, IPin, IPin, CommonType)

Initializes a new instance of the RgbLed class.

Declaration
public RgbLed(IDigitalOutputController device, IPin redPin, IPin greenPin, IPin bluePin, CommonType commonType = null)

Parameters

Type Name Description
IDigitalOutputController device

IO Device

IPin redPin

Red Pin

IPin greenPin

Green Pin

IPin bluePin

Blue Pin

CommonType commonType

Is Common Cathode

RgbLed(IDigitalOutputPort, IDigitalOutputPort, IDigitalOutputPort, CommonType)

Initializes a new instance of the RgbLed class.

Declaration
public RgbLed(IDigitalOutputPort redPort, IDigitalOutputPort greenPort, IDigitalOutputPort bluePort, CommonType commonType = null)

Parameters

Type Name Description
IDigitalOutputPort redPort

Red Port

IDigitalOutputPort greenPort

Green Port

IDigitalOutputPort bluePort

Blue Port

CommonType commonType

Is Common Cathode

Properties

BluePort

Get the blue LED port

Declaration
protected IDigitalOutputPort BluePort { get; set; }

Property Value

Type Description
IDigitalOutputPort

Color

Get the color the LED has been set to.

Declaration
public RgbLedColors Color { get; protected set; }

Property Value

Type Description
RgbLedColors

Common

Is the LED using a common cathode

Declaration
public CommonType Common { get; protected set; }

Property Value

Type Description
CommonType

GreenPort

Get the green LED port

Declaration
protected IDigitalOutputPort GreenPort { get; set; }

Property Value

Type Description
IDigitalOutputPort

IsOn

Turns on LED with current color or turns it off

Declaration
public bool IsOn { get; set; }

Property Value

Type Description
System.Boolean

RedPort

Get the red LED port

Declaration
protected IDigitalOutputPort RedPort { get; set; }

Property Value

Type Description
IDigitalOutputPort

Methods

SetColor(RgbLedColors)

Sets the current color of the LED.

Declaration
public void SetColor(RgbLedColors color)

Parameters

Type Name Description
RgbLedColors color

Starts the blink animation LED turning it on (500) and off (500)

Declaration
public void StartBlink(RgbLedColors color)

Parameters

Type Name Description
RgbLedColors color

Starts the blink animation with the specified on and off duration.

Declaration
public void StartBlink(RgbLedColors color, TimeSpan onDuration, TimeSpan offDuration)

Parameters

Type Name Description
RgbLedColors color
TimeSpan onDuration
TimeSpan offDuration

StartBlinkAsync(RgbLedColors, TimeSpan, TimeSpan, CancellationToken)

Turn the LED on and off (blink)

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

Parameters

Type Name Description
RgbLedColors color
TimeSpan onDuration
TimeSpan offDuration
CancellationToken cancellationToken

Returns

Type Description
Task

Stop()

Stops any running animations.

Declaration
public void Stop()