From 518cbc396d63b8483cfddd7d94be82da092d5231 Mon Sep 17 00:00:00 2001 From: Wilhelm Date: Tue, 10 Apr 2018 23:30:07 +0200 Subject: [PATCH 1/4] added the possebilety to change the ringbuffer size --- app/src/processing/app/SerialPlotter.java | 48 +++++++++++++++---- .../app/helpers/CircularBuffer.java | 10 +++- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/app/src/processing/app/SerialPlotter.java b/app/src/processing/app/SerialPlotter.java index 363753749fe..5843ac98dc7 100644 --- a/app/src/processing/app/SerialPlotter.java +++ b/app/src/processing/app/SerialPlotter.java @@ -37,18 +37,22 @@ public class SerialPlotter extends AbstractMonitor { private final StringBuffer messageBuffer; private JComboBox serialRates; + private JSpinner graphWidth; private Serial serial; private int serialRate, xCount; private ArrayList graphs; - private final static int BUFFER_CAPACITY = 500; + private final static int BUFFER_CAPACITY_DEFAULT = 500; + private final static int BUFFER_CAPACITY_MAX = 5000; + private final static int BUFFER_CAPACITY_MIN = 10; + private int buffer_capacity = BUFFER_CAPACITY_DEFAULT; private static class Graph { public CircularBuffer buffer; private Color color; - public Graph(int id) { - buffer = new CircularBuffer(BUFFER_CAPACITY); + public Graph(int id, int capacity) { + buffer = new CircularBuffer(capacity); color = Theme.getColorCycleColor("plotting.graphcolor", id); } @@ -146,12 +150,12 @@ public void paintComponent(Graphics g1) { } // handle data count - int cnt = xCount - BUFFER_CAPACITY; - if (xCount < BUFFER_CAPACITY) cnt = 0; + int cnt = xCount - buffer_capacity; + if (xCount < buffer_capacity) cnt = 0; double zeroTick = ticks.getTick(0); double lastTick = ticks.getTick(ticks.getTickCount() - 1); - double xTickRange = BUFFER_CAPACITY / ticks.getTickCount(); + double xTickRange = buffer_capacity / ticks.getTickCount(); for (int i = 0; i < ticks.getTickCount() + 1; i++) { String s; @@ -167,7 +171,7 @@ public void paintComponent(Graphics g1) { s = String.valueOf((int)(xTickRange * i)+cnt); fBounds = fm.getStringBounds(s, g); sWidth = (int)fBounds.getWidth()/2; - xValue = (int)((bounds.width - xOffset - xPadding) * ((xTickRange * i) / BUFFER_CAPACITY) + xOffset); + xValue = (int)((bounds.width - xOffset - xPadding) * ((xTickRange * i) / buffer_capacity) + xOffset); } // draw graph x axis, ticks and labels g.setColor(boundsColor); @@ -184,7 +188,7 @@ public void paintComponent(Graphics g1) { g.drawLine(xOffset, (int) transformY(zeroTick), bounds.width - xPadding, (int)transformY(zeroTick)); g.setTransform(AffineTransform.getTranslateInstance(xOffset, 0)); - float xstep = (float) (bounds.width - xOffset - xPadding) / (float) BUFFER_CAPACITY; + float xstep = (float) (bounds.width - xOffset - xPadding) / (float) buffer_capacity; int legendLength = graphs.size() * 10 + (graphs.size() - 1) * 3; for(int i = 0; i < graphs.size(); ++i) { @@ -231,6 +235,8 @@ public SerialPlotter(BoardPort port) { messageBuffer = new StringBuffer(); graphs = new ArrayList<>(); + + graphWidth.addChangeListener(cl -> {setNewBufferCapacity((int)graphWidth.getValue()); } ); } protected void onCreateWindow(Container mainPane) { @@ -249,21 +255,45 @@ protected void onCreateWindow(Container mainPane) { serialRates.setMaximumSize(serialRates.getMinimumSize()); + graphWidth = new JSpinner(new SpinnerNumberModel( + BUFFER_CAPACITY_DEFAULT, //initial value + BUFFER_CAPACITY_MIN, //min + BUFFER_CAPACITY_MAX, //max + 1)); //step + graphWidth.setMaximumSize(graphWidth.getMinimumSize()); + JSpinner.NumberEditor editor = new JSpinner.NumberEditor(graphWidth); + editor.getFormat().setGroupingUsed(false); + graphWidth.setEditor(editor); + pane.add(Box.createHorizontalGlue()); pane.add(Box.createRigidArea(new Dimension(8, 0))); pane.add(serialRates); + pane.add(graphWidth); mainPane.add(pane, BorderLayout.SOUTH); } protected void onEnableWindow(boolean enable) { serialRates.setEnabled(enable); + graphWidth.setEnabled(enable); } private void onSerialRateChange(ActionListener listener) { serialRates.addActionListener(listener); } + private void setNewBufferCapacity(int capacity){ + if(buffer_capacity != capacity) { + if(capacity > BUFFER_CAPACITY_MAX) capacity = BUFFER_CAPACITY_MAX; + else if(capacity < BUFFER_CAPACITY_MIN) capacity = BUFFER_CAPACITY_MIN; + buffer_capacity = capacity; + for(int i = 0; i < graphs.size(); i++) { + graphs.get(i).buffer.newCapacity(capacity); + } + xCount=0; + } + } + public void message(final String s) { messageBuffer.append(s); while (true) { @@ -286,7 +316,7 @@ public void message(final String s) { try { double value = Double.valueOf(parts[i]); if(validParts >= graphs.size()) { - graphs.add(new Graph(validParts)); + graphs.add(new Graph(validParts, buffer_capacity)); } graphs.get(validParts).buffer.add(value); validParts++; diff --git a/app/src/processing/app/helpers/CircularBuffer.java b/app/src/processing/app/helpers/CircularBuffer.java index 8396c8fd2e1..b4a2bada888 100644 --- a/app/src/processing/app/helpers/CircularBuffer.java +++ b/app/src/processing/app/helpers/CircularBuffer.java @@ -4,10 +4,10 @@ public class CircularBuffer { - private final double[] elements; + private double[] elements; private int start = -1; private int end = -1; - private final int capacity; + private int capacity; public void add(double num) { end = (end + 1) % capacity; @@ -37,6 +37,12 @@ public CircularBuffer(int capacity) { elements = new double[capacity]; } + public void newCapacity(int capacity) { + elements = new double[capacity]; + this.capacity = capacity; + start = end = 0; + } + public double min() { if (size() == 0) { throw new NoSuchElementException(); From 6c75f098092715a0c4ebdecb9ed1b8a28848324a Mon Sep 17 00:00:00 2001 From: Wilhelm Wiens Date: Thu, 9 Apr 2020 13:10:46 +0200 Subject: [PATCH 2/4] Remove unused variable in paintComponent do not modify parameter in setNewBufferCapacity --- app/src/processing/app/SerialPlotter.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/src/processing/app/SerialPlotter.java b/app/src/processing/app/SerialPlotter.java index 8d3589c3fdf..7c4847d1ad3 100644 --- a/app/src/processing/app/SerialPlotter.java +++ b/app/src/processing/app/SerialPlotter.java @@ -190,8 +190,7 @@ public void paintComponent(Graphics g1) { g.setTransform(AffineTransform.getTranslateInstance(xOffset, 0)); float xstep = (float) (bounds.width - xOffset - xPadding) / (float) buffer_capacity; - int legendLength = graphs.size() * 10 + (graphs.size() - 1) * 3; - + // draw legend int legendXOffset = 0; for(int i = 0; i < graphs.size(); ++i) { @@ -298,13 +297,16 @@ private void onSerialRateChange(ActionListener listener) { private void setNewBufferCapacity(int capacity){ if(buffer_capacity != capacity) { - if(capacity > BUFFER_CAPACITY_MAX) capacity = BUFFER_CAPACITY_MAX; - else if(capacity < BUFFER_CAPACITY_MIN) capacity = BUFFER_CAPACITY_MIN; - buffer_capacity = capacity; + + if(capacity > BUFFER_CAPACITY_MAX) buffer_capacity = BUFFER_CAPACITY_MAX; + else if(capacity < BUFFER_CAPACITY_MIN) buffer_capacity = BUFFER_CAPACITY_MIN; + else buffer_capacity = capacity; + for(int i = 0; i < graphs.size(); i++) { - graphs.get(i).buffer.newCapacity(capacity); + graphs.get(i).buffer.newCapacity(buffer_capacity); } - xCount=0; + + xCount = 0; } } @@ -377,7 +379,7 @@ public void message(final String s) { } if(label != null) { if(validLabels >= graphs.size()) { - graphs.add(new Graph(validLabels)); + graphs.add(new Graph(validLabels, BUFFER_CAPACITY_DEFAULT)); } graphs.get(validLabels).label = label; validLabels++; From 3a1bc371cd2e0848d5e1aa10cd0c91139b5358e0 Mon Sep 17 00:00:00 2001 From: Wilhelm Wiens Date: Thu, 9 Apr 2020 15:29:33 +0200 Subject: [PATCH 3/4] Undo removal of command textbox and send button in onEnableWindow function --- app/src/processing/app/SerialPlotter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/processing/app/SerialPlotter.java b/app/src/processing/app/SerialPlotter.java index 0dd6bf22053..700d479cc8c 100644 --- a/app/src/processing/app/SerialPlotter.java +++ b/app/src/processing/app/SerialPlotter.java @@ -398,7 +398,9 @@ public void appyPreferences() { } protected void onEnableWindow(boolean enable) { + textField.setEnabled(enable); serialRates.setEnabled(enable); + sendButton.setEnabled(enable); graphWidth.setEnabled(enable); } From 6e8312348aea142b82e37e6dc20a3ef37b86e21e Mon Sep 17 00:00:00 2001 From: Wilhelm Wiens Date: Thu, 9 Apr 2020 18:43:57 +0200 Subject: [PATCH 4/4] Fix typo of applyPreferences --- app/src/processing/app/SerialPlotter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/processing/app/SerialPlotter.java b/app/src/processing/app/SerialPlotter.java index 700d479cc8c..ebbd23f2cbe 100644 --- a/app/src/processing/app/SerialPlotter.java +++ b/app/src/processing/app/SerialPlotter.java @@ -390,7 +390,7 @@ public void onSendCommand(ActionListener listener) { sendButton.addActionListener(listener); } - public void appyPreferences() { + public void applyPreferences() { // Apply line endings. if (PreferencesData.get("serial.line_ending") != null) { lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));