From 6f8f07b1ae8d0cb739b9485b7b18e2544764206b Mon Sep 17 00:00:00 2001 From: James Barnett Date: Tue, 1 Aug 2017 21:36:39 +0100 Subject: Add fade cycle mode --- NucLedController/FadeColourCyclerControlMode.cs | 95 +++++++++++++++++++++++++ NucLedController/LEDController.cs | 7 +- NucLedController/MainWindow.xaml | 25 ++++--- NucLedController/MainWindow.xaml.cs | 9 +++ NucLedController/NucLedController.csproj | 1 + 5 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 NucLedController/FadeColourCyclerControlMode.cs 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(); + } + } +} diff --git a/NucLedController/LEDController.cs b/NucLedController/LEDController.cs index 31dc1b0..f070647 100644 --- a/NucLedController/LEDController.cs +++ b/NucLedController/LEDController.cs @@ -8,7 +8,12 @@ public static void SetLEDState(LEDTransition transition, LEDColour colour) { - byte brightness = 0x64; // Just hardcode to max brightness for now + byte brightness = 0x64; // Max brightness value + WMISystemManagement.Instance.WriteData(new byte[] { RING_LED_COMMAND_CODE, brightness, transition.ByteValue, colour.ByteValue }); + } + + public static void SetLEDState(LEDTransition transition, LEDColour colour, byte brightness) + { WMISystemManagement.Instance.WriteData(new byte[] { RING_LED_COMMAND_CODE, brightness, transition.ByteValue, colour.ByteValue }); } diff --git a/NucLedController/MainWindow.xaml b/NucLedController/MainWindow.xaml index dbe0113..b42921e 100644 --- a/NucLedController/MainWindow.xaml +++ b/NucLedController/MainWindow.xaml @@ -5,9 +5,9 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:NucLedController" mc:Ignorable="d" - Title="NUC LED Controller" Height="243.262" Width="364.333" SizeToContent="WidthAndHeight"> + Title="NUC LED Controller" Height="305.928" Width="382.999" SizeToContent="WidthAndHeight"> - + @@ -30,16 +30,21 @@ - - -