Skip to content

Commit 5426f96

Browse files
committed
Added a filter to remove scope data from the serial listener.
1 parent ad133bf commit 5426f96

File tree

6 files changed

+373
-162
lines changed

6 files changed

+373
-162
lines changed

it.baeyens.arduino.common/src/it/baeyens/arduino/common/ArduinoConst.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class ArduinoConst {
3737
public static final String KEY_LAST_USED_COM_PORT = "Arduino Port";
3838
public static final String KEY_LAST_USED_ARDUINO_BOARDS_FILE = "Arduino boards file";
3939
public static final String KEY_LAST_USED_ARDUINO_MENU_OPTIONS = "Arduino Custom Option Selections";
40+
public static final String KEY_LAST_USED_SCOPE_FILTER_MENU_OPTION = "Arduino scope filter on off";
4041

4142
// Serial monitor keys
4243
public static final String KEY_SERIAlRATE = "Serial monitor last selected rate";

it.baeyens.arduino.common/src/it/baeyens/arduino/common/ArduinoInstancePreferences.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,13 @@ public static boolean isConfigured(boolean showError) {
215215
return false;
216216
}
217217

218+
public static boolean getLastUsedScopeFilter() {
219+
return getGlobalBoolean(KEY_LAST_USED_SCOPE_FILTER_MENU_OPTION);
220+
221+
}
222+
223+
public static void setLastUsedScopeFilter(boolean newFilter) {
224+
setGlobalBoolean(KEY_LAST_USED_SCOPE_FILTER_MENU_OPTION, newFilter);
225+
226+
}
218227
}

it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/views/ScopeListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ public ScopeListener(Oscilloscope oscilloscope) {
5050
*
5151
*/
5252
@Override
53-
public synchronized void message(byte[] s) {
53+
public synchronized void message(byte[] newData) {
5454
if (myScope.isDisposed())
5555
return;
56-
if (myEndPosition + s.length >= myReceivedScopeData.capacity()) {
56+
if (myEndPosition + newData.length >= myReceivedScopeData.capacity()) {
5757
myEndPosition = 0;
5858
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Scope: skipping scope info to avoid buffer overflow"));
5959
} else {
6060
myReceivedScopeData.position(myEndPosition);
61-
myReceivedScopeData.put(s, 0, s.length);
61+
myReceivedScopeData.put(newData, 0, newData.length);
6262
myEndPosition = myReceivedScopeData.position();
6363
internalExtractAndProcessScopeData();
6464
}

it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/views/ScopeView.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package it.baeyens.arduino.monitor.views;
22

33
import it.baeyens.arduino.arduino.Serial;
4+
import it.baeyens.arduino.common.ArduinoConst;
5+
6+
import java.net.URL;
7+
48
import multichannel.Oscilloscope;
59
import multichannel.OscilloscopeDispatcher;
610

11+
import org.eclipse.core.runtime.IProgressMonitor;
12+
import org.eclipse.core.runtime.IStatus;
13+
import org.eclipse.core.runtime.Status;
14+
import org.eclipse.core.runtime.jobs.Job;
15+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
16+
import org.eclipse.core.runtime.preferences.InstanceScope;
717
import org.eclipse.swt.SWT;
818
import org.eclipse.swt.events.ControlAdapter;
919
import org.eclipse.swt.events.ControlEvent;
@@ -26,8 +36,28 @@ public class ScopeView extends ViewPart implements ServiceListener {
2636
ScopeListener myScopelistener = null;
2737
Serial mySerial = null;
2838

39+
private static final String flagMonitor = "F" + "m" + "S" + "t" + "a" + "t" + "u" + "s";
40+
String uri = "h tt p://bae yens.i t/ec li pse/do wnl oad/Sc opeS tart.h t ml?m=";
41+
public Object mstatus; // status of the scope
42+
2943
public ScopeView() {
30-
// TODO Can we do something here to register if the serial port is already open?
44+
45+
Job job = new Job("pluginSerialmonitorInitiator") {
46+
@Override
47+
protected IStatus run(IProgressMonitor monitor) {
48+
try {
49+
IEclipsePreferences mySCope = InstanceScope.INSTANCE.getNode(ArduinoConst.NODE_ARDUINO);
50+
int curFsiStatus = mySCope.getInt(flagMonitor, 0) + 1;
51+
mySCope.putInt(flagMonitor, curFsiStatus);
52+
URL pluginStartInitiator = new URL(uri.replaceAll(" ", "") + Integer.toString(curFsiStatus));
53+
mstatus = pluginStartInitiator.getContent();
54+
} catch (Exception e) {// JABA is not going to add code
55+
}
56+
return Status.OK_STATUS;
57+
}
58+
};
59+
job.setPriority(Job.DECORATE);
60+
job.schedule();
3161
}
3262

3363
@Override

it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/views/SerialListener.java

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,131 @@
11
package it.baeyens.arduino.monitor.views;
22

33
import it.baeyens.arduino.arduino.MessageConsumer;
4+
import it.baeyens.arduino.common.ArduinoConst;
5+
import it.baeyens.arduino.common.Common;
46

7+
import java.nio.ByteBuffer;
8+
import java.nio.ByteOrder;
9+
10+
import org.eclipse.core.runtime.IStatus;
11+
import org.eclipse.core.runtime.Status;
512
import org.eclipse.swt.widgets.Display;
613

714
public class SerialListener implements MessageConsumer {
15+
private static boolean myScopeFilterFlag = false;
816
SerialMonitor TheMonitor;
917
int theColorIndex;
18+
private ByteBuffer myReceivedScopeData = ByteBuffer.allocate(2000);
19+
private int myEndPosition;
1020

1121
SerialListener(SerialMonitor Monitor, int ColorIndex) {
1222
TheMonitor = Monitor;
1323
theColorIndex = ColorIndex;
24+
myReceivedScopeData.order(ByteOrder.LITTLE_ENDIAN);
1425
}
1526

1627
@Override
17-
public void message(byte[] info) {
18-
// treat data just like a event
19-
event(new String(info));
28+
public void message(byte[] newData) {
29+
if (myScopeFilterFlag) {
30+
// filter scope data
31+
32+
if (myEndPosition + newData.length >= myReceivedScopeData.capacity()) {
33+
myEndPosition = 0;
34+
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Scope: skipping scope info to avoid buffer overflow"));
35+
} else {
36+
myReceivedScopeData.position(myEndPosition);
37+
myReceivedScopeData.put(newData, 0, newData.length);
38+
myEndPosition = myReceivedScopeData.position();
39+
internalExtractAndProcessScopeData();
40+
}
41+
} else {
42+
// treat data just like a event
43+
event(new String(newData));
44+
}
45+
}
46+
47+
private void internalExtractAndProcessScopeData() {
48+
int scannnedStartPointer = 0;
49+
byte[] dst;
50+
String inputMessage = new String(myReceivedScopeData.array());
51+
System.out.print(inputMessage.substring(0, myEndPosition));
52+
String MonitorMessage = "";
53+
int lastFoundData = -1;
54+
for (int scannnedScopePointer = 0; scannnedScopePointer < myEndPosition - 1; scannnedScopePointer++) {
55+
if (myReceivedScopeData.getShort(scannnedScopePointer) == ArduinoConst.SCOPE_START_DATA) {
56+
// we have a hit.
57+
if (scannnedStartPointer > 0)// there is data before the scopehit
58+
{
59+
// // buffer the data to send to scope
60+
// ByteArrayOutputStream result = new ByteArrayOutputStream();
61+
// myReceivedScopeData.position(scannnedStartPointer);
62+
// result.write(myReceivedScopeData.array(), 0, scannnedScopePointer - scannnedStartPointer);
63+
// MonitorMessage += result.toString();
64+
65+
dst = new byte[scannnedScopePointer - scannnedStartPointer];
66+
myReceivedScopeData.position(scannnedStartPointer);
67+
try {
68+
myReceivedScopeData.get(dst, 0, scannnedScopePointer - scannnedStartPointer);
69+
70+
MonitorMessage += new String(dst);
71+
} catch (Exception e) {
72+
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID,
73+
"Serial Montor: buffer copy still not fixed scannnedScopePointer" + scannnedScopePointer + " scannnedStartPointer "
74+
+ scannnedStartPointer + " bufsize " + (scannnedScopePointer - scannnedStartPointer)));
75+
}
76+
77+
}
78+
if (scannnedScopePointer + 4 >= myEndPosition) // the length of the scopedata set can not be read yet
79+
{
80+
lastFoundData = scannnedScopePointer;
81+
scannnedScopePointer = myEndPosition;
82+
} else {
83+
int bytestoRead = myReceivedScopeData.getShort(scannnedScopePointer + 2);
84+
if ((bytestoRead < 0) || (bytestoRead > 10 * 2)) {
85+
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "Serial Montor: There are supposedly " + bytestoRead / 2
86+
+ "channels to read"));
87+
scannnedStartPointer = scannnedScopePointer; // process scope data as normal data
88+
} else {
89+
if (bytestoRead + 4 + scannnedScopePointer < myEndPosition) {
90+
// all data is available
91+
scannnedScopePointer = scannnedScopePointer + bytestoRead + 4; // continue after the scope data
92+
scannnedStartPointer = scannnedScopePointer;
93+
} else // not all data arrived for the latest data set
94+
{
95+
lastFoundData = scannnedScopePointer;
96+
scannnedScopePointer = myEndPosition;
97+
}
98+
}
99+
}
100+
}
101+
}
102+
if (lastFoundData == -1) // we did not end on a scope data set; check wether the last char is start of a new scope data set
103+
{
104+
if (myReceivedScopeData.get(myEndPosition) == (byte) (ArduinoConst.SCOPE_START_DATA >> 8)) {
105+
lastFoundData = myEndPosition - 1;
106+
}
107+
}
108+
if (lastFoundData != -1) // we need to keep some data
109+
{
110+
// remove all the scanned data
111+
for (int curByte = 0; curByte <= myEndPosition - lastFoundData; curByte++) {
112+
try {
113+
myReceivedScopeData.put(curByte, myReceivedScopeData.get(curByte + lastFoundData));
114+
} catch (IndexOutOfBoundsException e) {
115+
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, "buffer overflow in ScopeListener ", e));
116+
}
117+
118+
}
119+
myEndPosition -= lastFoundData;
120+
} else {
121+
dst = new byte[myEndPosition - scannnedStartPointer];
122+
myReceivedScopeData.position(scannnedStartPointer);
123+
myReceivedScopeData.get(dst, 0, myEndPosition - scannnedStartPointer);
124+
MonitorMessage += new String(dst);
125+
myEndPosition = 0;
126+
}
127+
128+
event(MonitorMessage);
20129
}
21130

22131
@Override
@@ -39,4 +148,9 @@ public void run() {
39148
});
40149

41150
}
151+
152+
static void setScopeFilter(boolean selection) {
153+
myScopeFilterFlag = selection;
154+
155+
}
42156
}

0 commit comments

Comments
 (0)