Skip to content

Commit

Permalink
Add Auto(Manual) state.
Browse files Browse the repository at this point in the history
The temperature at which the fan automatically operates can be set.
  • Loading branch information
t-miyake committed Sep 30, 2017
1 parent 982c7e9 commit b2eb5b4
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 66 deletions.
2 changes: 1 addition & 1 deletion PocketFanController/AboutWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Title="About | Pocket Fan Controller" Height="300" Width="300" ScrollViewer.VerticalScrollBarVisibility="Disabled" ShowInTaskbar="False" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<StackPanel>
<TextBlock Padding="8,0,0,0" Margin="0,5,0,0">Pocket Fan Controller</TextBlock>
<TextBlock Padding="15,0,0,0">Version 0.4.1</TextBlock>
<TextBlock Padding="15,0,0,0">Version 0.5.0</TextBlock>
<TextBlock Padding="15,0,0,0">© 2017 Takafumi Miyake</TextBlock>

<TextBlock Padding="8,0,0,0" Margin="0,10,0,0">Third-Party Software Usage and Licenses</TextBlock>
Expand Down
43 changes: 43 additions & 0 deletions PocketFanController/ConfigWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Window x:Class="PocketFanController.ConfigWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PocketFanController"
mc:Ignorable="d"
Title="Config | Pocket Fan Controller" Height="240" Width="380" Icon="Icon.ico" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<local:ConfigWindowViewModel />
</Window.DataContext>
<StackPanel Orientation="Vertical" Margin="5,5,5,5">
<Label Content="Config of the CPU temperature at which the fan operates.&#xa;This setting is applied in the case of Auto (Manual)."/>
<GroupBox Header="Config" Margin="3,1,3,0">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="0,2,2,2">
<Label Content="Margin"/>
<TextBox x:Name="Margin" Width="24" Margin="2" Text="{Binding Margin, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" HorizontalContentAlignment="Right"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,2,2,2">
<Label Content="Slowest ≧ 0℃"/>

<Label Content="Slow ≧" Margin="3,0,0,0"/>
<TextBox x:Name="BorderOfSlow" Width="24" Margin="0,2,3,2" Text="{Binding BorderOfSlow, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" HorizontalContentAlignment="Right"/>

<Label Content="Fast ≧" Margin="3,0,0,0"/>
<TextBox x:Name="BorderOfFast" Width="24" Margin="0,2,3,2" Text="{Binding BorderOfFast, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" HorizontalContentAlignment="Right"/>

<Label Content="Fastest ≧" Margin="3,0,0,0"/>
<TextBox x:Name="BorderOfFastest" Width="24" Margin="0,2,2,2" Text="{Binding BorderOfFastest, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" HorizontalContentAlignment="Right"/>
</StackPanel>
</StackPanel>
</GroupBox>
<StackPanel Orientation="Horizontal" Margin="0,10,3,1" HorizontalAlignment="Right">
<Button Content="Load default" Width="90" Command="{Binding LoadDefaultButton}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,10,3,5" HorizontalAlignment="Right">
<Button Content="OK" Width="55" Command="{Binding OkButton}"/>
<Button Content="Cancel" Width="55" Margin="8,0,0,0" IsCancel="True" Command="{Binding CancelButton}"/>
<Button Content="Apply" Width="55" Margin="8,0,0,0" Command="{Binding ApplyButton}"/>
</StackPanel>
</StackPanel>
</Window>
27 changes: 27 additions & 0 deletions PocketFanController/ConfigWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PocketFanController
{
/// <summary>
/// ConfigWindow.xaml の相互作用ロジック
/// </summary>
public partial class ConfigWindow : Window
{
public ConfigWindow()
{
InitializeComponent();
}
}
}
92 changes: 92 additions & 0 deletions PocketFanController/ConfigWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Windows;
using System.Windows.Input;

namespace PocketFanController
{
public class ConfigWindowViewModel : ViewModelBase
{
private Model Model = Model.Instance;

public ICommand LoadDefaultButton { get; }
public ICommand OkButton { get; }
public ICommand CancelButton { get; }
public ICommand ApplyButton { get; }

public int Margin
{
get => Model.ManualMargin;
set
{
Model.ManualMargin = value;
OnPropertyChanged("Margin");
}
}

public int BorderOfSlow
{
get => Model.ManualT0;
set
{
Model.ManualT0 =value;
OnPropertyChanged("BorderOfSlow");
}
}
public int BorderOfFast
{
get => Model.ManualT1;
set
{
Model.ManualT1 = value;
OnPropertyChanged("BorderOfFast");
}
}
public int BorderOfFastest
{
get => Model.ManualT2;
set
{
Model.ManualT2 = value;
OnPropertyChanged("BorderOfFastest");
}
}

public ConfigWindowViewModel()
{
LoadDefaultButton = new RelayCommand(() =>
{
Margin = 5;
BorderOfSlow = 40;
BorderOfFast = 60;
BorderOfFastest = 75;
});

OkButton = new RelayCommand(() =>
{
Model.SaveManualConfig(Margin,BorderOfSlow,BorderOfFast,BorderOfFastest);
Application.Current.MainWindow.Close();
});

ApplyButton = new RelayCommand(() =>
{
Model.SaveManualConfig(Margin, BorderOfSlow, BorderOfFast, BorderOfFastest);
MessageBox.Show("Apply completed.");
});

CancelButton = new RelayCommand(() =>
{
Application.Current.MainWindow.Close();
});

Model.GetManualConfigs();
UpdateStatus();
}

private void UpdateStatus()
{
OnPropertyChanged("Margin");
OnPropertyChanged("BorderOfSlow");
OnPropertyChanged("BorderOfFast");
OnPropertyChanged("BorderOfFastest");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<Window x:Class="PocketFanController.MainWindow"
<Window x:Class="PocketFanController.ControllerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PocketFanController"
mc:Ignorable="d"
Title="Pocket Fan Controller" Height="100" Width="352" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Icon="Icon.ico" ShowInTaskbar="False" ScrollViewer.VerticalScrollBarVisibility="Disabled">
Title="Pocket Fan Controller" Height="100" Width="462" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Icon="Icon.ico" ShowInTaskbar="False" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<Window.DataContext>
<local:ViewModel />
<local:ControllerWindowViewModel />
</Window.DataContext>
<StackPanel>
<Label x:Name="CurrentStatus" Content="{Binding CurrentStateText,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0" HorizontalAlignment="Left">
<Button x:Name="SetDefault" Content="Auto (Default)" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" Command="{Binding SetDefault}" Margin="5,0,10,0" HorizontalContentAlignment="Center" />
<Button x:Name="SetDefault" Content="Auto (Default)" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" Command="{Binding SetDefault}" Margin="5,0,10,0" HorizontalContentAlignment="Center" />
<Button x:Name="SetManual" Content="Auto (Manual)" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" Command="{Binding SetDefault}" Margin="5,0" HorizontalContentAlignment="Center" />
<Button x:Name="SetSlowest" Content="Slowest" HorizontalAlignment="Left" VerticalAlignment="Center" Width="45" Command="{Binding SetSlowest}" Margin="5,0"/>
<Button x:Name="SetSlow" Content="Slow" HorizontalAlignment="Left" VerticalAlignment="Center" Width="45" Command="{Binding SetSlow}" Margin="5,0"/>
<Button x:Name="SetFast" Content="Fast" HorizontalAlignment="Left" VerticalAlignment="Center" Width="45" Command="{Binding SetFast}" Margin="5,0"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace PocketFanController
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// ControllerWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
public partial class ControllerWindow : Window
{
public MainWindow()
public ControllerWindow()
{
InitializeComponent();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@

namespace PocketFanController
{
public class ViewModel:ViewModelBase
public class ControllerWindowViewModel : ViewModelBase
{
private Model Model = Model.Instance;

public ICommand SetDefault { get; }
public ICommand SetManual { get; }
public ICommand SetSlow { get; }
public ICommand SetFast { get; }
public ICommand SetFastest { get; }
public ICommand SetSlowest { get; }

public ViewModel()
public ControllerWindowViewModel()
{
SetDefault = new RelayCommand(()=>
{
Model.SetDefault();
CurrentState = 0;
});

SetManual = new RelayCommand(() =>
{
Model.SetManual();
CurrentState = 5;
});

SetSlow = new RelayCommand(() =>
{
Model.SetSlow();
Expand Down Expand Up @@ -73,6 +80,8 @@ public string CurrentStateText
return "Current status : Slow";
case 4:
return "Current status : Slowest";
case 5:
return "Current status : Auto (Manual)";
default:
return "Current status : Auto (Default)";
}
Expand Down
38 changes: 37 additions & 1 deletion PocketFanController/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ private Model(){}

public int CurrentState { get; set; } = 0;

public int ManualMargin { get; set; }
public int ManualT0 { get; set; }
public int ManualT1 { get; set; }
public int ManualT2 { get; set; }

public void GetManualConfigs()
{
ManualMargin = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin") == 0 ? 5 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin");
ManualT0 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0") == 0 ? 40 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0");
ManualT1 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1") == 0 ? 60 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1");
ManualT2 = ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2") == 0 ? 75 : ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2");
}

public void GetCurrentStatus()
{
var state0 = new[] {40, 60, 75};
Expand All @@ -37,10 +50,14 @@ public void GetCurrentStatus()
CurrentState = s.Index;
}
}

if (current.SequenceEqual(states[CurrentState])) return;
CurrentState = 5;
}

public void SetDefault()
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", 5);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", 40);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", 60);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", 75);
Expand All @@ -49,6 +66,7 @@ public void SetDefault()

public void SetSlow()
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", 5);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", 10);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", 99);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", 99);
Expand All @@ -57,6 +75,7 @@ public void SetSlow()

public void SetFast()
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", 5);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", 99);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", 10);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", 99);
Expand All @@ -65,6 +84,7 @@ public void SetFast()

public void SetFastest()
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", 5);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", 99);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", 99);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", 10);
Expand All @@ -73,13 +93,30 @@ public void SetFastest()

public void SetSlowest()
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", 5);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", 99);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", 99);
//熱くなりすぎたらファンを全開にする。
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", 85);
RestartService();
}

public void SetManual()
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "margin", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin"));
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t0", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0"));
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t1", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1"));
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "t2", ReadReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2"));
RestartService();
}

public void SaveManualConfig(int margin, int t0, int t1, int t2)
{
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedmargin", margin);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt0", t0);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt1", t1);
WriteReg(@"SYSTEM\CurrentControlSet\Services\wfan0109", "savedt2", t2);
}

public void WriteReg(string subKey, string keyName, int value)
{
Expand All @@ -98,7 +135,6 @@ public int ReadReg(string subKey, string keyName)
catch
{
return 0;

}
return 0;
}
Expand Down
Loading

0 comments on commit b2eb5b4

Please sign in to comment.