Implementing FrontView support for packed mode and time display.
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 string packedFirstLine=""; //We have 16 chars per line on our VFD
117 string packedSecondLine="";
120 //Update all sensors from our front view
121 foreach (SensorFrontView sensor in list)
128 string packedText = "";
129 packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
132 packedFirstLine = packedText;
137 while (packedFirstLine.Length + packedText.Length < 16)
139 packedFirstLine += " ";
141 packedFirstLine += packedText;
145 packedSecondLine = packedText;
150 while (packedSecondLine.Length + packedText.Length < 16)
152 packedSecondLine += " ";
154 packedSecondLine += packedText;
159 //SetText(sensor.iFirstLine, sensor.iSecondLine);
162 //Alternate between sensors
167 //string packedLine = "";
169 if (iTickCounter == 2) //Move to the next sensor only every second tick
172 if (iNextSensorToDisplay==1)
174 iNextSensorToDisplay=0;
178 iNextSensorToDisplay=1;
184 string time = DateTime.Now.ToShortTimeString();
185 SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
189 //Display packed sensors on our FrontView display
190 SetText(packedFirstLine, packedSecondLine);
197 //Display current sensor on our FrontView display
198 SetText(list[iNextSensorToDisplay].iFirstLine, list[iNextSensorToDisplay].iSecondLine);
200 if (iTickCounter == 2) //Move to the next sensor only every second tick
203 iNextSensorToDisplay++;
208 if (iNextSensorToDisplay == list.Count)
210 //Go back to first sensor
211 iNextSensorToDisplay = 0;
217 public bool Contains(ISensor sensor)
219 foreach (SensorFrontView icon in list)
220 if (icon.Sensor == sensor)
225 public void Add(ISensor sensor, bool balloonTip)
227 if (Contains(sensor))
234 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
235 //UpdateMainIconVisibilty();
236 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
237 iNextSensorToDisplay = 0;
242 public void Remove(ISensor sensor)
244 Remove(sensor, true);
245 iNextSensorToDisplay = 0;
248 private void Remove(ISensor sensor, bool deleteConfig)
253 new Identifier(sensor.Identifier, "FrontView").ToString());
255 SensorFrontView instance = null;
256 foreach (SensorFrontView icon in list)
257 if (icon.Sensor == sensor)
259 if (instance != null)
261 list.Remove(instance);
262 //UpdateMainIconVisibilty();
269 private void UpdateMainIconVisibilty()
274 mainIcon.Visible = list.Count == 0;
278 mainIcon.Visible = false;
285 iServer.SendMessage("init:");
290 iServer.SendMessage("uninit:");
293 public void SetText(string aUpperLine, string aLowerLine)
295 iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
300 iServer.SendMessage("quit:");
304 public bool IsMainIconEnabled
306 get { return mainIconEnabled; }
309 if (mainIconEnabled != value)
311 mainIconEnabled = value;
312 UpdateMainIconVisibilty();