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