Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pwm Led StartPulse fix #347

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Source/Meadow.Foundation.Core/Leds/PwmLed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ protected async Task StartPulseAsync(TimeSpan pulseDuration, float highBrightnes
float brightness = lowBrightness;
bool ascending = true;
TimeSpan intervalTime = TimeSpan.FromMilliseconds(60); // 60 miliseconds is probably the fastest update we want to do, given that threads are given 20 miliseconds by default.
float steps = pulseDuration.Milliseconds / intervalTime.Milliseconds;
float changeAmount = (highBrightness - lowBrightness) / steps;
float changeUp = changeAmount;
float changeDown = -1 * changeAmount;
float steps = (float)(pulseDuration.TotalMilliseconds / intervalTime.TotalMilliseconds);
float delta = (highBrightness - lowBrightness) / steps;

Console.WriteLine($"Steps: {steps}, ChangeAmount {delta}");

while (true)
{
Expand All @@ -316,19 +316,18 @@ protected async Task StartPulseAsync(TimeSpan pulseDuration, float highBrightnes
{
ascending = true;
}
else if (Math.Abs(brightness - highBrightness) < 0.001)
else if (brightness >= highBrightness)
{
ascending = false;
}

brightness += (ascending) ? changeUp : changeDown;
brightness += delta * (ascending ? 1 : -1);

if (brightness < 0)
{
brightness = 0;
}
else
if (brightness > 1)
else if (brightness > 1)
{
brightness = 1;
}
Expand Down
10 changes: 4 additions & 6 deletions Source/Meadow.Foundation.Core/Leds/RgbPwmLed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,8 @@ protected async Task StartPulseAsync(Color color, TimeSpan pulseDuration, float
float brightness = lowBrightness;
bool ascending = true;
TimeSpan intervalTime = TimeSpan.FromMilliseconds(60); // 60 miliseconds is probably the fastest update we want to do, given that threads are given 20 miliseconds by default.
float steps = pulseDuration.Milliseconds / intervalTime.Milliseconds;
float changeAmount = (highBrightness - lowBrightness) / steps;
float changeUp = changeAmount;
float changeDown = -1 * changeAmount;
float steps = (float)(pulseDuration.TotalMilliseconds / intervalTime.TotalMilliseconds);
float delta = (highBrightness - lowBrightness) / steps;

while (true)
{
Expand All @@ -474,12 +472,12 @@ protected async Task StartPulseAsync(Color color, TimeSpan pulseDuration, float
{
ascending = true;
}
else if (Math.Abs(brightness - highBrightness) < 0.001)
else if (brightness >= highBrightness)
{
ascending = false;
}

brightness += (ascending) ? changeUp : changeDown;
brightness += delta * (ascending ? 1 : -1);

if (brightness < 0)
{
Expand Down