GUI/SoundGraphDisplay.cs
author StephaneLenclud
Sun, 14 Apr 2013 22:38:41 +0200
branchMiniDisplay
changeset 439 06369ace500d
parent 438 f590956d3234
child 442 782b48689513
permissions -rw-r--r--
Better formatting FrontView lines.
Now cycling through each FrontView sensor every 2 seconds.
Fixing character conversion when sending VFD text.
Using special FrontView characters for Celsius and Farenheit.
     1 /*
     2  
     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/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using System.Text;
    15 using System.Diagnostics;
    16 using System.Windows.Forms;
    17 using System.Windows;
    18 using OpenHardwareMonitor.Hardware;
    19 using OpenHardwareMonitor.Utilities;
    20 using System.Runtime.InteropServices;
    21 using UacHelpers;
    22 
    23 
    24 
    25 
    26 
    27 
    28 
    29 namespace OpenHardwareMonitor.GUI
    30 {
    31     public class SoundGraphDisplay : IDisposable
    32     {
    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;
    38 
    39         private int iNextSensorToDisplay=0;
    40         private int iTickCounter=0;
    41 
    42 
    43         public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
    44           UnitManager unitManager)
    45         {
    46             this.computer = computer;
    47             this.settings = settings;
    48             this.unitManager = unitManager;
    49             computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    50             computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    51 
    52             //Start our client if needed
    53             Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
    54             if (!(processes.Length > 0))
    55             {
    56 
    57                 //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
    58                     /*
    59                 Process client = new Process();
    60                 client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
    61                 client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
    62                 client.Start();*/
    63             }
    64 
    65             //Start our SoundGraph server
    66             iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
    67             iServer.Start();
    68             //iServer.SendMessage("init:");
    69         }
    70 
    71         private void HardwareRemoved(IHardware hardware)
    72         {
    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);
    79         }
    80 
    81         private void HardwareAdded(IHardware hardware)
    82         {
    83             foreach (ISensor sensor in hardware.Sensors)
    84                 SensorAdded(sensor);
    85             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    86             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    87             foreach (IHardware subHardware in hardware.SubHardware)
    88                 HardwareAdded(subHardware);
    89         }
    90 
    91         private void SensorAdded(ISensor sensor)
    92         {
    93             if (settings.GetValue(new Identifier(sensor.Identifier,
    94               "FrontView").ToString(), false))
    95                 Add(sensor, false);
    96         }
    97 
    98         private void SensorRemoved(ISensor sensor)
    99         {
   100             if (Contains(sensor))
   101                 Remove(sensor, false);
   102         }
   103 
   104         public void Dispose()
   105         {
   106             foreach (SensorFrontView icon in list)
   107                 icon.Dispose();
   108 
   109             Quit();
   110             iServer.Stop();
   111 
   112         }
   113 
   114         public void Redraw()
   115         {
   116             //Update all sensors from our front view
   117             foreach (SensorFrontView sensor in list)
   118             {
   119                 sensor.Update();
   120                 //SetText(sensor.iFirstLine, sensor.iSecondLine);
   121             }
   122 
   123             //Alternate between sensors 
   124             if (list.Count > 0)
   125             {
   126                 //Display current sensor on our FrontView display
   127                 SetText(list[iNextSensorToDisplay].iFirstLine, list[iNextSensorToDisplay].iSecondLine);
   128                 iTickCounter++;
   129                 if (iTickCounter==2) //Move to the next sensor only every second tick
   130                 {
   131                     iTickCounter = 0;
   132                     iNextSensorToDisplay++;
   133                 }
   134             }
   135 
   136             if (iNextSensorToDisplay == list.Count)
   137             {
   138                 //Go back to first sensor
   139                 iNextSensorToDisplay = 0;
   140             }
   141 
   142             
   143         }
   144 
   145         public bool Contains(ISensor sensor)
   146         {
   147             foreach (SensorFrontView icon in list)
   148                 if (icon.Sensor == sensor)
   149                     return true;
   150             return false;
   151         }
   152 
   153         public void Add(ISensor sensor, bool balloonTip)
   154         {
   155             if (Contains(sensor))
   156             {
   157                 return;
   158             }
   159             else
   160             {
   161                 //SL:
   162                 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   163                 //UpdateMainIconVisibilty();
   164                 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
   165                 iNextSensorToDisplay = 0;
   166             }
   167 
   168         }
   169 
   170         public void Remove(ISensor sensor)
   171         {
   172             Remove(sensor, true);
   173             iNextSensorToDisplay = 0;
   174         }
   175 
   176         private void Remove(ISensor sensor, bool deleteConfig)
   177         {
   178             if (deleteConfig)
   179             {
   180                 settings.Remove(
   181                   new Identifier(sensor.Identifier, "FrontView").ToString());
   182             }
   183             SensorFrontView instance = null;
   184             foreach (SensorFrontView icon in list)
   185                 if (icon.Sensor == sensor)
   186                     instance = icon;
   187             if (instance != null)
   188             {
   189                 list.Remove(instance);
   190                 //UpdateMainIconVisibilty();
   191                 instance.Dispose();
   192             }
   193         }
   194 
   195 
   196 
   197         private void UpdateMainIconVisibilty()
   198         {
   199             /*
   200             if (mainIconEnabled)
   201             {
   202                 mainIcon.Visible = list.Count == 0;
   203             }
   204             else
   205             {
   206                 mainIcon.Visible = false;
   207             }
   208              */
   209         }
   210 
   211         public void Init()
   212         {
   213             iServer.SendMessage("init:");
   214         }
   215 
   216         public void Uninit()
   217         {
   218             iServer.SendMessage("uninit:");
   219         }
   220 
   221         public void SetText(string aUpperLine, string aLowerLine)
   222         {
   223             iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   224         }
   225 
   226         public void Quit()
   227         {
   228             iServer.SendMessage("quit:");
   229         }
   230 
   231         /*
   232         public bool IsMainIconEnabled
   233         {
   234             get { return mainIconEnabled; }
   235             set
   236             {
   237                 if (mainIconEnabled != value)
   238                 {
   239                     mainIconEnabled = value;
   240                     UpdateMainIconVisibilty();
   241                 }
   242             }
   243         }*/
   244 
   245 
   246     }
   247 }