-
Notifications
You must be signed in to change notification settings - Fork 11
/
esp8266fs.js
1304 lines (953 loc) · 39.5 KB
/
esp8266fs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
//------------------------------------------------------------------------------
Object.defineProperty(exports, "__esModule", { value: true });
//------------------------------------------------------------------------------
const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const process = require("process");
const tmp = require("tmp");
const vscode = require("vscode");
const WinReg = require("winreg");
//==============================================================================
// #region Constants
// --- VSCode Arduino Extension ---
const ARDUINO_CONFIG_FILE = path.join(".vscode", "arduino.json");
const PYTHON_PYTHONPATH = "python.pythonPath"; // Python Executable
// --- new items ---
const ESP8266FS_DATA_FILES = "esp8266fs.dataFiles"; // Location of SPIFFS files
const ESP8266FS_PREFERENCES = "esp8266fs.preferencesPath"; // Location of Arduino Preferences and Packages
const ESP8266FS_ARDUINO_USER_PATH = "esp8266fs.arduinoUserPath"; // Location of Ardiuno User Libraries
const ESP8266FS_SPIFFS_IMAGE = "esp8266fs.spiffsImage"; // Packed SPIFFS file
const ESP8266FS_LOGLEVEL = "esp8266fs.logLevel"; // Level of spew generated by this extension
const ESP8266FS_MKSPIFFS_EXECUTABLE = "esp8266fs.mkspiffs.executable"; // MKSPIFFS Executable
const ESP8266FS_MKSPIFFS_DEBUG_LEVEL = "esp8266fs.mkspiffs.debugLevel"; // Value passed to MKSPIFFS Executable
const ESP8266FS_MKSPIFFS_ALL_FILES = "esp8266fs.mkspiffs.allFiles"; // Value passed to MKSPIFFS Executable
const ESP8266FS_ESPTOOL_EXECUTABLE = "esp8266fs.esptool.executable"; // ESPTOOL Executable
const ESP8266FS_ESPTOOL_VERBOSITY = "esp8266fs.esptool.verbosity"; // Value passed to ESPTOOL Executable
const ESP8266FS_ESPTOOL_PY_BEFORE = "esp8266fs.esptool.before"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPTOOL_PY_AFTER = "esp8266fs.esptool.after"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPTOOL_PY_NO_STUB = "esp8266fs.esptool.no_stub"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPTOOL_PY_TRACE = "esp8266fs.esptool.trace"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPTOOL_PY_SPI = "esp8266fs.esptool.spi_connection"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPTOOL_PY_COMPRESS = "esp8266fs.esptool.compress"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPTOOL_PY_VERIFY = "esp8266fs.esptool.verify"; // Value passed to ESPTOOL.PY script
const ESP8266FS_ESPOTA_PY = "esp8266fs.espota.py"; // EspOTA Python script
const ESP8266FS_ESPOTA_ESP_PORT = "esp8266fs.espota.esp.port"; // IP Port for ESP8266
const ESP8266FS_ESPOTA_HOST_IP = "esp8266fs.espota.host.ip"; // IP Address for Host
const ESP8266FS_ESPOTA_HOST_PORT = "esp8266fs.espota.host.port"; // IP Port for Host
const ESP8266FS_ESPOTA_AUTH = "esp8266fs.espota.auth"; // Authentication password for espota.py
const ESP8266FS_ESPOTA_DEBUG = "esp8266fs.espota.debug"; // Enable debug output from espota.py
// #endregion
//==============================================================================
// #region Logging
const RESET = "\u001b[0m";
const BOLD = "\u001b[1m";
const RED = "\u001b[31m";
const GREEN = "\u001b[32m";
const YELLOW = "\u001b[33m";
const BLUE = "\u001b[34m";
const MAGENTA = "\u001b[35m";
const CYAN = "\u001b[36m";
const BOLD_RED = "\u001b[31;1m";
//------------------------------------------------------------------------------
let outputChannel = null;
let logLevel = "normal"; // "normal", "verbose", "silent", "debug"
function log(message, color)
{
if (logLevel === "silent")
return;
if (color)
console.log(`${color}${message}${RESET}`);
else
console.log(message);
outputChannel.appendLine(message.replace(/\x1b\[[\d|\;]{1,4}m/g, ""));
outputChannel.show();
}
//------------------------------------------------------------------------------
function logAnnounce(message) { log(message, GREEN); }
function logUrgent(message) { log(message, BOLD_RED); }
function logImportant(message) { log(message, RED); }
function logCommand(message) { log(message, YELLOW); }
function logSpiffs(message) { log(` [SPIFFS] ${message}`, BLUE); }
//------------------------------------------------------------------------------
function logVerbose(message)
{
if (logLevel === "verbose" || logLevel === "debug")
log(message, MAGENTA);
}
//------------------------------------------------------------------------------
function logDebug(message)
{
if (logLevel === "debug")
log(message, CYAN);
}
//------------------------------------------------------------------------------
function showErrorMessage(message)
{
const dismiss = { isCloseAffordance: true, title: "Dismiss" };
vscode.window.showErrorMessage(message, dismiss);
}
//------------------------------------------------------------------------------
function showWarningMessage(message)
{
vscode.window.showWarningMessage(message);
}
//------------------------------------------------------------------------------
function showInformationMessage(message)
{
vscode.window.showInformationMessage(message);
}
// #endregion
//==============================================================================
// #region Helper functions
function stringToInt(value)
{
return parseInt(value, value.match(/^0x/i) ? 16 : 10);
}
//------------------------------------------------------------------------------
function toHex(decimal, width = 4)
{
return ("0".repeat(width) + (Number(decimal).toString(16))).slice(-width).toUpperCase();
}
//------------------------------------------------------------------------------
function makeOsPath(dir)
{
dir = dir.replace(/\\/g, "/");
if (dir.indexOf(" ") != -1)
dir = `"${dir}"`;
return dir;
}
// #endregion
//==============================================================================
// #region Utility functions
function getVscodeConfigValue(key) {
return vscode.workspace.getConfiguration().get(key);
}
//------------------------------------------------------------------------------
function getOS() { return os.platform(); }
//------------------------------------------------------------------------------
function program(name) {
return (getOS() === "win32" && name.indexOf(".") == -1)
? (name + ".exe")
: name;
}
//------------------------------------------------------------------------------
function runCommand(command, args) {
logVerbose("Running: " + command + " " + args.join(" "));
const spawn = childProcess.spawnSync(command, args, { encoding: "utf8" });
if (spawn.error)
throw spawn.error;
spawn.stdout
.toString()
.replace(/\r\n/, "\n")
.split("\n")
.forEach(line => logCommand(line.trimRight()));
spawn.stderr
.toString()
.replace(/\r\n/, "\n")
.split("\n")
.forEach(line => logUrgent(line.trimRight()));
if (spawn.status)
throw `${command} returned ${spawn.status}`;
return spawn.stdout.toString();
}
//-------------------------------------------------------------------------------
function getTempPath() {
const temp = tmp.dirSync();
logDebug(`System tmp path: "${temp}"`);
return temp;
}
//------------------------------------------------------------------------------
function dirExists(dir) {
try {
return fs.statSync(dir).isDirectory();
}
catch (e) {
return false;
}
}
//------------------------------------------------------------------------------
function getFolders(dir) {
return fs.readdirSync(dir);
}
//------------------------------------------------------------------------------
function fileExists(file) {
try {
return fs.statSync(file).isFile();
}
catch (e) {
return false;
}
}
//------------------------------------------------------------------------------
function readFile(name) {
return fs.readFileSync(name, "utf8");
}
//------------------------------------------------------------------------------
function readLines(name) {
return readFile(name).split(/[\r\n|\r|\n]/);
}
//------------------------------------------------------------------------------
function JSONify(obj) {
return JSON.stringify(obj, null, " ");
}
//------------------------------------------------------------------------------
function getRegistryValue(hive, key, name) {
return new Promise((resolve, reject) => {
try {
const regKey = new WinReg({
hive,
key,
});
regKey.valueExists(name, (e, exists) => {
if (e) {
reject(e);
}
if (exists) {
regKey.get(name, (err, result) => {
if (!err) {
resolve(result ? result.value : "");
} else {
reject(err);
}
});
} else {
resolve("");
}
});
} catch (error) {
reject(error);
}
});
}
// #endregion
//==============================================================================
// #region ESP8266FS Specific code
//==============================================================================
function getPreferencesPath() {
let dir = getVscodeConfigValue(ESP8266FS_PREFERENCES);
if (!dir) {
switch (getOS()) {
case "win32":
dir = path.join(process.env.LOCALAPPDATA, "Arduino15");
break;
case "linux":
dir = path.join(process.env.HOME, ".arduino15");
break;
case "darwin":
dir = path.join(process.env.HOME, "Library/Arduino15");
break;
}
}
if (!dir)
throw `Can't find preferences path.`;
dir = path.resolve(dir);
if (!dirExists(dir))
throw `Preferences path "${dir}" doesn't exist.`;
logVerbose(`Preferences Path: "${dir}"`);
return dir;
}
//-------------------------------------------------------------------------------
function getArduinoUserPath() {
let dir = getVscodeConfigValue(ESP8266FS_ARDUINO_USER_PATH);
if (!dir) {
switch (getOS()) {
case "win32":
dir = path.join(process.env.USERPROFILE, "Documents", "Arduino");
break;
case "linux":
dir = path.join(process.env.HOME, "Arduino");
break;
case "darwin":
dir = path.join(process.env.HOME, "Documents", "Arduino");
break;
}
}
if (!dir)
throw `Can't find arduino user path.`;
dir = path.resolve(dir);
if (!dirExists(dir))
throw `Preferences path "${dir}" doesn't exist.`;
logVerbose(`Arduino User Path: "${dir}"`);
return dir;
}
//-------------------------------------------------------------------------------
function getDataFilesPath(arduinoJson) {
let dir = getVscodeConfigValue(ESP8266FS_DATA_FILES) || "./data";
if (dir.startsWith("."))
dir = path.join(vscode.workspace.rootPath, dir);
dir = path.resolve(dir);
if (!dirExists(dir))
throw `ESP8266 Data Files path "${dir}" not found.`;
logVerbose(`ESP8266 Data Files path: "${dir}"`);
return dir;
}
//-------------------------------------------------------------------------------
function getSpiffsImage() {
let file = getVscodeConfigValue(ESP8266FS_SPIFFS_IMAGE)
||path.join(getTempPath(), "./spiffs.bin");
if (file.startsWith("."))
file = path.join(vscode.workspace.rootPath, file);
file = path.resolve(file);
logVerbose(`SPIFFS Image: "${file}"`);
return file;
}
//-------------------------------------------------------------------------------
async function getArduinoPreferences(preferencesPath) {
const preferences = {};
const file = path.join(preferencesPath, "preferences.txt");
logVerbose(`Reading preferences from "${file}"`);
readLines(file)
.forEach(line => {
if (line.startsWith("#") || line.length == 0)
return;
const pair = line.split("=");
logDebug(` "${pair[0]}"="${pair[1]}"`);
preferences[pair[0]] = pair[1];
}
);
return preferences;
}
//-------------------------------------------------------------------------------
async function getArduinoJson() {
var json = JSON.parse(readFile(path.join(vscode.workspace.rootPath, ARDUINO_CONFIG_FILE)));
// Split the configuration settings into key/values
if (json.configuration) {
json.configuration.split(",").forEach(config => {
let param = config.split("=");
json[param[0]] = param[1];
});
}
logDebug(`arduinoJson:`);
JSONify(json).split("\n").map(line => logDebug(line));
return json;
}
//-------------------------------------------------------------------------------
function _getTarget(arduinJson, preferences) {
if (!arduinJson.board) {
const target =
{
package: preferences["target_package"],
architecture: preferences["target_platform"],
board: preferences["board"]
};
return target;
}
const values = arduinJson.board.split(":");
const target =
{
package: values[0],
architecture: values[1],
board: values[2]
};
return target;
}
//------------------------------------------------------------------------------
function getPreference(arduinoJson, preferences, index) {
if (arduinoJson.hasOwnProperty(index))
return arduinoJson[index];
const value = preferences["custom_" + index];
if (!value)
throw `Can't determine ${index}.`;
const match = value.match(/^(${target.board}|generic)_(\S+)/);
return match ? match[2] : "";
}
//------------------------------------------------------------------------------
function getTarget(arduinoJson, preferences) {
const target = _getTarget(arduinoJson, preferences);
if (!["esp8266", "esp32"].includes(target.architecture))
throw `Current Arduino package/architecture is not ESP8266 or ESP32.`;
target.flashSize = getPreference(arduinoJson, preferences, "FlashSize");
target.flashMode = getPreference(arduinoJson, preferences, "FlashMode");
target.flashFreq = getPreference(arduinoJson, preferences, "FlashFreq");
logDebug(`target:`);
JSONify(target).split("\n").map(line => logDebug(line));
return target;
}
//------------------------------------------------------------------------------
function getEspPackagePath(arduinoUserPath, preferencesPath, target) {
switch (target.architecture) {
case "esp8266": {
const dir = path.join(preferencesPath, "packages", target.package, "hardware", target.architecture);
if (!dirExists(dir))
throw `ESP8266 has not been installed with the Arduino Board Manager.`;
const folders = getFolders(dir);
if (folders.length != 1)
throw `There should only be one ESP8266 Package installed with the Arduino Board Manager.`;
const esp8266Path = path.join(dir, folders[0]);
logImportant(`Found ESP8266 packages: ${esp8266Path}`);
return esp8266Path;
}
case "esp32": {
const esp32Path = path.join(arduinoUserPath, "hardware", target.package, target.architecture);
if (!dirExists(esp32Path))
throw `ESP32 has not been installed correctly - see https://github.com/espressif/arduino-esp32.`;
logImportant(`Found ESP32 packages: ${esp32Path}`);
return esp32Path;
}
}
}
//------------------------------------------------------------------------------
function getSpiffsPartition(packagesPath, partition) {
var data = {};
readLines(path.join(packagesPath, "tools", "partitions", partition + ".csv"))
.forEach(line => {
const values = line.split(",");
data[values[0]] = {
"type": (values[1] || "").trim(),
"subType": (values[2] || "").trim(),
"offset": (values[3] || "").trim(),
"size": (values[4] || "").trim(),
"flags": (values[5] || "").trim()
};
}
);
return data;
}
//------------------------------------------------------------------------------
function getSpiffsOptions(packagesPath, target, arduinoJson, preferences) {
const spiffsOptions = {};
readLines(path.join(packagesPath, "boards.txt"))
.forEach(line => {
const match = line.match(`${target.board}\\.(?:build|upload)\\.(\\S+)=(\\S+)`)
|| line.match(`${target.board}\\.menu\\.FlashSize\\.${target.flashSize}\\.(?:build|upload)\\.(\\S+)=(\\S+)`)
|| line.match(`${target.board}\\.menu\\.PartitionScheme\\.${arduinoJson.PartitionScheme}\\.(?:build|upload)\\.(\\S+)=(\\S+)`);
if (match)
spiffsOptions[match[1]] = match[2];
}
);
switch (target.architecture) {
case "esp8266": {
if (!spiffsOptions.spiffs_start)
throw `Missing "spiffs_start" definition: target = ${target.architecture}, config = ${target.memoryConfig}.`;
if (!spiffsOptions.spiffs_end)
throw `Missing "spiffs_end" definition: target = ${target.architecture}, config = ${target.memoryConfig}.`;
spiffsOptions.dataSize = (stringToInt(spiffsOptions.spiffs_end) - stringToInt(spiffsOptions.spiffs_start)).toString();
}
break;
case "esp32": {
if (!spiffsOptions.partitions)
throw `Missing "partitions" definition: target = ${target}, config = ${arduinoJson.PartitionScheme}.`;
const partition = getSpiffsPartition(packagesPath, spiffsOptions.partitions);
spiffsOptions.spiffs_start = partition.spiffs.offset;
spiffsOptions.dataSize = partition.spiffs.size;
}
break;
}
spiffsOptions.flashMode = preferences.flash_mode;
spiffsOptions.flashFreq = preferences.flash_freq;
spiffsOptions.flashSize = "0x" + toHex(stringToInt(spiffsOptions.spiffs_start) + stringToInt(spiffsOptions.dataSize));
if (arduinoJson.UploadSpeed)
spiffsOptions.speed = arduinoJson.UploadSpeed;
if (arduinoJson.ResetMethod)
spiffsOptions.resetmethod = arduinoJson.ResetMethod;
logDebug(`spiffs:`);
JSONify(spiffsOptions).split("\n").map(line => logDebug(line));
return spiffsOptions;
}
//------------------------------------------------------------------------------
function getEspToolsPath(arduinoUserPath, preferencesPath, target) {
const dir = target.architecture == "esp8266"
? path.resolve(path.join(preferencesPath, "packages", target.architecture, "tools"))
: path.resolve(path.join(arduinoUserPath, "hardware", target.package, target.architecture, "tools"));
if (!dirExists(dir))
throw `Can't find tools path.`;
logVerbose(`Tools Path: "${dir}"`);
return dir;
}
//------------------------------------------------------------------------------
function getPythonExecutable() {
const python = getVscodeConfigValue(PYTHON_PYTHONPATH) || "python";
logVerbose(`Python Executable: "${python}"`);
return python;
}
//------------------------------------------------------------------------------
function getEspotaPy(packagePath) {
const file = getVscodeConfigValue(ESP8266FS_ESPOTA_PY) || path.join(packagePath, "tools", "espota.py");
if (!fileExists(file))
throw `Can't find ${file}.`;
logVerbose(`espota.py: ${CYAN}${file}`);
return file;
}
//------------------------------------------------------------------------------
function getPort(arduinoJson, preferences) {
let port = arduinoJson.port || preferences["serial.port"];
logVerbose(`Output Port: ${port}`);
return port;
}
//------------------------------------------------------------------------------
function isIP(port) {
return port.match(/^(\d+)\.(\d+).(\d+).(\d+)(:\d+)?$/);
}
// #endregion
//==============================================================================
// #region MKSPIFFS
function getMkSpiffs(target, espToolsPath) {
const configFile = getVscodeConfigValue(ESP8266FS_MKSPIFFS_EXECUTABLE);
if (configFile) {
if (!fileExists(configFile))
throw `Can't locate ${configFile}.`;
logVerbose(`mkspiffs: ${CYAN}${configFile}`);
logImportant(`Found "mkspiffs" via VSCode Configuration`);
return configFile;
}
switch (target.architecture) {
case "esp8266": {
const folders = getFolders(path.join(espToolsPath, "mkspiffs"));
if (folders.length != 1)
throw `"${target.architecture}" not installed correctly through Arduino Board Manager`;
const mkspiffs = path.join(espToolsPath, "mkspiffs", folders[0], program("mkspiffs"));
if (!fileExists(mkspiffs))
throw `"Can't locate "${mkspiffs}"`;
return mkspiffs;
}
case "esp32": {
const mkspiffs = path.join(espToolsPath, "mkspiffs", program("mkspiffs"));
if (!fileExists(mkspiffs))
throw `"Can't locate "${mkspiffs}"`;
return mkspiffs;
}
}
}
//------------------------------------------------------------------------------
function makeMkspiffsArgs(args) {
const allFiles = getVscodeConfigValue(ESP8266FS_MKSPIFFS_ALL_FILES);
if (allFiles)
args.unshift("--all-files", allFiles);
const debug = getVscodeConfigValue(ESP8266FS_MKSPIFFS_DEBUG_LEVEL);
if (debug)
args.unshift("--debug", debug);
return args;
}
//------------------------------------------------------------------------------
function packSpiffs(mkspiffs, dataPath, spiffsOptions, spiffsImage) {
log(`--- Packing SPIFFS file ---`);
const dataSize = spiffsOptions.dataSize;
const dataSizeInK = (dataSize >> 10) + 1;
const spiPage = stringToInt(spiffsOptions.spiffs_pagesize || "256");
const spiBlock = stringToInt(spiffsOptions.spiffs_blocksize || "4096");
logImportant(`SPIFFS Creating Image... (${spiffsImage})`);
logSpiffs(`program: ${mkspiffs}`);
logSpiffs(`data : ${dataPath}`);
logSpiffs(`size : ${dataSizeInK}K`);
logSpiffs(`page : ${spiPage}`);
logSpiffs(`block : ${spiBlock}`);
runCommand(
makeOsPath(mkspiffs),
makeMkspiffsArgs([
"--create", makeOsPath(dataPath),
"--size", dataSize,
"--page", spiPage,
"--block", spiBlock,
makeOsPath(spiffsImage)
])
);
}
//------------------------------------------------------------------------------
function unpackSpiffs(mkspiffs, dataPath, spiffsOptions, spiffsImage) {
log(`--- Unpacking SPIFFS file ---`);
const dataSize = spiffsOptions.dataSize;
const dataSizeInK = (dataSize >> 10) + 1;
const spiPage = stringToInt(spiffsOptions.spiffs_pagesize || "256");
const spiBlock = stringToInt(spiffsOptions.spiffs_blocksize || "4096");
logImportant(`SPIFFS Unpacking Image... (${spiffsImage})`);
logSpiffs(`program: ${mkspiffs}`);
logSpiffs(`data : ${dataPath}`);
logSpiffs(`size : ${dataSizeInK}K`);
logSpiffs(`page : ${spiPage}`);
logSpiffs(`block : ${spiBlock}`);
runCommand(
makeOsPath(mkspiffs),
makeMkspiffsArgs([
"--unpack", makeOsPath(dataPath),
"--size", dataSize,
"--page", spiPage,
"--block", spiBlock,
makeOsPath(spiffsImage)
])
);
}
//------------------------------------------------------------------------------
function listSpiffs(mkspiffs, spiffsOptions, spiffsImage) {
log(`--- List SPIFFS file ---`);
const spiPage = stringToInt(spiffsOptions.spiffs_pagesize || "256");
const spiBlock = stringToInt(spiffsOptions.spiffs_blocksize || "4096");
logImportant(`SPIFFS List Files... (${spiffsImage})`);
logSpiffs(`program: ${mkspiffs}`);
logSpiffs(`page : ${spiPage}`);
logSpiffs(`block : ${spiBlock}`);
runCommand(
makeOsPath(mkspiffs),
makeMkspiffsArgs([
"--list",
"--page", spiPage,
"--block", spiBlock,
makeOsPath(spiffsImage)
])
);
}
//------------------------------------------------------------------------------
function visualizeSpiffs(mkspiffs, spiffsOptions, spiffsImage) {
log(`--- Visualize SPIFFS file ---`);
const spiPage = stringToInt(spiffsOptions.spiffs_pagesize || "256");
const spiBlock = stringToInt(spiffsOptions.spiffs_blocksize || "4096");
logImportant(`SPIFFS Visualize Files... (${spiffsImage})`);
logSpiffs(`program: ${mkspiffs}`);
logSpiffs(`page : ${spiPage}`);
logSpiffs(`block : ${spiBlock}`);
runCommand(
makeOsPath(mkspiffs),
makeMkspiffsArgs([
"--visualize",
"--page", spiPage,
"--block", spiBlock,
makeOsPath(spiffsImage)
])
);
}
// #endregion
//==============================================================================
// #region ESPTOOL
function getEspTool(target, espToolsPath) {
const configFile = getVscodeConfigValue(ESP8266FS_ESPTOOL_EXECUTABLE);
if (configFile) {
if (!fileExists(configFile))
throw `Can't locate ${configFile}.`;
logVerbose(`esptool: ${CYAN}${configFile}`);
logImportant(`Found "esptool" via VSCode Configuration`);
return configFile;
}
switch (target.architecture) {
case "esp8266": {
const folders = getFolders(path.join(espToolsPath, "esptool"));
if (folders.length != 1)
throw `"${target.architecture}" not installed correctly through Arduino Board Manager`;
const version = folders[0];
const esptool = path.join(espToolsPath, "esptool", version, program("esptool"));
if (!fileExists(esptool))
throw `"Can't locate "${esptool}"`;
logVerbose(`esptool (${version}): ${CYAN}${esptool}`);
return esptool;
}
case "esp32": {
const esptoolPy = path.join(espToolsPath, program("esptool.py"));
if (!fileExists(esptoolPy))
throw `"Can't locate "${esptoolPy}"`;
logVerbose(`esptool: ${CYAN}${esptoolPy}`);
return esptoolPy;
}
}
}
//------------------------------------------------------------------------------
// -ca <address>
// -cd <resetMethod>
// -cp <port>
// -cb <speed>
// -vvv
function _uploadSpiffsEspTool(esptool, commPort, spiffsImage, spiffsOptions) {
log(`--- Uploading SPIFFS file with esptool[.exe] ---`);
const uploadAddress = `0x` + toHex(stringToInt(spiffsOptions.spiffs_start), 6);
const uploadSpeed = stringToInt(spiffsOptions.speed);
const resetMethod = spiffsOptions.resetmethod;
logImportant(`SPIFFS Uploading Image... (${spiffsImage})`);
logSpiffs(`program: ${esptool}`);
logSpiffs(`address: ${uploadAddress}`);
logSpiffs(`reset : ${resetMethod}`);
logSpiffs(`port : ${commPort}`);
logSpiffs(`speed : ${uploadSpeed}`);
let args = [
"-ca", uploadAddress, // Address in flash.
"-cd", resetMethod, // Board reset method: "none", "ck", "nodemcu", or "wifio".
"-cp", commPort, // Serial Port (Default Linux: /dev/ttyUSB0, Windows: COM1, OSx: /dev/tty.usbserial).
"-cb", uploadSpeed, // Baud rate (Default: 115200).
"-cf", makeOsPath(spiffsImage) // SPIFFS File
];
const verbosity = getVscodeConfigValue(ESP8266FS_ESPTOOL_VERBOSITY);
if (verbosity)
args.unshift(`-${verbosity}`);
runCommand(makeOsPath(esptool), args);
}
//------------------------------------------------------------------------------
// --chip auto,esp32,esp8266
// --baud <rate>
// --port <port>
// --before default_reset,no_reset
// --after hard_reset,soft_reset,no_reset
// --no_stub
// --trace
// write_flash
// --compress
// --flash_mode <mode>
// --flash_freq <freq>
// --flash_size <size>
// --spi_connection <spi>
// --verify
function _uploadSpiffsEspToolPy(esptool, commPort, spiffsImage, spiffsOptions, target) {
log(`--- Uploading SPIFFS file with esptool.py ---`);
const python = getPythonExecutable();
const uploadAddress = `0x` + toHex(stringToInt(spiffsOptions.spiffs_start), 6);
const uploadSpeed = stringToInt(spiffsOptions.speed);
const resetMethod = spiffsOptions.resetmethod;
const before = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_BEFORE) || "default_reset";
const after = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_AFTER) || "hard_reset";
const flashMode = target.flashMode;
const flashFreq = target.flashFreq;
const flashSize = target.flashSize || "detect";
logImportant(`SPIFFS Uploading Image... (${spiffsImage})`);
logSpiffs(`Python : ${python}`);
logSpiffs(`EspTool : ${esptool}`);
logSpiffs(`address : ${uploadAddress}`);
logSpiffs(`port : ${commPort}`);
logSpiffs(`speed : ${uploadSpeed}`);
logSpiffs(`before : ${before}`);
logSpiffs(`after : ${after}`);
logSpiffs(`flashMode: ${flashMode}`);
logSpiffs(`flashFreq: ${flashFreq}`);
logSpiffs(`flashSize: ${flashSize}`);
const spi = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_SPI) || "";
if (spi)
logSpiffs(`SPI : ${spi}`);
let compress = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_COMPRESS);
if (compress) {
logSpiffs(`compress : ${compress}`);
compress = compress == "true";
} else
compress = false;
let args = [
esptool,
"--chip", target.architecture,
"--baud", uploadSpeed,
"--port", commPort,
"--before", before,
"--after", after
];
if (getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_NO_STUB) == "true")
args.push("--no-stub");
if (getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_TRACE) == "true")
args.push("--trace");
args.push("write_flash");