Skip to content

Commit

Permalink
Merge pull request #380 from WildernessLabs/jorge-mf-core-cleanup
Browse files Browse the repository at this point in the history
Meadow.Foundation.Core comment updates and cleanup
  • Loading branch information
adrianstevens authored Jul 29, 2022
2 parents c1adefe + 0dc11d2 commit 70da2b2
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 158 deletions.
200 changes: 100 additions & 100 deletions Source/Meadow.Foundation.Core/Leds/Led.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,116 +10,116 @@ namespace Meadow.Foundation.Leds
/// Represents a simple LED
/// </summary>
public class Led : ILed
{
Task? animationTask;
CancellationTokenSource? cancellationTokenSource;
{
private Task? animationTask;
private CancellationTokenSource? cancellationTokenSource;

/// <summary>
/// Gets the port that is driving the LED
/// </summary>
/// <value>The port</value>
protected IDigitalOutputPort Port { get; set; }
/// <summary>
/// Gets the port that is driving the LED
/// </summary>
/// <value>The port</value>
protected IDigitalOutputPort Port { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this <see cref="T:Meadow.Foundation.Leds.Led"/> is on.
/// </summary>
/// <value><c>true</c> if is on; otherwise, <c>false</c>.</value>
public bool IsOn
{
get => isOn;
set
{
isOn = value;
Port.State = isOn;
}
}
bool isOn;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="T:Meadow.Foundation.Leds.Led"/> is on.
/// </summary>
/// <value><c>true</c> if is on; otherwise, <c>false</c>.</value>
public bool IsOn
{
get => isOn;
set
{
isOn = value;
Port.State = isOn;
}
}
bool isOn;

/// <summary>
/// Creates a LED through a pin directly from the Digital IO of the board
/// </summary>
/// <param name="device">IDigitalOutputController to instantiate output port</param>
/// <param name="pin"></param>
public Led(IDigitalOutputController device, IPin pin) :
this(device.CreateDigitalOutputPort(pin, false))
{ }
/// <summary>
/// Creates a LED through a pin directly from the Digital IO of the board
/// </summary>
/// <param name="device">IDigitalOutputController to instantiate output port</param>
/// <param name="pin"></param>
public Led(IDigitalOutputController device, IPin pin) :
this(device.CreateDigitalOutputPort(pin, false))
{ }

/// <summary>
/// Creates a LED through a DigitalOutPutPort from an IO Expander
/// </summary>
/// <param name="port"></param>
public Led(IDigitalOutputPort port)
{
Port = port;
}
/// <summary>
/// Creates a LED through a DigitalOutPutPort from an IO Expander
/// </summary>
/// <param name="port"></param>
public Led(IDigitalOutputPort port)
{
Port = port;
}

/// <summary>
/// Stops the LED when its blinking and/or turns it off.
/// </summary>
public void Stop()
{
cancellationTokenSource?.Cancel();
IsOn = false;
}
/// <summary>
/// Stops the LED when its blinking and/or turns it off.
/// </summary>
public void Stop()
{
cancellationTokenSource?.Cancel();
IsOn = false;
}

/// <summary>
/// Blink animation that turns the LED on (500ms) and off (500ms)
/// </summary>
public void StartBlink()
{
var onDuration = TimeSpan.FromMilliseconds(500);
var offDuration = TimeSpan.FromMilliseconds(500);
/// <summary>
/// Blink animation that turns the LED on (500ms) and off (500ms)
/// </summary>
public void StartBlink()
{
var onDuration = TimeSpan.FromMilliseconds(500);
var offDuration = TimeSpan.FromMilliseconds(500);

Stop();
Stop();

animationTask = new Task(async () =>
{
cancellationTokenSource = new CancellationTokenSource();
await StartBlinkAsync(onDuration, offDuration, cancellationTokenSource.Token);
});
animationTask.Start();
}
animationTask = new Task(async () =>
{
cancellationTokenSource = new CancellationTokenSource();
await StartBlinkAsync(onDuration, offDuration, cancellationTokenSource.Token);
});
animationTask.Start();
}

/// <summary>
/// Blink animation that turns the LED on and off based on the OnDuration and offDuration values in ms
/// </summary>
/// <param name="onDuration"></param>
/// <param name="offDuration"></param>
public void StartBlink(TimeSpan onDuration, TimeSpan offDuration)
{
Stop();
/// <summary>
/// Blink animation that turns the LED on and off based on the OnDuration and offDuration values in ms
/// </summary>
/// <param name="onDuration"></param>
/// <param name="offDuration"></param>
public void StartBlink(TimeSpan onDuration, TimeSpan offDuration)
{
Stop();

animationTask = new Task(async () =>
{
cancellationTokenSource = new CancellationTokenSource();
await StartBlinkAsync(onDuration, offDuration, cancellationTokenSource.Token);
});
animationTask.Start();
}
/// <summary>
/// Set LED to blink
/// </summary>
/// <param name="onDuration">on duration in ms</param>
/// <param name="offDuration">off duration in ms</param>
/// <param name="cancellationToken">cancellation token used to cancel blink</param>
/// <returns></returns>
protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration, CancellationToken cancellationToken)
{
while (true)
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
animationTask = new Task(async () =>
{
cancellationTokenSource = new CancellationTokenSource();
await StartBlinkAsync(onDuration, offDuration, cancellationTokenSource.Token);
});
animationTask.Start();
}
/// <summary>
/// Set LED to blink
/// </summary>
/// <param name="onDuration">on duration in ms</param>
/// <param name="offDuration">off duration in ms</param>
/// <param name="cancellationToken">cancellation token used to cancel blink</param>
/// <returns></returns>
protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration, CancellationToken cancellationToken)
{
while (true)
{
if (cancellationToken.IsCancellationRequested)
{
break;
}

Port.State = true;
await Task.Delay(onDuration);
Port.State = false;
await Task.Delay(offDuration);
}
Port.State = true;
await Task.Delay(onDuration);
Port.State = false;
await Task.Delay(offDuration);
}

Port.State = IsOn;
}
}
Port.State = IsOn;
}
}
}
17 changes: 15 additions & 2 deletions Source/Meadow.Foundation.Core/Leds/PwmLed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public void SetBrightness(float brightness)
/// <summary>
/// Start a Blink animation which sets the brightness of the LED alternating between a low and high brightness setting.
/// </summary>
/// <param name="highBrightness"></param>
/// <param name="lowBrightness"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="Exception"></exception>
public void StartBlink(float highBrightness = 1f, float lowBrightness = 0f)
{
var onDuration = TimeSpan.FromMilliseconds(500);
Expand Down Expand Up @@ -207,6 +211,12 @@ public void StartBlink(float highBrightness = 1f, float lowBrightness = 0f)
/// <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"></param>
/// <param name="offDuration"></param>
/// <param name="highBrightness"></param>
/// <param name="lowBrightness"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="Exception"></exception>
public void StartBlink(TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1f, float lowBrightness = 0f)
{
if (highBrightness > 1 || highBrightness <= 0)
Expand Down Expand Up @@ -240,7 +250,6 @@ public void StartBlink(TimeSpan onDuration, TimeSpan offDuration, float highBrig
/// <param name="highBrightness">maximum brightness</param>
/// <param name="lowBrightness">minimum brightness</param>
/// <param name="cancellationToken">token for cancellation</param>
/// <returns></returns>
protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)
{
while (true)
Expand All @@ -262,6 +271,8 @@ protected async Task StartBlinkAsync(TimeSpan onDuration, TimeSpan offDuration,
/// <summary>
/// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting.
/// </summary>
/// <param name="highBrightness"></param>
/// <param name="lowBrightness"></param>
public void StartPulse(float highBrightness = 1, float lowBrightness = 0.15F)
{
var pulseDuration = TimeSpan.FromMilliseconds(600);
Expand Down Expand Up @@ -292,6 +303,9 @@ public void StartPulse(float highBrightness = 1, float lowBrightness = 0.15F)
/// <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"></param>
/// <param name="highBrightness"></param>
/// <param name="lowBrightness"></param>
public void StartPulse(TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15F)
{
if (highBrightness > 1 || highBrightness <= 0)
Expand Down Expand Up @@ -324,7 +338,6 @@ public void StartPulse(TimeSpan pulseDuration, float highBrightness = 1, float l
/// <param name="highBrightness">maximum brightness</param>
/// <param name="lowBrightness">minimum brightness</param>
/// <param name="cancellationToken">token used to cancel pulse</param>
/// <returns></returns>
protected async Task StartPulseAsync(TimeSpan pulseDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)
{
float brightness = lowBrightness;
Expand Down
10 changes: 5 additions & 5 deletions Source/Meadow.Foundation.Core/Leds/PwmLedBarGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ public void StartBlink(TimeSpan onDuration, TimeSpan offDuration, float highBrig

/// <summary>
/// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting.
/// <param name="highBrightness">High brigtness.</param>
/// <param name="lowBrightness">Low brightness.</param>
/// </summary>
/// <param name="highBrightness"></param>
/// <param name="lowBrightness"></param>
public void StartPulse(float highBrightness = 1, float lowBrightness = 0.15F)
{
if (highBrightness > 1 || highBrightness <= 0)
Expand All @@ -291,10 +291,10 @@ public void StartPulse(float highBrightness = 1, float lowBrightness = 0.15F)

/// <summary>
/// Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.
/// <param name="pulseDuration">Pulse duration.</param>
/// <param name="highBrightness">High brigtness.</param>
/// <param name="lowBrightness">Low brightness.</param>
/// </summary>
/// <param name="pulseDuration"></param>
/// <param name="highBrightness"></param>
/// <param name="lowBrightness"></param>
public void StartPulse(TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15F)
{
if (highBrightness > 1 || highBrightness <= 0)
Expand Down
5 changes: 2 additions & 3 deletions Source/Meadow.Foundation.Core/Leds/RgbLed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Meadow.Foundation.Leds
/// </summary>
public partial class RgbLed : IRgbLed
{
Task? animationTask;
CancellationTokenSource? cancellationTokenSource;
private Task? animationTask;
private CancellationTokenSource? cancellationTokenSource;

/// <summary>
/// Get the color the LED has been set to.
Expand Down Expand Up @@ -200,7 +200,6 @@ public void StartBlink(RgbLedColors color, TimeSpan onDuration, TimeSpan offDura
/// <param name="onDuration"></param>
/// <param name="offDuration"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
protected async Task StartBlinkAsync(RgbLedColors color, TimeSpan onDuration, TimeSpan offDuration, CancellationToken cancellationToken)
{
while (true)
Expand Down
6 changes: 2 additions & 4 deletions Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace Meadow.Foundation.Leds
/// </summary>
public class RgbPwmLed
{
Task? animationTask = null;
CancellationTokenSource? cancellationTokenSource = null;
private Task? animationTask = null;
private CancellationTokenSource? cancellationTokenSource = null;

static readonly Frequency DefaultFrequency = new Frequency(200, Frequency.UnitType.Hertz);
readonly float DEFAULT_DUTY_CYCLE = 0f;
Expand Down Expand Up @@ -356,7 +356,6 @@ public void StartBlink(Color color, TimeSpan onDuration, TimeSpan offDuration, f
/// <param name="highBrightness">maximum brightness</param>
/// <param name="lowBrightness">minimum brightness</param>
/// <param name="cancellationToken">token to cancel blink</param>
/// <returns></returns>
protected async Task StartBlinkAsync(Color color, TimeSpan onDuration, TimeSpan offDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)
{
while (true)
Expand Down Expand Up @@ -452,7 +451,6 @@ public void StartPulse(Color color, TimeSpan pulseDuration, float highBrightness
/// <param name="highBrightness">maximum brightness</param>
/// <param name="lowBrightness">minimum brightness</param>
/// <param name="cancellationToken">token to cancel pulse</param>
/// <returns></returns>
protected async Task StartPulseAsync(Color color, TimeSpan pulseDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)
{
float brightness = lowBrightness;
Expand Down
4 changes: 1 addition & 3 deletions Source/Meadow.Foundation.Core/Motors/HBridgeMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,9 @@ public HBridgeMotor(IMeadowDevice device, IPin a1Pin, IPin a2Pin, IPin enablePin
/// <param name="a1Port"></param>
/// <param name="a2Port"></param>
/// <param name="enablePort"></param>
/// <param name="pwmFrequency"></param>
public HBridgeMotor(IPwmPort a1Port, IPwmPort a2Port, IDigitalOutputPort enablePort)
: this(a1Port, a2Port, enablePort, DefaultFrequency)
{
}
{ }

/// <summary>
/// Create an HBridgeMotor object
Expand Down
1 change: 0 additions & 1 deletion Source/Meadow.Foundation.Core/Relays/Relay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Meadow.Devices;
using Meadow.Hardware;
using Meadow.Peripherals.Relays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public abstract class AnalogObservableBase : SensorBase<Voltage>
/// </summary>
public Voltage Voltage { get; protected set; }


/// <summary>
/// Creates a new AnalogObservableBase driver
/// </summary>
Expand All @@ -31,8 +30,7 @@ public abstract class AnalogObservableBase : SensorBase<Voltage>
/// <param name="voltage">max voltage of analog port</param>
public AnalogObservableBase(IAnalogInputController device, IPin pin, int sampleCount = 5, TimeSpan? sampleInterval = null, Voltage? voltage = null)
: this(device.CreateAnalogInputPort(pin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), voltage ?? new Voltage(3.3)))
{
}
{ }

/// <summary>
/// Creates a new AnalogObservableBase driver
Expand Down Expand Up @@ -96,7 +94,7 @@ public void StopUpdating()
lock (samplingLock)
{
if (!IsSampling) return;
base.IsSampling = false;
IsSampling = false;
AnalogInputPort.StopUpdating();
}
}
Expand Down
Loading

0 comments on commit 70da2b2

Please sign in to comment.