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 | |
| 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')
| -rw-r--r-- | NucLedController/FadeColourCyclerControlMode.cs | 95 | ||||
| -rw-r--r-- | NucLedController/LEDController.cs | 7 | ||||
| -rw-r--r-- | NucLedController/MainWindow.xaml | 25 | ||||
| -rw-r--r-- | NucLedController/MainWindow.xaml.cs | 9 | ||||
| -rw-r--r-- | NucLedController/NucLedController.csproj | 1 |
5 files changed, 126 insertions, 11 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(); + } + } +} 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"> <Grid Margin="10"> - <TabControl x:Name="tabControl" Height="156" VerticalAlignment="Top"> + <TabControl x:Name="tabControl" Height="223" VerticalAlignment="Top"> <TabItem x:Name="MANUAL" Header="Manual Control"> <TabItem.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> @@ -30,16 +30,21 @@ </TabItem> <TabItem x:Name="AUTO" Header="Auto"> <Grid Background="White" HorizontalAlignment="Left" Width="332" Margin="0,0,-2,0"> - <RadioButton x:Name="radioButtonCycleColour" Content="Cycle Colours" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" Width="95"/> - <Label x:Name="label2" Content="The following additional modes are not provided by the Intel WMI
Interface, they are generated and controlled by this program. They will
only work while this program is running." HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="340" Height="54" FontSize="10"/> - <Slider x:Name="sliderCycleRate" HorizontalAlignment="Left" Margin="113,81,0,0" VerticalAlignment="Top" Minimum="1" Maximum="5000" Width="110" LargeChange="100" Height="20" SmallChange="10" TickPlacement="BottomRight" IsSnapToTickEnabled="True" TickFrequency="10" IsEnabled="{Binding IsChecked, ElementName=radioButtonCycleColour}" Value="500" /> - <Label x:Name="label3" Content="Cycle Rate " HorizontalAlignment="Left" Margin="40,74,0,0" VerticalAlignment="Top"/> - <TextBox x:Name="textBoxCycleRateVal" HorizontalAlignment="Left" Height="23" Margin="230,78,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="66" Text="{Binding ElementName=sliderCycleRate, Path=Value, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsChecked, ElementName=radioButtonCycleColour}"/> - <Label x:Name="label4" Content="(milliseconds)" HorizontalAlignment="Left" Margin="35,91,0,0" VerticalAlignment="Top" Width="73" FontSize="10" RenderTransformOrigin="0.511,0.319"/> + <RadioButton x:Name="radioButtonCycleColour" Content="Cycle Colours" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" Width="95" ToolTip="Cycle between the different LED colous, chaning every n milliseconds"/> + <Label x:Name="label2" Content="The following additional modes are not provided by the Intel WMI
Interface, they are generated and controlled by this program. They will
only work while this program is running, and will consume CPU time." HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="340" Height="54" FontSize="10"/> + <Slider x:Name="sliderCycleRate" HorizontalAlignment="Left" Margin="113,74,0,0" VerticalAlignment="Top" Minimum="1" Maximum="5000" Width="110" LargeChange="100" Height="20" SmallChange="10" TickPlacement="BottomRight" IsSnapToTickEnabled="True" TickFrequency="10" IsEnabled="{Binding IsChecked, ElementName=radioButtonCycleColour}" Value="500" /> + <Label x:Name="label3" Content="Cycle Rate " HorizontalAlignment="Left" Margin="40,69,0,0" VerticalAlignment="Top"/> + <TextBox x:Name="textBoxCycleRateVal" HorizontalAlignment="Left" Height="23" Margin="228,71,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="66" Text="{Binding ElementName=sliderCycleRate, Path=Value, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsChecked, ElementName=radioButtonCycleColour}"/> + <Label x:Name="label4" Content="(milliseconds)" HorizontalAlignment="Left" Margin="35,85,0,0" VerticalAlignment="Top" Width="73" FontSize="10" RenderTransformOrigin="0.511,0.319"/> + <RadioButton x:Name="radioButtonFadeColour" Content="Fade Colours" HorizontalAlignment="Left" Margin="10,119,0,0" VerticalAlignment="Top" Width="95" ToolTip="Fade between each colour (via fading to black). Low cycle rates will use a lot of CPU!"/> + <Label x:Name="label3_Copy" Content="Cycle Rate " HorizontalAlignment="Left" Margin="40,132,0,0" VerticalAlignment="Top"/> + <Label x:Name="label4_Copy" Content="(milliseconds)" HorizontalAlignment="Left" Margin="35,148,0,0" VerticalAlignment="Top" Width="73" FontSize="10" RenderTransformOrigin="0.511,0.319"/> + <Slider x:Name="sliderFadeRate" HorizontalAlignment="Left" Margin="113,138,0,0" VerticalAlignment="Top" Minimum="1" Maximum="30000" Width="110" LargeChange="500" Height="20" SmallChange="100" TickPlacement="BottomRight" IsSnapToTickEnabled="True" TickFrequency="100" IsEnabled="{Binding IsChecked, ElementName=radioButtonFadeColour}" Value="500" /> + <TextBox x:Name="textBoxFadeRateVal" HorizontalAlignment="Left" Height="23" Margin="228,136,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="66" Text="{Binding Value, ElementName=sliderFadeRate, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsChecked, ElementName=radioButtonFadeColour}" RenderTransformOrigin="0.52,1.217"/> </Grid> </TabItem> </TabControl> - <Button x:Name="button" Content="Apply" HorizontalAlignment="Left" Margin="10,168,0,0" VerticalAlignment="Top" Width="75" Click="applyChanges"/> - <Label x:Name="lableStatusText" Content="" Margin="100,162,10,0" VerticalAlignment="Top" Foreground="#FF807F7F"/> + <Button x:Name="button" Content="Apply" HorizontalAlignment="Left" Margin="0,234,0,0" VerticalAlignment="Top" Width="75" Click="applyChanges"/> + <Label x:Name="lableStatusText" Content="" Margin="92,229,0,0" VerticalAlignment="Top" Foreground="#FF807F7F"/> </Grid> </Window> diff --git a/NucLedController/MainWindow.xaml.cs b/NucLedController/MainWindow.xaml.cs index 0d943c7..c33c522 100644 --- a/NucLedController/MainWindow.xaml.cs +++ b/NucLedController/MainWindow.xaml.cs @@ -1,8 +1,10 @@ using System; +using System.Diagnostics; using System.Linq; using System.Timers; using System.Windows; using System.Windows.Controls; +using System.Windows.Navigation; namespace NucLedController { @@ -46,6 +48,12 @@ namespace NucLedController currentControlMode.Start(); lableStatusText.Content = "Colour cycle mode enabled"; } + else if (radioButtonFadeColour.IsChecked ?? false) + { + currentControlMode = new FadeColourCyclerControlMode((int) sliderFadeRate.Value); + currentControlMode.Start(); + lableStatusText.Content = "Colour fade mode enabled"; + } } else { @@ -71,5 +79,6 @@ namespace NucLedController } } + } } diff --git a/NucLedController/NucLedController.csproj b/NucLedController/NucLedController.csproj index b5849c2..88bf080 100644 --- a/NucLedController/NucLedController.csproj +++ b/NucLedController/NucLedController.csproj @@ -64,6 +64,7 @@ <SubType>Designer</SubType> </ApplicationDefinition> <Compile Include="BlinkColourCyclerControlMode.cs" /> + <Compile Include="FadeColourCyclerControlMode.cs" /> <Compile Include="IControlMode.cs" /> <Compile Include="LEDColour.cs" /> <Compile Include="LEDController.cs" /> |