|
| 1 | +/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 | + |
| 3 | +/* |
| 4 | + Tool to put the contents of the sketch's "data" subfolder |
| 5 | + into an SPIFFS partition image and upload it to an ESP8266 MCU |
| 6 | +
|
| 7 | + Copyright (c) 2015 Hristo Gochkov (ficeto at ficeto dot com) |
| 8 | +
|
| 9 | + This program is free software; you can redistribute it and/or modify |
| 10 | + it under the terms of the GNU General Public License as published by |
| 11 | + the Free Software Foundation; either version 2 of the License, or |
| 12 | + (at your option) any later version. |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + GNU General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU General Public License |
| 20 | + along with this program; if not, write to the Free Software Foundation, |
| 21 | + Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | +*/ |
| 23 | + |
| 24 | +package com.esp8266.mkspiffs; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.InputStreamReader; |
| 29 | + |
| 30 | +import java.text.SimpleDateFormat; |
| 31 | +import java.util.Date; |
| 32 | + |
| 33 | +import javax.swing.JOptionPane; |
| 34 | + |
| 35 | +import processing.app.PreferencesData; |
| 36 | +import processing.app.Editor; |
| 37 | +import processing.app.Base; |
| 38 | +import processing.app.Platform; |
| 39 | +import processing.app.Sketch; |
| 40 | +import processing.app.tools.Tool; |
| 41 | +import processing.app.helpers.ProcessUtils; |
| 42 | +import processing.app.debug.TargetPlatform; |
| 43 | + |
| 44 | + |
| 45 | +/** |
| 46 | + * Example Tools menu entry. |
| 47 | + */ |
| 48 | +public class ESP8266FS implements Tool { |
| 49 | + Editor editor; |
| 50 | + |
| 51 | + |
| 52 | + public void init(Editor editor) { |
| 53 | + this.editor = editor; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + public String getMenuTitle() { |
| 58 | + return "ESP8266 Sketch Data Upload"; |
| 59 | + } |
| 60 | + |
| 61 | + private int listenOnProcess(String[] arguments){ |
| 62 | + try { |
| 63 | + final Process p = ProcessUtils.exec(arguments); |
| 64 | + Thread thread = new Thread() { |
| 65 | + public void run() { |
| 66 | + try { |
| 67 | + String line; |
| 68 | + BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream())); |
| 69 | + while ((line = input.readLine()) != null) System.out.println(line); |
| 70 | + input.close(); |
| 71 | + } catch (Exception e){} |
| 72 | + } |
| 73 | + }; |
| 74 | + thread.start(); |
| 75 | + int res = p.waitFor(); |
| 76 | + thread.join(); |
| 77 | + return res; |
| 78 | + } catch (Exception e){ |
| 79 | + return -1; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void sysExec(final String[] arguments){ |
| 84 | + Thread thread = new Thread() { |
| 85 | + public void run() { |
| 86 | + try { |
| 87 | + if(listenOnProcess(arguments) != 0){ |
| 88 | + editor.statusError("SPIFFS Upload failed!"); |
| 89 | + } else { |
| 90 | + editor.statusNotice("SPIFFS Image Uploaded"); |
| 91 | + } |
| 92 | + } catch (Exception e){ |
| 93 | + editor.statusError("SPIFFS Upload failed!"); |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | + thread.start(); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private long getIntPref(String name){ |
| 102 | + String data = Base.getBoardPreferences().get(name); |
| 103 | + if(data == null || data.contentEquals("")) return 0; |
| 104 | + if(data.startsWith("0x")) return Long.parseLong(data.substring(2), 16); |
| 105 | + else return Integer.parseInt(data); |
| 106 | + } |
| 107 | + |
| 108 | + private void createAndUpload(){ |
| 109 | + if(!PreferencesData.get("target_platform").contentEquals("esp8266")){ |
| 110 | + System.err.println(); |
| 111 | + editor.statusError("SPIFFS Not Supported on "+PreferencesData.get("target_platform")); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if(!Base.getBoardPreferences().containsKey("build.spiffs_start") || !Base.getBoardPreferences().containsKey("build.spiffs_end")){ |
| 116 | + System.err.println(); |
| 117 | + editor.statusError("SPIFFS Not Defined for "+Base.getBoardPreferences().get("name")); |
| 118 | + return; |
| 119 | + } |
| 120 | + long spiStart, spiEnd, spiPage, spiBlock; |
| 121 | + try { |
| 122 | + spiStart = getIntPref("build.spiffs_start"); |
| 123 | + spiEnd = getIntPref("build.spiffs_end"); |
| 124 | + spiPage = getIntPref("build.spiffs_pagesize"); |
| 125 | + if(spiPage == 0) spiPage = 256; |
| 126 | + spiBlock = getIntPref("build.spiffs_blocksize"); |
| 127 | + if(spiBlock == 0) spiBlock = 4096; |
| 128 | + } catch(Exception e){ |
| 129 | + editor.statusError(e); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + TargetPlatform platform = Base.getTargetPlatform(); |
| 134 | + |
| 135 | + File esptool; |
| 136 | + if(!PreferencesData.get("runtime.os").contentEquals("windows")) esptool = new File(platform.getFolder()+"/tools", "esptool"); |
| 137 | + else esptool = new File(platform.getFolder()+"/tools", "esptool.exe"); |
| 138 | + if(!esptool.exists()){ |
| 139 | + System.err.println(); |
| 140 | + editor.statusError("SPIFFS Error: esptool not found!"); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + File tool; |
| 145 | + if(!PreferencesData.get("runtime.os").contentEquals("windows")) tool = new File(platform.getFolder()+"/tools", "mkspiffs"); |
| 146 | + else tool = new File(platform.getFolder()+"/tools", "mkspiffs.exe"); |
| 147 | + if(!tool.exists()){ |
| 148 | + System.err.println(); |
| 149 | + editor.statusError("SPIFFS Error: mkspiffs not found!"); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + int fileCount = 0; |
| 154 | + File dataFolder = editor.getSketch().prepareDataFolder(); |
| 155 | + if(dataFolder.exists() && dataFolder.isDirectory()){ |
| 156 | + File[] files = dataFolder.listFiles(); |
| 157 | + if(files.length > 0){ |
| 158 | + for(File file : files){ |
| 159 | + if(!file.isDirectory() && file.isFile() && !file.getName().startsWith(".")) fileCount++; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + String dataPath = dataFolder.getAbsolutePath(); |
| 165 | + String toolPath = tool.getAbsolutePath(); |
| 166 | + String esptoolPath = esptool.getAbsolutePath(); |
| 167 | + String sketchName = editor.getSketch().getName(); |
| 168 | + String buildPath = Base.getBuildFolder().getAbsolutePath(); |
| 169 | + String imagePath = buildPath+"/"+sketchName+".spiffs.bin"; |
| 170 | + String serialPort = PreferencesData.get("serial.port"); |
| 171 | + String resetMethod = Base.getBoardPreferences().get("upload.resetmethod"); |
| 172 | + String uploadSpeed = Base.getBoardPreferences().get("upload.speed"); |
| 173 | + String uploadAddress = Base.getBoardPreferences().get("build.spiffs_start"); |
| 174 | + |
| 175 | + Object[] options = { "Yes", "No" }; |
| 176 | + String title = "SPIFFS Create"; |
| 177 | + String message = "No files have been found in your data folder!\nAre you sure you want to create an empty SPIFFS image?"; |
| 178 | + |
| 179 | + if(fileCount == 0 && JOptionPane.showOptionDialog(editor, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]) != JOptionPane.YES_OPTION){ |
| 180 | + System.err.println(); |
| 181 | + editor.statusError("SPIFFS Warning: mkspiffs canceled!"); |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + editor.statusNotice("SPIFFS Creating Image..."); |
| 186 | + System.out.println("[SPIFFS] data : "+dataPath); |
| 187 | + System.out.println("[SPIFFS] size : "+((spiEnd - spiStart)/1024)); |
| 188 | + System.out.println("[SPIFFS] page : "+spiPage); |
| 189 | + System.out.println("[SPIFFS] block : "+spiBlock); |
| 190 | + |
| 191 | + try { |
| 192 | + if(listenOnProcess(new String[]{toolPath, "-c", dataPath, "-p", spiPage+"", "-b", spiBlock+"", "-s", (spiEnd - spiStart)+"", imagePath}) != 0){ |
| 193 | + System.err.println(); |
| 194 | + editor.statusError("SPIFFS Create Failed!"); |
| 195 | + return; |
| 196 | + } |
| 197 | + } catch (Exception e){ |
| 198 | + editor.statusError(e); |
| 199 | + editor.statusError("SPIFFS Create Failed!"); |
| 200 | + return; |
| 201 | + } |
| 202 | + |
| 203 | + editor.statusNotice("SPIFFS Uploading Image..."); |
| 204 | + System.out.println("[SPIFFS] upload : "+imagePath); |
| 205 | + System.out.println("[SPIFFS] reset : "+resetMethod); |
| 206 | + System.out.println("[SPIFFS] port : "+serialPort); |
| 207 | + System.out.println("[SPIFFS] speed : "+uploadSpeed); |
| 208 | + System.out.println("[SPIFFS] address: "+uploadAddress); |
| 209 | + System.out.println(); |
| 210 | + |
| 211 | + sysExec(new String[]{esptoolPath, "-cd", resetMethod, "-cb", uploadSpeed, "-cp", serialPort, "-ca", uploadAddress, "-cf", imagePath}); |
| 212 | + } |
| 213 | + |
| 214 | + public void run() { |
| 215 | + createAndUpload(); |
| 216 | + } |
| 217 | +} |
0 commit comments