Remarks

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.

Example Code

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

public MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // 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);
            }
        }
    }
}

Sample projects available on GitHub

Wiring Example

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

Constructors

RgbPwmLed(IIODevice, IPin, IPin, IPin, Single, Single, Single, CommonType)

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, CommonType commonType = null)

Parameters

Type Name Description
IIODevice device
IPin redPwmPin
IPin greenPwmPin
IPin bluePwmPin
System.Single redLedForwardVoltage
System.Single greenLedForwardVoltage
System.Single blueLedForwardVoltage
CommonType commonType

RgbPwmLed(IPwmPort, IPwmPort, IPwmPort, Single, Single, Single, CommonType)

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, CommonType commonType = null)

Parameters

Type Name Description
IPwmPort redPwm
IPwmPort greenPwm
IPwmPort bluePwm
System.Single redLedForwardVoltage
System.Single greenLedForwardVoltage
System.Single blueLedForwardVoltage
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

isEnabled

Declaration
protected bool isEnabled

Field Value

Type Description
System.Boolean

maxBlueDutyCycle

Declaration
protected double maxBlueDutyCycle

Field Value

Type Description
System.Double

maxGreenDutyCycle

Declaration
protected double maxGreenDutyCycle

Field Value

Type Description
System.Double

maxRedDutyCycle

Declaration
protected double maxRedDutyCycle

Field Value

Type Description
System.Double

Properties

BlueForwardVoltage

Get the blue LED forward voltage

Declaration
public float BlueForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single

BluePwm

Get the blue LED port

Declaration
public IPwmPort BluePwm { get; protected set; }

Property Value

Type Description
IPwmPort

Color

The color the LED has been set to.

Declaration
public Color Color { get; }

Property Value

Type Description
Color

Common

Is the LED using a common cathode

Declaration
public CommonType Common { get; protected set; }

Property Value

Type Description
CommonType

GreenForwardVoltage

Get the green LED forward voltage

Declaration
public float GreenForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single

GreenPwm

Get the green LED port

Declaration
public IPwmPort GreenPwm { get; protected set; }

Property Value

Type Description
IPwmPort

IsEnabled

Enables / disables the LED but toggling the PWM

TODO: What's the use case for enabling? maybe this should just be State, which would return whether or not it's running.

Declaration
public bool IsEnabled { get; set; }

Property Value

Type Description
System.Boolean

RedForwardVoltage

Get the red LED forward voltage

Declaration
public float RedForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single

RedPwm

Get the red LED port

Declaration
public IPwmPort RedPwm { get; protected set; }

Property Value

Type Description
IPwmPort

Methods

ResetPwms()

Resets all PWM ports

Declaration
protected void ResetPwms()

SetColor(Color)

Sets the current color of the LED.

Declaration
public void SetColor(Color ledColor)

Parameters

Type Name Description
Color ledColor

SetColor(Color, Single)

Declaration
protected void SetColor(Color color, float brightness)

Parameters

Type Name Description
Color color
System.Single brightness

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

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

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

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

Stop()

Stops any running animations.

Declaration
public void Stop()