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 LedBarGraph class allows you to control an led bar graph using GPIO.

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 property Percentage to slowly fill and empty a 10 LED battery level graph:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    DigitalOutputPort _blueLED;
    LedBarGraph _ledBarGraph;

    public MeadowApp()
    {
        _blueLED = new DigitalOutputPort(Device.Pins.OnboardLEDBlue, true);

        var pins = new IDigitalPin[10];
        pins[0] = Device.Pins.D06;
        pins[1] = Device.Pins.D07;
        pins[2] = Device.Pins.D08;
        pins[3] = Device.Pins.D09;
        pins[4] = Device.Pins.D10;
        pins[5] = Device.Pins.D11;
        pins[6] = Device.Pins.D12;
        pins[7] = Device.Pins.D13;
        pins[8] = Device.Pins.D14;
        pins[9] = Device.Pins.D15;

        _ledBarGraph = new LedBarGraph(pins);

        Run();
    }

    void Run()
    {
        while (true)
        {
            float percentage = 0;

            while (percentage < 1)
            {
                _ledBarGraph.Percentage = percentage;
                percentage += 0.1f;
                Thread.Sleep(200);                    
            }

            percentage = 1.0f;

            while (percentage > 0)
            {
                _ledBarGraph.Percentage = percentage;
                percentage -= 0.1f;
                Thread.Sleep(200);                    
            }
        }
    }
}

Sample projects available on GitHub

Wiring Example

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

Constructors

View Source

LedBarGraph(IDigitalOutputPort[])

Create an LedBarGraph instance from an array of IDigitalOutputPort

Declaration
public LedBarGraph(IDigitalOutputPort[] ports)

Parameters

Type Name Description
IDigitalOutputPort[] ports
View Source

LedBarGraph(IIODevice, IPin[])

Create an LedBarGraph instance from an array of IPins

Declaration
public LedBarGraph(IIODevice device, IPin[] pins)

Parameters

Type Name Description
IIODevice device
IPin[] pins
View Source

LedBarGraph(IIODevice, IPin[], Single)

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

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

Parameters

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

Fields

View Source

_isPwm

Declaration
protected bool _isPwm

Field Value

Type Description
System.Boolean
View Source

_leds

Declaration
protected Led[] _leds

Field Value

Type Description
Led[]
View Source

_pwmLeds

Declaration
protected PwmLed[] _pwmLeds

Field Value

Type Description
PwmLed[]

Properties

View Source

Count

The number of the LEDs in the bar graph

Declaration
public int Count { get; }

Property Value

Type Description
System.Int32
View Source

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

View Source

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
View Source

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