Skip to content

add option to save hex or bin to sketch folder #2322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 93 additions & 27 deletions app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public class Editor extends JFrame implements RunnerListener {

Runnable runHandler;
Runnable presentHandler;
Runnable runAndSaveHandler;
Runnable presentAndSaveHandler;
Runnable stopHandler;
Runnable exportHandler;
Runnable exportAppHandler;
Expand Down Expand Up @@ -555,21 +557,6 @@ public void actionPerformed(ActionEvent e) {
});
fileMenu.add(saveAsMenuItem);

item = newJMenuItem(_("Upload"), 'U');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleExport(false);
}
});
fileMenu.add(item);

item = newJMenuItemShift(_("Upload Using Programmer"), 'U');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleExport(true);
}
});
fileMenu.add(item);

fileMenu.addSeparator();

Expand Down Expand Up @@ -618,7 +605,7 @@ public void actionPerformed(ActionEvent e) {
protected JMenu buildSketchMenu() {
JMenuItem item;
sketchMenu = new JMenu(_("Sketch"));

item = newJMenuItem(_("Verify / Compile"), 'R');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Expand All @@ -627,13 +614,29 @@ public void actionPerformed(ActionEvent e) {
});
sketchMenu.add(item);

// item = newJMenuItemShift("Verify / Compile (verbose)", 'R');
// item.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent e) {
// handleRun(true);
// }
// });
// sketchMenu.add(item);
item = newJMenuItemShift("Compile and Save Hex", 'R');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleRunAndSave(false);
}
});
sketchMenu.add(item);

item = newJMenuItem(_("Upload"), 'U');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleExport(false);
}
});
sketchMenu.add(item);

item = newJMenuItemShift(_("Upload Using Programmer"), 'U');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleExport(true);
}
});
sketchMenu.add(item);

// item = new JMenuItem("Stop");
// item.addActionListener(new ActionListener() {
Expand Down Expand Up @@ -1405,11 +1408,17 @@ protected void updateRedoState() {
// abstract from the editor in this fashion.


public void setHandlers(Runnable runHandler, Runnable presentHandler,
public void setHandlers(Runnable runHandler,
Runnable presentHandler,
Runnable runAndSaveHandler,
Runnable presentAndSaveHandler,
Runnable stopHandler,
Runnable exportHandler, Runnable exportAppHandler) {
Runnable exportHandler,
Runnable exportAppHandler) {
this.runHandler = runHandler;
this.presentHandler = presentHandler;
this.runAndSaveHandler = runHandler;
this.presentAndSaveHandler = presentHandler;
this.stopHandler = stopHandler;
this.exportHandler = exportHandler;
this.exportAppHandler = exportAppHandler;
Expand All @@ -1419,6 +1428,8 @@ public void setHandlers(Runnable runHandler, Runnable presentHandler,
public void resetHandlers() {
runHandler = new DefaultRunHandler();
presentHandler = new DefaultPresentHandler();
runAndSaveHandler = new DefaultRunAndSaveHandler();
presentAndSaveHandler = new DefaultPresentAndSaveHandler();
stopHandler = new DefaultStopHandler();
exportHandler = new DefaultExportHandler();
exportAppHandler = new DefaultExportAppHandler();
Expand Down Expand Up @@ -1903,13 +1914,35 @@ public void handleRun(final boolean verbose) {
// placed on the event thread and causes a hang--bad idea all around.
new Thread(verbose ? presentHandler : runHandler).start();
}
/**
* Implements Sketch → Run and Save Hex.
* @param verbose Set true to run with verbose output.
*/
public void handleRunAndSave(final boolean verbose) {
internalCloseRunner();
running = true;
toolbar.activate(EditorToolbar.RUN);
status.progress(_("Compiling sketch..."));

// do this to advance/clear the terminal window / dos prompt / etc
for (int i = 0; i < 10; i++) System.out.println();

// clear the console on each run, unless the user doesn't want to
if (Preferences.getBoolean("console.auto_clear")) {
console.clear();
}

// Cannot use invokeLater() here, otherwise it gets
// placed on the event thread and causes a hang--bad idea all around.
new Thread(verbose ? presentAndSaveHandler : runAndSaveHandler).start();
}

// DAM: in Arduino, this is compile
class DefaultRunHandler implements Runnable {
public void run() {
try {
sketch.prepare();
sketch.build(false);
sketch.build(false, false);
statusNotice(_("Done compiling."));
} catch (Exception e) {
status.unprogress();
Expand All @@ -1926,7 +1959,7 @@ class DefaultPresentHandler implements Runnable {
public void run() {
try {
sketch.prepare();
sketch.build(true);
sketch.build(true, false);
statusNotice(_("Done compiling."));
} catch (Exception e) {
status.unprogress();
Expand All @@ -1937,6 +1970,39 @@ public void run() {
toolbar.deactivate(EditorToolbar.RUN);
}
}
//DAM: in Arduino, this is compile
class DefaultRunAndSaveHandler implements Runnable {
public void run() {
try {
sketch.prepare();
sketch.build(false, true);
statusNotice(_("Done compiling."));
} catch (Exception e) {
status.unprogress();
statusError(e);
}

status.unprogress();
toolbar.deactivate(EditorToolbar.RUN);
}
}

// DAM: in Arduino, this is compile (with verbose output)
class DefaultPresentAndSaveHandler implements Runnable {
public void run() {
try {
sketch.prepare();
sketch.build(true, true);
statusNotice(_("Done compiling."));
} catch (Exception e) {
status.unprogress();
statusError(e);
}

status.unprogress();
toolbar.deactivate(EditorToolbar.RUN);
}
}

class DefaultStopHandler implements Runnable {
public void run() {
Expand Down
10 changes: 5 additions & 5 deletions app/src/processing/app/Sketch.java
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,8 @@ public RunnerException placeException(String message,
* @return null if compilation failed, main class name if not
* @throws RunnerException
*/
public String build(boolean verbose) throws RunnerException {
return build(tempBuildFolder.getAbsolutePath(), verbose);
public String build(boolean verbose, boolean save) throws RunnerException {
return build(tempBuildFolder.getAbsolutePath(), verbose, save);
}

/**
Expand Down Expand Up @@ -1558,7 +1558,7 @@ protected String buildPrefsString(Compiler compiler) {
*
* @return null if compilation failed, main class name if not
*/
public String build(String buildPath, boolean verbose) throws RunnerException {
public String build(String buildPath, boolean verbose, boolean save) throws RunnerException {
String primaryClassName = name + ".cpp";
Compiler compiler = new Compiler(this, buildPath, primaryClassName);
File buildPrefsFile = new File(buildPath, BUILD_PREFS_FILE);
Expand All @@ -1585,7 +1585,7 @@ public String build(String buildPath, boolean verbose) throws RunnerException {

// compile the program. errors will happen as a RunnerException
// that will bubble up to whomever called build().
if (compiler.compile(verbose)) {
if (compiler.compile(verbose, save)) {
size(compiler.getBuildPreferences());
return primaryClassName;
}
Expand All @@ -1607,7 +1607,7 @@ public boolean exportApplet(String appletPath, boolean usingProgrammer)

// build the sketch
editor.status.progressNotice(_("Compiling sketch..."));
String foundName = build(appletPath, false);
String foundName = build(appletPath, false, false);
// (already reported) error during export, exit this function
if (foundName == null) return false;

Expand Down
28 changes: 27 additions & 1 deletion app/src/processing/app/debug/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class Compiler implements MessageConsumer {

private PreferencesMap prefs;
private boolean verbose;
private boolean saveHex;
private boolean sketchIsCompiled;
private String targetArch;

Expand All @@ -78,8 +79,9 @@ public Compiler(Sketch _sketch, String _buildPath, String _primaryClassName)
* @return true if successful.
* @throws RunnerException Only if there's a problem. Only then.
*/
public boolean compile(boolean _verbose) throws RunnerException {
public boolean compile(boolean _verbose, boolean _save) throws RunnerException {
verbose = _verbose || Preferences.getBoolean("build.verbose");
saveHex = _save;
sketchIsCompiled = false;
objectFiles = new ArrayList<File>();

Expand Down Expand Up @@ -142,6 +144,12 @@ public boolean compile(boolean _verbose) throws RunnerException {
// 6. build the .hex file
sketch.setCompilingProgress(80);
compileHex();

// 7. Save the .hex file
if (saveHex) {
sketch.setCompilingProgress(85);
saveHex();
}

sketch.setCompilingProgress(90);
return true;
Expand Down Expand Up @@ -790,6 +798,24 @@ void compileHex() throws RunnerException {
}
execAsynchronously(cmdArray);
}

// 7. Save the .hex file
void saveHex() throws RunnerException {
PreferencesMap dict = new PreferencesMap(prefs);
dict.put("ide_version", "" + Base.REVISION);

String[] cmdArray;
try {
String hexPattern = prefs.get("recipe.hex.pattern");
String hexPath = prefs.get("build.path") + "/" + hexPattern;
String savePath = sketch.getFolder().getAbsolutePath() + "/" + hexPattern;
String cmd = "cp -f " + hexPath + " " + savePath;
cmdArray = StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
execAsynchronously(cmdArray);
}

private static String prepareIncludes(List<File> includeFolders) {
String res = "";
Expand Down
3 changes: 3 additions & 0 deletions hardware/arduino/avr/platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ recipe.objcopy.eep.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.obj
## Create hex
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"

## Save hex
recipe.hex.pattern={build.project_name}.hex

## Compute size
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
recipe.size.regex=^(?:\.text|\.data|\.bootloader)\s+([0-9]+).*
Expand Down
3 changes: 3 additions & 0 deletions hardware/arduino/sam/platform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ recipe.objcopy.eep.pattern=
## Create hex
recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin"

## Save hex
recipe.hex.pattern={build.project_name}.bin

## Compute size
recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
recipe.size.regex=\.text\s+([0-9]+).*
Expand Down