Skip to content

Commit

Permalink
Merge pull request #863 from WildernessLabs/jorge-rgbled-updates
Browse files Browse the repository at this point in the history
Update RgbLed and RgbPwmLed
  • Loading branch information
adrianstevens authored Dec 13, 2023
2 parents b24b5b5 + 3e9ac84 commit 778b62d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 186 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
31 changes: 8 additions & 23 deletions Source/Meadow.Foundation.Core/Leds/RgbLed.Animations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Meadow.Peripherals.Leds;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -14,9 +15,7 @@ public partial class RgbLed
private Task? animationTask = null;
private CancellationTokenSource? cancellationTokenSource = null;

/// <summary>
/// Stops the current LED animation
/// </summary>
///<inheritdoc/>
public async Task StopAnimation()
{
if (animationTask != null)
Expand All @@ -28,29 +27,19 @@ public async Task StopAnimation()
}
}

/// <summary>
/// Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off)
/// </summary>
///<inheritdoc/>
public Task StartBlink()
{
return StartBlink(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500));
}

/// <summary>
/// Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off)
/// </summary>
/// <param name="color">The LED color</param>
///<inheritdoc/>
public Task StartBlink(RgbLedColors color)
{
return StartBlink(color, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500));
}

/// <summary>
/// Start the Blink animation which sets turns the LED on and off with the specified durations and color
/// </summary>
/// <param name="color">The LED color</param>
/// <param name="onDuration">The duration the LED stays on</param>
/// <param name="offDuration">The duration the LED stays off</param>
///<inheritdoc/>
public async Task StartBlink(
RgbLedColors color,
TimeSpan onDuration,
Expand All @@ -63,11 +52,7 @@ public async Task StartBlink(
await StartBlink(onDuration, offDuration);
}

/// <summary>
/// Start the Blink animation which sets turns the LED on and off with the specified durations and current color
/// </summary>
/// <param name="onDuration">The duration the LED stays on</param>
/// <param name="offDuration">The duration the LED stays off</param>
///<inheritdoc/>
public async Task StartBlink(TimeSpan onDuration, TimeSpan offDuration)
{
await StopAnimation();
Expand All @@ -92,4 +77,4 @@ public async Task StartBlink(TimeSpan onDuration, TimeSpan offDuration)
}
}
}
}
}
45 changes: 39 additions & 6 deletions Source/Meadow.Foundation.Core/Leds/RgbLed.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using Meadow.Hardware;
using Meadow.Peripherals.Leds;
using System;

namespace Meadow.Foundation.Leds
{
/// <summary>
/// Represents an RGB LED
/// </summary>
public partial class RgbLed : IRgbLed
public partial class RgbLed : IRgbLed, IDisposable
{
readonly bool createdPorts = false;

/// <summary>
/// The current LED color
/// </summary>
Expand Down Expand Up @@ -43,6 +46,11 @@ public bool IsOn
}
bool isOn;

/// <summary>
/// Is the object disposed
/// </summary>
public bool IsDisposed { get; private set; }

/// <summary>
/// Create instance of RgbLed
/// </summary>
Expand All @@ -60,7 +68,9 @@ public RgbLed(
greenPin.CreateDigitalOutputPort(),
bluePin.CreateDigitalOutputPort(),
commonType)
{ }
{
createdPorts = true;
}

/// <summary>
/// Create instance of RgbLed
Expand All @@ -81,10 +91,7 @@ public RgbLed(
Common = commonType;
}

/// <summary>
/// Sets the current color of the LED.
/// </summary>
/// <param name="color">The color value</param>
///<inheritdoc/>
public void SetColor(RgbLedColors color)
{
Color = color;
Expand Down Expand Up @@ -148,5 +155,31 @@ protected void UpdateLed(bool isOn)
BluePort.State = !onState;
}
}

///<inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Dispose of the object
/// </summary>
/// <param name="disposing">Is disposing</param>
public virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing && createdPorts)
{
RedPort.Dispose();
GreenPort.Dispose();
BluePort.Dispose();
}

IsDisposed = true;
}
}
}
}
41 changes: 0 additions & 41 deletions Source/Meadow.Foundation.Core/Leds/RgbLedColors.cs

This file was deleted.

100 changes: 25 additions & 75 deletions Source/Meadow.Foundation.Core/Leds/RgbPwmLed.Animations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public partial class RgbPwmLed
private Task? animationTask = null;
private CancellationTokenSource? cancellationTokenSource = null;

/// <summary>
/// Stops the current LED animation
/// </summary>
///<inheritdoc/>
public async Task StopAnimation()
{
if (animationTask != null)
Expand All @@ -28,24 +26,13 @@ public async Task StopAnimation()
}
}

/// <summary>
/// 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)
/// </summary>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>
///<inheritdoc/>
public Task StartBlink(float highBrightness = 1f, float lowBrightness = 0f)
{
return StartBlink(TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), highBrightness, lowBrightness);
}

/// <summary>
/// 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)
/// </summary>
/// <param name="color">The LED color</param>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>
///<inheritdoc/>
public Task StartBlink(
Color color,
float highBrightness = 1f,
Expand All @@ -54,39 +41,7 @@ public Task StartBlink(
return StartBlink(color, TimeSpan.FromMilliseconds(500), TimeSpan.FromMilliseconds(500), highBrightness, lowBrightness);
}

/// <summary>
/// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided.
/// </summary>
/// <param name="color">The LED color</param>
/// <param name="onDuration">The duration the LED stays on</param>
/// <param name="offDuration">The duration the LED stays off</param>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>

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);
}

/// <summary>
/// Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided.
/// </summary>
/// <param name="onDuration">The duration the LED stays on</param>
/// <param name="offDuration">The duration the LED stays off</param>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>

///<inheritdoc/>
public async Task StartBlink(
TimeSpan onDuration,
TimeSpan offDuration,
Expand Down Expand Up @@ -117,24 +72,30 @@ public async Task StartBlink(
}
}

/// <summary>
/// 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
/// </summary>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>
///<inheritdoc/>
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);
}

///<inheritdoc/>
public Task StartPulse(float highBrightness = 1, float lowBrightness = 0.15F)
{
return StartPulse(TimeSpan.FromMilliseconds(600), highBrightness, lowBrightness);
}

/// <summary>
/// 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
/// </summary>
/// <param name="color">The LED color</param>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>
///<inheritdoc/>
public Task StartPulse(
Color color,
float highBrightness = 1,
Expand All @@ -143,13 +104,7 @@ public Task StartPulse(
return StartPulse(color, TimeSpan.FromMilliseconds(600), highBrightness, lowBrightness);
}

/// <summary>
/// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.
/// </summary>
/// <param name="color">The LED color</param>
/// <param name="pulseDuration">The pulse animation duration</param>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>
///<inheritdoc/>
public async Task StartPulse(
Color color,
TimeSpan pulseDuration,
Expand All @@ -165,12 +120,7 @@ public async Task StartPulse(
await StartPulse(pulseDuration, highBrightness, lowBrightness);
}

/// <summary>
/// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.
/// </summary>
/// <param name="pulseDuration">The pulse animation duration</param>
/// <param name="highBrightness">The maximum brightness of the animation</param>
/// <param name="lowBrightness">The minimum brightness of the animation</param>
///<inheritdoc/>
public async Task StartPulse(
TimeSpan pulseDuration,
float highBrightness = 1,
Expand Down
Loading

0 comments on commit 778b62d

Please sign in to comment.