diff options
| author | James Barnett <james.barnett@fivium.co.uk> | 2017-08-01 21:36:39 +0100 |
|---|---|---|
| committer | James Barnett <james.barnett@fivium.co.uk> | 2017-08-01 21:36:39 +0100 |
| commit | 6f8f07b1ae8d0cb739b9485b7b18e2544764206b (patch) | |
| tree | 5bd58e00d41119f7d279d8fd98c8a2cb2893ead2 /NucLedController/FadeColourCyclerControlMode.cs | |
| parent | cd68a397f67308a9b915a98837780813750e29c5 (diff) | |
| download | intel-nuc-led-controller-0.2.tar.xz intel-nuc-led-controller-0.2.zip | |
Add fade cycle modev0.2
Diffstat (limited to 'NucLedController/FadeColourCyclerControlMode.cs')
| -rw-r--r-- | NucLedController/FadeColourCyclerControlMode.cs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/NucLedController/FadeColourCyclerControlMode.cs b/NucLedController/FadeColourCyclerControlMode.cs new file mode 100644 index 0000000..ba982ca --- /dev/null +++ b/NucLedController/FadeColourCyclerControlMode.cs @@ -0,0 +1,95 @@ +using System; +using System.Linq; +using System.Timers; + +namespace NucLedController +{ + class FadeColourCyclerControlMode : IControlMode + { + private enum FadeDirection + { + Up, + Down + } + + private readonly Timer timer; + private int currentColourIndex; + private readonly int maxColourIndex = LEDColour.AvailableColours.Count - 1; + + private readonly byte brightnessStepping = 0x05; + private byte currentBrightness = 0x00; + private byte maxBrightness = 0x64; + private byte minBrightness = 0x00; + private FadeDirection fadeDirection = FadeDirection.Up; + + public FadeColourCyclerControlMode(int intervalMs) + { + timer = new Timer(); + timer.Elapsed += new ElapsedEventHandler(Tick); + + // This should be enforced through the GUI but it causes the sliders input text box to behave weirdly when typing a single number + if (intervalMs < 100) + { + intervalMs = 100; + } + + // There are 0x64 (100d) brightness values, so divide the desired interval time by brightness stepping to get the tick rate + timer.Interval = intervalMs / (100/brightnessStepping); + } + + private void Tick(object source, ElapsedEventArgs e) + { + LEDController.SetLEDState(LEDTransition.AvailableTransitions.Find(t => t.Identifier.ToString() == "ALWAYS_ON"), LEDColour.AvailableColours.ElementAt(currentColourIndex), currentBrightness); + + // TODO more of the fade duration should be spent at the lower brightness levels, since the perceived change in brightness is much more apparent at the bottom end. + // e.g. the perceived difference between 0x10 and 0x20 is much larger than 0x50 and 0x60, so scaling up linearly wastes a lot of time increasing imperceivable brightness changes at the top of the range. + // presumably its logarithmic, like with sound? + if (fadeDirection == FadeDirection.Up) + { + if(currentBrightness == maxBrightness) + { + // Fade up finished, start fading down + fadeDirection = FadeDirection.Down; + } + else + { + // Continue fading up + currentBrightness += brightnessStepping; + } + } + else // Fading down + { + if(currentBrightness == minBrightness) + { + // Fade cycle complete. Change colour and fade up + fadeDirection = FadeDirection.Up; + + if (currentColourIndex == maxColourIndex) + { + currentColourIndex = 0; + } + else + { + currentColourIndex++; + } + } + else + { + // Continue fading down + currentBrightness -= brightnessStepping; + } + } + + } + + public void Start() + { + timer.Start(); + } + + public void Stop() + { + timer.Stop(); + } + } +} |