Meadow.Foundation.Leds.PwmLedBarGraph
PwmLedBarGraph | |
---|---|
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
PwmLedBarGraph pwmLedBarGraph;
public override Task Initialize()
{
Resolver.Log.Info("Initializing...");
// Using an array of Pins that support PWM (F7v2: D02-D04, D07-D13)
IPin[] pins =
{
Device.Pins.D11,
Device.Pins.D10,
Device.Pins.D09,
Device.Pins.D08,
Device.Pins.D07,
// Device.Pins.D06, // Only available for PWM on F7v1
// Device.Pins.D05, // Only available for PWM on F7v1
Device.Pins.D04,
Device.Pins.D03,
Device.Pins.D02
};
pwmLedBarGraph = new PwmLedBarGraph(pins, new Voltage(2.2));
return Task.CompletedTask;
}
public override async Task Run()
{
Resolver.Log.Info("TestLedBarGraph...");
float percentage = 0;
while (true)
{
Resolver.Log.Info("Turning them on and off for 200ms using SetLed...");
for (int i = 0; i < pwmLedBarGraph.Count; i++)
{
await pwmLedBarGraph.SetLed(i, true);
await Task.Delay(100);
await pwmLedBarGraph.SetLed(i, false);
}
await Task.Delay(1000);
Resolver.Log.Info("Turning them on using Percentage...");
while (percentage < 1)
{
percentage += 0.01f;
await pwmLedBarGraph.SetPercentage(Math.Min(1.0f, percentage));
await Task.Delay(100);
}
await Task.Delay(1000);
Resolver.Log.Info("Turning them off using Percentage...");
while (percentage > 0)
{
percentage -= 0.01f;
await pwmLedBarGraph.SetPercentage(Math.Max(0.0f, percentage));
await Task.Delay(100);
}
await Task.Delay(1000);
Resolver.Log.Info("Blinking for 5 seconds at 500ms on/off...");
await pwmLedBarGraph.StartBlink();
await Task.Delay(5000);
await pwmLedBarGraph.StopAnimation();
await Task.Delay(1000);
Resolver.Log.Info("Bar blinking with high and low brightness for 5 seconds...");
await pwmLedBarGraph.StartBlink(TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(200), 0.75f, 0.25f);
await Task.Delay(5000);
await pwmLedBarGraph.StopAnimation();
await Task.Delay(1000);
Resolver.Log.Info("Bar pulsing for 5 seconds...");
await pwmLedBarGraph.StartPulse();
await Task.Delay(5000);
await pwmLedBarGraph.StopAnimation();
await Task.Delay(1000);
await pwmLedBarGraph.SetBrightness(0);
await Task.Delay(1000);
}
}
Sample project(s) available on GitHub
Wiring Example
Class PwmLedBarGraph
Represents an LED bar graph composed on multiple PWM LEDs
Assembly: Meadow.Foundation.dll
View Source
public class PwmLedBarGraph
Properties
Count
The number of the LEDs in the bar graph
View Source
public int Count { get; }
Percentage
A value between 0 and 1 that controls the number of LEDs that are activated
View Source
public float Percentage { get; protected set; }
Fields
pwmLeds
Array to hold pwm leds for bar graph
View Source
protected PwmLed[] pwmLeds
Methods
StopAnimation()
Stops the LED bar graph when its blinking
View Source
public Task StopAnimation()
Returns
System.Threading.Tasks.Task
StopAnimation(int)
Stops the blinking animation on an individual LED
View Source
public Task StopAnimation(int index)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
StartBlink(int, float, float)
Starts a blink animation on an individual LED
View Source
public Task StartBlink(int index, float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartBlink(int, TimeSpan, TimeSpan, float, float)
Starts a blink animation on an individual LED
View Source
public Task StartBlink(int index, TimeSpan highBrightnessDuration, TimeSpan lowBrightnessDuration, float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
System.TimeSpan | highBrightnessDuration | The duration the LED stays in high brightness |
System.TimeSpan | lowBrightnessDuration | The duration the LED stays in low brightness |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartBlink(float, float)
Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting.
View Source
public Task StartBlink(float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartBlink(TimeSpan, TimeSpan, float, float)
Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided.
View Source
public Task StartBlink(TimeSpan highBrightnessDuration, TimeSpan lowBrightnessDuration, float highBrightness = 1, float lowBrightness = 0)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.TimeSpan | highBrightnessDuration | On duration. |
System.TimeSpan | lowBrightnessDuration | Off duration. |
System.Single | highBrightness | High brightness. |
System.Single | lowBrightness | Low brightness. |
StartPulse(int, float, float)
Starts a pulse animation on an individual LED
View Source
public Task StartPulse(int index, float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(int, TimeSpan, float, float)
Starts a pulse animation on an individual LED with the specified pulse cycle
View Source
public Task StartPulse(int index, TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
System.TimeSpan | pulseDuration | The pulse animation duration |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(float, float)
Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting.
View Source
public Task StartPulse(float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
StartPulse(TimeSpan, float, float)
Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.
View Source
public Task StartPulse(TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.TimeSpan | pulseDuration | The pulse animation duration |
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
ValidateBrightness(float, float)
Validates LED brightness to ensure they're within the range 0 (off) - 1 (full brightness)
View Source
protected void ValidateBrightness(float highBrightness, float lowBrightness)
Parameters
Type | Name | Description |
---|---|---|
System.Single | highBrightness | The maximum brightness of the animation |
System.Single | lowBrightness | The minimum brightness of the animation |
GetTopLedForPercentage()
Returns the index of the last LED turned on
View Source
public int GetTopLedForPercentage()
Returns
System.Int32
SetLed(int, bool)
Set the LED state
View Source
public Task SetLed(int index, bool isOn)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
System.Boolean | isOn | True for on, False for off |
SetLedBrightness(int, float)
Set the brightness of an individual LED when using PWM
View Source
public Task SetLedBrightness(int index, float brightness)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the LED |
System.Single | brightness | Valid values are from 0 to 1, inclusive |
SetPercentage(float)
Set the percentage of LEDs that are on starting from index 0
View Source
public Task SetPercentage(float percentage)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Single | percentage | Percentage (Range from 0 - 1) |
SetBrightness(float)
Set the brightness to the LED bar graph using PWM
View Source
public Task SetBrightness(float brightness)
Returns
System.Threading.Tasks.Task
Parameters
Type | Name | Description |
---|---|---|
System.Single | brightness | Valid values are from 0 to 1, inclusive |