Browse Source

Profile-specific weights for CPU and GPU temperatures

Tankernn 3 years ago
parent
commit
7951acf34b

+ 1 - 2
README.md

@@ -20,7 +20,6 @@ A Java-based, open-source alternative to the default control software for the NZ
 - On windows systems, this class communicates with an external program called openhardwaremonitor @ http://openhardwaremonitor.org/
 - This project uses [SystemTray](https://github.com/dorkbox/SystemTray).
 
-##TODO
-- Make it possible to set weights for different temperature readings.
+## TODO
 - Unit tests
 - Start on boot for Windows and Linux

+ 2 - 2
src/main/java/eu/tankernn/grid/Fan.java

@@ -25,8 +25,8 @@ public class Fan {
 		this.speed = (int) (100 * voltage / 12);
 	}
 
-	public void update(double temp, int minSpeed) {
-		int calcSpeed = profile.getSpeedPercentage(temp);
+	public void update(double cpuTemp, double gpuTemp, int minSpeed) {
+		int calcSpeed = profile.getSpeedPercentage(cpuTemp, gpuTemp);
 		try {
 			setFanSpeed(calcSpeed < minSpeed ? 0 : calcSpeed);
 		} catch (IOException | InterruptedException e) {

+ 15 - 2
src/main/java/eu/tankernn/grid/FanSpeedProfile.java

@@ -8,13 +8,17 @@ public class FanSpeedProfile {
 
 	private String name;
 	private int[] percentages;
+	private int cpuWeightPercent, gpuWeightPercent;
 
-	public FanSpeedProfile(String name, int[] percentages) {
+	public FanSpeedProfile(String name, int[] percentages, int cpuWeight) {
 		this.setName(name);
 		this.setPercentages(percentages);
+		this.setCpuWeight(cpuWeight);
 	}
 
-	public int getSpeedPercentage(double temp) {
+	public int getSpeedPercentage(double cpuTemp, double gpuTemp) {
+		double temp = (cpuTemp * cpuWeightPercent + gpuTemp * gpuWeightPercent) / 100;
+		System.out.println(temp);
 		double currentTemp = MIN_TEMP;
 
 		for (int i : getPercentages()) {
@@ -47,4 +51,13 @@ public class FanSpeedProfile {
 	public void setPercentages(int[] percentages) {
 		this.percentages = percentages;
 	}
+
+	public int getCpuWeight() {
+		return cpuWeightPercent;
+	}
+
+	public void setCpuWeight(int cpuWeight) {
+		this.cpuWeightPercent = cpuWeight;
+		this.gpuWeightPercent = 100 - cpuWeight;
+	}
 }

+ 14 - 9
src/main/java/eu/tankernn/grid/frame/GridControlPanel.java

@@ -97,7 +97,8 @@ public class GridControlPanel extends JFrame {
 		this.add(serialPanel, BorderLayout.NORTH);
 
 		fanPanels = model.getGrid().fanStream().map(f -> new FanPanel(f, model.getProfiles())).toArray(FanPanel[]::new);
-
+		updateProfileMenu();
+		
 		gridPanel.setLayout(new GridLayout(3, 2));
 		for (FanPanel p : fanPanels)
 			gridPanel.add(p);
@@ -136,16 +137,20 @@ public class GridControlPanel extends JFrame {
 		if (profile != null) {
 			model.addProfile(profile);
 			Arrays.stream(fanPanels).forEach(f -> f.addProfile(profile));
-			profileMenu.removeAll();
-			for (FanSpeedProfile p : model.getCustomProfiles()) {
-				JMenuItem item = new JMenuItem(p.getName());
-				item.addActionListener(a -> new ProfileEditor().editProfile(p));
-				profileMenu.add(item);
-			}
-			profileMenu.add(new JSeparator());
-			profileMenu.add(addProfile);
+			updateProfileMenu();
 		}
 	}
+	
+	private void updateProfileMenu() {
+		profileMenu.removeAll();
+		for (FanSpeedProfile p : model.getCustomProfiles()) {
+			JMenuItem item = new JMenuItem(p.getName());
+			item.addActionListener(a -> new ProfileEditor().editProfile(p));
+			profileMenu.add(item);
+		}
+		profileMenu.add(new JSeparator());
+		profileMenu.add(addProfile);
+	}
 
 	static JPanel labelledComponent(String labelText, JComponent component) {
 		JPanel panel = new JPanel(new FlowLayout());

+ 13 - 2
src/main/java/eu/tankernn/grid/frame/ProfileEditor.java

@@ -26,13 +26,15 @@ public class ProfileEditor {
 	}
 
 	public FanSpeedProfile editProfile(FanSpeedProfile profile) {
-		JPanel panel = new JPanel(), sliderPanel = new JPanel();
+		JPanel panel = new JPanel(), sliderPanel = new JPanel(), weightPanel = new JPanel();
 		JSlider[] sliders;
+		JSlider weightSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
 		JTextField nameField = new JTextField(20);
 		if (profile != null) {
 			nameField.setText(profile.getName());
 			sliders = Arrays.stream(profile.getPercentages()).mapToObj(i -> new JSlider(JSlider.VERTICAL, 0, 100, i))
 					.toArray(JSlider[]::new);
+			weightSlider.setValue(profile.getCpuWeight());
 		} else {
 			sliders = IntStream.range(0, FanSpeedProfile.STEPS).mapToObj(i -> new JSlider(JSlider.VERTICAL, 0, 100, 50))
 					.toArray(JSlider[]::new);
@@ -60,6 +62,14 @@ public class ProfileEditor {
 		panel.add(GridControlPanel.labelledComponent("Profile name: ", nameField), BorderLayout.NORTH);
 
 		panel.add(sliderPanel, BorderLayout.CENTER);
+		
+		weightPanel.add(new JLabel("GPU"));
+		weightSlider.setMajorTickSpacing(50);
+		weightSlider.setMinorTickSpacing(10);
+		weightSlider.setPaintTicks(true);
+		weightPanel.add(weightSlider);
+		weightPanel.add(new JLabel("CPU"));
+		panel.add(weightPanel, BorderLayout.SOUTH);
 
 		int response = JOptionPane.showConfirmDialog(null, panel, "Fan Speed Profile Editor",
 				JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
@@ -67,10 +77,11 @@ public class ProfileEditor {
 		if (response == JOptionPane.OK_OPTION) {
 			int[] percs = Arrays.stream(sliders).mapToInt(JSlider::getValue).toArray();
 			if (profile == null)
-				profile = new FanSpeedProfile(nameField.getText(), percs);
+				profile = new FanSpeedProfile(nameField.getText(), percs, weightSlider.getValue());
 			else {
 				profile.setPercentages(percs);
 				profile.setName(nameField.getText());
+				profile.setCpuWeight(weightSlider.getValue());
 			}
 			if (nameField.getText().isEmpty()) {
 				JOptionPane.showMessageDialog(null, "Please enter a name for the profile.");

+ 2 - 11
src/main/java/eu/tankernn/grid/model/ComputerModel.java

@@ -79,7 +79,7 @@ public class ComputerModel {
 	}
 
 	private List<FanSpeedProfile> generateProfiles() {
-		return IntStream.range(30 / 5, 100 / 5 + 1).map(i -> i * 5).mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i })).collect(Collectors.toCollection(ArrayList::new));
+		return IntStream.range(30 / 5, 100 / 5 + 1).map(i -> i * 5).mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i }, 50)).collect(Collectors.toCollection(ArrayList::new));
 	}
 
 	/**
@@ -91,7 +91,7 @@ public class ComputerModel {
 	 *
 	 */
 	public void compute() {
-		grid.updateFanSpeeds(getTemp(), minSpeed);
+		grid.updateFanSpeeds(sensor.getCPUTemp(), sensor.getGPUTemp(), minSpeed);
 	}
 
 	/**
@@ -138,15 +138,6 @@ public class ComputerModel {
 		return portMap;
 	}
 
-	/**
-	 * 
-	 * @return The temperature used to calculate fan speeds.
-	 */
-	public double getTemp() {
-		// TODO Make configurable
-		return (sensor.getCPUTemp() + sensor.getGPUTemp()) / 2;
-	}
-
 	public List<FanSpeedProfile> getProfiles() {
 		return Stream.concat(defaultProfiles.stream(), customProfiles.stream()).collect(Collectors.toList());
 	}

+ 2 - 2
src/main/java/eu/tankernn/grid/model/GRID.java

@@ -49,8 +49,8 @@ public class GRID {
 				f.poll();
 	}
 
-	public void updateFanSpeeds(double temp, int minSpeed) {
-		fanStream().forEach(f -> f.update(temp, minSpeed));
+	public void updateFanSpeeds(double cpuTemp, double gpuTemp, int minSpeed) {
+		fanStream().forEach(f -> f.update(cpuTemp, gpuTemp, minSpeed));
 	}
 
 	public Stream<Fan> fanStream() {