mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-26 02:15:31 +02:00
Bug 483729 - Make Arduino upload a proper launch.
Creates and ArduinoLaunch object to manage the pause and resume of the serial port on launch and terminate. Registers the uploader process with the debug framework so that it's connected properly to the launch and the console. Change-Id: If14cf8ddf2c1b6ceda19bce8d37e07ebce9700f2
This commit is contained in:
parent
207d2e1006
commit
ae02a50dba
2 changed files with 80 additions and 60 deletions
|
@ -0,0 +1,47 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* Copyright (c) 2015 QNX Software Systems and others.
|
||||||
|
* All rights reserved. This program and the accompanying materials
|
||||||
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
* which accompanies this distribution, and is available at
|
||||||
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
*******************************************************************************/
|
||||||
|
package org.eclipse.cdt.arduino.core.internal.launch;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.arduino.core.internal.remote.ArduinoRemoteConnection;
|
||||||
|
import org.eclipse.debug.core.DebugEvent;
|
||||||
|
import org.eclipse.debug.core.DebugPlugin;
|
||||||
|
import org.eclipse.debug.core.ILaunchConfiguration;
|
||||||
|
import org.eclipse.debug.core.Launch;
|
||||||
|
import org.eclipse.debug.core.model.ISourceLocator;
|
||||||
|
import org.eclipse.remote.core.IRemoteConnection;
|
||||||
|
|
||||||
|
public class ArduinoLaunch extends Launch {
|
||||||
|
|
||||||
|
private final ArduinoRemoteConnection target;
|
||||||
|
private boolean wasOpen;
|
||||||
|
|
||||||
|
public ArduinoLaunch(ILaunchConfiguration launchConfiguration, String mode, ISourceLocator locator,
|
||||||
|
IRemoteConnection target) {
|
||||||
|
super(launchConfiguration, mode, locator);
|
||||||
|
this.target = target.getService(ArduinoRemoteConnection.class);
|
||||||
|
|
||||||
|
DebugPlugin.getDefault().addDebugEventListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
this.wasOpen = target.getRemoteConnection().isOpen();
|
||||||
|
if (wasOpen) {
|
||||||
|
target.pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleDebugEvents(DebugEvent[] events) {
|
||||||
|
super.handleDebugEvents(events);
|
||||||
|
if (isTerminated() && wasOpen) {
|
||||||
|
target.resume();
|
||||||
|
wasOpen = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,14 +15,12 @@ import java.io.IOException;
|
||||||
import org.eclipse.cdt.arduino.core.internal.Activator;
|
import org.eclipse.cdt.arduino.core.internal.Activator;
|
||||||
import org.eclipse.cdt.arduino.core.internal.Messages;
|
import org.eclipse.cdt.arduino.core.internal.Messages;
|
||||||
import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuildConfiguration;
|
import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuildConfiguration;
|
||||||
import org.eclipse.cdt.arduino.core.internal.console.ArduinoConsoleService;
|
|
||||||
import org.eclipse.cdt.arduino.core.internal.remote.ArduinoRemoteConnection;
|
import org.eclipse.cdt.arduino.core.internal.remote.ArduinoRemoteConnection;
|
||||||
import org.eclipse.core.resources.IProject;
|
import org.eclipse.core.resources.IProject;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.core.runtime.IStatus;
|
import org.eclipse.core.runtime.IStatus;
|
||||||
import org.eclipse.core.runtime.Status;
|
import org.eclipse.core.runtime.Status;
|
||||||
import org.eclipse.core.runtime.jobs.Job;
|
|
||||||
import org.eclipse.debug.core.DebugPlugin;
|
import org.eclipse.debug.core.DebugPlugin;
|
||||||
import org.eclipse.debug.core.ILaunch;
|
import org.eclipse.debug.core.ILaunch;
|
||||||
import org.eclipse.debug.core.ILaunchConfiguration;
|
import org.eclipse.debug.core.ILaunchConfiguration;
|
||||||
|
@ -43,6 +41,11 @@ public class ArduinoLaunchConfigurationDelegate extends LaunchConfigurationDeleg
|
||||||
return connectionType.getConnection(connectionName);
|
return connectionType.getConnection(connectionName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
|
||||||
|
return new ArduinoLaunch(configuration, mode, null, getTarget(configuration));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor)
|
public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor)
|
||||||
throws CoreException {
|
throws CoreException {
|
||||||
|
@ -71,69 +74,39 @@ public class ArduinoLaunchConfigurationDelegate extends LaunchConfigurationDeleg
|
||||||
@Override
|
@Override
|
||||||
public void launch(final ILaunchConfiguration configuration, String mode, final ILaunch launch,
|
public void launch(final ILaunchConfiguration configuration, String mode, final ILaunch launch,
|
||||||
IProgressMonitor monitor) throws CoreException {
|
IProgressMonitor monitor) throws CoreException {
|
||||||
new Job(Messages.ArduinoLaunchConfigurationDelegate_0) {
|
try {
|
||||||
protected IStatus run(IProgressMonitor monitor) {
|
IRemoteConnection target = getTarget(configuration);
|
||||||
try {
|
if (target == null) {
|
||||||
ArduinoConsoleService consoleService = Activator.getConsoleService();
|
throw new CoreException(
|
||||||
IRemoteConnection target = getTarget(configuration);
|
new Status(IStatus.ERROR, Activator.getId(), Messages.ArduinoLaunchConfigurationDelegate_2));
|
||||||
if (target == null) {
|
}
|
||||||
return new Status(IStatus.ERROR, Activator.getId(),
|
ArduinoRemoteConnection arduinoTarget = target.getService(ArduinoRemoteConnection.class);
|
||||||
Messages.ArduinoLaunchConfigurationDelegate_2);
|
|
||||||
}
|
|
||||||
ArduinoRemoteConnection arduinoTarget = target.getService(ArduinoRemoteConnection.class);
|
|
||||||
|
|
||||||
// The project
|
// The project
|
||||||
IProject project = (IProject) configuration.getMappedResources()[0];
|
IProject project = (IProject) configuration.getMappedResources()[0];
|
||||||
|
|
||||||
// The build config
|
// The build config
|
||||||
ArduinoBuildConfiguration arduinoConfig = ArduinoBuildConfiguration.getConfig(project,
|
ArduinoBuildConfiguration arduinoConfig = ArduinoBuildConfiguration.getConfig(project, arduinoTarget,
|
||||||
arduinoTarget, monitor);
|
monitor);
|
||||||
String[] uploadCmd = arduinoConfig.getUploadCommand(arduinoTarget.getPortName());
|
String[] uploadCmd = arduinoConfig.getUploadCommand(arduinoTarget.getPortName());
|
||||||
|
|
||||||
// If opened, temporarily close the connection so we can use
|
StringBuffer cmdStr = new StringBuffer(uploadCmd[0]);
|
||||||
// it to download the firmware.
|
for (int i = 1; i < uploadCmd.length; ++i) {
|
||||||
boolean wasOpened = target.isOpen();
|
cmdStr.append(' ');
|
||||||
if (wasOpened) {
|
cmdStr.append(uploadCmd[i]);
|
||||||
arduinoTarget.pause();
|
}
|
||||||
}
|
// Start the launch
|
||||||
|
((ArduinoLaunch) launch).start();
|
||||||
|
|
||||||
StringBuffer cmdStr = new StringBuffer(uploadCmd[0]);
|
// Run the process and capture the results in the console
|
||||||
for (int i = 1; i < uploadCmd.length; ++i) {
|
ProcessBuilder processBuilder = new ProcessBuilder(uploadCmd).directory(arduinoConfig.getBuildDirectory());
|
||||||
cmdStr.append(' ');
|
arduinoConfig.setEnvironment(processBuilder.environment());
|
||||||
cmdStr.append(uploadCmd[i]);
|
Process process = processBuilder.start();
|
||||||
}
|
DebugPlugin.newProcess(launch, process, cmdStr.toString());
|
||||||
cmdStr.append('\n');
|
} catch (IOException e) {
|
||||||
consoleService.writeOutput(cmdStr.toString());
|
throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e));
|
||||||
|
}
|
||||||
|
|
||||||
// Run the process and capture the results in the console
|
|
||||||
ProcessBuilder processBuilder = new ProcessBuilder(uploadCmd)
|
|
||||||
.directory(arduinoConfig.getBuildDirectory());
|
|
||||||
arduinoConfig.setEnvironment(processBuilder.environment());
|
|
||||||
Process process = processBuilder.start();
|
|
||||||
|
|
||||||
consoleService.monitor(process, null, null);
|
|
||||||
try {
|
|
||||||
process.waitFor();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
|
|
||||||
consoleService.writeOutput("Upload complete\n");
|
|
||||||
|
|
||||||
// Reopen the connection
|
|
||||||
if (wasOpened) {
|
|
||||||
arduinoTarget.resume();
|
|
||||||
}
|
|
||||||
} catch (CoreException e) {
|
|
||||||
return e.getStatus();
|
|
||||||
} catch (IOException e) {
|
|
||||||
return new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e);
|
|
||||||
} finally {
|
|
||||||
DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Status.OK_STATUS;
|
|
||||||
};
|
|
||||||
}.schedule();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue