Remarks

RgbLed
Status
Source code GitHub
NuGet package

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

The following example code loops through all the colors possible with digital output ports only.

public class RgbLedApp : App<F7Micro, RgbLedApp>
{
    public RgbLedApp()
    {
        // create a new common cathode RgbLed (otherwise set IsCommonCathode = false)
        var rgbLed = new RgbLed(
            Device.CreateDigitalOutputPort(Device.Pins.D14),
            Device.CreateDigitalOutputPort(Device.Pins.D13),
            Device.CreateDigitalOutputPort(Device.Pins.D12));

        // alternate between blinking and pulsing the LED 
        while (true)
        {
            for (int i = 0; i < (int)RgbLed.Colors.count; i++)
            {
                rgbLed.SetColor((RgbLed.Colors)i);
                Thread.Sleep(500);
            }

            for (int i = 0; i < (int)RgbLed.Colors.count; i++)
            {
                rgbLed.StartBlink((RgbLed.Colors)i);
                Thread.Sleep(3000);
            }
        }
    }
}

Sample projects available on GitHub

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

Syntax

public class RgbLed : IRgbLed

Constructors

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

RgbLed(IIODevice, IPin, IPin, IPin, CommonType)

Initializes a new instance of the RgbLed class.

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

Parameters

Type Name Description
IIODevice device

IO Device

IPin redPin

Red Pin

IPin greenPin

Green Pin

IPin bluePin

Blue Pin

CommonType commonType

Is Common Cathode

Fields

animationTask

Declaration
protected Task animationTask

Field Value

Type Description
System.Threading.Tasks.Task

cancellationTokenSource

Declaration
protected CancellationTokenSource cancellationTokenSource

Field Value

Type Description
System.Threading.CancellationTokenSource

isOn

Declaration
protected bool isOn

Field Value

Type Description
System.Boolean

Properties

BluePort

Get the blue LED port

Declaration
public IDigitalOutputPort BluePort { get; protected set; }

Property Value

Type Description
IDigitalOutputPort

Color

Get the color the LED has been set to.

Declaration
public RgbLed.Colors Color { get; protected set; }

Property Value

Type Description
RgbLed.Colors

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
public IDigitalOutputPort GreenPort { get; protected 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
public IDigitalOutputPort RedPort { get; protected set; }

Property Value

Type Description
IDigitalOutputPort

Methods

SetColor(RgbLed.Colors)

Sets the current color of the LED.

Declaration
public void SetColor(RgbLed.Colors color)

Parameters

Type Name Description
RgbLed.Colors color

Starts the blink animation.

Declaration
public void StartBlink(RgbLed.Colors color, uint onDuration = 200U, uint offDuration = 200U)

Parameters

Type Name Description
RgbLed.Colors color
System.UInt32 onDuration
System.UInt32 offDuration

StartBlinkAsync(RgbLed.Colors, UInt32, UInt32, CancellationToken)

Declaration
protected Task StartBlinkAsync(RgbLed.Colors color, uint onDuration, uint offDuration, CancellationToken cancellationToken)

Parameters

Type Name Description
RgbLed.Colors color
System.UInt32 onDuration
System.UInt32 offDuration
System.Threading.CancellationToken cancellationToken

Returns

Type Description
System.Threading.Tasks.Task

Stop()

Stops any running animations.

Declaration
public void Stop()