]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/LEDNotifier/CPUUsageApp/CPUMonitor.cs
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / LEDNotifier / CPUUsageApp / CPUMonitor.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Microsoft.Win32;
10
11 namespace CPUMonitor
12 {
13 public partial class frmCPU : Form
14 {
15 private RegistryKey AppRegKey;
16
17 private const int LIGHT_MAX = 0x1F;
18
19 public frmCPU()
20 {
21 InitializeComponent();
22
23 nicoNotifyIcon.Icon = this.Icon;
24 nicoNotifyIcon.MouseClick += new MouseEventHandler(TrayIconClick);
25 }
26
27 private void Form1_Load(object sender, EventArgs e)
28 {
29 AppRegKey = Registry.CurrentUser.CreateSubKey("Software\\CPUMonitor");
30
31 String[] PortNames = System.IO.Ports.SerialPort.GetPortNames();
32 Array.Sort<String>(PortNames, delegate(string strA, string strB) { return int.Parse(strA.Substring(3)).CompareTo(int.Parse(strB.Substring(3))); });
33 cmbComPort.Items.Clear();
34 cmbComPort.Items.AddRange(PortNames);
35
36 cmbComPort.SelectedIndex = System.Convert.ToInt32(AppRegKey.GetValue("Port", "1")) - 1;
37 serSerialPort.PortName = cmbComPort.Text;
38
39 Hide();
40 }
41
42 private void NotifyLight(int Red, int Green, int Blue)
43 {
44 byte[] buffer = new byte[3];
45 buffer[0] = (byte)(0x80 | (Red & LIGHT_MAX));
46 buffer[1] = (byte)(0x40 | (Green & LIGHT_MAX));
47 buffer[2] = (byte)(0x20 | (Blue & LIGHT_MAX));
48
49 try
50 {
51 serSerialPort.PortName = cmbComPort.Text;
52 serSerialPort.Open();
53 serSerialPort.Write(buffer, 0, buffer.Length);
54 serSerialPort.Close();
55 }
56 catch (Exception e)
57 {
58
59 }
60 }
61
62 private void tmrCPUTimer_Tick(object sender, EventArgs e)
63 {
64 float CPUUsage = pcCPUUsage.NextValue();
65
66 int Red = 0;
67 int Green = 0;
68 int Blue = 0;
69
70 if (CPUUsage < 25)
71 {
72 Green = (int)((LIGHT_MAX / 25) * CPUUsage);
73 }
74 else if (CPUUsage < 50)
75 {
76 Blue = (int)((LIGHT_MAX / 25) * (CPUUsage - 25));
77 Green = LIGHT_MAX - Blue;
78 }
79 else if (CPUUsage < 75)
80 {
81 Red = (int)((LIGHT_MAX / 25) * (CPUUsage - 50));
82 Blue = LIGHT_MAX - Red;
83 }
84 else
85 {
86 Red = LIGHT_MAX;
87 }
88
89 NotifyLight(Red, Green, Blue);
90 lblCPU.Text = ((int)CPUUsage).ToString() + "%";
91 }
92
93 private void btnExit_Click(object sender, EventArgs e)
94 {
95 Application.Exit();
96 }
97
98 private void btnMinimizeToTray_Click(object sender, EventArgs e)
99 {
100 this.Hide();
101 }
102
103 private void TrayIconClick(object sender, MouseEventArgs e)
104 {
105 this.Show();
106 this.WindowState = FormWindowState.Normal;
107 }
108
109 private void cbPort_SelectedIndexChanged(object sender, EventArgs e)
110 {
111 AppRegKey.SetValue("Port", cmbComPort.SelectedIndex + 1);
112 serSerialPort.PortName = cmbComPort.Text;
113 }
114 }
115 }
Imprint / Impressum