diff --git a/Source/Meadow.Foundation.Core.Samples/Leds.LedBarGraph_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Core.Samples/Leds.LedBarGraph_Sample/MeadowApp.cs index ca5ed2b893..5be44092f3 100644 --- a/Source/Meadow.Foundation.Core.Samples/Leds.LedBarGraph_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Core.Samples/Leds.LedBarGraph_Sample/MeadowApp.cs @@ -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); } @@ -58,10 +58,10 @@ 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); @@ -69,10 +69,10 @@ public override async Task Run() 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); @@ -80,45 +80,40 @@ public override async Task Run() 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); - } } } diff --git a/Source/Meadow.Foundation.Core.Samples/Leds.PwmLedBarGraph_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Core.Samples/Leds.PwmLedBarGraph_Sample/MeadowApp.cs index 22cb3ad49d..9382315a70 100644 --- a/Source/Meadow.Foundation.Core.Samples/Leds.PwmLedBarGraph_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Core.Samples/Leds.PwmLedBarGraph_Sample/MeadowApp.cs @@ -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; @@ -42,24 +43,16 @@ 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); @@ -67,8 +60,8 @@ public override async Task Run() 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); } @@ -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); - } } } diff --git a/Source/Meadow.Foundation.Core/Leds/LedBarGraph.cs b/Source/Meadow.Foundation.Core/Leds/LedBarGraph.cs index 2000d30547..fb864a9ead 100644 --- a/Source/Meadow.Foundation.Core/Leds/LedBarGraph.cs +++ b/Source/Meadow.Foundation.Core/Leds/LedBarGraph.cs @@ -11,8 +11,6 @@ namespace Meadow.Foundation.Leds /// public class LedBarGraph { - private const int NONE_LED_BLINKING = -1; - private Task? animationTask; private CancellationTokenSource? cancellationTokenSource; @@ -21,11 +19,6 @@ public class LedBarGraph /// protected Led[] leds; - /// - /// Index of specific LED blinking - /// - protected int indexLedBlinking = NONE_LED_BLINKING; - /// /// The number of the LEDs in the bar graph /// @@ -34,12 +27,12 @@ public class LedBarGraph /// /// A value between 0 and 1 that controls the number of LEDs that are activated /// - public double Percentage + public float Percentage { get => percentage; set => SetPercentage(percentage = value); } - double percentage; + float percentage; /// /// Create an LedBarGraph instance from an array of IPins @@ -90,7 +83,7 @@ public void SetLed(int index, bool isOn) /// Set the percentage of LEDs that are on starting from index 0 /// /// Percentage (Range from 0 - 1) - protected void SetPercentage(double percentage) + protected void SetPercentage(float percentage) { if (percentage < 0 || percentage > 1) { @@ -126,23 +119,14 @@ public int GetTopLedForPercentage() /// Starts a blink animation on an individual LED on (500ms) and off (500ms) /// /// - 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(); } /// @@ -151,15 +135,13 @@ public void SetLedBlink(int index) /// /// /// - 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); } @@ -238,15 +220,20 @@ protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration, /// 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(); } } + + /// + /// Stops the blinking animation on an individual LED and/or turns it off + /// + public void Stop(int index) + { + leds[index].Stop(); + } } } \ No newline at end of file diff --git a/Source/Meadow.Foundation.Core/Leds/PwmLedBarGraph.cs b/Source/Meadow.Foundation.Core/Leds/PwmLedBarGraph.cs index 382f9968a0..4806f9771e 100644 --- a/Source/Meadow.Foundation.Core/Leds/PwmLedBarGraph.cs +++ b/Source/Meadow.Foundation.Core/Leds/PwmLedBarGraph.cs @@ -2,6 +2,7 @@ using Meadow.Hardware; using Meadow.Units; using System; +using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -12,8 +13,6 @@ namespace Meadow.Foundation.Leds /// public class PwmLedBarGraph { - private const int NONE_LED_BLINKING = -1; - private Task? animationTask; private CancellationTokenSource? cancellationTokenSource; @@ -22,11 +21,6 @@ public class PwmLedBarGraph /// protected PwmLed[] pwmLeds; - /// - /// Index of specific LED blinking - /// - protected int indexLedBlinking = NONE_LED_BLINKING; - /// /// The number of the LEDs in the bar graph /// @@ -35,12 +29,12 @@ public class PwmLedBarGraph /// /// A value between 0 and 1 that controls the number of LEDs that are activated /// - public double Percentage + public float Percentage { get => percentage; set => SetPercentage(percentage = value); } - double percentage; + float percentage; /// /// Create an LedBarGraph instance for single color LED bar graphs @@ -108,7 +102,7 @@ public PwmLedBarGraph(IPwmPort[] ports, Voltage[] forwardVoltage) /// Set the percentage of LEDs that are on starting from index 0 /// /// Percentage (Range from 0 - 1) - void SetPercentage(double percentage) + protected void SetPercentage(float percentage) { if (percentage < 0 || percentage > 1) { @@ -160,29 +154,25 @@ public void SetLed(int index, bool isOn) } /// - /// Set the brightness of an individual LED when using PWM + /// Set the brightness to the LED bar graph using PWM /// - /// /// - public void SetLedBrightness(int index, double brightness) + public void SetLedBrightness(double brightness) { - if (index >= Count) + foreach (var led in pwmLeds) { - throw new ArgumentOutOfRangeException(); + led.Stop(); + led.IsOn = false; + led.Brightness = (float)brightness; } - - pwmLeds[index].Stop(); - pwmLeds[index].IsOn = false; - pwmLeds[index].Brightness = (float)brightness; } /// - /// Starts a blink animation on an individual LED + /// Set the brightness of an individual LED when using PWM /// /// - /// - /// - public void SetLedBlink(int index, float highBrightness = 1, float lowBrightness = 0) + /// + public void SetLedBrightness(int index, float brightness) { if (index >= Count) { @@ -191,60 +181,41 @@ public void SetLedBlink(int index, float highBrightness = 1, float lowBrightness pwmLeds[index].Stop(); pwmLeds[index].IsOn = false; - pwmLeds[index].StartBlink(highBrightness, lowBrightness); + pwmLeds[index].Brightness = brightness; } /// /// Starts a blink animation on an individual LED /// /// - /// - /// - /// - /// - public void SetLedBlink(int index, TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1, float lowBrightness = 0) - { - if (index >= Count) - { - throw new ArgumentOutOfRangeException(); - } - - pwmLeds[index].Stop(); - pwmLeds[index].IsOn = false; - pwmLeds[index].StartBlink(onDuration, offDuration, highBrightness, lowBrightness); - } - - /// - /// Starts a pulse animation on an individual LED - /// - /// /// /// - public void SetLedPulse(int index, float highBrightness = 1, float lowBrightness = 0.15F) + public void StartBlink(int index, float highBrightness = 1, float lowBrightness = 0) { if (index >= Count) { throw new ArgumentOutOfRangeException(); } - - pwmLeds[index].StartPulse(highBrightness, lowBrightness); + + pwmLeds[index].StartBlink(highBrightness, lowBrightness); } /// - /// Starts a pulse animation on an individual LED with the specified pulse cycle + /// Starts a blink animation on an individual LED /// /// - /// + /// + /// /// /// - public void SetLedPulse(int index, TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15F) + public void StartBlink(int index, TimeSpan highBrightnessDuration, TimeSpan lowBrightnessDuration, float highBrightness = 1, float lowBrightness = 0) { if (index >= Count) { throw new ArgumentOutOfRangeException(); } - - pwmLeds[index].StartPulse(pulseDuration, highBrightness, lowBrightness); + + pwmLeds[index].StartBlink(highBrightnessDuration, lowBrightnessDuration, highBrightness, lowBrightness); } /// @@ -254,12 +225,15 @@ public void SetLedPulse(int index, TimeSpan pulseDuration, float highBrightness /// Low brightness. public void StartBlink(float highBrightness = 1, float lowBrightness = 0) { + var highBrightnessDuration = TimeSpan.FromMilliseconds(500); + var lowBrightnessDuration = TimeSpan.FromMilliseconds(500); + Stop(); animationTask = new Task(async () => { cancellationTokenSource = new CancellationTokenSource(); - await StartBlinkAsync(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1), highBrightness, lowBrightness, cancellationTokenSource.Token); + await StartBlinkAsync(highBrightnessDuration, lowBrightnessDuration, highBrightness, lowBrightness, cancellationTokenSource.Token); }); animationTask.Start(); } @@ -267,18 +241,18 @@ public void StartBlink(float highBrightness = 1, float lowBrightness = 0) /// /// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided. /// - /// On duration. - /// Off duration. + /// On duration. + /// Off duration. /// High brigtness. /// Low brightness. - public void StartBlink(TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1, float lowBrightness = 0) + public void StartBlink(TimeSpan highBrightnessDuration, TimeSpan lowBrightnessDuration, float highBrightness = 1, float lowBrightness = 0) { Stop(); animationTask = new Task(async () => { cancellationTokenSource = new CancellationTokenSource(); - await StartBlinkAsync(onDuration, offDuration, highBrightness, lowBrightness, cancellationTokenSource.Token); + await StartBlinkAsync(highBrightnessDuration, lowBrightnessDuration, highBrightness, lowBrightness, cancellationTokenSource.Token); }); animationTask.Start(); } @@ -320,6 +294,39 @@ protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration, } } + /// + /// Starts a pulse animation on an individual LED + /// + /// + /// + /// + public void StartPulse(int index, float highBrightness = 1, float lowBrightness = 0.15F) + { + if (index >= Count) + { + throw new ArgumentOutOfRangeException(); + } + + pwmLeds[index].StartPulse(highBrightness, lowBrightness); + } + + /// + /// Starts a pulse animation on an individual LED with the specified pulse cycle + /// + /// + /// + /// + /// + public void StartPulse(int index, TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15F) + { + if (index >= Count) + { + throw new ArgumentOutOfRangeException(); + } + + pwmLeds[index].StartPulse(pulseDuration, highBrightness, lowBrightness); + } + /// /// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting. /// @@ -429,15 +436,20 @@ protected async Task StartPulseAsync(TimeSpan pulseDuration, float highBrightnes /// public void Stop() { - if (indexLedBlinking != NONE_LED_BLINKING) - { - pwmLeds[indexLedBlinking].Stop(); - indexLedBlinking = NONE_LED_BLINKING; - } - else + cancellationTokenSource?.Cancel(); + + foreach (var led in pwmLeds) { - cancellationTokenSource?.Cancel(); + led.Stop(); } } + + /// + /// Stops any animation on an individual LED and/or turns it off + /// + public void Stop(int index) + { + pwmLeds[index].Stop(); + } } } \ No newline at end of file