Skip to content

Commit

Permalink
Add customizable max transition duration for settings changes (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithSilver authored May 6, 2024
1 parent 9e8b788 commit 548548c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
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 _configurationSmoothingMaxDuration = TimeSpan.FromSeconds(15);

// 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.ConfigurationSmoothingMaxDuration.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.ConfigurationSmoothingMaxDuration.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

0 comments on commit 548548c

Please sign in to comment.