Skip to content

Commit

Permalink
auto-build and async action performer
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Mar 17, 2024
1 parent 97316d3 commit 677e706
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 48 deletions.
105 changes: 100 additions & 5 deletions src/main/java/com/arduino/AppSettingsArduino.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,38 @@
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 java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cc.arduino.Compiler;
import cc.arduino.CompilerProgressListener;
import cc.arduino.utils.Progress;

import processing.app.helpers.PreferencesMapException;
import processing.app.debug.RunnerException;

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


import com.serifpersia.esp32partitiontool.AppSettings;

public class AppSettingsArduino extends AppSettings {

private Editor editor;
private final Editor editor;
private boolean debug_ui = true;
private boolean isWindows;
private File defaultSketchbookFolder;
Expand All @@ -33,7 +52,6 @@ public AppSettingsArduino(Editor editor) {
}

public void init() {

// these don't need to be evaluated more than once
isWindows = PreferencesData.get("runtime.os").contentEquals("windows");
espotaCmd = "espota" + (isWindows ? ".exe" : ".py");
Expand Down Expand Up @@ -75,8 +93,34 @@ public void init() {


@Override
public void load() {
public void clean() {
try {
deleteCompiledFiles();
} catch (IOException e) {
System.err.println( e.getMessage() );
}
}

@Override
public void build(JProgressBar progressBar, Runnable onSuccess) {

ProgressListener progressListener = new ProgressListener(progressBar);// {{ }};
progressBar.setIndeterminate(true);
progressBar.setVisible(true);

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
boolean success = build( progressListener );
progressBar.setIndeterminate(false);
progressBar.setVisible(false);
if( success ) onSuccess.run();
}
});
}

@Override
public void load() {
defaultSketchbookFolder = BaseNoGui.getDefaultSketchbookFolder();
toolsPathBase = BaseNoGui.getToolsPath();
platform = BaseNoGui.getTargetPlatform();
Expand Down Expand Up @@ -146,7 +190,6 @@ public void load() {

findFile(toolExeName, searchPaths, toolBinName);
}

}

private String getBootloaderImagePath() {
Expand Down Expand Up @@ -225,8 +268,60 @@ private String getBuildFolderPath() {
}
}
}
return "";
return null;
}


private class ProgressListener implements CompilerProgressListener {

JProgressBar progressBar;

public ProgressListener(JProgressBar progressBar) {
this.progressBar = progressBar;
progressBar.setValue(0);
}
public void progress(int value) {
progressBar.setValue( value );
progressBar.repaint();
//System.out.printf("Progress: %d\n", value );
}
}

private boolean build(ProgressListener progressListener ) {
try {
boolean deleteTemp = false;
File pathToSketch = editor.getSketch().getPrimaryFile().getFile();
try {
boolean save = true;
String ret = new Compiler(pathToSketch, editor.getSketch()).build(progressListener, save);
} finally {
}
} catch( Exception e ) {
System.err.println(e.getMessage());
return false;
}
return true;
}

private boolean deleteCompiledFiles() throws IOException {
String buildFolderPath = getBuildFolderPath();
if( buildFolderPath == null ) return false;
Path tempBuildFolder = Paths.get( buildFolderPath );
List<File> tempFiles = Files.list(tempBuildFolder)
.map(Path::toFile)
.filter(File::isFile)
.collect(Collectors.toList());

for (File tempFile : tempFiles) {
if( !tempFile.delete() ) {
System.err.println("Can't delete " + tempFile );
return false;
}
System.out.println("Deleted " + tempFile );
}
return true;
}



}
7 changes: 5 additions & 2 deletions src/main/java/com/arduino/ESP32PartitionTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ of this software and associated documentation files (the "Software"), to deal
import com.serifpersia.esp32partitiontool.UI;
import com.serifpersia.esp32partitiontool.UIController;




@SuppressWarnings("serial")
public class ESP32PartitionTool extends JFrame implements Tool {

Expand All @@ -65,7 +68,7 @@ public String getMenuTitle() {
}

public void addUI(UI contentPane) {
contentPane.setFrame( frame );
contentPane.setFrame( frame, "ESP32 Partition Tool (Arduino IDE)" );
frame.add(contentPane);
}

Expand All @@ -89,7 +92,7 @@ private void initGUI() {
}

if (frame == null) {
frame = new JFrame("ESP32 Partition Tool (Arduino IDE)");
frame = new JFrame();

frame.setSize(1024, 640);
frame.setResizable(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void init(String[] args) {

// Create and show the JFrame
if (frame == null) {
frame = new JFrame("ESP32 Partition Tool (standalone)");
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Size and display the frame
Expand Down Expand Up @@ -129,7 +129,7 @@ public void windowClosing(WindowEvent e) {
frame.toFront();
}

contentPane.setFrame( frame );
contentPane.setFrame( frame, "ESP32 Partition Tool (standalone)" );
fileManager.loadDefaultCSV();
frame.setVisible(true);
}
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/com/serifpersia/esp32partitiontool/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import java.util.Map;
import java.util.HashMap;
import javax.swing.*;

public class AppSettings {

public boolean debug_settings = true;
public boolean hasFSPanel = false;
private boolean changed = false;
public Map<String, String> prefs = new HashMap<>();


public String get( String key ) {
String value = prefs.get( key );
if( debug_settings && value == null ) {
System.out.printf("settings.%s is null\n", key );
System.out.printf("[debug] settings.%s is null\n", key );
}
return value;
}
Expand All @@ -22,16 +23,37 @@ public String set( String key, String value ) {
String oldvalue = prefs.put( key, value );
if( debug_settings && oldvalue != null && value != null ) {
if( !oldvalue.equals(value) ) {
System.out.printf("Value change for settings.%s:\n [old] %s\n [new] %s\n", key, oldvalue, value );
System.out.printf("[debug] Value change for settings.%s:\n [old] %s\n [new] %s\n", key, oldvalue, value );
changed = true;
}
}
return oldvalue;
}

// this method is meant to be overriden from platformio or arduino AppSettings classes
public void setChanged() {
changed = true;
}

public boolean changed() {
boolean have_changed = changed;
changed = false; // reset
return have_changed;
}

// this method is overriden from AppSettingsStandalone or AppSettingsArduino classes
public void load() {

}

// this method is overriden from AppSettingsArduino only
public void build(JProgressBar progressBar, Runnable runAfter) {

}

// this method is overriden from AppSettingsArduino only
public void clean() {

}


}
53 changes: 43 additions & 10 deletions src/main/java/com/serifpersia/esp32partitiontool/FSPanel.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.serifpersia.esp32partitiontool;

import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
Expand All @@ -30,6 +24,8 @@ public JTransparentPanel() {
public JTextField FSBlockSize;
public JButton uploadFSBtn;
public JButton mergeBinBtn;
public JButton uploadMergedBinBtn;
JProgressBar progressBar;


public FSPanel() {
Expand Down Expand Up @@ -60,6 +56,10 @@ public JButton getMergeBinBtn() {
return mergeBinBtn;
}

public JButton getUploadMergedBinBtn() {
return uploadMergedBinBtn;
}


public void createPanel() {

Expand Down Expand Up @@ -127,11 +127,20 @@ public void createPanel() {

JPanel panel5 = new JTransparentPanel();
panel4.add(panel5, BorderLayout.NORTH);
panel5.setLayout(new GridLayout(2, 0, 0, 0));
panel5.setLayout(new GridLayout(3, 0, 0, 0));

mergeBinBtn = new JButton("Merge Binary");
panel5.add(mergeBinBtn);

uploadMergedBinBtn = new JButton("Merge Binary & Upload");
panel5.add(uploadMergedBinBtn);

progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
progressBar.setValue(0);
progressBar.setVisible(false);
panel5.add(progressBar);

}


Expand All @@ -151,16 +160,40 @@ public void actionPerformed(ActionEvent e) {
getFSBlockSize().setText(blockSizeText);
}
});

getUploadFSBtn().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileManager.uploadSPIFFS();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
fileManager.uploadSPIFFS();
}
});
}
});

getMergeBinBtn().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileManager.createMergedBin();
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
fileManager.createMergedBin(null);
}
});
}
});

getUploadMergedBinBtn().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
fileManager.uploadMergedBin();
}
});
}
});

getPartitionFlashTypes().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fsName = getPartitionFlashTypes().getSelectedItem().toString();
Expand Down
Loading

0 comments on commit 677e706

Please sign in to comment.