Characteristic Locus
Inheritance System.Object > RgbPwmLed
Inherited Members System.Object.ToString() System.Object.Equals(System.Object) System.Object.Equals(System.Object, System.Object) System.Object.ReferenceEquals(System.Object, System.Object) System.Object.GetHashCode() System.Object.GetType() System.Object.MemberwiseClone()
Namespace Meadow.Foundation.Leds
Assembly Meadow.Foundation.dll

Syntax

public class RgbPwmLed

Remarks

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 redLedForwardVotlage, greenLedForwardVotlage, and blueLedForwardVotlage, 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.

Examples

Displaying the Rainbow of Hues

The following example code loops through the entire 360º of hue spectrum and displays that color on the RGB LED.

using System.Threading;
using Meadow;
using Meadow.Foundation.LEDs;

namespace PwmLedSample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

    public App : AppBase<F7Micro, App>
    {
        public App()
        {
            // create a new RgbPwmLed on pin 8
            var pwmLed = new RgbPwmLed(
                redPin: Device.Pins.D11,
                greenPin: Device.Pins.D10,
                bluePin: Device.Pins.D09,
                redLedForwardVoltage: 1.05f,
                greenLedForwardVoltage: 1.5f,
                blueLedForwardVoltage: 1.5f);
            );

            // alternate between blinking and pulsing the LED 
            while (true)
            {
                for (int i = 0; i < 360; i++)
                {
                    var hue = ((double)i / 360F);
                    Debug.Print(hue.ToString());

                    // set the color of the RGB
                    rgbPwmLed.SetColor(Color.FromHsba(((double)i / 360F), 1, 1));

                    // for a fun, fast rotation through the hue spectrum:
                    //Thread.Sleep (1);
                    // for a gentle walk through the forest of colors;
                    Thread.Sleep(18);
                }
            }
        }
    }
}
Example Circuit

Constructors

View Source

RgbPwmLed(IIODevice, IPin, IPin, IPin, Single, Single, Single, Boolean)

Instantiates a RgbPwmLed object with the especified IO device, connected to three digital pins for red, green and blue channels, respectively

Declaration
public RgbPwmLed(IIODevice device, IPin redPwmPin, IPin greenPwmPin, IPin bluePwmPin, float redLedForwardVoltage = 3.3F, float greenLedForwardVoltage = 3.3F, float blueLedForwardVoltage = 3.3F, bool isCommonCathode = true)

Parameters

Type Name Description
IIODevice device
IPin redPwmPin
IPin greenPwmPin
IPin bluePwmPin
System.Single redLedForwardVoltage
System.Single greenLedForwardVoltage
System.Single blueLedForwardVoltage
System.Boolean isCommonCathode
View Source

RgbPwmLed(IPwmPort, IPwmPort, IPwmPort, Single, Single, Single, Boolean)

Implementation notes: Architecturally, it would be much cleaner to construct this class as three PwmLeds. Then each one's implementation would be self-contained. However, that would require three additional threads during ON; one contained by each PwmLed. For this reason, I'm basically duplicating the functionality for all three in here.

Declaration
public RgbPwmLed(IPwmPort redPwm, IPwmPort greenPwm, IPwmPort bluePwm, float redLedForwardVoltage = 3.3F, float greenLedForwardVoltage = 3.3F, float blueLedForwardVoltage = 3.3F, bool isCommonCathode = true)

Parameters

Type Name Description
IPwmPort redPwm
IPwmPort greenPwm
IPwmPort bluePwm
System.Single redLedForwardVoltage
System.Single greenLedForwardVoltage
System.Single blueLedForwardVoltage
System.Boolean isCommonCathode

Fields

View Source

_animationTask

Declaration
protected Task _animationTask

Field Value

Type Description
System.Threading.Tasks.Task
View Source

cancellationTokenSource

Declaration
protected CancellationTokenSource cancellationTokenSource

Field Value

Type Description
System.Threading.CancellationTokenSource
View Source

isEnabled

Declaration
protected bool isEnabled

Field Value

Type Description
System.Boolean
View Source

maxBlueDutyCycle

Declaration
protected double maxBlueDutyCycle

Field Value

Type Description
System.Double
View Source

maxGreenDutyCycle

Declaration
protected double maxGreenDutyCycle

Field Value

Type Description
System.Double
View Source

maxRedDutyCycle

Declaration
protected double maxRedDutyCycle

Field Value

Type Description
System.Double

Properties

View Source

BlueForwardVoltage

Get the blue LED forward voltage

Declaration
public float BlueForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single
View Source

BluePwm

Get the blue LED port

Declaration
public IPwmPort BluePwm { get; protected set; }

Property Value

Type Description
IPwmPort
View Source

Color

The color the LED has been set to.

Declaration
public Color Color { get; }

Property Value

Type Description
Color
View Source

GreenForwardVoltage

Get the green LED forward voltage

Declaration
public float GreenForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single
View Source

GreenPwm

Get the green LED port

Declaration
public IPwmPort GreenPwm { get; protected set; }

Property Value

Type Description
IPwmPort
View Source

IsCommonCathode

Is the LED using a common cathode

Declaration
public bool IsCommonCathode { get; protected set; }

Property Value

Type Description
System.Boolean
View Source

IsEnabled

Enables / disables the LED but toggling the PWM

Declaration
public bool IsEnabled { get; set; }

Property Value

Type Description
System.Boolean
View Source

RedForwardVoltage

Get the red LED forward voltage

Declaration
public float RedForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single
View Source

RedPwm

Get the red LED port

Declaration
public IPwmPort RedPwm { get; protected set; }

Property Value

Type Description
IPwmPort

Methods

View Source

ResetPwms()

Resets all PWM ports

Declaration
protected void ResetPwms()
View Source

SetColor(Color)

Sets the current color of the LED.

Declaration
public void SetColor(Color ledColor)

Parameters

Type Name Description
Color ledColor
View Source

SetColor(Color, Single)

Declaration
protected void SetColor(Color color, float brightness)

Parameters

Type Name Description
Color color
System.Single brightness
View Source

Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided.

Declaration
public void StartBlink(Color color, uint onDuration = 200U, uint offDuration = 200U, float highBrightness = 1F, float lowBrightness = 0F)

Parameters

Type Name Description
Color color
System.UInt32 onDuration
System.UInt32 offDuration
System.Single highBrightness
System.Single lowBrightness
View Source

StartBlinkAsync(Color, UInt32, UInt32, Single, Single, CancellationToken)

Declaration
protected Task StartBlinkAsync(Color color, uint onDuration, uint offDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)

Parameters

Type Name Description
Color color
System.UInt32 onDuration
System.UInt32 offDuration
System.Single highBrightness
System.Single lowBrightness
System.Threading.CancellationToken cancellationToken

Returns

Type Description
System.Threading.Tasks.Task
View Source

StartPulse(Color, UInt32, Single, Single)

Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.

Declaration
public void StartPulse(Color color, uint pulseDuration = 600U, float highBrightness = 1F, float lowBrightness = 0.15F)

Parameters

Type Name Description
Color color
System.UInt32 pulseDuration
System.Single highBrightness
System.Single lowBrightness
View Source

StartPulseAsync(Color, UInt32, Single, Single, CancellationToken)

Declaration
protected Task StartPulseAsync(Color color, uint pulseDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)

Parameters

Type Name Description
Color color
System.UInt32 pulseDuration
System.Single highBrightness
System.Single lowBrightness
System.Threading.CancellationToken cancellationToken

Returns

Type Description
System.Threading.Tasks.Task
View Source

Stop()

Stops any running animations.

Declaration
public void Stop()