Hardware/TBalancer/TBalancer.cs
author moel.mich
Sun, 07 Feb 2010 20:11:30 +0000
changeset 32 a7f5e400f0da
parent 1 361e324a0ed4
child 33 800a36a1ca8e
permissions -rw-r--r--
Release version 0.1.14. Added more report details for T-Balancer bigNG.
     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 Sensor[] digitalTemperatures = new Sensor[8];
    52     private Sensor[] analogTemperatures = new Sensor[4];
    53     private Sensor[] fans = new Sensor[4];
    54     private List<ISensor> active = new List<ISensor>();
    55     private List<ISensor> deactivating = new List<ISensor>();
    56     private int[] data;
    57 
    58     public const byte STARTFLAG = 100;
    59     public const byte PROTOCOL_VERSION = 0x2A;
    60 
    61     public TBalancer(string portName) {
    62       icon = Utilities.EmbeddedResources.GetImage("bigng.png");
    63 
    64       try {
    65         serialPort = new SerialPort(portName, 19200, Parity.None, 8,
    66           StopBits.One);
    67         serialPort.Open();
    68         Update();
    69       } catch (IOException) { }
    70 
    71       for (int i = 0; i < digitalTemperatures.Length; i++) 
    72         digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1), i, 
    73           SensorType.Temperature, this);
    74 
    75       for (int i = 0; i < analogTemperatures.Length; i++)
    76         analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1),
    77           i + digitalTemperatures.Length, SensorType.Temperature, this);        
    78     }
    79 
    80     private void ActivateSensor(Sensor sensor) {
    81       deactivating.Remove(sensor);
    82       if (!active.Contains(sensor)) {
    83         active.Add(sensor);
    84         if (SensorAdded != null)
    85           SensorAdded(sensor);
    86       }      
    87     }
    88 
    89     private void DeactivateSensor(Sensor sensor) {
    90       if (deactivating.Contains(sensor)) {
    91         active.Remove(sensor);
    92         deactivating.Remove(sensor);
    93         if (SensorRemoved != null)
    94           SensorRemoved(sensor);
    95       } else if (active.Contains(sensor)) {
    96         deactivating.Add(sensor);
    97       }     
    98     }
    99 
   100     private void ReadData() {
   101       int[] data = new int[285];
   102       for (int i = 0; i < data.Length; i++)
   103         data[i] = serialPort.ReadByte();
   104 
   105       if (data[0] != STARTFLAG || data[274] != PROTOCOL_VERSION) {
   106         serialPort.DiscardInBuffer();   
   107         return;
   108       }
   109 
   110       for (int i = 0; i < digitalTemperatures.Length; i++)
   111         if (data[238 + i] > 0) {
   112           digitalTemperatures[i].Value = 0.5f * data[238 + i];
   113           ActivateSensor(digitalTemperatures[i]);
   114         } else {
   115           DeactivateSensor(digitalTemperatures[i]);
   116         }
   117 
   118       for (int i = 0; i < analogTemperatures.Length; i++)
   119         if (data[260 + i] > 0) {
   120           analogTemperatures[i].Value = 0.5f * data[260 + i];
   121           ActivateSensor(analogTemperatures[i]);
   122         } else {
   123           DeactivateSensor(analogTemperatures[i]);
   124         }
   125       
   126       for (int i = 0; i < fans.Length; i++) {
   127         float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
   128         
   129         if (fans[i] == null) 
   130           fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan, 
   131             this);
   132 
   133         if ((data[136] & (1 << i)) == 0)
   134           fans[i].Value = maxRPM * 0.01f * data[156 + i]; // pwm mode
   135         else
   136           fans[i].Value = maxRPM * 0.01f * data[141 + i]; // analog mode
   137         ActivateSensor(fans[i]);
   138       }
   139       this.data = data;
   140     }
   141 
   142     public Image Icon {
   143       get { return icon; }
   144     }
   145 
   146     public string Name {
   147       get { return "T-Balancer bigNG"; }
   148     }
   149 
   150     public string Identifier {
   151       get { return "/bigng/" + 
   152         serialPort.PortName.TrimStart(new char[]{'/'}).ToLower(); }
   153     }
   154 
   155     public ISensor[] Sensors {
   156       get { return active.ToArray(); }
   157     }
   158 
   159     public string GetReport() {
   160       StringBuilder r = new StringBuilder();
   161 
   162       r.AppendLine("T-Balancer bigNG");
   163       r.AppendLine();
   164       r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
   165       r.AppendLine();
   166       r.AppendLine("System Information Answer");
   167       r.AppendLine();
   168 
   169       r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   170       r.AppendLine();
   171       for (int i = 0; i <= 0x11; i++) {
   172         r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
   173         for (int j = 0; j <= 0xF; j++) {
   174           int index = ((i << 4) | j);
   175           if (index < data.Length) {
   176             r.Append(" ");
   177             r.Append(data[index].ToString("X2"));
   178           }          
   179         }
   180         r.AppendLine();
   181       }
   182       r.AppendLine();
   183 
   184       return r.ToString();
   185     }
   186 
   187     public void Update() {
   188       while (serialPort.BytesToRead >= 285)
   189         ReadData();
   190       serialPort.Write(new byte[] { 0x38 }, 0, 1);
   191     }
   192 
   193     public void Close() {
   194       serialPort.Close();
   195     }
   196 
   197     public event SensorEventHandler SensorAdded;
   198     public event SensorEventHandler SensorRemoved;
   199   }
   200 }