Meadow.Foundation.Leds.RgbLed
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
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
public class RgbLed : IRgbLed, ILed, IDisposable
Implements:
Meadow.Peripherals.Leds.IRgbLed
, Meadow.Peripherals.Leds.ILed
, System.IDisposable
Properties
Color
The current LED color
View Source
public RgbLedColors Color { get; protected set; }
RedPort
The red LED port
View Source
protected IDigitalOutputPort RedPort { get; set; }
GreenPort
The green LED port
View Source
protected IDigitalOutputPort GreenPort { get; set; }
BluePort
The blue LED port
View Source
protected IDigitalOutputPort BluePort { get; set; }
Common
The common type (common anode or common cathode)
View Source
public CommonType Common { get; protected set; }
IsOn
Gets or sets a value indicating whether the LED is on.
View Source
public bool IsOn { get; set; }
IsDisposed
Is the object disposed
View Source
public bool IsDisposed { get; }
Methods
StopAnimation()
Stops blink animation.
View Source
public Task StopAnimation()
Returns
System.Threading.Tasks.Task
StartBlink()
Blink animation that turns the LED on (500ms) and off (500ms)
View Source
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
public Task StartBlink(RgbLedColors color)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
Meadow.Peripherals.Leds.RgbLedColors | color | The 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
public Task StartBlink(RgbLedColors color, TimeSpan onDuration, TimeSpan offDuration)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
Meadow.Peripherals.Leds.RgbLedColors | color | The LED color |
System.TimeSpan | onDuration | The duration the LED stays on |
System.TimeSpan | offDuration | The duration the LED stays off |
StartBlink(TimeSpan, TimeSpan)
Blink animation that turns the LED on and off based on the OnDuration and offDuration values in ms
View Source
public Task StartBlink(TimeSpan onDuration, TimeSpan offDuration)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name |
---|---|
System.TimeSpan | onDuration |
System.TimeSpan | offDuration |
SetColor(RgbLedColors)
Sets the current color of the LED.
View Source
public void SetColor(RgbLedColors color)
Parameters
Type | Name | Description |
---|---|---|
Meadow.Peripherals.Leds.RgbLedColors | color | The color value |
UpdateLed(bool)
Turns on LED with current color or LED off
View Source
protected void UpdateLed(bool isOn)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | isOn | True for on, False for off |
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
View Source
public void Dispose()
Dispose(bool)
Dispose of the object
View Source
public virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | Is disposing |
Implements
Meadow.Peripherals.Leds.IRgbLed
Meadow.Peripherals.Leds.ILed
System.IDisposable