Remarks

LedBarGraph
Status
Source code GitHub
NuGet package

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

The following example shows how to use the intanciate a PwmLedBarGraph witn an array of PWM ports, and turn on/off leds individually, make the entire bar blink and pulse:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    PwmLedBarGraph pwmLedBarGraph;

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

        // Using an array of IPwmPorts
        IPwmPort[] ports =
        {
                Device.CreatePwmPort(Device.Pins.D04),
                Device.CreatePwmPort(Device.Pins.D05),
                Device.CreatePwmPort(Device.Pins.D06),
                Device.CreatePwmPort(Device.Pins.D07),
                Device.CreatePwmPort(Device.Pins.D08),
                Device.CreatePwmPort(Device.Pins.D09),
                Device.CreatePwmPort(Device.Pins.D10),
                Device.CreatePwmPort(Device.Pins.D11),
                Device.CreatePwmPort(Device.Pins.D12),
                Device.CreatePwmPort(Device.Pins.D13)
        };
        pwmLedBarGraph = new PwmLedBarGraph(ports, 0.25f);            

        TestPwmLedBarGraph();
    }

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

        float 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.01f;
                pwmLedBarGraph.Percentage = Math.Min(1.0f, percentage);
                Thread.Sleep(100);
            }

            Thread.Sleep(1000);

            Console.WriteLine("Turning them off using Percentage...");
            while (percentage >= 0)
            {
                percentage -= 0.01f;
                pwmLedBarGraph.Percentage = Math.Max(0.0f, 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 pulsing...");
            pwmLedBarGraph.StartPulse();
            Thread.Sleep(3000);
            pwmLedBarGraph.Stop();

            Thread.Sleep(1000);
        }
    }
}

Sample projects available on GitHub

Wiring Example

Characteristic Locus
Inheritance System.Object > PwmLedBarGraph
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 PwmLedBarGraph

Constructors

PwmLedBarGraph(IIODevice, IPin[], Single)

Create an LedBarGraph instance from an array of IPwnPin and a forwardVoltage for all LEDs in the bar graph

Declaration
public PwmLedBarGraph(IIODevice device, IPin[] pins, float forwardVoltage)

Parameters

Type Name Description
IIODevice device
IPin[] pins
System.Single forwardVoltage

PwmLedBarGraph(IPwmPort[], Single)

Create an LedBarGraph instance from an array of IDigitalOutputPort

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

Parameters

Type Name Description
IPwmPort[] ports
System.Single forwardVoltage

Fields

pwmLeds

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 { set; }

Property Value

Type Description
System.Single

Methods

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

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

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(uint onDuration = 200U, uint offDuration = 200U, float highBrightness = 1F, float lowBrightness = 0F)

Parameters

Type Name Description
System.UInt32 onDuration

On duration.

System.UInt32 offDuration

Off duration.

System.Single highBrightness

High brigtness.

System.Single lowBrightness

Low brightness.

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

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

Parameters

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

Stop()

Stops any running animations.

Declaration
public void Stop()