Skip to content

Commit

Permalink
Merge pull request openpnp#1074 from markmaker/feature/advanced-motio…
Browse files Browse the repository at this point in the history
…n-control--6

Feature / Advanced Motion Control - Update 6
  • Loading branch information
markmaker committed Nov 8, 2020
2 parents 56eae4d + cfdc8f2 commit e5b521c
Show file tree
Hide file tree
Showing 46 changed files with 1,313 additions and 309 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/openpnp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void run() {
try {
MainFrame frame = new MainFrame(configuration);
frame.setVisible(true);
Logger.debug(String.format("Bienvenue, Bienvenido, Willkommen, Hello, Namaskar, Welkom, Bonjour to OpenPnP version %s.", Main.getVersion()));
Logger.info(String.format("Bienvenue, Bienvenido, Willkommen, Hello, Namaskar, Welkom, Bonjour to OpenPnP version %s.", Main.getVersion()));
configuration.getScripting().on("Startup", null);
}
catch (Exception e) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/openpnp/gui/JogControlsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,14 @@ public void actionPerformed(ActionEvent arg0) {
public void actionPerformed(ActionEvent arg0) {
UiUtils.submitUiMachineTask(() -> {
HeadMountable hm = machineControlsPanel.getSelectedTool();
hm.moveToSafeZ();
// Note, we don't just moveToSafeZ(), because this will just sit still, if we're anywhere in the Safe Z Zone.
// instead we explicitly move to the Safe Z coordinate i.e. the lower bound of the Safe Z Zone, applicable
// for this hm.
Location location = hm.getLocation();
Length safeZLength = hm.getSafeZ();
double safeZ = (safeZLength != null ? safeZLength.convertToUnits(location.getUnits()).getValue() : Double.NaN);
location = location.derive(null, null, safeZ, null);
hm.moveTo(location);
});
}
};
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/openpnp/gui/LogPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@

import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.prefs.Preferences;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

Expand All @@ -42,6 +47,8 @@ public class LogPanel extends JPanel {
private LogEntryListModel.LogEntryFilter searchBarFilter = new LogEntryListModel.LogEntryFilter();
private LogEntryListModel.LogEntryFilter systemOutFilter = new LogEntryListModel.LogEntryFilter();

private ScheduledExecutorService scheduledExecutor;

public LogPanel() {

loadLoggingPreferences();
Expand Down Expand Up @@ -134,6 +141,21 @@ public void actionPerformed(ActionEvent e) {
copyStringToClipboard(sb.toString());
}
});

scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
scheduledExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
refreshLogIfOnTop();
}
}, 0, 500, TimeUnit.MILLISECONDS);

MainFrame.get().getTabs().addChangeListener(new ChangeListener() {

@Override
public void stateChanged(ChangeEvent e) {
refreshLogIfOnTop();
}
});
}

private JCheckBox createSystemOutputCheckbox() {
Expand Down Expand Up @@ -186,9 +208,11 @@ private JPanel createSearchFieldPanel() {
searchTextField.getDocument().addDocumentListener(new DocumentListener() {

private void updateSearchBarFilter() {
LogEntry entry = getSelectedEntry();
String searchText = searchTextField.getText();
searchBarFilter.setFilter((logEntry -> logEntry.getRenderedLogEntry().toLowerCase().contains(searchText.toLowerCase())));
logEntries.filter();
setSelectedEntry(entry);
}

@Override
Expand Down Expand Up @@ -218,9 +242,11 @@ private JPanel createFilterLogLevelPanel() {
JComboBox<Level> logLevelFilterComboBox = new JComboBox<>(Level.values());
logLevelFilterComboBox.setSelectedItem(filterLogLevel);
logLevelFilterComboBox.addActionListener(e -> {
LogEntry entry = getSelectedEntry();
Level logLevel = (Level) logLevelFilterComboBox.getSelectedItem();
logLevelFilter.setFilter(logEntry -> logEntry.getLevel().compareTo(logLevel) >= 0);
logEntries.filter();
setSelectedEntry(entry);
});
filterLogLevelPanel.add(logLevelFilterComboBox);
return filterLogLevelPanel;
Expand Down Expand Up @@ -249,4 +275,29 @@ private void copyStringToClipboard(String s) {
clipboard.setContents(selection, selection);
}

protected void refreshLogIfOnTop() {
if (MainFrame.get().getTabs().getSelectedComponent() == LogPanel.this) {
if (logEntries.isRefreshNeeded()) {
logEntries.refresh();
}
}
}

protected LogEntry getSelectedEntry() {
int index = logEntryJList.getSelectedIndex();
if (index >= 0) {
return logEntries.getElementAt(index);
}
return null;
}

protected void setSelectedEntry(LogEntry entry) {
if (entry != null) {
int index = logEntries.getFilteredLogEntries().indexOf(entry);
if (index >= 0) {
logEntryJList.setSelectedIndex(index);
}
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/openpnp/gui/components/SimpleGraphView.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public SimpleGraphView(SimpleGraph graph) {
public SimpleGraph getGraph() {
return graph;
}
public void setGraph(SimpleGraph graph) {
public synchronized void setGraph(SimpleGraph graph) {
this.graph = graph;
displayCycleMask = 0;
if (graph != null) {
Expand Down Expand Up @@ -111,7 +111,7 @@ protected String formatNumber(double y, double unit) {
}

@Override
public void paintComponent(Graphics g) {
public synchronized void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import java.awt.*;

public class LogEntryListCellRenderer extends JTextField implements ListCellRenderer<LogEntry> {

final Color colorTrace = new Color(64, 128, 64);
final Color colorDebug = new Color(0, 0, 0);
final Color colorInfo = new Color(00, 0x5B, 0xD9); // the OpenPNP blue
final Color colorWarning = new Color(255, 0, 0);
final Color colorError = new Color(255, 0, 0);
final Color colorErrorBg = new Color(255, 255, 220);

@Override
public Component getListCellRendererComponent(JList<? extends LogEntry> list, LogEntry logEntry, int index, boolean isSelected, boolean cellHasFocus) {

Expand All @@ -22,8 +28,33 @@ public Component getListCellRendererComponent(JList<? extends LogEntry> list, Lo
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
switch(logEntry.getLevel()) {
case ERROR:
setBackground(colorErrorBg);
break;
default:
setBackground(list.getBackground());
}
switch(logEntry.getLevel()) {
case TRACE:
setForeground(colorTrace);
break;
case DEBUG:
setForeground(colorDebug);
break;
case INFO:
setForeground(colorInfo);
break;
case WARNING:
setForeground(colorWarning);
break;
case ERROR:
setForeground(colorError);
break;
default:
setForeground(list.getForeground());
break;
}
}

return this;
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/org/openpnp/gui/support/LogEntryListModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LogEntryListModel extends AbstractListModel<LogEntry> implements Wr

private List<LogEntry> originalLogEntries = new ArrayList<>();
private List<LogEntry> filteredLogEntries = new ArrayList<>(originalLogEntries);
private List<LogEntry> newLogEntries = new ArrayList<>();
private HashSet<LogEntryFilter> filters = new HashSet<>();

public static class LogEntryFilter {
Expand Down Expand Up @@ -64,7 +65,12 @@ public synchronized int getSize() {

@Override
public synchronized LogEntry getElementAt(int index) {
return filteredLogEntries.get(index);
// This check is needed to exclude race conditions in heavily threaded logging, i.e. calls to getSize()
// followed by getElementAt() are not atomic.
if (index < filteredLogEntries.size()) {
return filteredLogEntries.get(index);
}
return null;
}

public synchronized void addFilter(LogEntryFilter filter) {
Expand All @@ -89,8 +95,7 @@ public void init(Configuration configuration) throws Exception {

@Override
public synchronized void write(LogEntry logEntry) throws Exception {
originalLogEntries.add(logEntry);
trim();
newLogEntries.add(logEntry);
}

public synchronized void clear() {
Expand All @@ -107,10 +112,22 @@ public synchronized void filter() {
SwingUtilities.invokeLater(() -> fireContentsChanged(this, 0, filteredLogEntries.size() - 1));
}

private synchronized void trim() {
if (originalLogEntries.size() > LINE_LIMIT) {
originalLogEntries.subList(0, originalLogEntries.size() - LINE_LIMIT).clear();
public synchronized boolean isRefreshNeeded() {
return !newLogEntries.isEmpty();
}

public synchronized void refresh() {
if (newLogEntries.size() > LINE_LIMIT) {
// New Log entries alone already surpass the limit.
newLogEntries.subList(0, newLogEntries.size() - LINE_LIMIT).clear();
originalLogEntries.clear();
}
else if (originalLogEntries.size() + newLogEntries.size() > LINE_LIMIT) {
// Make space for the new log entries.
originalLogEntries.subList(0, Math.max(0, originalLogEntries.size() - newLogEntries.size() - LINE_LIMIT)).clear();
}
originalLogEntries.addAll(newLogEntries);
newLogEntries.clear();
filter();
}

Expand Down
58 changes: 0 additions & 58 deletions src/main/java/org/openpnp/machine/marek/MarekNozzle.java

This file was deleted.

2 changes: 0 additions & 2 deletions src/main/java/org/openpnp/machine/neoden4/NeoDen4Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,6 @@ public void home(ReferenceMachine machine) throws Exception {

this.x = homeLocation.getCoordinate(homeLocation.getAxis(this, Axis.Type.X), units);
this.y = homeLocation.getCoordinate(homeLocation.getAxis(this, Axis.Type.Y), units);

machine.fireMachineHeadActivity(machine.getDefaultHead());
}

@Override
Expand Down

0 comments on commit e5b521c

Please sign in to comment.