Skip to content

Commit

Permalink
Refactor on both LedBarGraph and PwmLedBarGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgedevs committed Aug 16, 2022
1 parent a29d468 commit f74f16e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public override async Task Run()
{
Console.WriteLine("TestLedBarGraph...");

decimal percentage = 0;
float percentage = 0;

while (true)
{
Console.WriteLine("Turning them on and off for 1 second using SetLed...");
Console.WriteLine("Turning them on and off for 200ms using SetLed...");
for (int i = 0; i < ledBarGraph.Count; i++)
{
ledBarGraph.SetLed(i, true);
await Task.Delay(1000);
await Task.Delay(100);
ledBarGraph.SetLed(i, false);
}

Expand All @@ -58,67 +58,62 @@ public override async Task Run()
Console.WriteLine("Turning them on using Percentage...");
while (percentage < 1)
{
percentage += 0.10m;
percentage += 0.10f;
Console.WriteLine($"{percentage}");
ledBarGraph.Percentage = (float) Math.Min(1.0m, percentage);
await Task.Delay(1000);
ledBarGraph.Percentage = Math.Min(1.0f, percentage);
await Task.Delay(100);
}

await Task.Delay(1000);

Console.WriteLine("Turning them off using Percentage...");
while (percentage > 0)
{
percentage -= 0.10m;
percentage -= 0.10f;
Console.WriteLine($"{percentage}");
ledBarGraph.Percentage = (float) Math.Max(0.0m, percentage);
await Task.Delay(1000);
ledBarGraph.Percentage = Math.Max(0.0f, percentage);
await Task.Delay(100);
}

await Task.Delay(1000);

Console.WriteLine("Charging animation...");
while (percentage < 1)
{
percentage += 0.10m;
percentage += 0.10f;
Console.WriteLine($"{percentage}");
ledBarGraph.Percentage = (float)Math.Min(1.0m, percentage);
ledBarGraph.SetLedBlink(ledBarGraph.GetTopLedForPercentage());
await Task.Delay(1000);
ledBarGraph.Percentage = Math.Min(1.0f, percentage);
ledBarGraph.StartBlink(ledBarGraph.GetTopLedForPercentage());
await Task.Delay(2000);
}

await Task.Delay(1000);

Console.WriteLine("Discharging animation...");
while (percentage > 1)
while (percentage > 0)
{
percentage -= 0.10m;
percentage -= 0.10f;
Console.WriteLine($"{percentage}");
ledBarGraph.Percentage = (float)Math.Max(0.0m, percentage);
ledBarGraph.SetLedBlink(ledBarGraph.GetTopLedForPercentage());
await Task.Delay(1000);
ledBarGraph.Percentage = Math.Max(0.0f, percentage);
ledBarGraph.StartBlink(ledBarGraph.GetTopLedForPercentage());
await Task.Delay(2000);
}

await Task.Delay(1000);

Console.WriteLine("Blinking for 3 seconds...");
Console.WriteLine("Blinking for 5 seconds at 500ms on/off...");
ledBarGraph.StartBlink();
await Task.Delay(5000);
ledBarGraph.Stop();

await Task.Delay(1000);

Console.WriteLine("Blinking for 3 seconds...");
ledBarGraph.StartBlink(TimeSpan.FromSeconds(500), TimeSpan.FromSeconds(500));
Console.WriteLine("Blinking for 5 seconds at 200ms on/off...");
ledBarGraph.StartBlink(TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(200));
await Task.Delay(5000);
ledBarGraph.Stop();

await Task.Delay(1000);

for (int i = 0; i < ledBarGraph.Count; i++)
{
ledBarGraph.SetLed(i, false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Meadow.Devices;
using Meadow.Foundation.Leds;
using Meadow.Hardware;
using Meadow.Peripherals.Leds;
using Meadow.Units;
using System;
using System.Threading.Tasks;
Expand Down Expand Up @@ -42,33 +43,25 @@ public override async Task Run()
{
Console.WriteLine("TestLedBarGraph...");

double percentage = 0;
float percentage = 0;

while (true)
{
Console.WriteLine("Turning them on using SetLed...");
Console.WriteLine("Turning them on and off for 200ms using SetLed...");
for (int i = 0; i < pwmLedBarGraph.Count; i++)
{
pwmLedBarGraph.SetLed(i, true);
await Task.Delay(300);
}

await Task.Delay(1000);

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

await Task.Delay(1000);

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

Expand All @@ -77,38 +70,33 @@ public override async Task Run()
Console.WriteLine("Turning them off using Percentage...");
while (percentage > 0)
{
percentage -= 0.01;
pwmLedBarGraph.Percentage = (float)Math.Max(0.0, percentage);
percentage -= 0.01f;
pwmLedBarGraph.Percentage = Math.Max(0.0f, percentage);
await Task.Delay(100);
}

await Task.Delay(1000);

Console.WriteLine("Bar blinking on and off...");
Console.WriteLine("Blinking for 5 seconds at 500ms on/off...");
pwmLedBarGraph.StartBlink();
await Task.Delay(5000);
pwmLedBarGraph.Stop();

await Task.Delay(1000);

Console.WriteLine("Bar blinking with high and low brightness...");
pwmLedBarGraph.StartBlink(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), 1f, 0.25f);
Console.WriteLine("Bar blinking with high and low brightness for 5 seconds...");
pwmLedBarGraph.StartBlink(TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(200), 0.75f, 0.25f);
await Task.Delay(5000);
pwmLedBarGraph.Stop();

await Task.Delay(1000);

Console.WriteLine("Bar pulsing...");
Console.WriteLine("Bar pulsing for 5 seconds...");
pwmLedBarGraph.StartPulse();
await Task.Delay(5000);
pwmLedBarGraph.Stop();

await Task.Delay(1000);

for (int i = pwmLedBarGraph.Count - 1; i >= 0; i--)
{
pwmLedBarGraph.SetLed(i, false);
}
}
}

Expand Down
49 changes: 18 additions & 31 deletions Source/Meadow.Foundation.Core/Leds/LedBarGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Meadow.Foundation.Leds
/// </summary>
public class LedBarGraph
{
private const int NONE_LED_BLINKING = -1;

private Task? animationTask;
private CancellationTokenSource? cancellationTokenSource;

Expand All @@ -21,11 +19,6 @@ public class LedBarGraph
/// </summary>
protected Led[] leds;

/// <summary>
/// Index of specific LED blinking
/// </summary>
protected int indexLedBlinking = NONE_LED_BLINKING;

/// <summary>
/// The number of the LEDs in the bar graph
/// </summary>
Expand All @@ -34,12 +27,12 @@ public class LedBarGraph
/// <summary>
/// A value between 0 and 1 that controls the number of LEDs that are activated
/// </summary>
public double Percentage
public float Percentage
{
get => percentage;
set => SetPercentage(percentage = value);
}
double percentage;
float percentage;

/// <summary>
/// Create an LedBarGraph instance from an array of IPins
Expand Down Expand Up @@ -90,7 +83,7 @@ public void SetLed(int index, bool isOn)
/// Set the percentage of LEDs that are on starting from index 0
/// </summary>
/// <param name="percentage">Percentage (Range from 0 - 1)</param>
protected void SetPercentage(double percentage)
protected void SetPercentage(float percentage)
{
if (percentage < 0 || percentage > 1)
{
Expand Down Expand Up @@ -126,23 +119,14 @@ public int GetTopLedForPercentage()
/// Starts a blink animation on an individual LED on (500ms) and off (500ms)
/// </summary>
/// <param name="index"></param>
public void SetLedBlink(int index)
public void StartBlink(int index)
{
var onDuration = TimeSpan.FromMilliseconds(500);
var offDuration = TimeSpan.FromMilliseconds(500);

if (index >= Count)
{
throw new ArgumentOutOfRangeException();
}

if (indexLedBlinking != NONE_LED_BLINKING)
{
leds[indexLedBlinking].Stop();
}

indexLedBlinking = index;
leds[index].StartBlink(onDuration, offDuration);
leds[index].StartBlink();
}

/// <summary>
Expand All @@ -151,15 +135,13 @@ public void SetLedBlink(int index)
/// <param name="index"></param>
/// <param name="onDuration"></param>
/// <param name="offDuration"></param>
public void SetLedBlink(int index, TimeSpan onDuration, TimeSpan offDuration)
public void StartBlink(int index, TimeSpan onDuration, TimeSpan offDuration)
{
if (index >= Count)
{
throw new ArgumentOutOfRangeException();
}

leds[indexLedBlinking].Stop();
indexLedBlinking = index;
leds[index].StartBlink(onDuration, offDuration);
}

Expand Down Expand Up @@ -238,15 +220,20 @@ protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration,
/// </summary>
public void Stop()
{
if (indexLedBlinking != NONE_LED_BLINKING)
{
leds[indexLedBlinking].Stop();
indexLedBlinking = NONE_LED_BLINKING;
}
else
cancellationTokenSource?.Cancel();

foreach (var led in leds)
{
cancellationTokenSource?.Cancel();
led.Stop();
}
}

/// <summary>
/// Stops the blinking animation on an individual LED and/or turns it off
/// </summary>
public void Stop(int index)
{
leds[index].Stop();
}
}
}
Loading

0 comments on commit f74f16e

Please sign in to comment.