Skip to content

Commit add7005

Browse files
author
jantje
committed
Changed number of serial channels to 6. fix for #211 (untested)
Also some preparational actions are done for #325 which involved moving files around to externalize the add and remove serial port methods
1 parent 84455c8 commit add7005

File tree

9 files changed

+246
-54
lines changed

9 files changed

+246
-54
lines changed
Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
package it.baeyens.arduino.actions;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.apache.commons.io.FileUtils;
7+
import org.eclipse.cdt.core.CCorePlugin;
8+
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
9+
import org.eclipse.cdt.core.index.IIndex;
10+
import org.eclipse.cdt.core.index.IIndexBinding;
11+
import org.eclipse.cdt.core.index.IIndexName;
12+
import org.eclipse.cdt.core.index.IndexFilter;
13+
import org.eclipse.cdt.core.model.CoreModel;
14+
import org.eclipse.cdt.core.model.ICProject;
315
import org.eclipse.core.commands.AbstractHandler;
416
import org.eclipse.core.commands.ExecutionEvent;
517
import org.eclipse.core.commands.ExecutionException;
18+
import org.eclipse.core.resources.IProject;
19+
import org.eclipse.core.runtime.CoreException;
20+
import org.eclipse.core.runtime.NullProgressMonitor;
621
import org.eclipse.ui.PartInitException;
722
import org.eclipse.ui.PlatformUI;
823

24+
import it.baeyens.arduino.common.ArduinoConst;
25+
import it.baeyens.arduino.common.Common;
26+
import it.baeyens.arduino.listeners.ProjectExplorerListener;
27+
928
/**
10-
* This is a handler to connect the plugin.xml to the code for opening the serial monitor
29+
* This is a handler to connect the plugin.xml to the code for opening the
30+
* serial monitor
31+
*
32+
*
33+
* The code looks for all selected projeects for the com port and the baudrate
34+
* and connects if they both are found
1135
*
1236
* @author jan
1337
*
@@ -17,11 +41,96 @@ public class OpenSerialMonitorHandler extends AbstractHandler {
1741
@Override
1842
public Object execute(ExecutionEvent event) throws ExecutionException {
1943
try {
20-
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("it.baeyens.arduino.monitor.views.SerialMonitor"); //$NON-NLS-1$
44+
45+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
46+
.showView("it.baeyens.arduino.monitor.views.SerialMonitor"); //$NON-NLS-1$
47+
// find all projects
48+
IProject SelectedProjects[] = ProjectExplorerListener.getSelectedProjects();
49+
// if there are project selected and the autoConnectScope feature is
50+
// on
51+
for (IProject curproject : SelectedProjects) {
52+
int baud = getBaudRate(curproject);
53+
if (baud > 0) {
54+
String comPort = Common.getBuildEnvironmentVariable(curproject,
55+
ArduinoConst.ENV_KEY_JANTJE_COM_PORT, "");
56+
if (!comPort.isEmpty()) {
57+
58+
// it.baeyens.arduino.monitor.views.connectSerial(comPort,
59+
// Integer.toString(baud));
60+
// SerialPortsUpdated();
61+
}
62+
}
63+
}
2164
} catch (PartInitException e) {
2265
e.printStackTrace();
2366
}
2467
return null;
2568
}
2669

70+
/**
71+
* given a project look in the source code for the line of code that sets
72+
* the baud rate on the board Serial.begin([baudRate]);
73+
*
74+
*
75+
*
76+
* return the integer value of [baudrate] or in case of error a negative
77+
* value
78+
*
79+
* @param iProject
80+
* @return
81+
*/
82+
private int getBaudRate(IProject iProject) {
83+
String setupFunctionName = "setup";
84+
String serialVariable = "Serial.begin";
85+
86+
ICProject curProject = CoreModel.getDefault().getCModel().getCProject(iProject.getName());
87+
88+
IIndex index = null;
89+
try {
90+
index = CCorePlugin.getIndexManager().getIndex(curProject);
91+
index.acquireReadLock();
92+
// find bindings for name
93+
IIndexBinding[] bindings = index.findBindings(setupFunctionName.toCharArray(),
94+
IndexFilter.ALL_DECLARED_OR_IMPLICIT, new NullProgressMonitor());
95+
if (bindings.length != 1) {
96+
// there should be just 1 setup function
97+
return -1;
98+
}
99+
100+
if (!(bindings[0] instanceof ICPPFunction)) {
101+
return -2;// that on found binding must be a function
102+
}
103+
ICPPFunction setupFunc = (ICPPFunction) bindings[0];
104+
105+
IIndexName[] names = index.findNames(setupFunc, org.eclipse.cdt.core.index.IIndex.FIND_DEFINITIONS);
106+
if (names.length != 1) {
107+
return -3;
108+
}
109+
110+
String SetupFileName = names[0].getFileLocation().getFileName();
111+
String SetupFileContent = FileUtils.readFileToString(new File(SetupFileName));
112+
int serialBeginStart = SetupFileContent.indexOf(serialVariable);
113+
if (serialBeginStart != -1) {
114+
int serialBeginStartbraket = SetupFileContent.indexOf("(", serialBeginStart);
115+
if (serialBeginStartbraket != -1) {
116+
int serialBeginCloseBraket = SetupFileContent.indexOf(")", serialBeginStartbraket);
117+
if (serialBeginCloseBraket != -1) {
118+
String baudrate = SetupFileContent.substring(serialBeginStartbraket + 1, serialBeginCloseBraket)
119+
.trim();
120+
return Integer.parseInt(baudrate);
121+
}
122+
}
123+
}
124+
125+
} catch (CoreException | InterruptedException | IOException e) {
126+
e.printStackTrace();
127+
} finally {
128+
if (index != null) {
129+
index.releaseReadLock();
130+
}
131+
}
132+
133+
return -4;
134+
}
135+
27136
}

it.baeyens.arduino.monitor/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
33
Bundle-Name: Monitor
44
Bundle-SymbolicName: it.baeyens.arduino.monitor; singleton:=true
55
Bundle-Version: 3.0.0.qualifier
6-
Bundle-Activator: it.baeyens.arduino.monitor.Activator
6+
Bundle-Activator: it.baeyens.arduino.monitor.internal.Activator
77
Bundle-Vendor: jan baeyens
88
Require-Bundle: org.eclipse.ui,
99
org.eclipse.core.runtime,

it.baeyens.arduino.monitor/plugin.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,23 @@ The color used by the third serial connection of the serial monitor
123123
The color used by the forth serial connection of the serial monitor
124124
</description>
125125
</colorDefinition>
126+
<colorDefinition
127+
categoryId="it.baeyens.arduino.serialMonitor.themeCategory"
128+
id="it.baeyens.serial.color.6"
129+
label="serial Channel 6 Color"
130+
value="COLOR_DARK_YELLOW">
131+
<description>
132+
The color used by the sixth serial connection of the serial monitor
133+
</description>
134+
</colorDefinition>
135+
<colorDefinition
136+
categoryId="it.baeyens.arduino.serialMonitor.themeCategory"
137+
id="it.baeyens.serial.color.5"
138+
label="serial Channel 5 Color"
139+
value="COLOR_DARK_RED">
140+
<description>
141+
The color used by the fifth serial connection of the serial monitor
142+
</description>
143+
</colorDefinition>
126144
</extension>
127145
</plugin>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package it.baeyens.arduino.monitor;
2+
3+
public class SerialConnection {
4+
static void add(String comPort, int baudrate) {
5+
6+
}
7+
8+
static void remove(String comPort) {
9+
10+
}
11+
12+
}

it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/Activator.java renamed to it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/internal/Activator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package it.baeyens.arduino.monitor;
1+
package it.baeyens.arduino.monitor.internal;
22

33
import org.eclipse.jface.resource.ImageDescriptor;
44
import org.eclipse.ui.plugin.AbstractUIPlugin;

it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/views/ScopeListener.java renamed to it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/internal/ScopeListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package it.baeyens.arduino.monitor.views;
1+
package it.baeyens.arduino.monitor.internal;
22

33
import java.nio.ByteBuffer;
44
import java.nio.ByteOrder;
@@ -18,6 +18,7 @@
1818
import it.baeyens.arduino.arduino.MessageConsumer;
1919
import it.baeyens.arduino.common.ArduinoConst;
2020
import it.baeyens.arduino.common.Common;
21+
import it.baeyens.arduino.monitor.views.Messages;
2122
import multichannel.Oscilloscope;
2223

2324
public class ScopeListener implements MessageConsumer {

it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/views/SerialListener.java renamed to it.baeyens.arduino.monitor/src/it/baeyens/arduino/monitor/internal/SerialListener.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package it.baeyens.arduino.monitor.views;
1+
package it.baeyens.arduino.monitor.internal;
22

33
import java.nio.BufferOverflowException;
44
import java.nio.ByteBuffer;
@@ -11,6 +11,8 @@
1111
import it.baeyens.arduino.arduino.MessageConsumer;
1212
import it.baeyens.arduino.common.ArduinoConst;
1313
import it.baeyens.arduino.common.Common;
14+
import it.baeyens.arduino.monitor.views.Messages;
15+
import it.baeyens.arduino.monitor.views.SerialMonitor;
1416

1517
public class SerialListener implements MessageConsumer {
1618
private static boolean myScopeFilterFlag = false;
@@ -29,7 +31,7 @@ public void removeBytesFromStart(int n) {
2931
this.myReceivedScopeData.position(index);
3032
}
3133

32-
SerialListener(SerialMonitor Monitor, int ColorIndex) {
34+
public SerialListener(SerialMonitor Monitor, int ColorIndex) {
3335
this.TheMonitor = Monitor;
3436
this.theColorIndex = ColorIndex;
3537
this.myReceivedScopeData.order(ByteOrder.LITTLE_ENDIAN);
@@ -43,7 +45,8 @@ public void message(byte[] newData) {
4345
this.myReceivedScopeData.put(newData);
4446
} catch (BufferOverflowException e) {
4547
this.myReceivedScopeData.clear();
46-
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID, Messages.SerialListener_scope_skipping_data));
48+
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID,
49+
Messages.SerialListener_scope_skipping_data));
4750
}
4851
internalExtractAndProcessScopeData();
4952
} else {
@@ -55,26 +58,41 @@ public void message(byte[] newData) {
5558
private void internalExtractAndProcessScopeData() {
5659
String MonitorMessage = ArduinoConst.EMPTY_STRING;
5760
boolean dontProcessLastPart = false;
58-
for (int scannnedScopePointer = 0; scannnedScopePointer < this.myReceivedScopeData.position() - 1; scannnedScopePointer++) {
61+
for (int scannnedScopePointer = 0; scannnedScopePointer < this.myReceivedScopeData.position()
62+
- 1; scannnedScopePointer++) {
5963
if (this.myReceivedScopeData.getShort(scannnedScopePointer) == ArduinoConst.SCOPE_START_DATA) {
6064
// we have a hit.
61-
if (scannnedScopePointer > 0)// there is data before the scopehit->handle it and remove it
65+
if (scannnedScopePointer > 0) // there is data before the
66+
// scopehit->handle it and remove
67+
// it
6268
{
6369
for (int n = 0; n < scannnedScopePointer; n++)
6470
MonitorMessage += Character.toString((char) this.myReceivedScopeData.get(n));
6571
removeBytesFromStart(scannnedScopePointer);
6672
}
6773
// now we have a hit at the beginning of the buffer.
68-
if (this.myReceivedScopeData.position() < 4) // the scopedata is not complete yet
74+
if (this.myReceivedScopeData.position() < 4) // the scopedata is
75+
// not complete yet
6976
{
70-
scannnedScopePointer = this.myReceivedScopeData.position(); // stop the loop
77+
scannnedScopePointer = this.myReceivedScopeData.position(); // stop
78+
// the
79+
// loop
7180
dontProcessLastPart = true;
7281
} else {
7382
int bytestoRead = this.myReceivedScopeData.getShort(2);
7483
if ((bytestoRead < 0) || (bytestoRead > (10 * 2))) {
7584
Common.log(new Status(IStatus.WARNING, ArduinoConst.CORE_PLUGIN_ID,
76-
Messages.SerialListener_error_input_part_1 + bytestoRead / 2 + Messages.SerialListener_error_input_part_2));
77-
this.myReceivedScopeData.putShort(0, (short) 0); // process scope data as normal data remove the ArduinoConst.SCOPE_START_DATA
85+
Messages.SerialListener_error_input_part_1 + bytestoRead / 2
86+
+ Messages.SerialListener_error_input_part_2));
87+
this.myReceivedScopeData.putShort(0, (short) 0); // process
88+
// scope
89+
// data
90+
// as
91+
// normal
92+
// data
93+
// remove
94+
// the
95+
// ArduinoConst.SCOPE_START_DATA
7896
} else {
7997
if (bytestoRead + 4 < this.myReceivedScopeData.position()) {
8098
// all data is available
@@ -89,9 +107,12 @@ private void internalExtractAndProcessScopeData() {
89107
}
90108
}
91109
}
92-
if (!dontProcessLastPart) // we don't end on a scope data set; check whether the last char is start of a new scope data set
110+
if (!dontProcessLastPart) // we don't end on a scope data set; check
111+
// whether the last char is start of a new
112+
// scope data set
93113
{
94-
if (this.myReceivedScopeData.get(this.myReceivedScopeData.position()) == (byte) (ArduinoConst.SCOPE_START_DATA >> 8)) {
114+
if (this.myReceivedScopeData
115+
.get(this.myReceivedScopeData.position()) == (byte) (ArduinoConst.SCOPE_START_DATA >> 8)) {
95116
for (int n = 0; n < this.myReceivedScopeData.position() - 1; n++)
96117
MonitorMessage += Character.toString((char) this.myReceivedScopeData.get(n));
97118
removeBytesFromStart(this.myReceivedScopeData.position() - 1);
@@ -127,7 +148,7 @@ public void run() {
127148

128149
}
129150

130-
static void setScopeFilter(boolean selection) {
151+
public static void setScopeFilter(boolean selection) {
131152
myScopeFilterFlag = selection;
132153

133154
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
import it.baeyens.arduino.arduino.Serial;
3131
import it.baeyens.arduino.common.ArduinoConst;
32-
import it.baeyens.arduino.monitor.Activator;
32+
import it.baeyens.arduino.monitor.internal.Activator;
33+
import it.baeyens.arduino.monitor.internal.ScopeListener;
3334
import multichannel.Oscilloscope;
3435
import multichannel.OscilloscopeDispatcher;
3536

0 commit comments

Comments
 (0)