Skip to content

Commit

Permalink
restored FS/merge panel (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Mar 15, 2024
1 parent a1eb253 commit bf734cd
Show file tree
Hide file tree
Showing 10 changed files with 872 additions and 65 deletions.
220 changes: 192 additions & 28 deletions src/main/java/com/arduino/AppSettingsArduino.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,229 @@
package com.arduino;

import java.nio.file.*;

import processing.app.Editor;
import processing.app.Sketch;
import processing.app.PreferencesData;
import org.apache.commons.codec.digest.DigestUtils;
import processing.app.BaseNoGui;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.FileUtils;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.*;
import java.io.*;

import com.serifpersia.esp32partitiontool.AppSettings;

public class AppSettingsArduino extends AppSettings {

private Editor editor;
private boolean debug_ui = true;
private boolean isWindows;
private File defaultSketchbookFolder;
private String toolsPathBase;
private TargetPlatform platform;
private String espotaCmd;
private String esptoolCmd;
private String genEsp32PartCmd;

public AppSettingsArduino(Editor editor) {
this.editor = editor;
this.hasFSPanel = true;
init();
}

public void load() {
// figure out what csv file is selected in the boards menu, and where it is
TargetPlatform platform = BaseNoGui.getTargetPlatform();
String defaultCsvSaveDir = editor.getSketch().getFolder().toString();
String platformPath = platform.getFolder().toString();
String csvName = BaseNoGui.getBoardPreferences().get("build.partitions");
String customCsvName = BaseNoGui.getBoardPreferences().get("build.custom_partitions");
String variantName = BaseNoGui.getBoardPreferences().get("build.variant");
String variantPath = platformPath + "/variants/" + variantName;
String partitionsPath = platformPath + "/tools/partitions";
String csvPath = partitionsPath + "/" + csvName + ".csv";
String variantCsvPath = null;
public void init() {

load();

set("csvDir.path", get("sketchDir.path") );

// check if the board uses a custom partition, could be stored in variants or
// tools folder

String partitionsPath = get("platform.path") + "/tools/partitions";
String csvPath = partitionsPath + "/" + get("build.partitions") + ".csv";
String variantCsvPath = null;

if (BaseNoGui.getBoardPreferences().containsKey("build.custom_partitions")) {
variantCsvPath = variantPath + "/" + customCsvName + ".csv";
variantCsvPath = get("variant.path") + "/" + get("build.custom_partitions") + ".csv";
} else {
variantCsvPath = variantPath + "/" + csvName + ".csv";
variantCsvPath = get("variant.path") + "/" + get("build.partitions") + ".csv";
}

System.out.println("build.custom_partitions = " + customCsvName);
System.out.println("build.partitions = " + csvName);
System.out.println("build.variant = " + variantName);
System.out.println("variantCsvPath = " + variantCsvPath);
System.out.println("csvPath = " + csvPath);

String searchPaths[] = { defaultCsvSaveDir + "/partitions.csv", variantCsvPath, csvPath };
String searchPaths[] = { get("sketchDir.path") + "/partitions.csv", variantCsvPath, csvPath };

for (int i = 0; i < searchPaths.length; i++) {
if (Files.exists(Paths.get(searchPaths[i]))) {
// load csv file
csvFilePath = searchPaths[i];
set("csvFile.path", searchPaths[i] );
break;
}
}

if( debug_ui ) {
prefs.forEach((key, value) -> {
System.out.printf("%-24s : %s\n", key, value );
});
}
}

System.out.println("CSV File: " + csvFilePath);
System.out.println("CSV Path: " + defaultCsvSaveDir);

break;
@Override
public void load() {
// figure out what csv file is selected in the boards menu, and where it is
isWindows = PreferencesData.get("runtime.os").contentEquals("windows");
espotaCmd = "espota" + (isWindows ? ".exe" : ".py");
esptoolCmd = "esptool" + (isWindows ? ".exe" : ".py");
genEsp32PartCmd = "gen_esp32part" + (isWindows ? ".exe" : ".py");
defaultSketchbookFolder = BaseNoGui.getDefaultSketchbookFolder();
toolsPathBase = BaseNoGui.getToolsPath();
platform = BaseNoGui.getTargetPlatform();
set("sketch.name", editor.getSketch().getName() );
set("sketch.path", editor.getSketch().getMainFilePath() );
set("sketchDir.path", editor.getSketch().getFolder().getAbsolutePath() );
set("platform.path", platform.getFolder().toString() );
set("build.partitions", BaseNoGui.getBoardPreferences().get("build.partitions") );
set("build.custom_partitions", BaseNoGui.getBoardPreferences().get("build.custom_partitions") );
set("build.variant", BaseNoGui.getBoardPreferences().get("build.variant") );
set("variant.path", get("platform.path") + "/variants/" + get("build.variant") );
set("build.path", getBuildFolderPath() );
set("bootloader.path", getBootloaderImagePath() );
set("upload.speed", BaseNoGui.getBoardPreferences().get("upload.speed") );
set("serial.port", BaseNoGui.getBoardPreferences().get("serial.port") );
set("build.bootloader_addr", BaseNoGui.getBoardPreferences().get("build.bootloader_addr") );
set("build.mcu", BaseNoGui.getBoardPreferences().get("build.mcu") );
set("flashMode", BaseNoGui.getBoardPreferences().get("build.flash_mode") );
set("flashFreq", BaseNoGui.getBoardPreferences().get("build.flash_freq") );
set("pythonCmd", isWindows ? "python3.exe" : "python3");
set("espotaCmd", espotaCmd );
set("esptoolCmd", esptoolCmd );
set("genEsp32PartCmd", genEsp32PartCmd );

String espToolSearchPaths[] = {
PreferencesData.get("runtime.tools.esptool_py.path"), // preferences.txt
get("platform.path") + "/tools", get("platform.path") + "/tools/esptool_py", defaultSketchbookFolder + "/tools",
defaultSketchbookFolder + "/tools/esptool_py", toolsPathBase, toolsPathBase + "/tools/esptool_py" };

if (!findFile(esptoolCmd, espToolSearchPaths, "esptool")) {
editor.statusError(" Error: esptool not found!");
this.hasFSPanel = false;
}

// search for esptool.[py|exe] in all the following folders
String otaToolSearchPaths[] = { //getProperty("espota.path"), // user.properties (always first)
get("platform.path") + "/tools", defaultSketchbookFolder + "/tools", toolsPathBase, };

if (!findFile(espotaCmd, espToolSearchPaths, "espota")) {
editor.statusError(" Error: espota not found!");
this.hasFSPanel = false;
}

if (!findFile(genEsp32PartCmd, espToolSearchPaths, "genEsp32Part")) {
editor.statusError(" Error: genEsp32Part not found!");
this.hasFSPanel = false;
}

String[] fsNames = {"SPIFFS", "LittleFS", "FatFS"};
int size = fsNames.length;

for (int i = 0; i < size; i++) {

String toolBinName = "mk" + fsNames[i].toLowerCase();
String toolExeName = toolBinName + (isWindows ? ".exe" : "");

String toolPrefName = "runtime.tools." + toolBinName + ".path";
String toolPathPref = PreferencesData.get(toolPrefName);

// search for mk{filesystem}fs binary in the following folders:
String searchPaths[] = { // getProperty(toolBinName + ".path"), // user.properties (always first)
toolPathPref, // preferences.txt (always second)
get("platform.path") + "/tools", get("platform.path") + "/tools/" + toolBinName,
defaultSketchbookFolder + "/tools", defaultSketchbookFolder + "/tools/" + toolBinName,
toolsPathBase, toolsPathBase + "/" + toolBinName };

findFile(toolExeName, searchPaths, toolBinName);
}

}

private String getBootloaderImagePath() {
// precedence: source > variant > build files
String searchPaths[] = { get("sketchDir.path") + "/bootloader.bin", // in source directory
get("variant.path") + "/bootloader.bin", // in board variants directory
get("build.path") + "/" + get("sketch.name") + ".ino" + ".bootloader.bin" // in build directory
};
// walk the array
for (int i = 0; i < searchPaths.length; i++) {
if (Files.exists(Paths.get(searchPaths[i]))) {
// got a match!
return searchPaths[i];
}
}
return null;
}

private String findFile(String fileName, String searchPaths[]) {
for (int j = 0; j < searchPaths.length; j++) {
if (searchPaths[j] == null)
continue;
if (Files.exists(Paths.get(searchPaths[j] + "/" + fileName))) {
return searchPaths[j] + "/" + fileName;
}
}
return null;
}

public void save() {
private boolean findFile(String fileName, String searchPaths[], String propertyName) {
String full_path = findFile(fileName, searchPaths);
boolean found = full_path != null;
if (found) { // save found path in properties file
//if (debug_ui)
// System.out.println("[" + propertyName + "] => " + full_path);
set(propertyName + ".path", full_path);
} else {
if (debug_ui)
System.out.println("[" + propertyName + "] " + fileName + " not found in any tools folder");
}
return found;
}

private String getBuildFolderPath() {
Sketch sketch = editor.getSketch();
// first of all try the getBuildPath() function introduced with IDE 1.6.12
// see commit arduino/Arduino#fd1541eb47d589f9b9ea7e558018a8cf49bb6d03
try {
String buildpath = sketch.getBuildPath().getAbsolutePath();
return buildpath;
} catch (IOException er) {
editor.statusError(er);
} catch (Exception er) {
try {
File buildFolder = FileUtils.createTempFolder("build",
DigestUtils.md5Hex(sketch.getMainFilePath()) + ".tmp");
return buildFolder.getAbsolutePath();
} catch (IOException e) {
editor.statusError(e);
} catch (Exception e) {
// Arduino 1.6.5 doesn't have FileUtils.createTempFolder
// String buildPath = BaseNoGui.getBuildFolder().getAbsolutePath();
java.lang.reflect.Method method;
try {
method = BaseNoGui.class.getMethod("getBuildFolder");
File f = (File) method.invoke(null);
return f.getAbsolutePath();
} catch (SecurityException ex) {
editor.statusError(ex);
} catch (IllegalAccessException ex) {
editor.statusError(ex);
} catch (InvocationTargetException ex) {
editor.statusError(ex);
} catch (NoSuchMethodException ex) {
editor.statusError(ex);
}
}
}
return "";
}


}
5 changes: 2 additions & 3 deletions src/main/java/com/arduino/ESP32PartitionTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,16 @@ private void initGUI() {

if (settings == null) {
settings = new AppSettingsArduino(editor);
} else {
settings.init();
}

settings.load();

if (fileManager == null) {
fileManager = new FileManager(contentPane, settings);
}

if (frame == null) {
frame = new JFrame("ESP32 Partition Tool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(1024, 640);
frame.setResizable(false);
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/platformio/AppSettingsStandalone.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ public class AppSettingsStandalone extends AppSettings {

public AppSettingsStandalone(String[] args) {
this.args = args;
init();
}

public void load() {
public void init() {
if (args == null) {
System.out.println("No args given");
} else {
for (int i = 0; i < args.length; i++) {
if (args[i].endsWith(".csv") && Files.exists(Paths.get(args[i]))) {
csvFilePath = args[i];
defaultCsvSaveDir = Paths.get(csvFilePath).getFileName().toString();
System.out.println("CSV File: " + csvFilePath);
System.out.println("CSV Path: " + defaultCsvSaveDir);
prefs.put("csvFile.path", args[i] );
//defaultCsvSaveDir = Paths.get(args[i]).getFileName().toString();
System.out.println("CSV File: " + args[i]);
//System.out.println("CSV Path: " + defaultCsvSaveDir);
} else if (!args[i].trim().isEmpty()) {
System.out.println("Ignored Arg#" + i + ": " + args[i]);
}
}
}
}

public void save() {
@Override
public void load() {

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public void addUI(UI contentPane) {
private void init(String[] args) {

settings = new AppSettingsStandalone(args);
settings.load();

fileManager = new FileManager(contentPane, settings);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.serifpersia.esp32partitiontool;

import javax.swing.*;
import java.awt.Font;


@SuppressWarnings("serial")
class AboutPanel extends JEditorPane {
Expand All @@ -10,6 +12,9 @@ public AboutPanel() {
}

private void createPanel() {


final String fontFace = "<font face=sans-serif>";
final String boxpadding = "padding-top: 0px;padding-right: 10px;padding-bottom: 10px;padding-left: 10px;";
final String titleSpanned = "<span style=\"background-color: #d7a631\">&nbsp;ESP32&nbsp;</span>"
+ "<span style=\"background-color: #bf457a\">&nbsp;Partition&nbsp;</span>"
Expand All @@ -23,9 +28,11 @@ private void createPanel() {
final String projectlink = "<p><b>Source:</b><br>https://github.com/serifpersia/esp32partitiontool</p>";
final String copyright = "<p><b>Copyright (c) 2024 @serifpersia</b><br>https://github.com/serifpersia</p>";
final String credits = "<p><b>Contributors:</b><br>serifpersia, tobozo</p>";
final String message = "<html>" + title + "<div style=\"" + boxpadding + "\">" + description + projectlink
final String message = "<html>" + fontFace + title + "<div style=\"" + boxpadding + "\">" + description + projectlink
+ copyright + credits + "</div></html>";

// fixes weird font problem with the standalone version
putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
setContentType("text/html");
setText(message);
}
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/serifpersia/esp32partitiontool/AppSettings.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package com.serifpersia.esp32partitiontool;

import java.util.Map;
import java.util.HashMap;

public class AppSettings {

public String csvFilePath;
public String defaultCsvSaveDir;
public boolean hasFSPanel = false;
public Map<String, String> prefs = new HashMap<>();

public String get( String key ) {
return prefs.get( key );
}

public String set( String key, String value ) {
return prefs.put( key, value );
}

// this method is meant to be overloaded by platformio or arduino AppSettings classes
public void load() {

}


}
Loading

0 comments on commit bf734cd

Please sign in to comment.