diff -r 77e062628ef5 -r 361e324a0ed4 Hardware/TBalancer/TBalancer.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Hardware/TBalancer/TBalancer.cs Tue Jan 26 22:37:48 2010 +0000 @@ -0,0 +1,198 @@ +/* + + Version: MPL 1.1/GPL 2.0/LGPL 2.1 + + The contents of this file are subject to the Mozilla Public License Version + 1.1 (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.mozilla.org/MPL/ + + Software distributed under the License is distributed on an "AS IS" basis, + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + for the specific language governing rights and limitations under the License. + + The Original Code is the Open Hardware Monitor code. + + The Initial Developer of the Original Code is + Michael Möller . + Portions created by the Initial Developer are Copyright (C) 2009-2010 + the Initial Developer. All Rights Reserved. + + Contributor(s): + + Alternatively, the contents of this file may be used under the terms of + either the GNU General Public License Version 2 or later (the "GPL"), or + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + in which case the provisions of the GPL or the LGPL are applicable instead + of those above. If you wish to allow use of your version of this file only + under the terms of either the GPL or the LGPL, and not to allow others to + use your version of this file under the terms of the MPL, indicate your + decision by deleting the provisions above and replace them with the notice + and other provisions required by the GPL or the LGPL. If you do not delete + the provisions above, a recipient may use your version of this file under + the terms of any one of the MPL, the GPL or the LGPL. + +*/ + +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Drawing; +using System.IO; +using System.IO.Ports; +using System.Text; + +namespace OpenHardwareMonitor.Hardware.TBalancer { + public class TBalancer : IHardware { + + private Image icon; + private SerialPort serialPort; + private Sensor[] digitalTemperatures = new Sensor[8]; + private Sensor[] analogTemperatures = new Sensor[4]; + private Sensor[] fans = new Sensor[4]; + private List active = new List(); + private List deactivating = new List(); + private int[] data; + + public const byte STARTFLAG = 100; + public const byte PROTOCOL_VERSION = 0x2A; + + public TBalancer(string portName) { + icon = Utilities.EmbeddedResources.GetImage("bigng.png"); + + try { + serialPort = new SerialPort(portName, 19200, Parity.None, 8, + StopBits.One); + serialPort.Open(); + Update(); + } catch (IOException) { } + + for (int i = 0; i < digitalTemperatures.Length; i++) + digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1), i, + SensorType.Temperature, this); + + for (int i = 0; i < analogTemperatures.Length; i++) + analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1), + i + digitalTemperatures.Length, SensorType.Temperature, this); + } + + private void ActivateSensor(Sensor sensor) { + deactivating.Remove(sensor); + if (!active.Contains(sensor)) { + active.Add(sensor); + SensorAdded(sensor); + } + } + + private void DeactivateSensor(Sensor sensor) { + if (deactivating.Contains(sensor)) { + active.Remove(sensor); + deactivating.Remove(sensor); + SensorRemoved(sensor); + } else if (active.Contains(sensor)) { + deactivating.Add(sensor); + } + } + + private void ReadData() { + int[] data = new int[285]; + for (int i = 0; i < data.Length; i++) + data[i] = serialPort.ReadByte(); + + if (data[0] != STARTFLAG || data[274] != PROTOCOL_VERSION) { + serialPort.DiscardInBuffer(); + return; + } + + for (int i = 0; i < digitalTemperatures.Length; i++) + if (data[238 + i] > 0) { + digitalTemperatures[i].Value = 0.5f * data[238 + i]; + ActivateSensor(digitalTemperatures[i]); + } else { + DeactivateSensor(digitalTemperatures[i]); + } + + for (int i = 0; i < analogTemperatures.Length; i++) + if (data[260 + i] > 0) { + analogTemperatures[i].Value = 0.5f * data[260 + i]; + ActivateSensor(analogTemperatures[i]); + } else { + DeactivateSensor(analogTemperatures[i]); + } + + for (int i = 0; i < fans.Length; i++) { + float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]); + + if (fans[i] == null) + fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan, + this); + + if ((data[136] & (1 << i)) == 0) + fans[i].Value = maxRPM * 0.01f * data[156 + i]; // pwm mode + else + fans[i].Value = maxRPM * 0.01f * data[141 + i]; // analog mode + ActivateSensor(fans[i]); + } + this.data = data; + } + + public Image Icon { + get { return icon; } + } + + public string Name { + get { return "T-Balancer bigNG"; } + } + + public string Identifier { + get { return "/bigng/" + + serialPort.PortName.TrimStart(new char[]{'/'}).ToLower(); } + } + + public ISensor[] Sensors { + get { return active.ToArray(); } + } + + public string GetReport() { + StringBuilder r = new StringBuilder(); + + r.AppendLine("T-Balancer bigNG"); + r.AppendLine(); + r.Append("Port Name: "); r.AppendLine(serialPort.PortName); + r.AppendLine(); + r.AppendLine("System Information Answer"); + r.AppendLine(); + + r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"); + r.AppendLine(); + for (int i = 0; i <= 0x11; i++) { + r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append(" "); + for (int j = 0; j <= 0xF; j++) { + int index = ((i << 4) | j); + if (index < data.Length) { + r.Append(" "); + r.Append(data[index].ToString("X2")); + } + } + r.AppendLine(); + } + r.AppendLine(); + + return r.ToString(); + } + + public void Update() { + while (serialPort.BytesToRead >= 285) + ReadData(); + serialPort.Write(new byte[] { 0x38 }, 0, 1); + } + + public void Close() { + serialPort.Close(); + } + + public event SensorEventHandler SensorAdded; + public event SensorEventHandler SensorRemoved; + } +}