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

Add customizable max transition duration for settings changes #291

Merged
merged 7 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions LightBulb.PlatformInterop/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public Timer(TimeSpan firstTickDelay, TimeSpan interval, Action tick)
public Timer(TimeSpan interval, Action callback)
: this(TimeSpan.Zero, interval, callback) { }

public TimeSpan Interval => _interval;

private void Tick()
{
// Prevent multiple reentry
Expand Down
3 changes: 3 additions & 0 deletions LightBulb/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public partial class SettingsService() : SettingsBase(GetFilePath())
[ObservableProperty]
private double _configurationTransitionOffset;

[ObservableProperty]
private TimeSpan _configurationSmoothingDuration = TimeSpan.FromSeconds(15);
LilithSilver marked this conversation as resolved.
Show resolved Hide resolved

// Location

[ObservableProperty]
Expand Down
58 changes: 56 additions & 2 deletions LightBulb/ViewModels/Components/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,66 @@ private void UpdateInstant()
}
}

private const double _brightnessDefaultStep = 0.08;
private const double _temperatureDefaultStep = 30;

private ColorConfiguration _lastTarget;

private void UpdateConfiguration()
{
var isSmooth = _settingsService.IsConfigurationSmoothingEnabled && !IsCyclePreviewEnabled;
if (CurrentConfiguration == TargetConfiguration)
{
_gammaService.SetGamma(CurrentConfiguration);
return;
}

double stepsPerSecond = 1000 / _updateConfigurationTimer.Interval.TotalMilliseconds;

var isSmooth =
_settingsService.IsConfigurationSmoothingEnabled
&& !IsCyclePreviewEnabled
&& _settingsService.ConfigurationSmoothingDuration.TotalSeconds >= 0.1;

// If we've changed targets, restart with default settings.
if (_lastTarget != TargetConfiguration && isSmooth)
{
_lastTarget = TargetConfiguration;
}

double brightnessMaxStep = _brightnessDefaultStep;
double temperatureMaxStep = _temperatureDefaultStep;

// Calculate the step size...
var tempDelta = Math.Abs(
TargetConfiguration.Temperature - CurrentConfiguration.Temperature
);
var brightnessDelta = Math.Abs(
TargetConfiguration.Brightness - CurrentConfiguration.Brightness
);
var expectedTemperatureDuration = tempDelta / (temperatureMaxStep * stepsPerSecond);
var expectedBrightnessDuration = brightnessDelta / (brightnessMaxStep * stepsPerSecond);

// If the expected durations are longer than our duration limit, we adjust the step amount to stay at the max duration.
var goalDuration = Math.Max(expectedTemperatureDuration, expectedBrightnessDuration);
goalDuration = Math.Min(
goalDuration,
_settingsService.ConfigurationSmoothingDuration.TotalSeconds
);

// Calculate the step-rate needed to reach the goal.
brightnessMaxStep = brightnessDelta / (goalDuration * stepsPerSecond);
temperatureMaxStep = tempDelta / (goalDuration * stepsPerSecond);

// If we ended up slower on either of the durations, speed us up.
brightnessMaxStep = Math.Max(brightnessMaxStep, _brightnessDefaultStep);
temperatureMaxStep = Math.Max(temperatureMaxStep, _temperatureDefaultStep);

CurrentConfiguration = isSmooth
? CurrentConfiguration.StepTo(TargetConfiguration, 30, 0.008)
? CurrentConfiguration.StepTo(
TargetConfiguration,
temperatureMaxStep,
brightnessMaxStep
)
: TargetConfiguration;

_gammaService.SetGamma(CurrentConfiguration);
Expand Down