Quick and dirty 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;
22 using System.ServiceModel;
26 namespace OpenHardwareMonitor.GUI
28 public class SharpDisplay : ICallback, IDisposable
30 private IComputer computer;
31 private PersistentSettings settings;
32 private UnitManager unitManager;
33 private List<SensorSharpDisplay> iSensors = new List<SensorSharpDisplay>();
34 private global::SharpDisplay.Client iClient;
35 TextField iTextFieldTop;
36 TextField iTextFieldBottom;
37 TextField iTextFieldTopRight;
38 TextField iTextFieldBottomRight;
40 TextField[] iTextFields;
42 private int iNextSensorToDisplay = 0;
43 private int iTickCounter = 0;
46 public SharpDisplay(IComputer computer, PersistentSettings settings, UnitManager unitManager)
48 this.computer = computer;
49 this.settings = settings;
50 this.unitManager = unitManager;
51 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
52 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
55 //Instance context is then managed by our client class
56 iClient = new global::SharpDisplay.Client(this);
58 iTextFieldTop = new TextField(0);
59 iTextFieldBottom = new TextField(1);
60 iTextFieldTopRight = new TextField(2, "", ContentAlignment.MiddleRight);
61 iTextFieldBottomRight = new TextField(3, "", ContentAlignment.MiddleRight);
63 iTextFields = new TextField[] { iTextFieldTop, iTextFieldBottom };
68 public void OnConnected()
70 //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
71 //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
72 //MessageBox.Show("OnConnected()", "Client");
76 public void OnCloseOrder()
82 private void HardwareRemoved(IHardware hardware)
84 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
85 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
86 foreach (ISensor sensor in hardware.Sensors)
87 SensorRemoved(sensor);
88 foreach (IHardware subHardware in hardware.SubHardware)
89 HardwareRemoved(subHardware);
92 private void HardwareAdded(IHardware hardware)
94 foreach (ISensor sensor in hardware.Sensors)
96 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
97 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
98 foreach (IHardware subHardware in hardware.SubHardware)
99 HardwareAdded(subHardware);
102 private void SensorAdded(ISensor sensor)
104 if (settings.GetValue(new Identifier(sensor.Identifier,
105 "SharpDisplay").ToString(), false))
109 private void SensorRemoved(ISensor sensor)
111 if (Contains(sensor))
112 Remove(sensor, false);
115 public void Dispose()
117 foreach (SensorSharpDisplay icon in iSensors)
126 public void Redraw(bool aPacked, bool aDisplayTime)
128 const int KNumberOfTickBeforeSwitch = 4;
129 const int KMaxCharacterPerLine = 16;
130 string packedFirstLine = ""; //We have 16 chars per line on our VFD
131 string packedSecondLine = "";
134 //string time = DateTime.Now.ToShortTimeString();
135 string time = DateTime.Now.ToLongTimeString();
137 if (iSensors.Count > 0)
139 if (iPacked != aPacked)
146 //We just switched to packed mode
147 //Make sure our layout is proper
148 TableLayout layout = new TableLayout(2, 2);
149 iClient.SetLayout(layout);
154 TableLayout layout = new TableLayout(1, 2);
155 iClient.SetLayout(layout);
161 //Update all sensors from our front view
162 foreach (SensorSharpDisplay sensor in iSensors)
167 if (aDisplayTime && count == 1)
169 //First slot is taken by time display
171 iTextFieldTop.Text = time;
172 iClient.SetText(iTextFieldTop);
177 //Build strings for packed mode
178 string packedText = "";
179 packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
182 iTextFieldTop.Text = packedText;
183 iClient.SetText(iTextFieldTop);
187 iTextFieldBottom.Text = packedText;
188 iClient.SetText(iTextFieldBottom);
192 iTextFieldTopRight.Text = packedText;
193 iClient.SetText(iTextFieldTopRight);
197 iTextFieldBottomRight.Text = packedText;
198 iClient.SetText(iTextFieldBottomRight);
203 //Alternate between sensors
204 if (iSensors.Count > 0)
208 //Review that stuff cause as it is it's probably useless
209 //string packedLine = "";
211 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
214 if (iNextSensorToDisplay == 1)
216 iNextSensorToDisplay = 0;
220 iNextSensorToDisplay = 1;
226 string secondLine = iSensors[iNextSensorToDisplay].iSecondLine;
230 while (secondLine.Length + time.Length < KMaxCharacterPerLine)
236 //Display current sensor on our FrontView display
237 SetText(iSensors[iNextSensorToDisplay].iFirstLine, secondLine);
239 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
242 iNextSensorToDisplay++;
247 if (iNextSensorToDisplay == iSensors.Count)
249 //Go back to first sensor
250 iNextSensorToDisplay = 0;
256 public bool Contains(ISensor sensor)
258 foreach (SensorSharpDisplay icon in iSensors)
259 if (icon.Sensor == sensor)
264 public void Add(ISensor sensor, bool balloonTip)
266 if (Contains(sensor))
273 iSensors.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager));
274 //UpdateMainIconVisibilty();
275 settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true);
276 iNextSensorToDisplay = 0;
277 if (iSensors.Count == 1)
279 //Just added first sensor in FrontView, unable FrontView plug-in mode
286 public void Remove(ISensor sensor)
288 Remove(sensor, true);
289 iNextSensorToDisplay = 0;
290 if (iSensors.Count == 0)
292 //No sensor to display in FrontView, just disable FrontView plug-in mode
298 private void Remove(ISensor sensor, bool deleteConfig)
303 new Identifier(sensor.Identifier, "SharpDisplay").ToString());
305 SensorSharpDisplay instance = null;
306 foreach (SensorSharpDisplay icon in iSensors)
307 if (icon.Sensor == sensor)
309 if (instance != null)
311 iSensors.Remove(instance);
312 //UpdateMainIconVisibilty();
319 private void UpdateMainIconVisibilty()
324 mainIcon.Visible = list.Count == 0;
328 mainIcon.Visible = false;
335 //iServer.SendMessage("init:");
340 //iServer.SendMessage("uninit:");
343 public void SetText(string aUpperLine, string aLowerLine)
345 //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
346 iTextFieldTop.Text = aUpperLine;
347 iTextFieldBottom.Text = aLowerLine;
348 iClient.SetTexts(iTextFields);
354 //iServer.SendMessage("quit:");
358 public bool IsMainIconEnabled
360 get { return mainIconEnabled; }
363 if (mainIconEnabled != value)
365 mainIconEnabled = value;
366 UpdateMainIconVisibilty();