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