Hardware/Control.cs
author moel.mich
Thu, 20 Jan 2011 21:31:54 +0000
changeset 247 6dc755f1970e
child 344 3145aadca3d2
permissions -rw-r--r--
Added a minimal control interface to allow manual fan control and implemented the interface for ATI GPUs.
moel@247
     1
/*
moel@247
     2
  
moel@247
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@247
     4
moel@247
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@247
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@247
     7
  the License. You may obtain a copy of the License at
moel@247
     8
 
moel@247
     9
  http://www.mozilla.org/MPL/
moel@247
    10
moel@247
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@247
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@247
    13
  for the specific language governing rights and limitations under the License.
moel@247
    14
moel@247
    15
  The Original Code is the Open Hardware Monitor code.
moel@247
    16
moel@247
    17
  The Initial Developer of the Original Code is 
moel@247
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@247
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@247
    20
  the Initial Developer. All Rights Reserved.
moel@247
    21
moel@247
    22
  Contributor(s):
moel@247
    23
moel@247
    24
  Alternatively, the contents of this file may be used under the terms of
moel@247
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@247
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@247
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@247
    28
  of those above. If you wish to allow use of your version of this file only
moel@247
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@247
    30
  use your version of this file under the terms of the MPL, indicate your
moel@247
    31
  decision by deleting the provisions above and replace them with the notice
moel@247
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@247
    33
  the provisions above, a recipient may use your version of this file under
moel@247
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@247
    35
 
moel@247
    36
*/
moel@247
    37
moel@247
    38
using System;
moel@247
    39
using System.Globalization;
moel@247
    40
moel@247
    41
namespace OpenHardwareMonitor.Hardware {
moel@247
    42
moel@247
    43
  internal delegate void ControlEventHandler(Control control);
moel@247
    44
moel@247
    45
  internal class Control : IControl {
moel@247
    46
moel@247
    47
    private readonly Identifier identifier;
moel@247
    48
    private readonly ISettings settings;
moel@247
    49
    private ControlMode mode;
moel@247
    50
    private float softwareValue;
moel@247
    51
    private float minSoftwareValue;
moel@247
    52
    private float maxSoftwareValue;
moel@247
    53
moel@247
    54
    public Control(ISensor sensor, ISettings settings, float minSoftwareValue,
moel@247
    55
      float maxSoftwareValue) 
moel@247
    56
    {
moel@247
    57
      this.identifier = new Identifier(sensor.Identifier, "control");
moel@247
    58
      this.settings = settings;
moel@247
    59
      this.minSoftwareValue = minSoftwareValue;
moel@247
    60
      this.maxSoftwareValue = maxSoftwareValue;
moel@247
    61
moel@247
    62
      if (!float.TryParse(settings.GetValue(
moel@247
    63
          new Identifier(identifier, "value").ToString(), "0"),
moel@247
    64
        NumberStyles.Float, CultureInfo.InvariantCulture,
moel@247
    65
        out this.softwareValue)) 
moel@247
    66
      {
moel@247
    67
        this.softwareValue = 0;
moel@247
    68
      }
moel@247
    69
      int mode;
moel@247
    70
      if (!int.TryParse(settings.GetValue(
moel@247
    71
          new Identifier(identifier, "mode").ToString(),
moel@247
    72
          ((int)ControlMode.Default).ToString(CultureInfo.InvariantCulture)),
moel@247
    73
        NumberStyles.Integer, CultureInfo.InvariantCulture,
moel@247
    74
        out mode)) 
moel@247
    75
      {
moel@247
    76
        this.mode = ControlMode.Default;
moel@247
    77
      } else {
moel@247
    78
        this.mode = (ControlMode)mode;
moel@247
    79
      }
moel@247
    80
    }
moel@247
    81
moel@247
    82
    public Identifier Identifier {
moel@247
    83
      get {
moel@247
    84
        return identifier;
moel@247
    85
      }
moel@247
    86
    }
moel@247
    87
moel@247
    88
    public ControlMode ControlMode {
moel@247
    89
      get {
moel@247
    90
        return mode;
moel@247
    91
      }
moel@247
    92
      private set {
moel@247
    93
        if (mode != value) {
moel@247
    94
          mode = value;
moel@247
    95
          if (ControlModeChanged != null)
moel@247
    96
            ControlModeChanged(this);
moel@247
    97
          this.settings.SetValue(new Identifier(identifier, "mode").ToString(),
moel@247
    98
            ((int)mode).ToString(CultureInfo.InvariantCulture));
moel@247
    99
        }
moel@247
   100
      }
moel@247
   101
    }
moel@247
   102
moel@247
   103
    public float SoftwareValue {
moel@247
   104
      get {
moel@247
   105
        return softwareValue;
moel@247
   106
      }
moel@247
   107
      private set {
moel@247
   108
        if (softwareValue != value) {
moel@247
   109
          softwareValue = value;
moel@247
   110
          if (SoftwareControlValueChanged != null)
moel@247
   111
            SoftwareControlValueChanged(this);
moel@247
   112
          this.settings.SetValue(new Identifier(identifier,
moel@247
   113
            "value").ToString(),
moel@247
   114
            value.ToString(CultureInfo.InvariantCulture));
moel@247
   115
        }
moel@247
   116
      }
moel@247
   117
    }
moel@247
   118
moel@247
   119
    public void SetDefault() {
moel@247
   120
      ControlMode = ControlMode.Default;
moel@247
   121
    }
moel@247
   122
moel@247
   123
    public float MinSoftwareValue {
moel@247
   124
      get {
moel@247
   125
        return minSoftwareValue;
moel@247
   126
      }
moel@247
   127
    }
moel@247
   128
moel@247
   129
    public float MaxSoftwareValue {
moel@247
   130
      get {
moel@247
   131
        return maxSoftwareValue;
moel@247
   132
      }
moel@247
   133
    }
moel@247
   134
moel@247
   135
    public void SetSoftware(float value) {
moel@247
   136
      ControlMode = ControlMode.Software;
moel@247
   137
      SoftwareValue = value;
moel@247
   138
    }
moel@247
   139
moel@247
   140
    internal event ControlEventHandler ControlModeChanged;
moel@247
   141
    internal event ControlEventHandler SoftwareControlValueChanged;
moel@247
   142
  }
moel@247
   143
}