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/CPUUsageIndicatorControlMode.cs | 68 ++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 NucLedController/CPUUsageIndicatorControlMode.cs (limited to 'NucLedController/CPUUsageIndicatorControlMode.cs') 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(); + } + } +} -- cgit v1.2.3