Skip to content

Commit 73cbb8e

Browse files
committed
Merge branch 'master' into donatello-2
2 parents 4db3283 + de0175a commit 73cbb8e

File tree

6 files changed

+95
-50
lines changed

6 files changed

+95
-50
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.marginallyclever</groupId>
88
<artifactId>Makelangelo</artifactId>
9-
<version>7.62.1</version>
9+
<version>7.62.3</version>
1010
<name>Makelangelo</name>
1111
<description>Makelangelo Software is a Java program that prepares art for CNC plotters. It is especially designed for the Makelangelo Robot.
1212
It pairs really well with Marlin-polargraph, the code in the brain of the robot that receives instructions and moves the motors.</description>
@@ -448,6 +448,11 @@ It pairs really well with Marlin-polargraph, the code in the brain of the robot
448448
<artifactId>logback-classic</artifactId>
449449
<version>1.5.9</version>
450450
</dependency>
451+
<dependency>
452+
<groupId>org.codehaus.janino</groupId>
453+
<artifactId>janino</artifactId>
454+
<version>3.1.8</version>
455+
</dependency>
451456
<!-- logging -->
452457
<dependency>
453458
<groupId>org.slf4j</groupId>

src/main/java/com/marginallyclever/makelangelo/firmwareuploader/AVRDudeDownloader.java

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static void main(String[] args) throws IOException {
3737

3838
/**
3939
* Download AVRDude for the current OS architecture.
40-
* @return the path to the extracted avrdude executable.
40+
* @return the path to the first folder. If the target is c:\a\b\ and the extracted files are in c:\a\b\c\d\
41+
* then this will return c:\a\b\c
4142
* @throws IOException if the download fails.
4243
*/
4344
public static String downloadAVRDude() throws IOException {
@@ -52,22 +53,20 @@ public static String downloadAVRDude() throws IOException {
5253
*/
5354
public static String downloadAVRDude(String arch) throws IOException {
5455
String url = getURLforOS(arch);
55-
if (url != null) {
56-
try {
57-
return downloadAndExtract(url);
58-
} catch (IOException e) {
59-
logger.error("Error downloading avrdude", e);
60-
throw e;
61-
}
56+
if (url == null) return null;
57+
58+
try {
59+
return downloadAndExtract(url);
60+
} catch (IOException e) {
61+
logger.error("Error downloading avrdude", e);
62+
throw e;
6263
}
63-
return null;
6464
}
6565

6666
public static String getArch() {
67-
String arch = LINUX;
68-
if (OSHelper.isWindows()) arch = WINDOWS;
69-
if (OSHelper.isOSX()) arch = MACOS;
70-
return arch;
67+
if (OSHelper.isWindows()) return WINDOWS;
68+
if (OSHelper.isOSX()) return MACOS;
69+
return LINUX;
7170
}
7271

7372
/**
@@ -160,7 +159,7 @@ private static JSONObject getJSONFromURL(String url) throws IOException {
160159
public static String downloadAndExtract(String urlStr) throws IOException {
161160
File toDeleteLater = downloadFileToTemp(urlStr);
162161
String path = extractFile(toDeleteLater, urlStr);
163-
logger.info("new path: "+path);
162+
logger.info("new path: {}", path);
164163
makeExecutable(path);
165164

166165
return path;
@@ -210,6 +209,13 @@ private static File downloadFileToTemp(String urlStr) throws IOException {
210209
return toDeleteLater;
211210
}
212211

212+
/**
213+
* Extract the downloaded file to ~/.makelangelo
214+
* @param toDeleteLater the file to delete after extraction
215+
* @param urlStr the URL of the file
216+
* @return the path to the extracted file, which should be ~/.makelangelo/firstSubDirectory
217+
* @throws IOException if the extraction fails
218+
*/
213219
private static String extractFile(File toDeleteLater, String urlStr) throws IOException {
214220
String targetDirStr = System.getProperty("user.home") + File.separator + ".makelangelo" + File.separator;
215221
String newFolderName = "";
@@ -225,18 +231,26 @@ private static String extractFile(File toDeleteLater, String urlStr) throws IOEx
225231
newFolderName = extractBz2File(toDeleteLater, targetDirStr);
226232
}
227233

228-
if(!newFolderName.isEmpty()) {
229-
newFolderName = targetDirStr + newFolderName;
234+
// sometimes the path returned might be /a/b/... and we only want /a/
235+
if(newFolderName.contains("/")) {
236+
newFolderName = newFolderName.split("/")[0];
230237
}
231238

232-
return newFolderName;
239+
return targetDirStr + newFolderName;
233240
}
234241

235242
private static String getFileExtension(String fileName) {
236243
int dotIndex = fileName.lastIndexOf('.');
237244
return (dotIndex == -1) ? "" : fileName.substring(dotIndex);
238245
}
239246

247+
/**
248+
* Extract a zip file to the target directory.
249+
* @param file the zip file
250+
* @param targetDirStr the target directory
251+
* @return the name of the first directory in the zip file
252+
* @throws IOException if the extraction fails
253+
*/
240254
private static String extractZipFile(File file, String targetDirStr) throws IOException {
241255
try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(file))) {
242256
ZipEntry entry = zipIn.getNextEntry();
@@ -245,6 +259,10 @@ private static String extractZipFile(File file, String targetDirStr) throws IOEx
245259
Path filePath = Paths.get(targetDirStr, entry.getName());
246260
if (entry.isDirectory()) {
247261
Files.createDirectories(filePath);
262+
// the first directory is the one we want to return
263+
if (newFolderName.isEmpty()) {
264+
newFolderName = entry.getName();
265+
}
248266
} else {
249267
Files.createDirectories(filePath.getParent());
250268
try (OutputStream outputStream = new FileOutputStream(filePath.toFile())) {
@@ -256,15 +274,19 @@ private static String extractZipFile(File file, String targetDirStr) throws IOEx
256274
}
257275
}
258276
zipIn.closeEntry();
259-
if (newFolderName.isEmpty() && entry.isDirectory()) {
260-
newFolderName = entry.getName();
261-
}
262277
entry = zipIn.getNextEntry();
263278
}
264279
return newFolderName;
265280
}
266281
}
267282

283+
/**
284+
* Extract a bz2 file to the target directory.
285+
* @param file the zip file
286+
* @param targetDirStr the target directory
287+
* @return the name of the first directory in the bz2 file
288+
* @throws IOException if the extraction fails
289+
*/
268290
private static String extractBz2File(File file, String targetDirStr) throws IOException {
269291
try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(new FileInputStream(file))) {
270292
Path targetFilePath = Paths.get(targetDirStr, file.getName().replace(".bz2", ""));
@@ -280,15 +302,25 @@ private static String extractBz2File(File file, String targetDirStr) throws IOEx
280302
}
281303
}
282304

305+
/**
306+
* Extract a .tar.bz2 file to the target directory.
307+
* @param file the zip file
308+
* @param targetDirStr the target directory
309+
* @return the name of the first directory in the bz2 file
310+
* @throws IOException if the extraction fails
311+
*/
283312
private static String extractTarBz2File(File file, String targetDirStr) throws IOException {
284313
try (BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(new FileInputStream(file));
285314
TarArchiveInputStream tarIn = new TarArchiveInputStream(bzIn)) {
286315
TarArchiveEntry entry;
287316
String newFolderName = "";
288-
while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
317+
while ((entry = tarIn.getNextEntry()) != null) {
289318
Path filePath = Paths.get(targetDirStr, entry.getName());
290319
if (entry.isDirectory()) {
291320
Files.createDirectories(filePath);
321+
if (newFolderName.isEmpty()) {
322+
newFolderName = getFolderNameFromEntry(entry.getName());
323+
}
292324
} else {
293325
Files.createDirectories(filePath.getParent());
294326
try (OutputStream outputStream = new FileOutputStream(filePath.toFile())) {
@@ -299,9 +331,6 @@ private static String extractTarBz2File(File file, String targetDirStr) throws I
299331
}
300332
}
301333
}
302-
if (newFolderName.isEmpty()) {
303-
newFolderName = getFolderNameFromEntry(entry.getName());
304-
}
305334
}
306335
return newFolderName;
307336
}

src/main/java/com/marginallyclever/makelangelo/firmwareuploader/FirmwareUploader.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ protected FirmwareUploader() {
2929
}
3030

3131
/**
32-
* Search the tree starting at avrdudePath for the given filename.
32+
* Search the tree starting at the install path for the given filename.
3333
* @param target the name of the file to find.
3434
* @return the file if found, null otherwise.
3535
*/
36-
public File findFile(String target) {
36+
public File findFileRecursively(String target) {
3737
logger.info("Searching for "+target+" starting in "+installPath);
3838
Path startPath = Paths.get(installPath);
3939
try {
@@ -50,16 +50,16 @@ public File findFile(String target) {
5050
public boolean findAVRDude() {
5151
String path = "avrdude";
5252
if( OSHelper.isWindows()) path+=".exe";
53-
File f = findFile(path);
54-
if(!f.exists()) return false;
53+
File f = findFileRecursively(path);
54+
if(f==null || !f.exists()) return false;
5555
avrdudePath = f.getAbsolutePath();
5656
return true;
5757
}
5858

5959
// find avrdude.conf
6060
public boolean findConf() {
61-
File f = findFile(CONF);
62-
if(!f.exists()) return false;
61+
File f = findFileRecursively(CONF);
62+
if(f==null || !f.exists()) return false;
6363
confPath = f.getAbsolutePath();
6464
return true;
6565
}
@@ -147,13 +147,21 @@ public void setHexPath(String s) {
147147
hexPath = s;
148148
}
149149

150-
public void setAVRDude(String avrDudePath) {
151-
logger.debug("setting avrdude to {}",avrDudePath);
152-
avrdudePath = avrDudePath;
150+
/**
151+
* Set the path to the avrdude executable.
152+
* @param path the path to the avrdude executable.
153+
*/
154+
public void setAVRDude(String path) {
155+
logger.debug("setting avrdude to {}",path);
156+
avrdudePath = path;
153157
}
154158

155-
public void setInstallPath(String avrDudePath) {
156-
logger.debug("setting install path to {}",avrDudePath);
157-
installPath = avrDudePath;
159+
/**
160+
* Set the path to the install directory, which should be something like ~/.makelangelo/avrdude
161+
* @param path the path to the install directory.
162+
*/
163+
public void setInstallPath(String path) {
164+
logger.debug("setting install path to {}",path);
165+
this.installPath = path;
158166
}
159167
}

src/main/java/com/marginallyclever/makelangelo/firmwareuploader/FirmwareUploaderPanel.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ private String[] getListOfCOMPorts() {
8080
private void run(ActionEvent evt, String name) {
8181
String title = Translator.get("FirmwareUploaderPanel.status");
8282

83+
logger.debug("setup...");
84+
if(port.getSelectedIndex()==-1) {
85+
JOptionPane.showMessageDialog(this, Translator.get("FirmwareUploaderPanel.noPortSelected"), title, JOptionPane.ERROR_MESSAGE);
86+
return;
87+
}
88+
8389
logger.debug("maybe downloading avrdude...");
8490
try {
85-
String AVRDudePath = AVRDudeDownloader.downloadAVRDude();
86-
firmwareUploader.setInstallPath(AVRDudePath);
91+
String installPath = AVRDudeDownloader.downloadAVRDude();
92+
firmwareUploader.setInstallPath(installPath);
8793
} catch(Exception e) {
8894
JOptionPane.showMessageDialog(this,Translator.get("FirmwareUploaderPanel.avrdudeNotDownloaded"),title,JOptionPane.ERROR_MESSAGE);
8995
return;
@@ -107,11 +113,6 @@ private void run(ActionEvent evt, String name) {
107113
return;
108114
}
109115

110-
logger.debug("setup...");
111-
if(port.getSelectedIndex()==-1) {
112-
JOptionPane.showMessageDialog(this, Translator.get("FirmwareUploaderPanel.noPortSelected"), title, JOptionPane.ERROR_MESSAGE);
113-
return;
114-
}
115116
firmwareUploader.setHexPath(firmwareDownloader.getDownloadPath(name));
116117
startM5.setEnabled(false);
117118
startHuge.setEnabled(false);

src/main/java/com/marginallyclever/makelangelo/makeart/io/LoadSVG.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,16 @@ private Color stringToColor(String strokeName) {
306306
int a = Integer.parseInt(strokeName.substring(6,8),16);
307307
return new Color(r,g,b,a);
308308
}
309-
} else if(strokeName.startsWith("rgb(")) {
310-
// isolate the portion between the ()
309+
} else if(strokeName.startsWith("rgb(") && strokeName.endsWith(")")) {
310+
// isolate the portion between the parentheses
311311
strokeName = strokeName.substring(4,strokeName.length()-1);
312-
strokeName = strokeName.substring(0,strokeName.indexOf(")"));
313312
if(strokeName.contains("%")) {
314313
// convert from percent to 0-255
315314
strokeName = strokeName.replace("%","");
316315
String [] parts = strokeName.split(",");
317-
int r = (int)(Integer.parseInt(parts[0])*255.0/100.0);
318-
int g = (int)(Integer.parseInt(parts[1])*255.0/100.0);
319-
int b = (int)(Integer.parseInt(parts[2])*255.0/100.0);
316+
int r = Integer.parseInt(parts[0]) * 255 / 100;
317+
int g = Integer.parseInt(parts[1]) * 255 / 100;
318+
int b = Integer.parseInt(parts[2]) * 255 / 100;
320319
return new Color(r,g,b);
321320
} else {
322321
// already in 0-255

src/main/java/module-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
opens com.marginallyclever.makelangelo;
3737
opens com.marginallyclever.makelangelo.makeart.io;
3838
opens com.marginallyclever.makelangelo.texture;
39+
opens com.marginallyclever.convenience.noise;
40+
opens com.marginallyclever.convenience.helpers;
41+
opens com.marginallyclever.convenience.log to ch.qos.logback.core;
3942

4043
// A Java module that wants to implement a service interface from a service interface module must:
4144
// - Require the service interface module in its own module descriptor.

0 commit comments

Comments
 (0)