Remarks

PwmLedBarGraph
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for PwmLedBarGraph

An LED Bar Graph is packaged array of LEDs commonly used to indicate level or progress and commonly include 8 or 10 leds.

The PwmLedBarGraph class allows you to control an led bar graph using PWM ports.

Use the SetLed method to turn on or off LEDs individually, or assign a value on the Percentage property to represent it visually on the graph bar, like a battery indicator, temperature, humidity, etc.

Code Example

PwmLedBarGraph pwmLedBarGraph;

public MeadowApp()
{
    Console.WriteLine("Initializing...");

    // Using an array of Pins that support PWM (D02 - D13)
    IPin[] pins =
    {
         Device.Pins.D11,
         Device.Pins.D10,
         Device.Pins.D09,
         Device.Pins.D08,
         Device.Pins.D07,
         Device.Pins.D06,
         Device.Pins.D05,
         Device.Pins.D04,
         Device.Pins.D03,
         Device.Pins.D02
    };
    pwmLedBarGraph = new PwmLedBarGraph(Device, pins, new Voltage(3.3));

    TestPwmLedBarGraph();
}

protected void TestPwmLedBarGraph()
{
    Console.WriteLine("TestLedBarGraph...");

    decimal percentage = 0;

    while (true)
    {
        Console.WriteLine("Turning them on using SetLed...");
        for (int i = 0; i < pwmLedBarGraph.Count; i++)
        {
            pwmLedBarGraph.SetLed(i, true);
            Thread.Sleep(300);
        }

        Thread.Sleep(1000);

        Console.WriteLine("Turning them off using SetLed...");
        for (int i = pwmLedBarGraph.Count - 1; i >= 0; i--)
        {
            pwmLedBarGraph.SetLed(i, false);
            Thread.Sleep(300);
        }

        Thread.Sleep(1000);

        Console.WriteLine("Turning them on using Percentage...");
        while (percentage < 1)
        {
            percentage += 0.01m;
            pwmLedBarGraph.Percentage = (float) Math.Min(1.0m, percentage);
            Thread.Sleep(100);
        }

        Thread.Sleep(1000);

        Console.WriteLine("Turning them off using Percentage...");
        while (percentage > 0)
        {
            percentage -= 0.01m;
            pwmLedBarGraph.Percentage = (float)Math.Max(0.0m, percentage);
            Thread.Sleep(100);
        }

        Thread.Sleep(1000);

        Console.WriteLine("Bar blinking on and off...");
        pwmLedBarGraph.StartBlink();
        Thread.Sleep(3000);
        pwmLedBarGraph.Stop();

        Thread.Sleep(1000);

        Console.WriteLine("Bar blinking with high and low brightness...");
        pwmLedBarGraph.StartBlink(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), 1f, 0.25f);
        Thread.Sleep(3000);
        pwmLedBarGraph.Stop();

        Thread.Sleep(1000);

        Console.WriteLine("Bar pulsing...");
        pwmLedBarGraph.StartPulse();
        Thread.Sleep(3000);
        pwmLedBarGraph.Stop();

        Thread.Sleep(1000);
    }
}

Sample project(s) available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Leds.PwmLedBarGraph/PwmLedBarGraph_Fritzing.png"

Characteristic Locus
Inheritance System.Object > PwmLedBarGraph
Namespace Meadow.Foundation.Leds
Assembly Meadow.Foundation.dll

Syntax

public class PwmLedBarGraph : object

Constructors

PwmLedBarGraph(IPwmOutputController, IPin[], Voltage)

Create an LedBarGraph instance for single color LED bar graphs

Declaration
public PwmLedBarGraph(IPwmOutputController device, IPin[] pins, Voltage forwardVoltage)

Parameters

Type Name Description
IPwmOutputController device
IPin[] pins

Array of pins

Voltage forwardVoltage

Single forward voltage

PwmLedBarGraph(IPwmOutputController, IPin[], Voltage[])

Create an LedBarGraph instance for multi color LED bar graphs

Declaration
public PwmLedBarGraph(IPwmOutputController device, IPin[] pins, Voltage[] forwardVoltage)

Parameters

Type Name Description
IPwmOutputController device
IPin[] pins

Array of pins

Voltage[] forwardVoltage

Array of forward voltages

PwmLedBarGraph(IPwmPort[], Voltage)

Create an LedBarGraph instance for single color LED bar graphs

Declaration
public PwmLedBarGraph(IPwmPort[] ports, Voltage forwardVoltage)

Parameters

Type Name Description
IPwmPort[] ports

Array of Pwm Ports

Voltage forwardVoltage

Single forward voltage

PwmLedBarGraph(IPwmPort[], Voltage[])

Create an LedBarGraph instance for multi color LED bar graphs

Declaration
public PwmLedBarGraph(IPwmPort[] ports, Voltage[] forwardVoltage)

Parameters

Type Name Description
IPwmPort[] ports

Array of ports

Voltage[] forwardVoltage

Array of forward voltages

Fields

pwmLeds

Array to hold pwm leds for bar graph

Declaration
protected PwmLed[] pwmLeds

Field Value

Type Description
PwmLed[]

Properties

Count

The number of the LEDs in the bar graph

Declaration
public int Count { get; }

Property Value

Type Description
System.Int32

Percentage

A value between 0 and 1 that controls the number of LEDs that are activated

Declaration
public float Percentage { get; set; }

Property Value

Type Description
System.Single

Methods

GetTopLedForPercentage()

Returns the index of the last LED turned on

Declaration
public int GetTopLedForPercentage()

Returns

Type Description
System.Int32

SetLed(Int32, Boolean)

Set the LED state

Declaration
public void SetLed(int index, bool isOn)

Parameters

Type Name Description
System.Int32 index

index of the LED

System.Boolean isOn

Starts a blink animation on an individual LED

Declaration
public void SetLedBlink(int index, float highBrightness = null, float lowBrightness = null)

Parameters

Type Name Description
System.Int32 index
System.Single highBrightness
System.Single lowBrightness

Starts a blink animation on an individual LED

Declaration
public void SetLedBlink(int index, TimeSpan onDuration, TimeSpan offDuration, float highBrightness = null, float lowBrightness = null)

Parameters

Type Name Description
System.Int32 index
TimeSpan onDuration
TimeSpan offDuration
System.Single highBrightness
System.Single lowBrightness

SetLedBrightness(Int32, Single)

Set the brightness of an individual LED when using PWM

Declaration
public void SetLedBrightness(int index, float brightness)

Parameters

Type Name Description
System.Int32 index
System.Single brightness

SetLedPulse(Int32, Single, Single)

Starts a pulse animation on an individual LED

Declaration
public void SetLedPulse(int index, float highBrightness = null, float lowBrightness = 0.15F)

Parameters

Type Name Description
System.Int32 index
System.Single highBrightness
System.Single lowBrightness

SetLedPulse(Int32, TimeSpan, Single, Single)

Starts a pulse animation on an individual LED with the specified pulse cycle

Declaration
public void SetLedPulse(int index, TimeSpan pulseDuration, float highBrightness = null, float lowBrightness = 0.15F)

Parameters

Type Name Description
System.Int32 index
TimeSpan pulseDuration
System.Single highBrightness
System.Single lowBrightness

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

Declaration
public void StartBlink(float highBrightness = null, float lowBrightness = null)

Parameters

Type Name Description
System.Single highBrightness

High brigtness.

System.Single lowBrightness

Low 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(TimeSpan onDuration, TimeSpan offDuration, float highBrightness = null, float lowBrightness = null)

Parameters

Type Name Description
TimeSpan onDuration

On duration.

TimeSpan offDuration

Off duration.

System.Single highBrightness

High brigtness.

System.Single lowBrightness

Low brightness.

StartPulse(Single, Single)

Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting. High brigtness. Low brightness.

Declaration
public void StartPulse(float highBrightness = null, float lowBrightness = 0.15F)

Parameters

Type Name Description
System.Single highBrightness
System.Single lowBrightness

StartPulse(TimeSpan, 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. Pulse duration. High brigtness. Low brightness.

Declaration
public void StartPulse(TimeSpan pulseDuration, float highBrightness = null, float lowBrightness = 0.15F)

Parameters

Type Name Description
TimeSpan pulseDuration
System.Single highBrightness
System.Single lowBrightness

Stop()

Stops any running animations.

Declaration
public void Stop()