|
| 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 | +packagecom.esp8266.mkspiffs; |
| 25 | + |
| 26 | +importjava.io.File; |
| 27 | +importjava.io.BufferedReader; |
| 28 | +importjava.io.InputStreamReader; |
| 29 | + |
| 30 | +importjava.text.SimpleDateFormat; |
| 31 | +importjava.util.Date; |
| 32 | + |
| 33 | +importjavax.swing.JOptionPane; |
| 34 | + |
| 35 | +importprocessing.app.PreferencesData; |
| 36 | +importprocessing.app.Editor; |
| 37 | +importprocessing.app.Base; |
| 38 | +importprocessing.app.Platform; |
| 39 | +importprocessing.app.Sketch; |
| 40 | +importprocessing.app.tools.Tool; |
| 41 | +importprocessing.app.helpers.ProcessUtils; |
| 42 | +importprocessing.app.debug.TargetPlatform; |
| 43 | + |
| 44 | + |
| 45 | +/** |
| 46 | + * Example Tools menu entry. |
| 47 | + */ |
| 48 | +publicclassESP8266FSimplementsTool { |
| 49 | +Editoreditor; |
| 50 | + |
| 51 | + |
| 52 | +publicvoidinit(Editoreditor) { |
| 53 | +this.editor =editor; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +publicStringgetMenuTitle() { |
| 58 | +return"ESP8266 Sketch Data Upload"; |
| 59 | + } |
| 60 | + |
| 61 | +privateintlistenOnProcess(String[]arguments){ |
| 62 | +try { |
| 63 | +finalProcessp =ProcessUtils.exec(arguments); |
| 64 | +Threadthread =newThread() { |
| 65 | +publicvoidrun() { |
| 66 | +try { |
| 67 | +Stringline; |
| 68 | +BufferedReaderinput =newBufferedReader (newInputStreamReader(p.getInputStream())); |
| 69 | +while ((line =input.readLine()) !=null)System.out.println(line); |
| 70 | +input.close(); |
| 71 | + }catch (Exceptione){} |
| 72 | + } |
| 73 | + }; |
| 74 | +thread.start(); |
| 75 | +intres =p.waitFor(); |
| 76 | +thread.join(); |
| 77 | +returnres; |
| 78 | + }catch (Exceptione){ |
| 79 | +return -1; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +privatevoidsysExec(finalString[]arguments){ |
| 84 | +Threadthread =newThread() { |
| 85 | +publicvoidrun() { |
| 86 | +try { |
| 87 | +if(listenOnProcess(arguments) !=0){ |
| 88 | +editor.statusError("SPIFFS Upload failed!"); |
| 89 | + }else { |
| 90 | +editor.statusNotice("SPIFFS Image Uploaded"); |
| 91 | + } |
| 92 | + }catch (Exceptione){ |
| 93 | +editor.statusError("SPIFFS Upload failed!"); |
| 94 | + } |
| 95 | + } |
| 96 | + }; |
| 97 | +thread.start(); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +privatelonggetIntPref(Stringname){ |
| 102 | +Stringdata =Base.getBoardPreferences().get(name); |
| 103 | +if(data ==null ||data.contentEquals(""))return0; |
| 104 | +if(data.startsWith("0x"))returnLong.parseLong(data.substring(2),16); |
| 105 | +elsereturnInteger.parseInt(data); |
| 106 | + } |
| 107 | + |
| 108 | +privatevoidcreateAndUpload(){ |
| 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 | +longspiStart,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(Exceptione){ |
| 129 | +editor.statusError(e); |
| 130 | +return; |
| 131 | + } |
| 132 | + |
| 133 | +TargetPlatformplatform =Base.getTargetPlatform(); |
| 134 | + |
| 135 | +Fileesptool; |
| 136 | +if(!PreferencesData.get("runtime.os").contentEquals("windows"))esptool =newFile(platform.getFolder()+"/tools","esptool"); |
| 137 | +elseesptool =newFile(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 | +Filetool; |
| 145 | +if(!PreferencesData.get("runtime.os").contentEquals("windows"))tool =newFile(platform.getFolder()+"/tools","mkspiffs"); |
| 146 | +elsetool =newFile(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 | +intfileCount =0; |
| 154 | +FiledataFolder =editor.getSketch().prepareDataFolder(); |
| 155 | +if(dataFolder.exists() &&dataFolder.isDirectory()){ |
| 156 | +File[]files =dataFolder.listFiles(); |
| 157 | +if(files.length >0){ |
| 158 | +for(Filefile :files){ |
| 159 | +if(!file.isDirectory() &&file.isFile() && !file.getName().startsWith("."))fileCount++; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +StringdataPath =dataFolder.getAbsolutePath(); |
| 165 | +StringtoolPath =tool.getAbsolutePath(); |
| 166 | +StringesptoolPath =esptool.getAbsolutePath(); |
| 167 | +StringsketchName =editor.getSketch().getName(); |
| 168 | +StringbuildPath =Base.getBuildFolder().getAbsolutePath(); |
| 169 | +StringimagePath =buildPath+"/"+sketchName+".spiffs.bin"; |
| 170 | +StringserialPort =PreferencesData.get("serial.port"); |
| 171 | +StringresetMethod =Base.getBoardPreferences().get("upload.resetmethod"); |
| 172 | +StringuploadSpeed =Base.getBoardPreferences().get("upload.speed"); |
| 173 | +StringuploadAddress =Base.getBoardPreferences().get("build.spiffs_start"); |
| 174 | + |
| 175 | +Object[]options = {"Yes","No" }; |
| 176 | +Stringtitle ="SPIFFS Create"; |
| 177 | +Stringmessage ="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(newString[]{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 (Exceptione){ |
| 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(newString[]{esptoolPath,"-cd",resetMethod,"-cb",uploadSpeed,"-cp",serialPort,"-ca",uploadAddress,"-cf",imagePath}); |
| 212 | + } |
| 213 | + |
| 214 | +publicvoidrun() { |
| 215 | +createAndUpload(); |
| 216 | + } |
| 217 | +} |