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)

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

Parameters

Type Name Description
IDigitalOutputPort redPort
IDigitalOutputPort greenPort
IDigitalOutputPort bluePort
CommonType commonType

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

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

Parameters

Type Name Description
IIODevice device
IPin redPin
IPin greenPin
IPin bluePin
CommonType commonType

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

Properties

BluePort

Declaration
public IDigitalOutputPort BluePort { get; set; }

Property Value

Type Description
IDigitalOutputPort

Color

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

Property Value

Type Description
RgbLed.Colors

Common

Declaration
public CommonType Common { get; protected set; }

Property Value

Type Description
CommonType

GreenPort

Declaration
public IDigitalOutputPort GreenPort { get; set; }

Property Value

Type Description
IDigitalOutputPort

RedPort

Declaration
public IDigitalOutputPort RedPort { get; set; }

Property Value

Type Description
IDigitalOutputPort

Methods

SetColor(RgbLed.Colors)

Declaration
public void SetColor(RgbLed.Colors color)

Parameters

Type Name Description
RgbLed.Colors color
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()

Declaration
public void Stop()

TurnOff()

Declaration
public void TurnOff()

TurnOn()

Declaration
public void TurnOn()