Hardware/Control.cs
changeset 247 6dc755f1970e
child 344 3145aadca3d2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/Control.cs	Thu Jan 20 21:31:54 2011 +0000
     1.3 @@ -0,0 +1,143 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Globalization;
    1.43 +
    1.44 +namespace OpenHardwareMonitor.Hardware {
    1.45 +
    1.46 +  internal delegate void ControlEventHandler(Control control);
    1.47 +
    1.48 +  internal class Control : IControl {
    1.49 +
    1.50 +    private readonly Identifier identifier;
    1.51 +    private readonly ISettings settings;
    1.52 +    private ControlMode mode;
    1.53 +    private float softwareValue;
    1.54 +    private float minSoftwareValue;
    1.55 +    private float maxSoftwareValue;
    1.56 +
    1.57 +    public Control(ISensor sensor, ISettings settings, float minSoftwareValue,
    1.58 +      float maxSoftwareValue) 
    1.59 +    {
    1.60 +      this.identifier = new Identifier(sensor.Identifier, "control");
    1.61 +      this.settings = settings;
    1.62 +      this.minSoftwareValue = minSoftwareValue;
    1.63 +      this.maxSoftwareValue = maxSoftwareValue;
    1.64 +
    1.65 +      if (!float.TryParse(settings.GetValue(
    1.66 +          new Identifier(identifier, "value").ToString(), "0"),
    1.67 +        NumberStyles.Float, CultureInfo.InvariantCulture,
    1.68 +        out this.softwareValue)) 
    1.69 +      {
    1.70 +        this.softwareValue = 0;
    1.71 +      }
    1.72 +      int mode;
    1.73 +      if (!int.TryParse(settings.GetValue(
    1.74 +          new Identifier(identifier, "mode").ToString(),
    1.75 +          ((int)ControlMode.Default).ToString(CultureInfo.InvariantCulture)),
    1.76 +        NumberStyles.Integer, CultureInfo.InvariantCulture,
    1.77 +        out mode)) 
    1.78 +      {
    1.79 +        this.mode = ControlMode.Default;
    1.80 +      } else {
    1.81 +        this.mode = (ControlMode)mode;
    1.82 +      }
    1.83 +    }
    1.84 +
    1.85 +    public Identifier Identifier {
    1.86 +      get {
    1.87 +        return identifier;
    1.88 +      }
    1.89 +    }
    1.90 +
    1.91 +    public ControlMode ControlMode {
    1.92 +      get {
    1.93 +        return mode;
    1.94 +      }
    1.95 +      private set {
    1.96 +        if (mode != value) {
    1.97 +          mode = value;
    1.98 +          if (ControlModeChanged != null)
    1.99 +            ControlModeChanged(this);
   1.100 +          this.settings.SetValue(new Identifier(identifier, "mode").ToString(),
   1.101 +            ((int)mode).ToString(CultureInfo.InvariantCulture));
   1.102 +        }
   1.103 +      }
   1.104 +    }
   1.105 +
   1.106 +    public float SoftwareValue {
   1.107 +      get {
   1.108 +        return softwareValue;
   1.109 +      }
   1.110 +      private set {
   1.111 +        if (softwareValue != value) {
   1.112 +          softwareValue = value;
   1.113 +          if (SoftwareControlValueChanged != null)
   1.114 +            SoftwareControlValueChanged(this);
   1.115 +          this.settings.SetValue(new Identifier(identifier,
   1.116 +            "value").ToString(),
   1.117 +            value.ToString(CultureInfo.InvariantCulture));
   1.118 +        }
   1.119 +      }
   1.120 +    }
   1.121 +
   1.122 +    public void SetDefault() {
   1.123 +      ControlMode = ControlMode.Default;
   1.124 +    }
   1.125 +
   1.126 +    public float MinSoftwareValue {
   1.127 +      get {
   1.128 +        return minSoftwareValue;
   1.129 +      }
   1.130 +    }
   1.131 +
   1.132 +    public float MaxSoftwareValue {
   1.133 +      get {
   1.134 +        return maxSoftwareValue;
   1.135 +      }
   1.136 +    }
   1.137 +
   1.138 +    public void SetSoftware(float value) {
   1.139 +      ControlMode = ControlMode.Software;
   1.140 +      SoftwareValue = value;
   1.141 +    }
   1.142 +
   1.143 +    internal event ControlEventHandler ControlModeChanged;
   1.144 +    internal event ControlEventHandler SoftwareControlValueChanged;
   1.145 +  }
   1.146 +}