Răsfoiți Sursa

Individual manual control now fully working

Tankernn 8 ani în urmă
părinte
comite
7d11e00080

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

@@ -7,7 +7,7 @@ public class Fan {
 	private int rpm, index;
 	private GRID grid;
 	private FanSpeedProfile profile;
-	private int speed;
+	private int speed = 100;
 
 	public Fan(GRID grid, int index) {
 		this.grid = grid;
@@ -128,7 +128,10 @@ public class Fan {
 			grid.getCommunicator().writeData(command);
 
 		}
-
+	}
+	
+	public void setProfile(FanSpeedProfile profile) {
+		this.profile = profile;
 	}
 
 	/**

+ 0 - 33
src/eu/tankernn/grid/FanPanel.java

@@ -1,33 +0,0 @@
-package eu.tankernn.grid;
-
-import java.text.DecimalFormat;
-
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.border.TitledBorder;
-
-public class FanPanel extends JPanel {
-
-	/**
-	 * 
-	 */
-	private static final long serialVersionUID = 1L;
-	
-	private Fan fan;
-	
-	private JLabel voltageLabel, currentLabel, rpmLabel;
-
-	public FanPanel(Fan fan) {
-		this.fan = fan;
-		
-		this.setBorder(new TitledBorder("Fan " + fan.getIndex()));
-	}
-	
-	public void update() {
-		DecimalFormat df = new DecimalFormat("#.##");
-		
-		voltageLabel.setText("Voltage: " + df.format(fan.getVoltage()) + "V");
-		currentLabel.setText("Current: " + df.format(fan.getAMP()) + "A");
-		rpmLabel.setText("Speed: " + df.format(fan.getRPM()) + "RPM");
-	}
-}

+ 14 - 1
src/eu/tankernn/grid/FanSpeedProfile.java

@@ -1,9 +1,17 @@
 package eu.tankernn.grid;
 
+import java.util.Arrays;
+
 public class FanSpeedProfile {
 	public static final int MAX_TEMP = 80, MIN_TEMP = 30, STEPS = 5;
 	
-	private int[] percentages = new int[STEPS];
+	public final String name;
+	public final int[] percentages;
+	
+	public FanSpeedProfile(String name, int[] percentages) {
+		this.name = name;
+		this.percentages = percentages;
+	}
 	
 	public int getSpeedPercentage(double temp) {
 		int stepSize = (MAX_TEMP - MIN_TEMP) / STEPS;
@@ -19,4 +27,9 @@ public class FanSpeedProfile {
 		
 		return 100;
 	}
+	
+	@Override
+	public String toString() {
+		return name + ": " + Arrays.toString(percentages);
+	}
 }

+ 2 - 2
src/eu/tankernn/grid/GridControl.java

@@ -5,6 +5,7 @@ import java.awt.event.WindowListener;
 
 import javax.swing.JFrame;
 
+import eu.tankernn.grid.frame.GridControlPanel;
 import eu.tankernn.grid.model.ComputerModel;
 
 public class GridControl extends JFrame implements WindowListener {
@@ -14,12 +15,11 @@ public class GridControl extends JFrame implements WindowListener {
 	 */
 	private static final long serialVersionUID = 1L;
 	
-	GridControlPanel panel = new GridControlPanel();
 	ComputerModel model = new ComputerModel();
+	GridControlPanel panel = new GridControlPanel(model);
 
 	public GridControl() {
 		this.add(panel);
-		panel.setModel(model);
 		setResizable(true);
 		setTitle("JavaGridControl");
 		pack();

+ 57 - 0
src/eu/tankernn/grid/frame/FanPanel.java

@@ -0,0 +1,57 @@
+package eu.tankernn.grid.frame;
+
+import java.awt.GridLayout;
+import java.text.DecimalFormat;
+
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.border.TitledBorder;
+
+import eu.tankernn.grid.Fan;
+import eu.tankernn.grid.FanSpeedProfile;
+
+public class FanPanel extends JPanel {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	private Fan fan;
+
+	private JLabel voltageLabel = new JLabel(), currentLabel = new JLabel(),
+			rpmLabel = new JLabel();
+	private JComboBox<FanSpeedProfile> profileBox = new JComboBox<>();
+
+	public FanPanel(Fan fan, FanSpeedProfile[] profiles) {
+		for (FanSpeedProfile p : profiles)
+			profileBox.addItem(p);
+		this.fan = fan;
+
+		this.setBorder(new TitledBorder("Fan " + fan.getIndex()));
+
+		this.setLayout(new GridLayout(4, 1));
+
+		this.add(voltageLabel);
+		this.add(currentLabel);
+		this.add(rpmLabel);
+		this.add(profileBox);
+
+		profileBox.addActionListener((e) -> {
+			fan.setProfile((FanSpeedProfile) profileBox.getSelectedItem());
+		});
+	}
+
+	public void update() {
+		DecimalFormat df = new DecimalFormat("#.##");
+
+		voltageLabel.setText("Voltage: " + df.format(fan.getVoltage()) + "V");
+		currentLabel.setText("Current: " + df.format(fan.getAMP()) + "A");
+		rpmLabel.setText("Speed: " + df.format(fan.getRPM()) + "RPM");
+	}
+	
+	public void addProfile(FanSpeedProfile p) {
+		profileBox.addItem(p);
+	}
+}

+ 32 - 30
src/eu/tankernn/grid/GridControlPanel.java → src/eu/tankernn/grid/frame/GridControlPanel.java

@@ -1,17 +1,19 @@
-package eu.tankernn.grid;
+package eu.tankernn.grid.frame;
 
+import java.awt.BorderLayout;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.text.DecimalFormat;
+import java.util.stream.IntStream;
 
 import javax.swing.JComboBox;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
-import javax.swing.JSlider;
 import javax.swing.JTextField;
 
+import eu.tankernn.grid.FanSpeedProfile;
 import eu.tankernn.grid.model.ComputerModel;
 
 public class GridControlPanel extends JPanel implements Runnable {
@@ -26,13 +28,12 @@ public class GridControlPanel extends JPanel implements Runnable {
 	private ComputerModel model;
 
 	private FanPanel[] fanPanels;
+	private JPanel gridPanel = new JPanel();
 
 	private JTextField minRPM = new JTextField();
 
 	private JComboBox<String> portMap = new JComboBox<>();
 
-	private JSlider manualSlider = new JSlider();
-
 	private JLabel CPULabel = new JLabel("CPU preferred");
 
 	private JLabel CPULabelMax = new JLabel("CPU max");
@@ -43,6 +44,8 @@ public class GridControlPanel extends JPanel implements Runnable {
 
 	private JLabel PowerLabel = new JLabel("Power");
 
+	private FanSpeedProfile[] profiles;
+
 	private void setMinRPM(ActionEvent event) {
 		getModel().setMinRPM(Integer.parseInt(minRPM.getText()));
 	}
@@ -55,34 +58,36 @@ public class GridControlPanel extends JPanel implements Runnable {
 		getModel().setGrid(selectedPort);
 	}
 
-	public GridControlPanel() {
-		this.setLayout(new GridLayout(3, 2));
-
-		for (FanPanel p : fanPanels)
-			this.add(p);
+	public GridControlPanel(ComputerModel model) {
+		setModel(model);
+		this.setLayout(new BorderLayout());
 
-		minRPM.addActionListener(this::setMinRPM);
+		profiles = generateProfiles();
+		fanPanels = model.getGrid().fanStream().map(f -> new FanPanel(f, profiles)).toArray(FanPanel[]::new);
 
-		manualSlider.setMaximum(100);
+		gridPanel.setLayout(new GridLayout(3, 2));
+		for (FanPanel p : fanPanels)
+			gridPanel.add(p);
 
-		manualSlider.setMinimum(0);
+		this.add(gridPanel, BorderLayout.CENTER);
 
-		// manualSlider.addChangeListener(new ChangeListener() {
-		//
-		// @Override
-		// public void stateChanged(ChangeEvent e) {
-		// manualSpeed(e);
-		// }
-		// });
+		minRPM.addActionListener(this::setMinRPM);
 
 		portMap.addItemListener(new ItemListener() {
-
 			@Override
 			public void itemStateChanged(ItemEvent e) {
 				setPort(e);
 			}
 		});
+		this.add(portMap, BorderLayout.NORTH);
+
+		t = new Thread(this);
+		t.setDaemon(true);
+		t.start();
+	}
 
+	private FanSpeedProfile[] generateProfiles() {
+		return IntStream.range(30 / 5, 100 / 5).map(i -> i * 5).mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i })).toArray(FanSpeedProfile[]::new);
 	}
 
 	/**
@@ -92,19 +97,15 @@ public class GridControlPanel extends JPanel implements Runnable {
 	 *
 	 * @param model the model to set
 	 */
-	public void setModel(ComputerModel model) {
+	private void setModel(ComputerModel model) {
 		this.model = model;
 
 		portMap.removeAllItems();
 		for (String key : model.getGrid().getCommunicator().getPortMap().keySet()) {
 			portMap.addItem(key);
 		}
-
-		updateProperties();
-
-		t = new Thread(this);
-		t.setDaemon(true);
-		t.start();
+		
+		setPort(null);
 	}
 
 	/**
@@ -120,17 +121,18 @@ public class GridControlPanel extends JPanel implements Runnable {
 		CPULabelMax.setText(df.format(getModel().getSensor().getCpuMax()) + " °C Max");
 		GPULabel.setText(df.format(getModel().getSensor().getGPUTemp()) + " °C");
 		GPULabelMax.setText(df.format(getModel().getSensor().getGpuMax()) + " °C Max");
+
+		for (FanPanel p : fanPanels)
+			p.update();
 	}
 
 	@Override
 	public void run() {
 		while (!t.isInterrupted()) {
 			model.poll();
+			model.compute();
 
 			updateProperties();
-
-			getModel().compute();
-
 			try {
 				Thread.sleep(1000);
 			} catch (InterruptedException ex) {

+ 61 - 0
src/eu/tankernn/grid/frame/ProfileEditor.java

@@ -0,0 +1,61 @@
+package eu.tankernn.grid.frame;
+
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+import java.util.Arrays;
+import java.util.stream.IntStream;
+
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JSlider;
+import javax.swing.JTextField;
+
+import eu.tankernn.grid.FanSpeedProfile;
+
+public class ProfileEditor {
+
+	public FanSpeedProfile editProfile(FanSpeedProfile profile) {
+		JPanel panel = new JPanel(), sliderPanel = new JPanel();
+		JSlider[] sliders;
+		JTextField nameField = new JTextField();
+		if (profile != null) {
+			nameField.setText(profile.name);
+			sliders = Arrays.stream(profile.percentages).mapToObj(i -> new JSlider(JSlider.VERTICAL, 0, 100, i)).toArray(JSlider[]::new);
+		} else {
+			sliders = IntStream.range(0, FanSpeedProfile.STEPS).mapToObj(i -> new JSlider(JSlider.VERTICAL)).toArray(JSlider[]::new);
+		}
+		
+		for (JSlider s : sliders) {
+			s.setSnapToTicks(true);
+			s.setMinorTickSpacing(5);
+		}
+		
+		panel.setLayout(new BorderLayout());
+		panel.add(new JLabel("Profile name: "));
+		panel.add(nameField, BorderLayout.NORTH);
+		
+		sliderPanel.setLayout(new GridLayout(1, sliders.length));
+		for (JSlider s : sliders)
+			sliderPanel.add(s);
+		panel.add(sliderPanel, BorderLayout.CENTER);
+		
+
+		int response = JOptionPane.showConfirmDialog(null, panel, "Fan Speed Profile Editor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
+
+		if (response == JOptionPane.OK_OPTION) {
+			FanSpeedProfile newProfile = new FanSpeedProfile(nameField.getText(), Arrays.stream(sliders).mapToInt(JSlider::getValue).toArray());
+			if (nameField.getText().isEmpty()) {
+				JOptionPane.showMessageDialog(null, "Please enter a name for the profile.");
+				return editProfile(newProfile);
+			}
+			return newProfile;
+		} else {
+			return profile;
+		}
+	}
+	
+	public static void main(String[] args) {
+		System.out.println(new ProfileEditor().editProfile(null));
+	}
+}

+ 2 - 2
src/eu/tankernn/grid/model/Communicator.java

@@ -107,7 +107,7 @@ public class Communicator {
 	 * 
 	 */
 	public void disconnect() {
-		if (!serialPort.isOpen())
+		if (serialPort == null || !serialPort.isOpen())
 			return;
 		try {
 			input.close();
@@ -219,7 +219,7 @@ public class Communicator {
 	}
 
 	public boolean isConnected() {
-		return serialPort.isOpen();
+		return serialPort != null && serialPort.isOpen();
 	}
 
 }

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

@@ -45,8 +45,6 @@ public class ComputerModel {
 	 *
 	 */
 	public void poll() {
-		System.out.println("Polling...");
-
 		grid.pollFans();
 
 		try {

+ 1 - 1
src/eu/tankernn/grid/model/GRID.java

@@ -28,7 +28,7 @@ public class GRID {
 	 * at the selected port
 	 */
 	public GRID() {
-		fans = IntStream.range(0, 5).mapToObj(i -> new Fan(this, i)).toArray(Fan[]::new);
+		fans = IntStream.range(0, 6).mapToObj(i -> new Fan(this, i)).toArray(Fan[]::new);
 
 		communicator = new Communicator();
 		communicator.searchForPorts();