From e5b6e2ba09d259a22a6648f96c5670bb2f0dd021 Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Tue, 12 Dec 2023 16:08:28 -0800 Subject: [PATCH 1/2] Update RgbLed and RgbPwmLed with Interfaces --- .../Leds/RgbLed.Animations.cs | 31 ++---- Source/Meadow.Foundation.Core/Leds/RgbLed.cs | 45 +++++++- .../Leds/RgbLedColors.cs | 41 ------- .../Leds/RgbPwmLed.Animations.cs | 100 +++++------------ .../Meadow.Foundation.Core/Leds/RgbPwmLed.cs | 103 ++++++++++++------ 5 files changed, 140 insertions(+), 180 deletions(-) delete mode 100644 Source/Meadow.Foundation.Core/Leds/RgbLedColors.cs diff --git a/Source/Meadow.Foundation.Core/Leds/RgbLed.Animations.cs b/Source/Meadow.Foundation.Core/Leds/RgbLed.Animations.cs index 32d8d23e83..e3f6a47805 100644 --- a/Source/Meadow.Foundation.Core/Leds/RgbLed.Animations.cs +++ b/Source/Meadow.Foundation.Core/Leds/RgbLed.Animations.cs @@ -1,4 +1,5 @@ -using System; +using Meadow.Peripherals.Leds; +using System; using System.Threading; using System.Threading.Tasks; @@ -14,9 +15,7 @@ public partial class RgbLed private Task? animationTask = null; private CancellationTokenSource? cancellationTokenSource = null; - /// - /// Stops the current LED animation - /// + /// public async Task StopAnimation() { if (animationTask != null) @@ -28,29 +27,19 @@ public async Task StopAnimation() } } - /// - /// Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off) - /// + /// public Task StartBlink() { return StartBlink(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500)); } - /// - /// Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off) - /// - /// The LED color + /// public Task StartBlink(RgbLedColors color) { return StartBlink(color, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500)); } - /// - /// Start the Blink animation which sets turns the LED on and off with the specified durations and color - /// - /// The LED color - /// The duration the LED stays on - /// The duration the LED stays off + /// public async Task StartBlink( RgbLedColors color, TimeSpan onDuration, @@ -63,11 +52,7 @@ public async Task StartBlink( await StartBlink(onDuration, offDuration); } - /// - /// Start the Blink animation which sets turns the LED on and off with the specified durations and current color - /// - /// The duration the LED stays on - /// The duration the LED stays off + /// public async Task StartBlink(TimeSpan onDuration, TimeSpan offDuration) { await StopAnimation(); @@ -92,4 +77,4 @@ public async Task StartBlink(TimeSpan onDuration, TimeSpan offDuration) } } } -} +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Core/Leds/RgbLed.cs b/Source/Meadow.Foundation.Core/Leds/RgbLed.cs index 10826c0379..03f6fc2fae 100644 --- a/Source/Meadow.Foundation.Core/Leds/RgbLed.cs +++ b/Source/Meadow.Foundation.Core/Leds/RgbLed.cs @@ -1,13 +1,16 @@ using Meadow.Hardware; using Meadow.Peripherals.Leds; +using System; namespace Meadow.Foundation.Leds { /// /// Represents an RGB LED /// - public partial class RgbLed : IRgbLed + public partial class RgbLed : IRgbLed, IDisposable { + readonly bool createdPorts = false; + /// /// The current LED color /// @@ -43,6 +46,11 @@ public bool IsOn } bool isOn; + /// + /// Is the object disposed + /// + public bool IsDisposed { get; private set; } + /// /// Create instance of RgbLed /// @@ -60,7 +68,9 @@ public RgbLed( greenPin.CreateDigitalOutputPort(), bluePin.CreateDigitalOutputPort(), commonType) - { } + { + createdPorts = true; + } /// /// Create instance of RgbLed @@ -81,10 +91,7 @@ public RgbLed( Common = commonType; } - /// - /// Sets the current color of the LED. - /// - /// The color value + /// public void SetColor(RgbLedColors color) { Color = color; @@ -148,5 +155,31 @@ protected void UpdateLed(bool isOn) BluePort.State = !onState; } } + + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + /// + /// Dispose of the object + /// + /// Is disposing + public virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing && createdPorts) + { + RedPort.Dispose(); + GreenPort.Dispose(); + BluePort.Dispose(); + } + + IsDisposed = true; + } + } } } \ No newline at end of file diff --git a/Source/Meadow.Foundation.Core/Leds/RgbLedColors.cs b/Source/Meadow.Foundation.Core/Leds/RgbLedColors.cs deleted file mode 100644 index fa731d899f..0000000000 --- a/Source/Meadow.Foundation.Core/Leds/RgbLedColors.cs +++ /dev/null @@ -1,41 +0,0 @@ -namespace Meadow.Foundation.Leds -{ - /// - /// Colors for RGB Led - /// - public enum RgbLedColors - { - /// - /// Red (red LED only) - /// - Red, - /// - /// Green (green LED only) - /// - Green, - /// - /// Blue (blue LED only) - /// - Blue, - /// - /// Yellow (red and green LEDs) - /// - Yellow, - /// - /// Magenta (blue and red LEDs) - /// - Magenta, - /// - /// Cyan (blue and green LEDs) - /// - Cyan, - /// - /// White (red, green and blue LEDs) - /// - White, - /// - /// Count of colors - /// - count, - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.Animations.cs b/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.Animations.cs index 395a74217f..194cf32547 100644 --- a/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.Animations.cs +++ b/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.Animations.cs @@ -14,9 +14,7 @@ public partial class RgbPwmLed private Task? animationTask = null; private CancellationTokenSource? cancellationTokenSource = null; - /// - /// Stops the current LED animation - /// + /// public async Task StopAnimation() { if (animationTask != null) @@ -28,24 +26,13 @@ public async Task StopAnimation() } } - /// - /// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness - /// On an interval of 1 second (500ms on, 500ms off) - /// - /// The maximum brightness of the animation - /// The minimum brightness of the animation + /// public Task StartBlink(float highBrightness = 1f, float lowBrightness = 0f) { return StartBlink(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), highBrightness, lowBrightness); } - /// - /// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness - /// On an interval of 1 second (500ms on, 500ms off) - /// - /// The LED color - /// The maximum brightness of the animation - /// The minimum brightness of the animation + /// public Task StartBlink( Color color, float highBrightness = 1f, @@ -54,39 +41,7 @@ public Task StartBlink( return StartBlink(color, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), highBrightness, lowBrightness); } - /// - /// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided. - /// - /// The LED color - /// The duration the LED stays on - /// The duration the LED stays off - /// The maximum brightness of the animation - /// The minimum brightness of the animation - - public async Task StartBlink( - Color color, - TimeSpan onDuration, - TimeSpan offDuration, - float highBrightness = 1f, - float lowBrightness = 0f) - { - ValidateBrightness(highBrightness, lowBrightness); - - await StopAnimation(); - - SetColor(color); - - await StartBlink(onDuration, offDuration, highBrightness, lowBrightness); - } - - /// - /// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided. - /// - /// The duration the LED stays on - /// The duration the LED stays off - /// The maximum brightness of the animation - /// The minimum brightness of the animation - + /// public async Task StartBlink( TimeSpan onDuration, TimeSpan offDuration, @@ -117,24 +72,30 @@ public async Task StartBlink( } } - /// - /// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting - /// with a cycle time of 600ms - /// - /// The maximum brightness of the animation - /// The minimum brightness of the animation + /// + public async Task StartBlink( + Color color, + TimeSpan onDuration, + TimeSpan offDuration, + float highBrightness = 1f, + float lowBrightness = 0f) + { + ValidateBrightness(highBrightness, lowBrightness); + + await StopAnimation(); + + SetColor(color); + + await StartBlink(onDuration, offDuration, highBrightness, lowBrightness); + } + + /// public Task StartPulse(float highBrightness = 1, float lowBrightness = 0.15F) { return StartPulse(TimeSpan.FromMilliseconds(600), highBrightness, lowBrightness); } - /// - /// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting - /// with a cycle time of 600ms - /// - /// The LED color - /// The maximum brightness of the animation - /// The minimum brightness of the animation + /// public Task StartPulse( Color color, float highBrightness = 1, @@ -143,13 +104,7 @@ public Task StartPulse( return StartPulse(color, TimeSpan.FromMilliseconds(600), highBrightness, lowBrightness); } - /// - /// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided. - /// - /// The LED color - /// The pulse animation duration - /// The maximum brightness of the animation - /// The minimum brightness of the animation + /// public async Task StartPulse( Color color, TimeSpan pulseDuration, @@ -165,12 +120,7 @@ public async Task StartPulse( await StartPulse(pulseDuration, highBrightness, lowBrightness); } - /// - /// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided. - /// - /// The pulse animation duration - /// The maximum brightness of the animation - /// The minimum brightness of the animation + /// public async Task StartPulse( TimeSpan pulseDuration, float highBrightness = 1, diff --git a/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs b/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs index 6f4f9b709b..caf47f7a37 100644 --- a/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs +++ b/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs @@ -8,8 +8,10 @@ namespace Meadow.Foundation.Leds /// /// Represents a Pulse-Width-Modulation (PWM) controlled RGB LED /// - public partial class RgbPwmLed : IRgbPwmLed + public partial class RgbPwmLed : IRgbPwmLed, IDisposable { + readonly bool createdPorts = false; + static readonly Frequency DefaultFrequency = new Frequency(200, Frequency.UnitType.Hertz); readonly float DEFAULT_DUTY_CYCLE = 0f; @@ -87,6 +89,11 @@ public bool IsOn /// public Voltage BlueForwardVoltage { get; protected set; } + /// + /// Is the object disposed + /// + public bool IsDisposed { get; private set; } + /// /// Create instance of RgbPwmLed /// @@ -134,35 +141,9 @@ public RgbPwmLed( greenPwmPin.CreatePwmPort(DefaultFrequency), bluePwmPin.CreatePwmPort(DefaultFrequency), commonType) - { } - - /// - /// Create instance of RgbPwmLed - /// - /// The PWM pin for the red LED - /// The PWM pin for the green LED - /// The PWM pin for the blue LED - /// The forward voltage for the red LED - /// The forward voltage for the green LED - /// The forward voltage for the blue LED - /// Common anode or common cathode - public RgbPwmLed( - IPin redPwmPin, - IPin greenPwmPin, - IPin bluePwmPin, - Voltage redLedForwardVoltage, - Voltage greenLedForwardVoltage, - Voltage blueLedForwardVoltage, - CommonType commonType = CommonType.CommonCathode) : - this( - redPwmPin.CreatePwmPort(DefaultFrequency), - greenPwmPin.CreatePwmPort(DefaultFrequency), - bluePwmPin.CreatePwmPort(DefaultFrequency), - redLedForwardVoltage, - greenLedForwardVoltage, - blueLedForwardVoltage, - commonType) - { } + { + createdPorts = true; + } /// /// Create instance of RgbPwmLed @@ -202,6 +183,36 @@ public RgbPwmLed( ResetPwmPorts(); } + /// + /// Create instance of RgbPwmLed + /// + /// The PWM pin for the red LED + /// The PWM pin for the green LED + /// The PWM pin for the blue LED + /// The forward voltage for the red LED + /// The forward voltage for the green LED + /// The forward voltage for the blue LED + /// Common anode or common cathode + public RgbPwmLed( + IPin redPwmPin, + IPin greenPwmPin, + IPin bluePwmPin, + Voltage redLedForwardVoltage, + Voltage greenLedForwardVoltage, + Voltage blueLedForwardVoltage, + CommonType commonType = CommonType.CommonCathode) : + this( + redPwmPin.CreatePwmPort(DefaultFrequency), + greenPwmPin.CreatePwmPort(DefaultFrequency), + bluePwmPin.CreatePwmPort(DefaultFrequency), + redLedForwardVoltage, + greenLedForwardVoltage, + blueLedForwardVoltage, + commonType) + { + createdPorts = true; + } + /// /// Validates forward voltages to ensure they're within the range MIN_FORWARD_VOLTAGE to MAX_FORWARD_VOLTAGE /// @@ -258,11 +269,7 @@ public void SetBrightness(float brightness) SetColor(Color, brightness); } - /// - /// Sets the current color of the LED - /// - /// The LED color - /// Valid values are from 0 to 1, inclusive + /// public void SetColor(Color color, float brightness = 1) { if (color == Color && brightness == Brightness) @@ -277,5 +284,31 @@ public void SetColor(Color color, float brightness = 1) GreenPwm.DutyCycle = (float)(Color.G / 255.0 * maxGreenDutyCycle * brightness); BluePwm.DutyCycle = (float)(Color.B / 255.0 * maxBlueDutyCycle * brightness); } + + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + /// + /// Dispose of the object + /// + /// Is disposing + public virtual void Dispose(bool disposing) + { + if (!IsDisposed) + { + if (disposing && createdPorts) + { + RedPwm.Dispose(); + GreenPwm.Dispose(); + BluePwm.Dispose(); + } + + IsDisposed = true; + } + } } } \ No newline at end of file From 3e9ac840adfac2ad0e22f452d92e8d4beb919b68 Mon Sep 17 00:00:00 2001 From: Jorge Ramirez Date: Tue, 12 Dec 2023 16:14:17 -0800 Subject: [PATCH 2/2] Update samples that use RgbLed --- .../Leds.Led_Sample/MeadowApp.cs | 1 + .../Leds.PwmLed_Sample/MeadowApp.cs | 1 + .../Leds.RgbLed_Sample/MeadowApp.cs | 1 + Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs | 8 ++------ 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Source/Meadow.Foundation.Core.Samples/Leds.Led_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Core.Samples/Leds.Led_Sample/MeadowApp.cs index 3648f23776..975997052d 100644 --- a/Source/Meadow.Foundation.Core.Samples/Leds.Led_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Core.Samples/Leds.Led_Sample/MeadowApp.cs @@ -1,6 +1,7 @@ using Meadow; using Meadow.Devices; using Meadow.Foundation.Leds; +using Meadow.Peripherals.Leds; using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Source/Meadow.Foundation.Core.Samples/Leds.PwmLed_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Core.Samples/Leds.PwmLed_Sample/MeadowApp.cs index c1e511c8a9..c78c4db869 100644 --- a/Source/Meadow.Foundation.Core.Samples/Leds.PwmLed_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Core.Samples/Leds.PwmLed_Sample/MeadowApp.cs @@ -1,6 +1,7 @@ using Meadow; using Meadow.Devices; using Meadow.Foundation.Leds; +using Meadow.Peripherals.Leds; using Meadow.Units; using System; using System.Collections.Generic; diff --git a/Source/Meadow.Foundation.Core.Samples/Leds.RgbLed_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Core.Samples/Leds.RgbLed_Sample/MeadowApp.cs index e5ecbdae97..8261beb1cb 100644 --- a/Source/Meadow.Foundation.Core.Samples/Leds.RgbLed_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Core.Samples/Leds.RgbLed_Sample/MeadowApp.cs @@ -1,6 +1,7 @@ using Meadow; using Meadow.Devices; using Meadow.Foundation.Leds; +using Meadow.Peripherals.Leds; using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs b/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs index caf47f7a37..52813eed51 100644 --- a/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs +++ b/Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs @@ -30,9 +30,7 @@ public partial class RgbPwmLed : IRgbPwmLed, IDisposable /// public Voltage MIN_FORWARD_VOLTAGE => new Voltage(0); - /// - /// Turns on LED with current color or turns it off - /// + /// public bool IsOn { get => isOn; @@ -49,9 +47,7 @@ public bool IsOn /// public Color Color { get; protected set; } = Color.White; - /// - /// The brightness value assigned to the LED - /// + /// public float Brightness { get; protected set; } = 1f; ///