Hardware/TBalancer/TBalancer.cs
author moel.mich
Mon, 05 Apr 2010 15:51:40 +0000
changeset 88 bce8363c119c
parent 87 ecdc3bcef083
child 109 70d0c3102424
permissions -rw-r--r--
Fixed Issue 24.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.Configuration;
moel@1
    41
using System.Drawing;
moel@1
    42
using System.Text;
moel@1
    43
moel@1
    44
namespace OpenHardwareMonitor.Hardware.TBalancer {
moel@1
    45
  public class TBalancer : IHardware {
moel@1
    46
moel@87
    47
    private int portIndex;
moel@87
    48
    private FT_HANDLE handle;
moel@1
    49
    private Image icon;
moel@33
    50
    private byte protocolVersion;
moel@1
    51
    private Sensor[] digitalTemperatures = new Sensor[8];
moel@1
    52
    private Sensor[] analogTemperatures = new Sensor[4];
moel@57
    53
    private Sensor[] sensorhubTemperatures = new Sensor[6];
moel@57
    54
    private Sensor[] sensorhubFlows = new Sensor[2];
moel@1
    55
    private Sensor[] fans = new Sensor[4];
moel@57
    56
    private Sensor[] miniNGTemperatures = new Sensor[4];
moel@57
    57
    private Sensor[] miniNGFans = new Sensor[4];
moel@1
    58
    private List<ISensor> active = new List<ISensor>();
moel@1
    59
    private List<ISensor> deactivating = new List<ISensor>();
moel@57
    60
    private int[] primaryData = new int[0];
moel@57
    61
    private int[] alternativeData = new int[0];
moel@1
    62
moel@1
    63
    public const byte STARTFLAG = 100;
moel@57
    64
    public const byte ENDFLAG = 254;
moel@57
    65
moel@57
    66
    private delegate void MethodDelegate();
moel@57
    67
    private MethodDelegate alternativeRequest;    
moel@1
    68
moel@87
    69
    public TBalancer(int portIndex, byte protocolVersion) {
moel@87
    70
      this.portIndex = portIndex;
moel@57
    71
      this.icon = Utilities.EmbeddedResources.GetImage("bigng.png");
moel@33
    72
      this.protocolVersion = protocolVersion;
moel@1
    73
moel@63
    74
      ParameterDescription[] parameter = new ParameterDescription[] {
moel@63
    75
        new ParameterDescription("Offset", "Temperature offset.", 0)
moel@63
    76
      };
moel@57
    77
      int offset = 0;
moel@57
    78
      for (int i = 0; i < digitalTemperatures.Length; i++)
moel@88
    79
        digitalTemperatures[i] = new Sensor("Digital Sensor " + i,
moel@63
    80
          offset + i, null, SensorType.Temperature, this, parameter);
moel@57
    81
      offset += digitalTemperatures.Length;
moel@57
    82
moel@57
    83
      for (int i = 0; i < analogTemperatures.Length; i++)
moel@88
    84
        analogTemperatures[i] = new Sensor("Analog Sensor " + (i + 1),
moel@63
    85
          offset + i, null, SensorType.Temperature, this, parameter);
moel@57
    86
      offset += analogTemperatures.Length;
moel@57
    87
moel@57
    88
      for (int i = 0; i < sensorhubTemperatures.Length; i++)
moel@88
    89
        sensorhubTemperatures[i] = new Sensor("Sensorhub Sensor " + i,
moel@63
    90
          offset + i, null, SensorType.Temperature, this, parameter);
moel@57
    91
      offset += sensorhubTemperatures.Length;
moel@57
    92
moel@57
    93
      for (int i = 0; i < sensorhubFlows.Length; i++)
moel@88
    94
        sensorhubFlows[i] = new Sensor("Flowmeter " + (i + 1),
moel@63
    95
          offset + i, null, SensorType.Flow, this, new ParameterDescription[] {
moel@63
    96
            new ParameterDescription("Impulse Rate", 
moel@63
    97
              "The impulse rate of the flowmeter in pulses/L", 509)
moel@63
    98
          });
moel@57
    99
      offset += sensorhubFlows.Length;
moel@57
   100
moel@57
   101
      for (int i = 0; i < miniNGTemperatures.Length; i++)
moel@63
   102
        miniNGTemperatures[i] = new Sensor("miniNG #" + (i / 2 + 1) +
moel@88
   103
          " Sensor " + (i % 2 + 1), offset + i, null, SensorType.Temperature, 
moel@63
   104
          this, parameter);
moel@57
   105
      offset += miniNGTemperatures.Length;
moel@57
   106
moel@57
   107
      alternativeRequest = new MethodDelegate(DelayedAlternativeRequest);
moel@57
   108
moel@87
   109
      Open();
moel@87
   110
      Update(); 
moel@1
   111
    }
moel@1
   112
moel@1
   113
    private void ActivateSensor(Sensor sensor) {
moel@1
   114
      deactivating.Remove(sensor);
moel@1
   115
      if (!active.Contains(sensor)) {
moel@1
   116
        active.Add(sensor);
moel@15
   117
        if (SensorAdded != null)
moel@15
   118
          SensorAdded(sensor);
moel@1
   119
      }      
moel@1
   120
    }
moel@1
   121
moel@1
   122
    private void DeactivateSensor(Sensor sensor) {
moel@1
   123
      if (deactivating.Contains(sensor)) {
moel@1
   124
        active.Remove(sensor);
moel@1
   125
        deactivating.Remove(sensor);
moel@15
   126
        if (SensorRemoved != null)
moel@15
   127
          SensorRemoved(sensor);
moel@1
   128
      } else if (active.Contains(sensor)) {
moel@1
   129
        deactivating.Add(sensor);
moel@1
   130
      }     
moel@1
   131
    }
moel@1
   132
moel@57
   133
    private void ReadminiNG(int[] data, int number) {
moel@57
   134
      int offset = 1 + number * 65;
moel@57
   135
moel@57
   136
      if (data[offset + 61] != ENDFLAG)
moel@57
   137
        return;
moel@57
   138
moel@57
   139
      for (int i = 0; i < 2; i++) {
moel@57
   140
        Sensor sensor = miniNGTemperatures[number * 2 + i];
moel@57
   141
        if (data[offset + 7 + i] > 0) {
moel@63
   142
          sensor.Value = 0.5f * data[offset + 7 + i] + 
moel@63
   143
            sensor.Parameters[0].Value;
moel@57
   144
          ActivateSensor(sensor);
moel@57
   145
        } else {
moel@57
   146
          DeactivateSensor(sensor);
moel@57
   147
        }
moel@57
   148
      }
moel@57
   149
moel@57
   150
      for (int i = 0; i < 2; i++) {
moel@57
   151
        float maxRPM = 20.0f * data[offset + 44 + 2 * i];
moel@57
   152
moel@57
   153
        if (miniNGFans[number * 2 + i] == null)
moel@57
   154
          miniNGFans[number * 2 + i] = 
moel@88
   155
            new Sensor("miniNG #" + (number + 1) + " Fan Channel " + (i + 1),
moel@57
   156
            4 + number * 2 + i, maxRPM, SensorType.Fan, this);
moel@57
   157
        
moel@57
   158
        Sensor sensor = miniNGFans[number * 2 + i];
moel@57
   159
moel@57
   160
        sensor.Value = 20.0f * data[offset + 43 + 2 * i];
moel@57
   161
        ActivateSensor(sensor);
moel@57
   162
      }
moel@57
   163
    }
moel@57
   164
moel@1
   165
    private void ReadData() {
moel@1
   166
      int[] data = new int[285];
moel@1
   167
      for (int i = 0; i < data.Length; i++)
moel@87
   168
        data[i] = FTD2XX.ReadByte(handle);
moel@1
   169
moel@57
   170
      if (data[0] != STARTFLAG) {
moel@87
   171
        FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_RX);   
moel@1
   172
        return;
moel@1
   173
      }
moel@1
   174
moel@73
   175
      if (data[1] == 255 || data[1] == 88) { // bigNG
moel@57
   176
moel@57
   177
        if (data[274] != protocolVersion) 
moel@57
   178
          return;
moel@57
   179
moel@57
   180
        this.primaryData = data;
moel@57
   181
moel@57
   182
        for (int i = 0; i < digitalTemperatures.Length; i++)
moel@57
   183
          if (data[238 + i] > 0) {
moel@63
   184
            digitalTemperatures[i].Value = 0.5f * data[238 + i] + 
moel@63
   185
              digitalTemperatures[i].Parameters[0].Value;
moel@57
   186
            ActivateSensor(digitalTemperatures[i]);
moel@57
   187
          } else {
moel@57
   188
            DeactivateSensor(digitalTemperatures[i]);
moel@57
   189
          }
moel@57
   190
moel@57
   191
        for (int i = 0; i < analogTemperatures.Length; i++)
moel@57
   192
          if (data[260 + i] > 0) {
moel@63
   193
            analogTemperatures[i].Value = 0.5f * data[260 + i] +
moel@63
   194
              analogTemperatures[i].Parameters[0].Value;
moel@57
   195
            ActivateSensor(analogTemperatures[i]);
moel@57
   196
          } else {
moel@57
   197
            DeactivateSensor(analogTemperatures[i]);
moel@57
   198
          }
moel@57
   199
moel@57
   200
        for (int i = 0; i < sensorhubTemperatures.Length; i++)
moel@57
   201
          if (data[246 + i] > 0) {
moel@63
   202
            sensorhubTemperatures[i].Value = 0.5f * data[246 + i] +
moel@63
   203
              sensorhubTemperatures[i].Parameters[0].Value;
moel@57
   204
            ActivateSensor(sensorhubTemperatures[i]);
moel@57
   205
          } else {
moel@57
   206
            DeactivateSensor(sensorhubTemperatures[i]);
moel@57
   207
          }
moel@57
   208
moel@57
   209
        for (int i = 0; i < sensorhubFlows.Length; i++)
moel@57
   210
          if (data[231 + i] > 0 && data[234] > 0) {
moel@66
   211
            float pulsesPerSecond = (data[231 + i] * 4.0f) / data[234];
moel@63
   212
            float pulsesPerLiter = sensorhubFlows[i].Parameters[0].Value;
moel@57
   213
            sensorhubFlows[i].Value = pulsesPerSecond * 3600 / pulsesPerLiter;
moel@57
   214
            ActivateSensor(sensorhubFlows[i]);
moel@57
   215
          } else {
moel@57
   216
            DeactivateSensor(sensorhubFlows[i]);
moel@57
   217
          }
moel@86
   218
        
moel@57
   219
        for (int i = 0; i < fans.Length; i++) {
moel@57
   220
          float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
moel@57
   221
moel@57
   222
          if (fans[i] == null)
moel@88
   223
            fans[i] = new Sensor("Fan Channel " + i, i, maxRPM, SensorType.Fan,
moel@63
   224
              this, new ParameterDescription[] {
moel@63
   225
                new ParameterDescription("MaxRPM", 
moel@63
   226
                  "Maximum revolutions per minute (RPM) of the fan.", maxRPM)
moel@63
   227
              });
moel@57
   228
moel@63
   229
          if ((data[136] & (1 << i)) == 0) // pwm mode
moel@75
   230
            fans[i].Value = fans[i].Parameters[0].Value * 0.02f * data[137 + i];
moel@63
   231
          else // analog mode
moel@63
   232
            fans[i].Value = fans[i].Parameters[0].Value * 0.01f * data[141 + i]; 
moel@57
   233
          ActivateSensor(fans[i]);
moel@1
   234
        }
moel@1
   235
moel@57
   236
      } else if (data[1] == 253) { // miniNG #1
moel@57
   237
        this.alternativeData = data;
moel@1
   238
moel@57
   239
        ReadminiNG(data, 0);        
moel@57
   240
              
moel@57
   241
        if (data[66] == 252)  // miniNG #2
moel@57
   242
          ReadminiNG(data, 1);
moel@57
   243
      } 
moel@1
   244
    }
moel@1
   245
moel@1
   246
    public Image Icon {
moel@1
   247
      get { return icon; }
moel@1
   248
    }
moel@1
   249
moel@1
   250
    public string Name {
moel@1
   251
      get { return "T-Balancer bigNG"; }
moel@1
   252
    }
moel@1
   253
moel@1
   254
    public string Identifier {
moel@87
   255
      get { return "/bigng/" + this.portIndex; }
moel@1
   256
    }
moel@1
   257
moel@64
   258
    public IHardware[] SubHardware {
moel@64
   259
      get { return new IHardware[0]; }
moel@64
   260
    }
moel@64
   261
moel@1
   262
    public ISensor[] Sensors {
moel@1
   263
      get { return active.ToArray(); }
moel@1
   264
    }
moel@1
   265
moel@1
   266
    public string GetReport() {
moel@1
   267
      StringBuilder r = new StringBuilder();
moel@1
   268
moel@1
   269
      r.AppendLine("T-Balancer bigNG");
moel@1
   270
      r.AppendLine();
moel@87
   271
      r.Append("Port Index: "); r.AppendLine(portIndex.ToString());
moel@1
   272
      r.AppendLine();
moel@57
   273
moel@57
   274
      r.AppendLine("Primary System Information Answer");
moel@1
   275
      r.AppendLine();
moel@1
   276
      r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@1
   277
      r.AppendLine();
moel@1
   278
      for (int i = 0; i <= 0x11; i++) {
moel@1
   279
        r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
moel@1
   280
        for (int j = 0; j <= 0xF; j++) {
moel@1
   281
          int index = ((i << 4) | j);
moel@57
   282
          if (index < primaryData.Length) {
moel@1
   283
            r.Append(" ");
moel@57
   284
            r.Append(primaryData[index].ToString("X2"));
moel@1
   285
          }          
moel@1
   286
        }
moel@1
   287
        r.AppendLine();
moel@1
   288
      }
moel@1
   289
      r.AppendLine();
moel@1
   290
moel@57
   291
      if (alternativeData.Length > 0) {
moel@57
   292
        r.AppendLine("Alternative System Information Answer");
moel@57
   293
        r.AppendLine();
moel@57
   294
        r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@57
   295
        r.AppendLine();
moel@57
   296
        for (int i = 0; i <= 0x11; i++) {
moel@57
   297
          r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
moel@57
   298
          for (int j = 0; j <= 0xF; j++) {
moel@57
   299
            int index = ((i << 4) | j);
moel@57
   300
            if (index < alternativeData.Length) {
moel@57
   301
              r.Append(" ");
moel@57
   302
              r.Append(alternativeData[index].ToString("X2"));
moel@57
   303
            }
moel@57
   304
          }
moel@57
   305
          r.AppendLine();
moel@57
   306
        }
moel@57
   307
        r.AppendLine();
moel@57
   308
      }
moel@57
   309
moel@1
   310
      return r.ToString();
moel@1
   311
    }
moel@1
   312
moel@57
   313
    private void DelayedAlternativeRequest() {
moel@87
   314
      System.Threading.Thread.Sleep(500);      
moel@87
   315
      FTD2XX.Write(handle, new byte[] { 0x37 });
moel@57
   316
    }
moel@57
   317
moel@87
   318
    public void Open() {
moel@87
   319
      FTD2XX.FT_Open(portIndex, out handle); 
moel@87
   320
      FTD2XX.FT_SetBaudRate(handle, 19200);
moel@87
   321
      FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
moel@87
   322
      FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11,
moel@87
   323
        0x13);
moel@87
   324
      FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
moel@87
   325
      FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
moel@87
   326
    }
moel@57
   327
moel@87
   328
    public void Update() {
moel@87
   329
      while (FTD2XX.BytesToRead(handle) >= 285)
moel@87
   330
        ReadData();
moel@87
   331
      if (FTD2XX.BytesToRead(handle) == 1)
moel@87
   332
        FTD2XX.ReadByte(handle);
moel@87
   333
moel@87
   334
      FTD2XX.Write(handle, new byte[] { 0x38 });
moel@87
   335
      alternativeRequest.BeginInvoke(null, null);
moel@1
   336
    }
moel@1
   337
moel@1
   338
    public void Close() {
moel@87
   339
      FTD2XX.FT_Close(handle);
moel@1
   340
    }
moel@1
   341
moel@1
   342
    public event SensorEventHandler SensorAdded;
moel@1
   343
    public event SensorEventHandler SensorRemoved;
moel@1
   344
  }
moel@1
   345
}