Skip to content

Commit

Permalink
Automatic powerbox apply, crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocipriani01 committed Feb 20, 2021
1 parent 8ec3e63 commit 647162b
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 79 deletions.
4 changes: 2 additions & 2 deletions InnoSetup.iss
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[Setup]
AppId={{863670cf-a7a6-4314-9479-c99e1c6fce06}
AppName=ThunderFocus
AppVersion=2.5.1
AppVerName=ThunderFocus v2.5.1
AppVersion=2.5.2
AppVerName=ThunderFocus v2.5.2
AppPublisher=marcocipriani01
AppPublisherURL=https://marcocipriani01.github.io/
AppSupportURL=https://marcocipriani01.github.io/
Expand Down
2 changes: 1 addition & 1 deletion ThunderFocus-GUI/deb-builder/ThunderFocus/DEBIAN/control
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ Maintainer: marcocipriani01
Depends:
Suggests: openjdk-14-jre
Priority: optional
Version: 2.5.1
Version: 2.5.2
Description: ThunderFocus for Debian.
Binary file not shown.
2 changes: 1 addition & 1 deletion ThunderFocus-GUI/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Specification-Vendor: marcocipriani01
Main-Class: marcocipriani01.thunderfocus.Main
Name: marcocipriani01/thunder/focus
Specification-Title: ThunderFocus
Specification-Version: 2.5.1
Specification-Version: 2.5.2
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package marcocipriani01.thunderfocus;

import marcocipriani01.simplesocket.ConnectionException;
import marcocipriani01.thunderfocus.board.ArduinoPin;
import marcocipriani01.thunderfocus.board.PowerBox;
import marcocipriani01.thunderfocus.board.ThunderFocuser;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.io.IOException;

import static marcocipriani01.thunderfocus.Main.i18n;

Expand All @@ -19,13 +22,15 @@ public class JPowerBoxTable extends JTable {

private static final int DEF_ROW_HEIGHT = 45;
private final SliderEditorAndRenderer sliderEditorAndRenderer = new SliderEditorAndRenderer();
private final MainWindow mainWindow;
private PowerBox powerBox = null;

/**
* Class constructor. Initializes the JTable.
*/
public JPowerBoxTable() {
public JPowerBoxTable(MainWindow mainWindow) {
super();
this.mainWindow = mainWindow;
setModel(new PowerBoxTableModel());
setRowSelectionAllowed(true);
setCellSelectionEnabled(false);
Expand Down Expand Up @@ -199,19 +204,42 @@ public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (powerBox == null) throw new NullPointerException("Null pins list.");
ArduinoPin pin = powerBox.getIndex(rowIndex);
switch (columnIndex) {
case 0 -> pin.setName((String) aValue);

case 1 -> pin.setNumber((int) aValue);
case 0 -> {
pin.setName((String) aValue);
Main.settings.setPowerBox(new PowerBox(powerBox), mainWindow);
try {
Main.settings.save();
} catch (IOException ex) {
ex.printStackTrace();
}
}

case 2 -> {
if (pin.isPwm()) {
pin.setValue((int) aValue);
} else {
pin.setValue((boolean) aValue);
}
try {
Main.focuser.run(ThunderFocuser.Commands.POWER_BOX_SET, mainWindow, pin.getNumber(), pin.getValuePwm());
} catch (ConnectionException ex) {
mainWindow.connectionErr(ex);
} catch (ThunderFocuser.InvalidParamException | NumberFormatException ex) {
mainWindow.valueOutOfLimits(ex);
}
}

case 3 -> pin.setAutoModeEn((boolean) aValue);
case 3 -> {
boolean b = (boolean) aValue;
pin.setAutoModeEn(b);
try {
Main.focuser.run(ThunderFocuser.Commands.POWER_BOX_SET_PIN_AUTO, mainWindow, pin.getNumber(), b ? 1 : 0);
} catch (ConnectionException ex) {
mainWindow.connectionErr(ex);
} catch (ThunderFocuser.InvalidParamException | NumberFormatException ex) {
mainWindow.valueOutOfLimits(ex);
}
}
}
fireTableCellUpdated(rowIndex, columnIndex);
}
Expand Down
22 changes: 4 additions & 18 deletions ThunderFocus-GUI/src/marcocipriani01/thunderfocus/MainWindow.form
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@
</grid>
</children>
</grid>
<grid id="f43e7" binding="powerBoxTab" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="f43e7" binding="powerBoxTab" layout-manager="GridLayoutManager" row-count="4" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="16" left="16" bottom="16" right="16"/>
<constraints>
<tabbedpane title-resource-bundle="marcocipriani01/thunderfocus/lang" title-key="powerbox.tab" icon="marcocipriani01/thunderfocus/res/powerboxtab.png"/>
Expand All @@ -528,7 +528,7 @@
<children>
<scrollpane id="beb96">
<constraints>
<grid row="0" column="1" row-span="1" col-span="3" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="1" row-span="1" col-span="2" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -548,20 +548,6 @@
<text resource-bundle="marcocipriani01/thunderfocus/lang" key="pbox.control"/>
</properties>
</component>
<component id="9e059" class="javax.swing.JButton" binding="applyPowerBoxButton">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="42"/>
<maximum-size width="-1" height="42"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
<icon value="marcocipriani01/thunderfocus/res/thunder.png"/>
<text resource-bundle="marcocipriani01/thunderfocus/lang" key="pbox.apply"/>
<toolTipText resource-bundle="marcocipriani01/thunderfocus/lang" key="pbox.apply.tooltip"/>
</properties>
</component>
<component id="f2c8" class="javax.swing.JButton" binding="powerBoxOffButton">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
Expand Down Expand Up @@ -602,7 +588,7 @@
</component>
<component id="ef84c" class="javax.swing.JComboBox" binding="powerBoxAutoModeBox" custom-create="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText resource-bundle="marcocipriani01/thunderfocus/lang" key="pbox.auto.mode.tooltip"/>
Expand All @@ -611,7 +597,7 @@
</component>
<component id="211c2" class="javax.swing.JTextField" binding="sunElevationField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="3" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand Down
69 changes: 30 additions & 39 deletions ThunderFocus-GUI/src/marcocipriani01/thunderfocus/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

public class MainWindow extends JFrame implements
ChangeListener, ActionListener, KeyListener, FocusListener,
ThunderFocuser.Listener, Settings.SettingsListener {
ThunderFocuser.Listener, Settings.SettingsListener, ItemListener {

private static final ImageIcon POWERBOX_TAB =
new ImageIcon(MainWindow.class.getResource("/marcocipriani01/thunderfocus/res/powerboxtab.png"));
Expand Down Expand Up @@ -80,7 +80,6 @@ protected void onHide() {
private JCheckBox fokPowerSaverBox;
private JCheckBox fokReverseDirBox;
private JPowerBoxTable powerBoxTable;
private JButton applyPowerBoxButton;
private JButton saveConfigButton;
private JSpinner indiPortSpinner;
private JTextField driverNameBox;
Expand Down Expand Up @@ -160,9 +159,8 @@ public void mouseClicked(MouseEvent e) {
stopButton, requestedPosField, aboutLabel, currentPosField, ticksPosSlider, posSlider,
relativeMovField, pinWindowButton);
setButtonListeners(connectButton, refreshButton, setRequestedPosButton, fokBacklashCalButton,
setZeroButton, fokInButton, fokOutButton, miniWindowButton, stopButton,
applyPowerBoxButton, copyIndiDriverNameButton, saveConfigButton,
powerBoxOnButton, powerBoxOffButton, cleanGraphButton);
setZeroButton, fokInButton, fokOutButton, miniWindowButton, stopButton, copyIndiDriverNameButton,
saveConfigButton, powerBoxOnButton, powerBoxOffButton, cleanGraphButton);
pinWindowButton.addActionListener(this);
requestedPosField.addActionListener(this);
relativeMovField.addActionListener(this);
Expand Down Expand Up @@ -298,9 +296,9 @@ private void createUIComponents() {
appThemeCombo = new JComboBox<>(Settings.Theme.values());
localOrRemoteCombo = new JComboBox<>(Settings.INDIConnectionMode.values());
indiPortSpinner = new JSpinner(new SpinnerNumberModel(
Main.settings.getIndiServerPort(), 1024, 99999, 1));
Main.settings.getIndiServerPort(), 1024, 65535, 1));
ascomPortSpinner = new JSpinner(new SpinnerNumberModel(
Main.settings.getAscomBridgePort(), 1024, 99999, 1));
Main.settings.getAscomBridgePort(), 1024, 65535, 1));
fokTicksCountSpinner = new JSpinner(new SpinnerNumberModel(
Main.settings.getFokTicksCount(), 10, 2147483647, 1));
fokUnitsCombo = new JComboBox<>(Settings.Units.values());
Expand All @@ -309,8 +307,9 @@ private void createUIComponents() {
Main.settings.getFokMaxTravel(), 1, 2147483647, 1));
fokBacklashSpinner = new JSpinner(new SpinnerNumberModel(
Main.focuser.getBacklash(), 0, 1000, 1));
powerBoxTable = new JPowerBoxTable();
powerBoxTable = new JPowerBoxTable(this);
powerBoxAutoModeBox = new JComboBox<>();
powerBoxAutoModeBox.addItemListener(this);
powerBoxLatSpinner = new JSpinner(new SpinnerNumberModel(0.0, -180.0, 180.0, 0.001));
powerBoxLongSpinner = new JSpinner(new SpinnerNumberModel(0.0, -180.0, 180.0, 0.001));

Expand Down Expand Up @@ -590,33 +589,6 @@ public void actionPerformed(ActionEvent e) {
APP_NAME, JOptionPane.ERROR_MESSAGE);
}

} else if (source == applyPowerBoxButton) {
try {
PowerBox powerBox = powerBoxTable.getPowerBox();
Main.settings.setPowerBox(new PowerBox(powerBox), this);
try {
Main.settings.save();
} catch (IOException ex) {
ex.printStackTrace();
}
PowerBox.AutoModes autoMode = (PowerBox.AutoModes) powerBoxAutoModeBox.getSelectedItem();
if (autoMode != null) {
Main.focuser.run(ThunderFocuser.Commands.POWER_BOX_SET_AUTO_MODE, this, autoMode.ordinal());
}
for (ArduinoPin p : powerBox.asList()) {
int number = p.getNumber();
if (p.isAutoModeEn()) {
Main.focuser.run(ThunderFocuser.Commands.POWER_BOX_SET_PIN_AUTO, this, number, 1);
} else {
Main.focuser.run(ThunderFocuser.Commands.POWER_BOX_SET, this, number, p.getValuePwm());
}
}
} catch (ConnectionException ex) {
connectionErr(ex);
} catch (ThunderFocuser.InvalidParamException | NumberFormatException ex) {
valueOutOfLimits(ex);
}

} else if (source == powerBoxOnButton) {
try {
for (ArduinoPin p : Main.focuser.getPowerBox().asList()) {
Expand Down Expand Up @@ -654,12 +626,12 @@ public void actionPerformed(ActionEvent e) {
}
}

private void valueOutOfLimits(Exception e) {
public void valueOutOfLimits(Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, i18n("error.invalid"), APP_NAME, JOptionPane.ERROR_MESSAGE);
}

private void connectionErr(ConnectionException e) {
public void connectionErr(ConnectionException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, i18n("error.connection"), APP_NAME, JOptionPane.ERROR_MESSAGE);
}
Expand Down Expand Up @@ -705,7 +677,6 @@ private void enableComponents(boolean connected) {
powerBoxOnButton.setEnabled(pbEn);
powerBoxOffButton.setEnabled(pbEn);
powerBoxAutoModeBox.setEnabled(pbEn);
applyPowerBoxButton.setEnabled(pbEn);
}

private void startOrStopINDI(boolean forceRestart) {
Expand Down Expand Up @@ -851,7 +822,11 @@ public void updateParam(ThunderFocuser.Parameters p) {
case REVERSE_DIR -> fokReverseDirBox.setSelected(Main.focuser.isReverseDir());
case ENABLE_POWER_SAVE -> fokPowerSaverBox.setSelected(Main.focuser.isPowerSaverOn());
case POWERBOX_PINS -> powerBoxTable.refresh();
case POWERBOX_AUTO_MODE -> powerBoxAutoModeBox.setSelectedItem(Main.focuser.getPowerBox().getAutoMode());
case POWERBOX_AUTO_MODE -> {
powerBoxAutoModeBox.removeItemListener(this);
powerBoxAutoModeBox.setSelectedItem(Main.focuser.getPowerBox().getAutoMode());
powerBoxAutoModeBox.addItemListener(this);
}
case POWERBOX_AMBIENT_DATA -> {
PowerBox powerBox = Main.focuser.getPowerBox();
double temperature = powerBox.getTemperature();
Expand Down Expand Up @@ -904,8 +879,10 @@ public void updateConnSate(ThunderFocuser.ConnState connState) {
powerBoxAutoModeLabel.setVisible(supportsAutoModes);
powerBoxAutoModeBox.setVisible(supportsAutoModes);
if (supportsAutoModes) {
powerBoxAutoModeBox.removeItemListener(this);
powerBoxAutoModeBox.setModel(new DefaultComboBoxModel<>(powerBox.supportedAutoModesArray()));
powerBoxAutoModeBox.setSelectedItem(Main.focuser.getPowerBox().getAutoMode());
powerBoxAutoModeBox.addItemListener(this);
}
if (powerBox.supportsAmbient()) {
tabPane.insertTab(i18n("sensors.tab"), AMBIENT_TAB, ambientTab, "", 2);
Expand Down Expand Up @@ -1041,4 +1018,18 @@ public void updateSetting(Settings.ExternalControl value) {
indiServerRadio.setSelected(value == INDI);
indiStatusLabel.setText(Main.indiServerCreator.isRunning() ? i18n("server.active") : i18n("server.inactive"));
}

@Override
public void itemStateChanged(ItemEvent e) {
try {
PowerBox.AutoModes autoMode = (PowerBox.AutoModes) powerBoxAutoModeBox.getSelectedItem();
if (autoMode != null) {
Main.focuser.run(ThunderFocuser.Commands.POWER_BOX_SET_AUTO_MODE, this, autoMode.ordinal());
}
} catch (ConnectionException ex) {
connectionErr(ex);
} catch (ThunderFocuser.InvalidParamException | NumberFormatException ex) {
valueOutOfLimits(ex);
}
}
}
18 changes: 9 additions & 9 deletions ThunderFocus-GUI/src/marcocipriani01/thunderfocus/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ public class Settings {
@Expose
private boolean autoConnect = false;

/**
* Class constructor.
*/
public Settings() {

}

public static String getSettingsFolder() throws IOException {
if (Settings.folder != null) return Settings.folder;
String folder = System.getProperty("user.home");
Expand Down Expand Up @@ -109,8 +102,15 @@ public static Settings load() {
return new Settings();
}
// Normalize invalid values
if (s.indiServerPort <= 1024) s.indiServerPort = 7625;
if (s.ascomBridgePort <= 1024) s.ascomBridgePort = 5001;
if (s.indiServerPort <= 1024 || s.indiServerPort >= 65535) s.indiServerPort = 7625;
if (s.ascomBridgePort <= 1024 || s.ascomBridgePort >= 65535) s.ascomBridgePort = 5001;
if (s.fokTicksCount < 10 || s.fokTicksCount >= 2147483647) s.fokTicksCount = 70;
if (s.fokMaxTravel < 1 || s.fokMaxTravel >= 2147483647) s.fokMaxTravel = 32767;
if (s.theme == null) s.theme = Theme.LIGHT;
if (s.externalControl == null) s.externalControl = ExternalControl.NONE;
if (s.indiConnectionMode == null) s.indiConnectionMode = INDIConnectionMode.LOCAL;
if (s.fokTicksUnit == null) s.fokTicksUnit = Units.TICKS;
if (s.powerBox == null) s.powerBox = new PowerBox();
return s;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pbox.off=All off
pbox.off.tooltip=Turns off all the manual outputs.
pbox.on=All on
pbox.on.tooltip=Turns on all the manual outputs.
pbox.apply=Apply
pbox.apply.tooltip=Apply the changes.
ambient.clean=Clean
ambient.clean.tooltip=Clean the graph.
theme=Theme:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ pbox.off=Spegni tutto
pbox.off.tooltip=Spegni tutte le uscite manuali.
pbox.on=Accendi tutto
pbox.on.tooltip=Accendi tutte le uscite manuali.
pbox.apply=Applica
pbox.apply.tooltip=Applica i cambiamenti.
ambient.clean=Pulisci
ambient.clean.tooltip=Pulisci il grafico.
theme=Tema:
Expand Down
Binary file not shown.

0 comments on commit 647162b

Please sign in to comment.