Meadow.Foundation.Leds.RgbPwmLed
RgbPwmLed | |
---|---|
Status | |
Source code | GitHub |
NuGet package |
RgbPwmLed represents an RGB LED whose color is controlled by the duty-cycle of three PWM signals. Can be used both with LEDs that have been current limited with in-series resistors, or LEDs without resistors.
Controlling an RGB LED via a PWM signal is more power efficient than using a current-limiting resistor, and it provides more control; allowing thousands of different colors, as opposed to the 8 colors of non-PWM powered RGB LED.
To use without resistors, pass in the forward voltages (voltage drop) of each of the LED components to the redLedForwardVoltage
, greenLedForwardVoltage
, and blueLedForwardVoltage
, constructor parameters, and the class will limit its output to the maximum forward voltage rating for those LEDs.
To use with an LED that has a resistor in series, pass 0.0
or TypicalForwardVoltage.ResistorLimited
for the forwardVoltage
parameter.
Code Example
RgbPwmLed onboardLed;
public override Task Initialize()
{
Resolver.Log.Info("Creating peripherals...");
onboardLed = new RgbPwmLed(
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);
return Task.CompletedTask;
}
public override Task Run()
{
return TestColors();
}
public async Task TestColors()
{
while (true)
{
Resolver.Log.Info("SetColor(RgbLedColors.Red);");
onboardLed.SetColor(Color.Red);
await Task.Delay(3000);
Resolver.Log.Info("StartPulse();");
await onboardLed.StartPulse();
await Task.Delay(3000);
Resolver.Log.Info("StartPulse(RgbLedColors.Green);");
await onboardLed.StartPulse(Color.Green);
await Task.Delay(3000);
Resolver.Log.Info("SetColor(RgbLedColors.Yellow);");
onboardLed.SetColor(Color.Yellow);
await Task.Delay(3000);
Resolver.Log.Info("StartPulse(RgbLedColors.Cyan, 200, 200);");
await onboardLed.StartPulse(Color.Cyan, TimeSpan.FromMilliseconds(400));
await Task.Delay(3000);
await onboardLed.StopAnimation();
}
}
Sample project(s) available on GitHub
Wiring Example
Class RgbPwmLed
Utility functions to provide blinking and pulsing for RgbPwmLed
Assembly: Meadow.Foundation.dll
View Source
public class RgbPwmLed : IRgbPwmLed, IPwmLed, IRgbLed, ILed, IDisposable
Implements:
Meadow.Peripherals.Leds.IRgbPwmLed
, Meadow.Peripherals.Leds.IPwmLed
, Meadow.Peripherals.Leds.IRgbLed
, Meadow.Peripherals.Leds.ILed
, System.IDisposable
Properties
MAX_FORWARD_VOLTAGE
Maximum forward voltage (3.3 Volts)
View Source
public Voltage MAX_FORWARD_VOLTAGE { get; }
MIN_FORWARD_VOLTAGE
Minimum forward voltage (0 Volts)
View Source
public Voltage MIN_FORWARD_VOLTAGE { get; }
IsOn
Gets or sets a value indicating whether the LED is on.
View Source
public bool IsOn { get; set; }
Color
The current LED color
View Source
public Color Color { get; protected set; }
Brightness
Gets the brightness of the LED, controlled by a PWM signal
View Source
public float Brightness { get; protected set; }
RedPwm
The red LED port
View Source
protected IPwmPort RedPwm { get; set; }
BluePwm
The blue LED port
View Source
protected IPwmPort BluePwm { get; set; }
GreenPwm
The green LED port
View Source
protected IPwmPort GreenPwm { get; set; }
Common
The common type (common anode or common cathode)
View Source
public CommonType Common { get; protected set; }
RedForwardVoltage
The red LED forward voltage
View Source
public Voltage RedForwardVoltage { get; protected set; }
GreenForwardVoltage
The green LED forward voltage
View Source
public Voltage GreenForwardVoltage { get; protected set; }
BlueForwardVoltage
The blue LED forward voltage
View Source
public Voltage BlueForwardVoltage { get; protected 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(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 |
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(float, float)
Start a Blink animation which sets the brightness of the LED alternating between a low and high brightness setting.
View Source
public Task StartBlink(float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartBlink(Color, float, float)
Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness on an interval of 1 second (500ms on, 500ms off)
View Source
public Task StartBlink(Color color, float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
Meadow.Color | color | The LED color |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartBlink(TimeSpan, TimeSpan, float, float)
Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided.
View Source
public Task StartBlink(TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.TimeSpan | onDuration | |
System.TimeSpan | offDuration | |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartBlink(Color, TimeSpan, TimeSpan, float, float)
Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided
View Source
public Task StartBlink(Color color, TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
Meadow.Color | color | The LED color |
System.TimeSpan | onDuration | The duration the LED stays on |
System.TimeSpan | offDuration | The duration the LED stays off |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(float, float)
Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting.
View Source
public Task StartPulse(float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(Color, float, float)
Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting with a cycle time of 600ms
View Source
public Task StartPulse(Color color, float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
Meadow.Color | color | The LED color |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(Color, TimeSpan, float, float)
Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the duration provided and specified color
View Source
public Task StartPulse(Color color, TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
Meadow.Color | color | The LED color |
System.TimeSpan | pulseDuration | The pulse animation duration |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(TimeSpan, float, float)
Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.
View Source
public Task StartPulse(TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.TimeSpan | pulseDuration | The pulse animation duration |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
ValidateBrightness(float, float)
Validates LED brightness to ensure they're within the range 0 (off) - 1 (full brightness)
View Source
protected void ValidateBrightness(float highBrightness, float lowBrightness)
Parameters
Type | Name | Description |
---|---|---|
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
ValidateForwardVoltages(Voltage, Voltage, Voltage)
Validates forward voltages to ensure they're within the range MIN_FORWARD_VOLTAGE to MAX_FORWARD_VOLTAGE
View Source
protected void ValidateForwardVoltages(Voltage redLedForwardVoltage, Voltage greenLedForwardVoltage, Voltage blueLedForwardVoltage)
Parameters
Type | Name | Description |
---|---|---|
Meadow.Units.Voltage | redLedForwardVoltage | The forward voltage for the red LED |
Meadow.Units.Voltage | greenLedForwardVoltage | The forward voltage for the green LED |
Meadow.Units.Voltage | blueLedForwardVoltage | The forward voltage for the blue LED |
ResetPwmPorts()
Resets all PWM ports
View Source
protected void ResetPwmPorts()
SetBrightness(float)
Set the led brightness
View Source
public void SetBrightness(float brightness)
Parameters
Type | Name | Description |
---|---|---|
System.Single | brightness | Valid values are from 0 to 1, inclusive |
SetColor(Color, float)
Sets the current color of the LED
View Source
public void SetColor(Color color, float brightness = 1)
Parameters
Type | Name | Description |
---|---|---|
Meadow.Color | color | The LED color |
System.Single | brightness | Valid values are from 0 to 1, inclusive |
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 |
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.IRgbPwmLed
Meadow.Peripherals.Leds.IPwmLed
Meadow.Peripherals.Leds.IRgbLed
Meadow.Peripherals.Leds.ILed
System.IDisposable