Quick and dearty usage of our new SharpDisplay layout for packed mode.
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))
56 //Try to launch the sound graph process from the same folder as this executable
57 string exeName=System.IO.Path.GetDirectoryName(Application.ExecutablePath);
58 exeName += @"\SoundGraphAccess.exe";
59 Process client = UserAccountControl.CreateProcessAsStandardUser(exeName,"");
62 //Start our SoundGraph server
63 iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
67 private void HardwareRemoved(IHardware hardware)
69 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
70 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
71 foreach (ISensor sensor in hardware.Sensors)
72 SensorRemoved(sensor);
73 foreach (IHardware subHardware in hardware.SubHardware)
74 HardwareRemoved(subHardware);
77 private void HardwareAdded(IHardware hardware)
79 foreach (ISensor sensor in hardware.Sensors)
81 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
82 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
83 foreach (IHardware subHardware in hardware.SubHardware)
84 HardwareAdded(subHardware);
87 private void SensorAdded(ISensor sensor)
89 if (settings.GetValue(new Identifier(sensor.Identifier,
90 "FrontView").ToString(), false))
94 private void SensorRemoved(ISensor sensor)
97 Remove(sensor, false);
100 public void Dispose()
102 foreach (SensorFrontView icon in list)
110 public void Redraw(bool aPacked, bool aDisplayTime)
112 const int KNumberOfTickBeforeSwitch = 4;
113 const int KMaxCharacterPerLine = 16;
114 string packedFirstLine=""; //We have 16 chars per line on our VFD
115 string packedSecondLine="";
118 string time = DateTime.Now.ToShortTimeString();
120 //Update all sensors from our front view
121 foreach (SensorFrontView sensor in list)
126 if (aDisplayTime && count == 1)
128 //First slot is take by time display
130 packedFirstLine = time + " ";
135 //Build strings for packed mode
136 string packedText = "";
137 packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
140 packedFirstLine = packedText + " "; //Minimum one space to separate sensors on the same line
144 //Add enough spaces to align to right hand side
145 while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
147 packedFirstLine += " ";
149 packedFirstLine += packedText;
153 packedSecondLine = packedText + " "; //Minimum one space to separate sensors on the same line
157 //Add enough spaces to align to right hand side
158 while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
160 packedSecondLine += " ";
162 packedSecondLine += packedText;
165 //SetText(sensor.iFirstLine, sensor.iSecondLine);
168 //Alternate between sensors
173 //string packedLine = "";
175 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
178 if (iNextSensorToDisplay==1)
180 iNextSensorToDisplay=0;
184 iNextSensorToDisplay=1;
188 //TODO: Do something like that to cycle lines if ever we want to
189 //SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
191 //Display packed sensors on our FrontView display
192 SetText(packedFirstLine, packedSecondLine);
196 string secondLine = list[iNextSensorToDisplay].iSecondLine;
200 while (secondLine.Length + time.Length < KMaxCharacterPerLine)
206 //Display current sensor on our FrontView display
207 SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
209 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
212 iNextSensorToDisplay++;
217 if (iNextSensorToDisplay == list.Count)
219 //Go back to first sensor
220 iNextSensorToDisplay = 0;
226 public bool Contains(ISensor sensor)
228 foreach (SensorFrontView icon in list)
229 if (icon.Sensor == sensor)
234 public void Add(ISensor sensor, bool balloonTip)
236 if (Contains(sensor))
243 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
244 //UpdateMainIconVisibilty();
245 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
246 iNextSensorToDisplay = 0;
249 //Just added first sensor in FrontView, unable FrontView plug-in mode
256 public void Remove(ISensor sensor)
258 Remove(sensor, true);
259 iNextSensorToDisplay = 0;
262 //No sensor to display in FrontView, just disable FrontView plug-in mode
268 private void Remove(ISensor sensor, bool deleteConfig)
273 new Identifier(sensor.Identifier, "FrontView").ToString());
275 SensorFrontView instance = null;
276 foreach (SensorFrontView icon in list)
277 if (icon.Sensor == sensor)
279 if (instance != null)
281 list.Remove(instance);
282 //UpdateMainIconVisibilty();
289 private void UpdateMainIconVisibilty()
294 mainIcon.Visible = list.Count == 0;
298 mainIcon.Visible = false;
305 iServer.SendMessage("init:");
310 iServer.SendMessage("uninit:");
313 public void SetText(string aUpperLine, string aLowerLine)
315 iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
320 iServer.SendMessage("quit:");
324 public bool IsMainIconEnabled
326 get { return mainIconEnabled; }
329 if (mainIconEnabled != value)
331 mainIconEnabled = value;
332 UpdateMainIconVisibilty();