FrontView can now display time when not packed.
Now waiting 4 ticks before cycling.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
15 using System.Diagnostics;
16 using System.Windows.Forms;
18 using OpenHardwareMonitor.Hardware;
19 using OpenHardwareMonitor.Utilities;
20 using System.Runtime.InteropServices;
29 namespace OpenHardwareMonitor.GUI
31 public class SoundGraphDisplay : IDisposable
33 private IComputer computer;
34 private PersistentSettings settings;
35 private UnitManager unitManager;
36 private List<SensorFrontView> list = new List<SensorFrontView>();
37 private SoundGraph.Server iServer;
39 private int iNextSensorToDisplay=0;
40 private int iTickCounter=0;
43 public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
44 UnitManager unitManager)
46 this.computer = computer;
47 this.settings = settings;
48 this.unitManager = unitManager;
49 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
50 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
52 //Start our client if needed
53 Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
54 if (!(processes.Length > 0))
57 //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
59 Process client = new Process();
60 client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
61 client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
65 //Start our SoundGraph server
66 iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
68 //iServer.SendMessage("init:");
71 private void HardwareRemoved(IHardware hardware)
73 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
74 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
75 foreach (ISensor sensor in hardware.Sensors)
76 SensorRemoved(sensor);
77 foreach (IHardware subHardware in hardware.SubHardware)
78 HardwareRemoved(subHardware);
81 private void HardwareAdded(IHardware hardware)
83 foreach (ISensor sensor in hardware.Sensors)
85 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
86 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
87 foreach (IHardware subHardware in hardware.SubHardware)
88 HardwareAdded(subHardware);
91 private void SensorAdded(ISensor sensor)
93 if (settings.GetValue(new Identifier(sensor.Identifier,
94 "FrontView").ToString(), false))
98 private void SensorRemoved(ISensor sensor)
100 if (Contains(sensor))
101 Remove(sensor, false);
104 public void Dispose()
106 foreach (SensorFrontView icon in list)
114 public void Redraw(bool aPacked, bool aDisplayTime)
116 const int KNumberOfTickBeforeSwitch = 4;
117 const int KMaxCharacterPerLine = 16;
118 string packedFirstLine=""; //We have 16 chars per line on our VFD
119 string packedSecondLine="";
122 string time = DateTime.Now.ToShortTimeString();
124 //Update all sensors from our front view
125 foreach (SensorFrontView sensor in list)
132 string packedText = "";
133 packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
136 packedFirstLine = packedText;
141 while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
143 packedFirstLine += " ";
145 packedFirstLine += packedText;
149 packedSecondLine = packedText;
154 while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
156 packedSecondLine += " ";
158 packedSecondLine += packedText;
163 //SetText(sensor.iFirstLine, sensor.iSecondLine);
166 //Alternate between sensors
171 //string packedLine = "";
173 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
176 if (iNextSensorToDisplay==1)
178 iNextSensorToDisplay=0;
182 iNextSensorToDisplay=1;
188 SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
192 //Display packed sensors on our FrontView display
193 SetText(packedFirstLine, packedSecondLine);
200 string secondLine = list[iNextSensorToDisplay].iSecondLine;
204 while (secondLine.Length + time.Length < KMaxCharacterPerLine)
210 //Display current sensor on our FrontView display
211 SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
213 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
216 iNextSensorToDisplay++;
221 if (iNextSensorToDisplay == list.Count)
223 //Go back to first sensor
224 iNextSensorToDisplay = 0;
230 public bool Contains(ISensor sensor)
232 foreach (SensorFrontView icon in list)
233 if (icon.Sensor == sensor)
238 public void Add(ISensor sensor, bool balloonTip)
240 if (Contains(sensor))
247 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
248 //UpdateMainIconVisibilty();
249 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
250 iNextSensorToDisplay = 0;
255 public void Remove(ISensor sensor)
257 Remove(sensor, true);
258 iNextSensorToDisplay = 0;
261 private void Remove(ISensor sensor, bool deleteConfig)
266 new Identifier(sensor.Identifier, "FrontView").ToString());
268 SensorFrontView instance = null;
269 foreach (SensorFrontView icon in list)
270 if (icon.Sensor == sensor)
272 if (instance != null)
274 list.Remove(instance);
275 //UpdateMainIconVisibilty();
282 private void UpdateMainIconVisibilty()
287 mainIcon.Visible = list.Count == 0;
291 mainIcon.Visible = false;
298 iServer.SendMessage("init:");
303 iServer.SendMessage("uninit:");
306 public void SetText(string aUpperLine, string aLowerLine)
308 iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
313 iServer.SendMessage("quit:");
317 public bool IsMainIconEnabled
319 get { return mainIconEnabled; }
322 if (mainIconEnabled != value)
324 mainIconEnabled = value;
325 UpdateMainIconVisibilty();