Hardware/TBalancer/TBalancer.cs
author moel.mich
Fri, 19 Feb 2010 18:33:08 +0000
changeset 54 f940fe2a7c2b
parent 33 800a36a1ca8e
child 57 142907c75be4
permissions -rw-r--r--
Added support for Winbond W83627THF super I/O chip.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Configuration;
    41 using System.Drawing;
    42 using System.IO;
    43 using System.IO.Ports;
    44 using System.Text;
    45 
    46 namespace OpenHardwareMonitor.Hardware.TBalancer {
    47   public class TBalancer : IHardware {
    48 
    49     private Image icon;
    50     private SerialPort serialPort;
    51     private byte protocolVersion;
    52     private Sensor[] digitalTemperatures = new Sensor[8];
    53     private Sensor[] analogTemperatures = new Sensor[4];
    54     private Sensor[] fans = new Sensor[4];
    55     private List<ISensor> active = new List<ISensor>();
    56     private List<ISensor> deactivating = new List<ISensor>();
    57     private int[] data;
    58 
    59     public const byte STARTFLAG = 100;
    60 
    61     public TBalancer(string portName, byte protocolVersion) {
    62       icon = Utilities.EmbeddedResources.GetImage("bigng.png");
    63       this.protocolVersion = protocolVersion;
    64 
    65       try {
    66         serialPort = new SerialPort(portName, 19200, Parity.None, 8,
    67           StopBits.One);
    68         serialPort.Open();
    69         Update();
    70       } catch (IOException) { }
    71 
    72       for (int i = 0; i < digitalTemperatures.Length; i++) 
    73         digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1), i, 
    74           SensorType.Temperature, this);
    75 
    76       for (int i = 0; i < analogTemperatures.Length; i++)
    77         analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1),
    78           i + digitalTemperatures.Length, SensorType.Temperature, this);        
    79     }
    80 
    81     private void ActivateSensor(Sensor sensor) {
    82       deactivating.Remove(sensor);
    83       if (!active.Contains(sensor)) {
    84         active.Add(sensor);
    85         if (SensorAdded != null)
    86           SensorAdded(sensor);
    87       }      
    88     }
    89 
    90     private void DeactivateSensor(Sensor sensor) {
    91       if (deactivating.Contains(sensor)) {
    92         active.Remove(sensor);
    93         deactivating.Remove(sensor);
    94         if (SensorRemoved != null)
    95           SensorRemoved(sensor);
    96       } else if (active.Contains(sensor)) {
    97         deactivating.Add(sensor);
    98       }     
    99     }
   100 
   101     private void ReadData() {
   102       int[] data = new int[285];
   103       for (int i = 0; i < data.Length; i++)
   104         data[i] = serialPort.ReadByte();
   105 
   106       if (data[0] != STARTFLAG || data[274] != protocolVersion) {
   107         serialPort.DiscardInBuffer();   
   108         return;
   109       }
   110 
   111       for (int i = 0; i < digitalTemperatures.Length; i++)
   112         if (data[238 + i] > 0) {
   113           digitalTemperatures[i].Value = 0.5f * data[238 + i];
   114           ActivateSensor(digitalTemperatures[i]);
   115         } else {
   116           DeactivateSensor(digitalTemperatures[i]);
   117         }
   118 
   119       for (int i = 0; i < analogTemperatures.Length; i++)
   120         if (data[260 + i] > 0) {
   121           analogTemperatures[i].Value = 0.5f * data[260 + i];
   122           ActivateSensor(analogTemperatures[i]);
   123         } else {
   124           DeactivateSensor(analogTemperatures[i]);
   125         }
   126       
   127       for (int i = 0; i < fans.Length; i++) {
   128         float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
   129         
   130         if (fans[i] == null) 
   131           fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan, 
   132             this);
   133 
   134         if ((data[136] & (1 << i)) == 0)
   135           fans[i].Value = maxRPM * 0.01f * data[156 + i]; // pwm mode
   136         else
   137           fans[i].Value = maxRPM * 0.01f * data[141 + i]; // analog mode
   138         ActivateSensor(fans[i]);
   139       }
   140       this.data = data;
   141     }
   142 
   143     public Image Icon {
   144       get { return icon; }
   145     }
   146 
   147     public string Name {
   148       get { return "T-Balancer bigNG"; }
   149     }
   150 
   151     public string Identifier {
   152       get { return "/bigng/" + 
   153         serialPort.PortName.TrimStart(new char[]{'/'}).ToLower(); }
   154     }
   155 
   156     public ISensor[] Sensors {
   157       get { return active.ToArray(); }
   158     }
   159 
   160     public string GetReport() {
   161       StringBuilder r = new StringBuilder();
   162 
   163       r.AppendLine("T-Balancer bigNG");
   164       r.AppendLine();
   165       r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
   166       r.AppendLine();
   167       r.AppendLine("System Information Answer");
   168       r.AppendLine();
   169 
   170       r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   171       r.AppendLine();
   172       for (int i = 0; i <= 0x11; i++) {
   173         r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
   174         for (int j = 0; j <= 0xF; j++) {
   175           int index = ((i << 4) | j);
   176           if (index < data.Length) {
   177             r.Append(" ");
   178             r.Append(data[index].ToString("X2"));
   179           }          
   180         }
   181         r.AppendLine();
   182       }
   183       r.AppendLine();
   184 
   185       return r.ToString();
   186     }
   187 
   188     public void Update() {
   189       while (serialPort.BytesToRead >= 285)
   190         ReadData();
   191       serialPort.Write(new byte[] { 0x38 }, 0, 1);
   192     }
   193 
   194     public void Close() {
   195       serialPort.Close();
   196     }
   197 
   198     public event SensorEventHandler SensorAdded;
   199     public event SensorEventHandler SensorRemoved;
   200   }
   201 }