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