Hardware/TBalancer/TBalancer.cs
changeset 1 361e324a0ed4
child 15 0e812fe67bac
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancer.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,198 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Configuration;
    1.44 +using System.Drawing;
    1.45 +using System.IO;
    1.46 +using System.IO.Ports;
    1.47 +using System.Text;
    1.48 +
    1.49 +namespace OpenHardwareMonitor.Hardware.TBalancer {
    1.50 +  public class TBalancer : IHardware {
    1.51 +
    1.52 +    private Image icon;
    1.53 +    private SerialPort serialPort;
    1.54 +    private Sensor[] digitalTemperatures = new Sensor[8];
    1.55 +    private Sensor[] analogTemperatures = new Sensor[4];
    1.56 +    private Sensor[] fans = new Sensor[4];
    1.57 +    private List<ISensor> active = new List<ISensor>();
    1.58 +    private List<ISensor> deactivating = new List<ISensor>();
    1.59 +    private int[] data;
    1.60 +
    1.61 +    public const byte STARTFLAG = 100;
    1.62 +    public const byte PROTOCOL_VERSION = 0x2A;
    1.63 +
    1.64 +    public TBalancer(string portName) {
    1.65 +      icon = Utilities.EmbeddedResources.GetImage("bigng.png");
    1.66 +
    1.67 +      try {
    1.68 +        serialPort = new SerialPort(portName, 19200, Parity.None, 8,
    1.69 +          StopBits.One);
    1.70 +        serialPort.Open();
    1.71 +        Update();
    1.72 +      } catch (IOException) { }
    1.73 +
    1.74 +      for (int i = 0; i < digitalTemperatures.Length; i++) 
    1.75 +        digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1), i, 
    1.76 +          SensorType.Temperature, this);
    1.77 +
    1.78 +      for (int i = 0; i < analogTemperatures.Length; i++)
    1.79 +        analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1),
    1.80 +          i + digitalTemperatures.Length, SensorType.Temperature, this);        
    1.81 +    }
    1.82 +
    1.83 +    private void ActivateSensor(Sensor sensor) {
    1.84 +      deactivating.Remove(sensor);
    1.85 +      if (!active.Contains(sensor)) {
    1.86 +        active.Add(sensor);
    1.87 +        SensorAdded(sensor);
    1.88 +      }      
    1.89 +    }
    1.90 +
    1.91 +    private void DeactivateSensor(Sensor sensor) {
    1.92 +      if (deactivating.Contains(sensor)) {
    1.93 +        active.Remove(sensor);
    1.94 +        deactivating.Remove(sensor);
    1.95 +        SensorRemoved(sensor);
    1.96 +      } else if (active.Contains(sensor)) {
    1.97 +        deactivating.Add(sensor);
    1.98 +      }     
    1.99 +    }
   1.100 +
   1.101 +    private void ReadData() {
   1.102 +      int[] data = new int[285];
   1.103 +      for (int i = 0; i < data.Length; i++)
   1.104 +        data[i] = serialPort.ReadByte();
   1.105 +
   1.106 +      if (data[0] != STARTFLAG || data[274] != PROTOCOL_VERSION) {
   1.107 +        serialPort.DiscardInBuffer();   
   1.108 +        return;
   1.109 +      }
   1.110 +
   1.111 +      for (int i = 0; i < digitalTemperatures.Length; i++)
   1.112 +        if (data[238 + i] > 0) {
   1.113 +          digitalTemperatures[i].Value = 0.5f * data[238 + i];
   1.114 +          ActivateSensor(digitalTemperatures[i]);
   1.115 +        } else {
   1.116 +          DeactivateSensor(digitalTemperatures[i]);
   1.117 +        }
   1.118 +
   1.119 +      for (int i = 0; i < analogTemperatures.Length; i++)
   1.120 +        if (data[260 + i] > 0) {
   1.121 +          analogTemperatures[i].Value = 0.5f * data[260 + i];
   1.122 +          ActivateSensor(analogTemperatures[i]);
   1.123 +        } else {
   1.124 +          DeactivateSensor(analogTemperatures[i]);
   1.125 +        }
   1.126 +      
   1.127 +      for (int i = 0; i < fans.Length; i++) {
   1.128 +        float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
   1.129 +        
   1.130 +        if (fans[i] == null) 
   1.131 +          fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan, 
   1.132 +            this);
   1.133 +
   1.134 +        if ((data[136] & (1 << i)) == 0)
   1.135 +          fans[i].Value = maxRPM * 0.01f * data[156 + i]; // pwm mode
   1.136 +        else
   1.137 +          fans[i].Value = maxRPM * 0.01f * data[141 + i]; // analog mode
   1.138 +        ActivateSensor(fans[i]);
   1.139 +      }
   1.140 +      this.data = data;
   1.141 +    }
   1.142 +
   1.143 +    public Image Icon {
   1.144 +      get { return icon; }
   1.145 +    }
   1.146 +
   1.147 +    public string Name {
   1.148 +      get { return "T-Balancer bigNG"; }
   1.149 +    }
   1.150 +
   1.151 +    public string Identifier {
   1.152 +      get { return "/bigng/" + 
   1.153 +        serialPort.PortName.TrimStart(new char[]{'/'}).ToLower(); }
   1.154 +    }
   1.155 +
   1.156 +    public ISensor[] Sensors {
   1.157 +      get { return active.ToArray(); }
   1.158 +    }
   1.159 +
   1.160 +    public string GetReport() {
   1.161 +      StringBuilder r = new StringBuilder();
   1.162 +
   1.163 +      r.AppendLine("T-Balancer bigNG");
   1.164 +      r.AppendLine();
   1.165 +      r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
   1.166 +      r.AppendLine();
   1.167 +      r.AppendLine("System Information Answer");
   1.168 +      r.AppendLine();
   1.169 +
   1.170 +      r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   1.171 +      r.AppendLine();
   1.172 +      for (int i = 0; i <= 0x11; i++) {
   1.173 +        r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
   1.174 +        for (int j = 0; j <= 0xF; j++) {
   1.175 +          int index = ((i << 4) | j);
   1.176 +          if (index < data.Length) {
   1.177 +            r.Append(" ");
   1.178 +            r.Append(data[index].ToString("X2"));
   1.179 +          }          
   1.180 +        }
   1.181 +        r.AppendLine();
   1.182 +      }
   1.183 +      r.AppendLine();
   1.184 +
   1.185 +      return r.ToString();
   1.186 +    }
   1.187 +
   1.188 +    public void Update() {
   1.189 +      while (serialPort.BytesToRead >= 285)
   1.190 +        ReadData();
   1.191 +      serialPort.Write(new byte[] { 0x38 }, 0, 1);
   1.192 +    }
   1.193 +
   1.194 +    public void Close() {
   1.195 +      serialPort.Close();
   1.196 +    }
   1.197 +
   1.198 +    public event SensorEventHandler SensorAdded;
   1.199 +    public event SensorEventHandler SensorRemoved;
   1.200 +  }
   1.201 +}