Hardware/TBalancer/TBalancer.cs
author moel.mich
Sat, 15 May 2010 14:16:00 +0000
changeset 118 407f98562c3b
parent 110 411b72b73d8f
child 122 3ef997c53b50
permissions -rw-r--r--
Added a sensor type Control (with unit percent) for (fan) PWM control sensors. Fixed Issue 2.
     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.Text;
    43 
    44 namespace OpenHardwareMonitor.Hardware.TBalancer {
    45   public class TBalancer : IHardware {
    46 
    47     private int portIndex;
    48     private FT_HANDLE handle;
    49     private Image icon;
    50     private byte protocolVersion;
    51     private Sensor[] digitalTemperatures = new Sensor[8];
    52     private Sensor[] analogTemperatures = new Sensor[4];
    53     private Sensor[] sensorhubTemperatures = new Sensor[6];
    54     private Sensor[] sensorhubFlows = new Sensor[2];
    55     private Sensor[] fans = new Sensor[4];
    56     private Sensor[] controls = new Sensor[4];
    57     private Sensor[] miniNGTemperatures = new Sensor[4];
    58     private Sensor[] miniNGFans = new Sensor[4];
    59     private List<ISensor> active = new List<ISensor>();
    60     private List<ISensor> deactivating = new List<ISensor>();
    61     private int[] primaryData = new int[0];
    62     private int[] alternativeData = new int[0];
    63 
    64     public const byte STARTFLAG = 100;
    65     public const byte ENDFLAG = 254;
    66 
    67     private delegate void MethodDelegate();
    68     private MethodDelegate alternativeRequest;    
    69 
    70     public TBalancer(int portIndex, byte protocolVersion) {
    71       this.portIndex = portIndex;
    72       this.icon = Utilities.EmbeddedResources.GetImage("bigng.png");
    73       this.protocolVersion = protocolVersion;
    74 
    75       ParameterDescription[] parameter = new ParameterDescription[] {
    76         new ParameterDescription("Offset", "Temperature offset.", 0)
    77       };
    78       int offset = 0;
    79       for (int i = 0; i < digitalTemperatures.Length; i++)
    80         digitalTemperatures[i] = new Sensor("Digital Sensor " + i,
    81           offset + i, null, SensorType.Temperature, this, parameter);
    82       offset += digitalTemperatures.Length;
    83 
    84       for (int i = 0; i < analogTemperatures.Length; i++)
    85         analogTemperatures[i] = new Sensor("Analog Sensor " + (i + 1),
    86           offset + i, null, SensorType.Temperature, this, parameter);
    87       offset += analogTemperatures.Length;
    88 
    89       for (int i = 0; i < sensorhubTemperatures.Length; i++)
    90         sensorhubTemperatures[i] = new Sensor("Sensorhub Sensor " + i,
    91           offset + i, null, SensorType.Temperature, this, parameter);
    92       offset += sensorhubTemperatures.Length;
    93 
    94       for (int i = 0; i < miniNGTemperatures.Length; i++)
    95         miniNGTemperatures[i] = new Sensor("miniNG #" + (i / 2 + 1) +
    96           " Sensor " + (i % 2 + 1), offset + i, null, SensorType.Temperature, 
    97           this, parameter);
    98       offset += miniNGTemperatures.Length;
    99 
   100       for (int i = 0; i < sensorhubFlows.Length; i++)
   101         sensorhubFlows[i] = new Sensor("Flowmeter " + (i + 1),
   102           i, null, SensorType.Flow, this, new ParameterDescription[] {
   103             new ParameterDescription("Impulse Rate", 
   104               "The impulse rate of the flowmeter in pulses/L", 509)
   105           });
   106 
   107       for (int i = 0; i < controls.Length; i++) {
   108         controls[i] = new Sensor("Fan Channel " + i, i, null, 
   109           SensorType.Control, this, null);
   110         ActivateSensor(controls[i]);
   111       }
   112 
   113       alternativeRequest = new MethodDelegate(DelayedAlternativeRequest);
   114 
   115       Open();
   116       Update(); 
   117     }
   118 
   119     private void ActivateSensor(Sensor sensor) {
   120       deactivating.Remove(sensor);
   121       if (!active.Contains(sensor)) {
   122         active.Add(sensor);
   123         if (SensorAdded != null)
   124           SensorAdded(sensor);
   125       }      
   126     }
   127 
   128     private void DeactivateSensor(Sensor sensor) {
   129       if (deactivating.Contains(sensor)) {
   130         active.Remove(sensor);
   131         deactivating.Remove(sensor);
   132         if (SensorRemoved != null)
   133           SensorRemoved(sensor);
   134       } else if (active.Contains(sensor)) {
   135         deactivating.Add(sensor);
   136       }     
   137     }
   138 
   139     private void ReadminiNG(int[] data, int number) {
   140       int offset = 1 + number * 65;
   141 
   142       if (data[offset + 61] != ENDFLAG)
   143         return;
   144 
   145       for (int i = 0; i < 2; i++) {
   146         Sensor sensor = miniNGTemperatures[number * 2 + i];
   147         if (data[offset + 7 + i] > 0) {
   148           sensor.Value = 0.5f * data[offset + 7 + i] + 
   149             sensor.Parameters[0].Value;
   150           ActivateSensor(sensor);
   151         } else {
   152           DeactivateSensor(sensor);
   153         }
   154       }
   155 
   156       for (int i = 0; i < 2; i++) {
   157         float maxRPM = 20.0f * data[offset + 44 + 2 * i];
   158 
   159         if (miniNGFans[number * 2 + i] == null)
   160           miniNGFans[number * 2 + i] = 
   161             new Sensor("miniNG #" + (number + 1) + " Fan Channel " + (i + 1),
   162             4 + number * 2 + i, maxRPM, SensorType.Fan, this, null);
   163         
   164         Sensor sensor = miniNGFans[number * 2 + i];
   165 
   166         sensor.Value = 20.0f * data[offset + 43 + 2 * i];
   167         ActivateSensor(sensor);
   168       }
   169     }
   170 
   171     private void ReadData() {
   172       int[] data = new int[285];
   173       for (int i = 0; i < data.Length; i++)
   174         data[i] = FTD2XX.ReadByte(handle);
   175 
   176       if (data[0] != STARTFLAG) {
   177         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_RX);   
   178         return;
   179       }
   180 
   181       if (data[1] == 255 || data[1] == 88) { // bigNG
   182 
   183         if (data[274] != protocolVersion) 
   184           return;
   185 
   186         this.primaryData = data;
   187 
   188         for (int i = 0; i < digitalTemperatures.Length; i++)
   189           if (data[238 + i] > 0) {
   190             digitalTemperatures[i].Value = 0.5f * data[238 + i] + 
   191               digitalTemperatures[i].Parameters[0].Value;
   192             ActivateSensor(digitalTemperatures[i]);
   193           } else {
   194             DeactivateSensor(digitalTemperatures[i]);
   195           }
   196 
   197         for (int i = 0; i < analogTemperatures.Length; i++)
   198           if (data[260 + i] > 0) {
   199             analogTemperatures[i].Value = 0.5f * data[260 + i] +
   200               analogTemperatures[i].Parameters[0].Value;
   201             ActivateSensor(analogTemperatures[i]);
   202           } else {
   203             DeactivateSensor(analogTemperatures[i]);
   204           }
   205 
   206         for (int i = 0; i < sensorhubTemperatures.Length; i++)
   207           if (data[246 + i] > 0) {
   208             sensorhubTemperatures[i].Value = 0.5f * data[246 + i] +
   209               sensorhubTemperatures[i].Parameters[0].Value;
   210             ActivateSensor(sensorhubTemperatures[i]);
   211           } else {
   212             DeactivateSensor(sensorhubTemperatures[i]);
   213           }
   214 
   215         for (int i = 0; i < sensorhubFlows.Length; i++)
   216           if (data[231 + i] > 0 && data[234] > 0) {
   217             float pulsesPerSecond = (data[231 + i] * 4.0f) / data[234];
   218             float pulsesPerLiter = sensorhubFlows[i].Parameters[0].Value;
   219             sensorhubFlows[i].Value = pulsesPerSecond * 3600 / pulsesPerLiter;
   220             ActivateSensor(sensorhubFlows[i]);
   221           } else {
   222             DeactivateSensor(sensorhubFlows[i]);
   223           }
   224         
   225         for (int i = 0; i < fans.Length; i++) {
   226           float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
   227 
   228           if (fans[i] == null)
   229             fans[i] = new Sensor("Fan Channel " + i, i, maxRPM, SensorType.Fan,
   230               this, new ParameterDescription[] {
   231                 new ParameterDescription("MaxRPM", 
   232                   "Maximum revolutions per minute (RPM) of the fan.", maxRPM)
   233               });
   234 
   235           float value;
   236           if ((data[136] & (1 << i)) == 0)  // pwm mode
   237             value = 0.02f * data[137 + i];
   238           else // analog mode
   239             value = 0.01f * data[141 + i];
   240           
   241           fans[i].Value = fans[i].Parameters[0].Value * value;
   242           ActivateSensor(fans[i]);
   243 
   244           controls[i].Value = 100 * value;          
   245         }
   246 
   247       } else if (data[1] == 253) { // miniNG #1
   248         this.alternativeData = data;
   249 
   250         ReadminiNG(data, 0);        
   251               
   252         if (data[66] == 252)  // miniNG #2
   253           ReadminiNG(data, 1);
   254       } 
   255     }
   256 
   257     public Image Icon {
   258       get { return icon; }
   259     }
   260 
   261     public string Name {
   262       get { return "T-Balancer bigNG"; }
   263     }
   264 
   265     public Identifier Identifier {
   266       get { return new Identifier("bigng", this.portIndex.ToString()); }
   267     }
   268 
   269     public IHardware[] SubHardware {
   270       get { return new IHardware[0]; }
   271     }
   272 
   273     public ISensor[] Sensors {
   274       get { return active.ToArray(); }
   275     }
   276 
   277     public string GetReport() {
   278       StringBuilder r = new StringBuilder();
   279 
   280       r.AppendLine("T-Balancer bigNG");
   281       r.AppendLine();
   282       r.Append("Port Index: "); r.AppendLine(portIndex.ToString());
   283       r.AppendLine();
   284 
   285       r.AppendLine("Primary System Information Answer");
   286       r.AppendLine();
   287       r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   288       r.AppendLine();
   289       for (int i = 0; i <= 0x11; i++) {
   290         r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
   291         for (int j = 0; j <= 0xF; j++) {
   292           int index = ((i << 4) | j);
   293           if (index < primaryData.Length) {
   294             r.Append(" ");
   295             r.Append(primaryData[index].ToString("X2"));
   296           }          
   297         }
   298         r.AppendLine();
   299       }
   300       r.AppendLine();
   301 
   302       if (alternativeData.Length > 0) {
   303         r.AppendLine("Alternative System Information Answer");
   304         r.AppendLine();
   305         r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   306         r.AppendLine();
   307         for (int i = 0; i <= 0x11; i++) {
   308           r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
   309           for (int j = 0; j <= 0xF; j++) {
   310             int index = ((i << 4) | j);
   311             if (index < alternativeData.Length) {
   312               r.Append(" ");
   313               r.Append(alternativeData[index].ToString("X2"));
   314             }
   315           }
   316           r.AppendLine();
   317         }
   318         r.AppendLine();
   319       }
   320 
   321       return r.ToString();
   322     }
   323 
   324     private void DelayedAlternativeRequest() {
   325       System.Threading.Thread.Sleep(500);      
   326       FTD2XX.Write(handle, new byte[] { 0x37 });
   327     }
   328 
   329     public void Open() {
   330       FTD2XX.FT_Open(portIndex, out handle); 
   331       FTD2XX.FT_SetBaudRate(handle, 19200);
   332       FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
   333       FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11,
   334         0x13);
   335       FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
   336       FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   337     }
   338 
   339     public void Update() {
   340       while (FTD2XX.BytesToRead(handle) >= 285)
   341         ReadData();
   342       if (FTD2XX.BytesToRead(handle) == 1)
   343         FTD2XX.ReadByte(handle);
   344 
   345       FTD2XX.Write(handle, new byte[] { 0x38 });
   346       alternativeRequest.BeginInvoke(null, null);
   347     }
   348 
   349     public void Close() {
   350       FTD2XX.FT_Close(handle);
   351     }
   352 
   353     public event SensorEventHandler SensorAdded;
   354     public event SensorEventHandler SensorRemoved;
   355 
   356     public void Accept(IVisitor visitor) {
   357       visitor.VisitHardware(this);
   358     }
   359 
   360     public void Traverse(IVisitor visitor) { }
   361   }
   362 }