Skip to main content

Meadow.Foundation.Leds.RgbLed

RgbLed
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet 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.

Common Cathode/Common Anode RGB LEDs

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

Common Anode RGB LEDs

Circuit of a common cathode RGB LED

Common Cathode RGB LEDs

Code Example

protected List<RgbLed> rgbLeds;

public override Task Initialize()
{
Resolver.Log.Info("Initializing...");

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))
};

return Task.CompletedTask;
}

public override async Task Run()
{
Resolver.Log.Info("TestRgbLeds...");

while (true)
{
Resolver.Log.Info("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);

Resolver.Log.Info("Blinking through each color on each RGB LED (on 500ms / off 500ms)...");
foreach (var rgbLed in rgbLeds)
{
for (int i = 0; i < (int)RgbLedColors.count; i++)
{
await rgbLed.StartBlink((RgbLedColors)i);
await Task.Delay(3000);
await rgbLed.StopAnimation();
rgbLed.IsOn = false;
}
}

await Task.Delay(1000);

Resolver.Log.Info("Blinking through each color on each RGB LED (on 1s / off 1s)...");
foreach (var rgbLed in rgbLeds)
{
for (int i = 0; i < (int)RgbLedColors.count; i++)
{
await rgbLed.StartBlink((RgbLedColors)i, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
await Task.Delay(3000);
await rgbLed.StopAnimation();
rgbLed.IsOn = false;
}
}

await Task.Delay(1000);
}
}

Sample project(s) available on GitHub

Class RgbLed

Utility functions to provide blinking and pulsing for RgbLed

Assembly: Meadow.Foundation.dll
View Source
Declaration
public class RgbLed : IRgbLed, IDisposable

Implements:
Meadow.Peripherals.Leds.IRgbLed, System.IDisposable

Properties

Color

The current LED color

View Source
Declaration
public RgbLedColors Color { get; protected set; }

RedPort

The red LED port

View Source
Declaration
protected IDigitalOutputPort RedPort { get; set; }

GreenPort

The green LED port

View Source
Declaration
protected IDigitalOutputPort GreenPort { get; set; }

BluePort

The blue LED port

View Source
Declaration
protected IDigitalOutputPort BluePort { get; set; }

Common

The common type (common anode or common cathode)

View Source
Declaration
public CommonType Common { get; protected set; }

IsOn

Turns on LED with current color or turns it off

View Source
Declaration
public bool IsOn { get; set; }

IsDisposed

Is the object disposed

View Source
Declaration
public bool IsDisposed { get; }

Methods

StopAnimation()

Stops the current LED animation

View Source
Declaration
public Task StopAnimation()
Returns

System.Threading.Tasks.Task

Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off)

View Source
Declaration
public Task StartBlink()
Returns

System.Threading.Tasks.Task

StartBlink(RgbLedColors)

Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off)

View Source
Declaration
public Task StartBlink(RgbLedColors color)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.Peripherals.Leds.RgbLedColorscolorThe LED color

StartBlink(RgbLedColors, TimeSpan, TimeSpan)

Start the Blink animation which sets turns the LED on and off with the specified durations and color

View Source
Declaration
public Task StartBlink(RgbLedColors color, TimeSpan onDuration, TimeSpan offDuration)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.Peripherals.Leds.RgbLedColorscolorThe LED color
System.TimeSpanonDurationThe duration the LED stays on
System.TimeSpanoffDurationThe duration the LED stays off

StartBlink(TimeSpan, TimeSpan)

Start the Blink animation which sets turns the LED on and off with the specified durations and current color

View Source
Declaration
public Task StartBlink(TimeSpan onDuration, TimeSpan offDuration)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
System.TimeSpanonDurationThe duration the LED stays on
System.TimeSpanoffDurationThe duration the LED stays off

SetColor(RgbLedColors)

Sets the current color of the LED.

View Source
Declaration
public void SetColor(RgbLedColors color)
Parameters
TypeNameDescription
Meadow.Peripherals.Leds.RgbLedColorscolorThe color value

UpdateLed(bool)

Turns on LED with current color or LED off

View Source
Declaration
protected void UpdateLed(bool isOn)
Parameters
TypeNameDescription
System.BooleanisOnTrue for on, False for off

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Dispose(bool)

Dispose of the object

View Source
Declaration
public virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Implements

  • Meadow.Peripherals.Leds.IRgbLed
  • System.IDisposable