1
1
package it .baeyens .arduino .monitor .views ;
2
2
3
3
import it .baeyens .arduino .arduino .MessageConsumer ;
4
+ import it .baeyens .arduino .common .ArduinoConst ;
5
+ import it .baeyens .arduino .common .Common ;
4
6
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 ;
5
12
import org .eclipse .swt .widgets .Display ;
6
13
7
14
public class SerialListener implements MessageConsumer {
15
+ private static boolean myScopeFilterFlag = false ;
8
16
SerialMonitor TheMonitor ;
9
17
int theColorIndex ;
18
+ private ByteBuffer myReceivedScopeData = ByteBuffer .allocate (2000 );
19
+ private int myEndPosition ;
10
20
11
21
SerialListener (SerialMonitor Monitor , int ColorIndex ) {
12
22
TheMonitor = Monitor ;
13
23
theColorIndex = ColorIndex ;
24
+ myReceivedScopeData .order (ByteOrder .LITTLE_ENDIAN );
14
25
}
15
26
16
27
@ 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 );
20
129
}
21
130
22
131
@ Override
@@ -39,4 +148,9 @@ public void run() {
39
148
});
40
149
41
150
}
151
+
152
+ static void setScopeFilter (boolean selection ) {
153
+ myScopeFilterFlag = selection ;
154
+
155
+ }
42
156
}
0 commit comments