diff options
| author | James Barnett <james.barnett@fivium.co.uk> | 2017-08-29 12:21:38 +0100 |
|---|---|---|
| committer | James Barnett <james.barnett@fivium.co.uk> | 2017-08-29 12:21:38 +0100 |
| commit | 87ebd4bc76c85cd0f954b0e39c2e2757abff6b48 (patch) | |
| tree | 6172feb7ab8643158d8bd7481d35635e65716e1c /NucLedController/CPUUsageIndicatorControlMode.cs | |
| parent | f51203de8436052c04b4c733a910791ed1d4e3f7 (diff) | |
| download | intel-nuc-led-controller-0.3.tar.xz intel-nuc-led-controller-0.3.zip | |
Add CPU utilisaiton indicator modev0.3
Diffstat (limited to 'NucLedController/CPUUsageIndicatorControlMode.cs')
| -rw-r--r-- | NucLedController/CPUUsageIndicatorControlMode.cs | 68 |
1 files changed, 68 insertions, 0 deletions
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(); + } + } +} |