From 87ebd4bc76c85cd0f954b0e39c2e2757abff6b48 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Tue, 29 Aug 2017 12:21:38 +0100 Subject: Add CPU utilisaiton indicator mode --- NucLedController/BlinkColourCyclerControlMode.cs | 2 +- NucLedController/CPUUsageIndicatorControlMode.cs | 68 ++++++++++++++++++++++++ NucLedController/FadeColourCyclerControlMode.cs | 2 +- NucLedController/LEDColour.cs | 7 ++- NucLedController/LEDTransition.cs | 6 ++- NucLedController/MainWindow.xaml | 9 ++-- NucLedController/MainWindow.xaml.cs | 6 +++ NucLedController/NucLedController.csproj | 1 + 8 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 NucLedController/CPUUsageIndicatorControlMode.cs diff --git a/NucLedController/BlinkColourCyclerControlMode.cs b/NucLedController/BlinkColourCyclerControlMode.cs index dac0a51..c95a007 100644 --- a/NucLedController/BlinkColourCyclerControlMode.cs +++ b/NucLedController/BlinkColourCyclerControlMode.cs @@ -23,7 +23,7 @@ namespace NucLedController private void Tick(object source, ElapsedEventArgs e) { - LEDController.SetLEDState(LEDTransition.AvailableTransitions.Find(t => t.Identifier.ToString() == "ALWAYS_ON"), LEDColour.AvailableColours.ElementAt(currentColourIndex)); + LEDController.SetLEDState(LEDTransition.getLEDTransition("ALWAYS_ON"), LEDColour.AvailableColours.ElementAt(currentColourIndex)); if (currentColourIndex == maxColourIndex) { currentColourIndex = 0; diff --git a/NucLedController/CPUUsageIndicatorControlMode.cs b/NucLedController/CPUUsageIndicatorControlMode.cs new file mode 100644 index 0000000..5d62339 --- /dev/null +++ b/NucLedController/CPUUsageIndicatorControlMode.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Timers; + +namespace NucLedController +{ + class CPUUsageIndicatorControlMode : IControlMode + { + + private readonly Timer timer; + private readonly PerformanceCounter perfCounter; + private LEDColour currentColour; + + public CPUUsageIndicatorControlMode(int intervalMs) + { + timer = new Timer(); + timer.Elapsed += new ElapsedEventHandler(Tick); + timer.Interval = intervalMs; + + perfCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + currentColour = LEDColour.getLEDColour("WHITE"); + } + + private void Tick(object source, ElapsedEventArgs e) + { + float cpuUtilisation = perfCounter.NextValue(); + + LEDColour targetColour = mapUsageToColour(cpuUtilisation); + if(currentColour != targetColour) + { + LEDController.SetLEDState(LEDTransition.getLEDTransition("ALWAYS_ON"), targetColour); + currentColour = targetColour; + } + + } + + // TODO add option to map usage to brighness + private LEDColour mapUsageToColour(float utilisation) + { + if(utilisation <= 33) + { + return LEDColour.getLEDColour("GREEN"); + } + else if (utilisation > 66) + { + return LEDColour.getLEDColour("RED"); + } + else + { + return LEDColour.getLEDColour("YELLOW"); + } + } + + public void Start() + { + timer.Start(); + } + + public void Stop() + { + timer.Start(); + } + } +} diff --git a/NucLedController/FadeColourCyclerControlMode.cs b/NucLedController/FadeColourCyclerControlMode.cs index ba982ca..9beed18 100644 --- a/NucLedController/FadeColourCyclerControlMode.cs +++ b/NucLedController/FadeColourCyclerControlMode.cs @@ -39,7 +39,7 @@ namespace NucLedController private void Tick(object source, ElapsedEventArgs e) { - LEDController.SetLEDState(LEDTransition.AvailableTransitions.Find(t => t.Identifier.ToString() == "ALWAYS_ON"), LEDColour.AvailableColours.ElementAt(currentColourIndex), currentBrightness); + LEDController.SetLEDState(LEDTransition.getLEDTransition("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. diff --git a/NucLedController/LEDColour.cs b/NucLedController/LEDColour.cs index 68fb22c..e512ef9 100644 --- a/NucLedController/LEDColour.cs +++ b/NucLedController/LEDColour.cs @@ -28,7 +28,12 @@ namespace NucLedController public override string ToString() { - return this.Identifier + this.ByteValue; + return Identifier + ByteValue; + } + + public static LEDColour getLEDColour(string identifier) + { + return AvailableColours.Find(c => c.Identifier == identifier); } } } diff --git a/NucLedController/LEDTransition.cs b/NucLedController/LEDTransition.cs index 6f226ee..d965158 100644 --- a/NucLedController/LEDTransition.cs +++ b/NucLedController/LEDTransition.cs @@ -28,8 +28,12 @@ namespace NucLedController public override string ToString() { - return this.Identifier + this.ByteValue; + return Identifier + ByteValue; } + public static LEDTransition getLEDTransition(string identifier) + { + return AvailableTransitions.Find(c => c.Identifier == identifier); + } } } diff --git a/NucLedController/MainWindow.xaml b/NucLedController/MainWindow.xaml index b42921e..80161ba 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="305.928" Width="382.999" SizeToContent="WidthAndHeight"> + Title="NUC LED Controller" Height="315.156" Width="382.999" SizeToContent="WidthAndHeight"> - + @@ -41,10 +41,11 @@ -