Skip to content

Commit fbfe029

Browse files
committed
Move from esptool.exe to python version for ESP8266 as per breaking change described in esp8266/Arduino#5635
1 parent b6b77e1 commit fbfe029

File tree

1 file changed

+53
-103
lines changed

1 file changed

+53
-103
lines changed

esp8266fs.js

+53-103
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ function fileExists(file) {
255255

256256
//------------------------------------------------------------------------------
257257

258+
function getSingleSubfolder(parent) {
259+
if (!dirExists(parent)) {
260+
throw `"${parent}" is not initialized`;
261+
}
262+
263+
const folders = getFolders(parent);
264+
265+
if (folders.length != 1)
266+
throw `Cannot find single version of "${parent}"`;
267+
268+
return path.join(parent, folders[0]);
269+
}
270+
271+
//------------------------------------------------------------------------------
272+
258273
function readFile(name) {
259274
return fs.readFileSync(name, "utf8");
260275
}
@@ -522,16 +537,7 @@ function getEspPackagePath(arduinoUserPath, preferencesPath, target) {
522537
switch (target.architecture) {
523538
case "esp8266": {
524539
const dir = path.join(preferencesPath, "packages", target.package, "hardware", target.architecture);
525-
526-
if (!dirExists(dir))
527-
throw `ESP8266 has not been installed with the Arduino Board Manager.`;
528-
529-
const folders = getFolders(dir);
530-
531-
if (folders.length != 1)
532-
throw `There should only be one ESP8266 Package installed with the Arduino Board Manager.`;
533-
534-
const esp8266Path = path.join(dir, folders[0]);
540+
const esp8266Path = getSingleSubfolder(dir);
535541
logImportant(`Found ESP8266 packages: ${esp8266Path}`);
536542

537543
return esp8266Path;
@@ -642,10 +648,28 @@ function getEspToolsPath(arduinoUserPath, preferencesPath, target) {
642648
return dir;
643649
}
644650

651+
function getEspPackageTools(packagePath) {
652+
const dir = path.resolve(path.join(packagePath, "tools"));
653+
654+
if (!dirExists(dir))
655+
throw `Can't find hardware tools path.`;
656+
657+
logVerbose(`Tools Path: "${dir}"`);
658+
return dir;
659+
}
660+
645661
//------------------------------------------------------------------------------
646662

647-
function getPythonExecutable() {
648-
const python = getVscodeConfigValue(PYTHON_PYTHONPATH) || "python";
663+
function getPythonExecutable(toolsDir) {
664+
let python;
665+
if (getVscodeConfigValue(PYTHON_PYTHONPATH)) {
666+
python = getVscodeConfigValue(PYTHON_PYTHONPATH);
667+
} else if (toolsDir != undefined && dirExists(path.join(toolsDir, "python3"))) {
668+
const pythonPath = getSingleSubfolder(path.join(toolsDir, "python3"));
669+
python = path.join(pythonPath, program("python"));
670+
} else {
671+
python = "python";
672+
}
649673

650674
logVerbose(`Python Executable: "${python}"`);
651675
return python;
@@ -698,12 +722,8 @@ function getMkSpiffs(target, espToolsPath) {
698722

699723
switch (target.architecture) {
700724
case "esp8266": {
701-
const folders = getFolders(path.join(espToolsPath, "mkspiffs"));
702-
703-
if (folders.length != 1)
704-
throw `"${target.architecture}" not installed correctly through Arduino Board Manager`;
705-
706-
const mkspiffs = path.join(espToolsPath, "mkspiffs", folders[0], program("mkspiffs"));
725+
const folder = getSingleSubfolder(path.join(espToolsPath, "mkspiffs"));
726+
const mkspiffs = path.join(folder, program("mkspiffs"));
707727

708728
if (!fileExists(mkspiffs))
709729
throw `"Can't locate "${mkspiffs}"`;
@@ -862,74 +882,24 @@ function getEspTool(target, espToolsPath) {
862882
return configFile;
863883
}
864884

885+
let esptoolPy;
865886
switch (target.architecture) {
866-
case "esp8266": {
867-
const folders = getFolders(path.join(espToolsPath, "esptool"));
868-
869-
if (folders.length != 1)
870-
throw `"${target.architecture}" not installed correctly through Arduino Board Manager`;
871-
872-
const version = folders[0];
873-
874-
const esptool = path.join(espToolsPath, "esptool", version, program("esptool"));
875-
876-
if (!fileExists(esptool))
877-
throw `"Can't locate "${esptool}"`;
878-
879-
logVerbose(`esptool (${version}): ${CYAN}${esptool}`);
880-
881-
return esptool;
882-
}
883-
887+
case "esp8266":
888+
esptoolPy = path.join(espToolsPath, program("upload.py"));
889+
break;
884890
case "esp32": {
885-
const esptoolPy = path.join(espToolsPath, program("esptool.py"));
886-
887-
if (!fileExists(esptoolPy))
888-
throw `"Can't locate "${esptoolPy}"`;
889-
890-
logVerbose(`esptool: ${CYAN}${esptoolPy}`);
891-
892-
return esptoolPy;
891+
esptoolPy = path.join(espToolsPath, program("esptool.py"));
892+
break;
893893
}
894-
}
895-
}
896-
897-
//------------------------------------------------------------------------------
898894

899-
// -ca <address>
900-
// -cd <resetMethod>
901-
// -cp <port>
902-
// -cb <speed>
903-
// -vvv
904-
905-
function _uploadSpiffsEspTool(esptool, commPort, spiffsImage, spiffsOptions) {
906-
log(`--- Uploading SPIFFS file with esptool[.exe] ---`);
907-
908-
const uploadAddress = `0x` + toHex(stringToInt(spiffsOptions.spiffs_start), 6);
909-
const uploadSpeed = stringToInt(spiffsOptions.speed);
910-
const resetMethod = spiffsOptions.resetmethod;
911-
912-
logImportant(`SPIFFS Uploading Image... (${spiffsImage})`);
913-
logSpiffs(`program: ${esptool}`);
914-
logSpiffs(`address: ${uploadAddress}`);
915-
logSpiffs(`reset : ${resetMethod}`);
916-
logSpiffs(`port : ${commPort}`);
917-
logSpiffs(`speed : ${uploadSpeed}`);
918-
919-
let args = [
920-
"-ca", uploadAddress, // Address in flash.
921-
"-cd", resetMethod, // Board reset method: "none", "ck", "nodemcu", or "wifio".
922-
"-cp", commPort, // Serial Port (Default Linux: /dev/ttyUSB0, Windows: COM1, OSx: /dev/tty.usbserial).
923-
"-cb", uploadSpeed, // Baud rate (Default: 115200).
924-
"-cf", makeOsPath(spiffsImage) // SPIFFS File
925-
];
895+
}
926896

927-
const verbosity = getVscodeConfigValue(ESP8266FS_ESPTOOL_VERBOSITY);
897+
if (!fileExists(esptoolPy))
898+
throw `"Can't locate "${esptoolPy}"`;
928899

929-
if (verbosity)
930-
args.unshift(`-${verbosity}`);
900+
logVerbose(`esptool: ${CYAN}${esptoolPy}`);
931901

932-
runCommand(makeOsPath(esptool), args);
902+
return esptoolPy;
933903
}
934904

935905
//------------------------------------------------------------------------------
@@ -949,21 +919,16 @@ function _uploadSpiffsEspTool(esptool, commPort, spiffsImage, spiffsOptions) {
949919
// --spi_connection <spi>
950920
// --verify
951921

952-
function _uploadSpiffsEspToolPy(esptool, commPort, spiffsImage, spiffsOptions, target) {
922+
function uploadSpiffsEspToolPy(python, esptool, commPort, spiffsImage, spiffsOptions, target) {
953923
log(`--- Uploading SPIFFS file with esptool.py ---`);
954924

955-
const python = getPythonExecutable();
956-
957925
const uploadAddress = `0x` + toHex(stringToInt(spiffsOptions.spiffs_start), 6);
958926
const uploadSpeed = stringToInt(spiffsOptions.speed);
959927
const resetMethod = spiffsOptions.resetmethod;
960928

961929
const before = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_BEFORE) || "default_reset";
962930
const after = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_AFTER) || "hard_reset";
963931

964-
const flashMode = target.flashMode;
965-
const flashFreq = target.flashFreq;
966-
const flashSize = target.flashSize || "detect";
967932

968933
logImportant(`SPIFFS Uploading Image... (${spiffsImage})`);
969934
logSpiffs(`Python : ${python}`);
@@ -973,9 +938,6 @@ function _uploadSpiffsEspToolPy(esptool, commPort, spiffsImage, spiffsOptions, t
973938
logSpiffs(`speed : ${uploadSpeed}`);
974939
logSpiffs(`before : ${before}`);
975940
logSpiffs(`after : ${after}`);
976-
logSpiffs(`flashMode: ${flashMode}`);
977-
logSpiffs(`flashFreq: ${flashFreq}`);
978-
logSpiffs(`flashSize: ${flashSize}`);
979941

980942
const spi = getVscodeConfigValue(ESP8266FS_ESPTOOL_PY_SPI) || "";
981943
if (spi)
@@ -1008,12 +970,6 @@ function _uploadSpiffsEspToolPy(esptool, commPort, spiffsImage, spiffsOptions, t
1008970
if (compress)
1009971
args.push("--compress");
1010972

1011-
args.push(
1012-
"--flash_mode", flashMode,
1013-
"--flash_freq", flashFreq,
1014-
"--flash_size", flashSize
1015-
);
1016-
1017973
if (spi)
1018974
args.push("--spi-connection", spi);
1019975

@@ -1103,14 +1059,6 @@ function _downloadSpiffsEspToolPy(esptool, commPort, spiffsImage, spiffsOptions,
11031059

11041060
runCommand(makeOsPath(python), args);
11051061
}
1106-
//------------------------------------------------------------------------------
1107-
1108-
function uploadSpiffsEspTool(esptool, commPort, spiffsImage, spiffsOptions, target) {
1109-
if (esptool.match(/\.py$/))
1110-
_uploadSpiffsEspToolPy(esptool, commPort, spiffsImage, spiffsOptions, target);
1111-
else
1112-
_uploadSpiffsEspTool(esptool, commPort, spiffsImage, spiffsOptions);
1113-
}
11141062

11151063
//------------------------------------------------------------------------------
11161064

@@ -1209,13 +1157,15 @@ async function _executeSpiffs(command) {
12091157

12101158
const arduinoUserPath = getArduinoUserPath();
12111159
const espPackagePath = getEspPackagePath(arduinoUserPath, preferencesPath, target);
1160+
const espPackageToolPath = getEspPackageTools(espPackagePath);
12121161
const espToolsPath = getEspToolsPath(arduinoUserPath, preferencesPath, target);
12131162

12141163
const spiffsOptions = getSpiffsOptions(espPackagePath, target, arduinoJson, preferences);
12151164

12161165
const port = getPort(arduinoJson, preferences);
12171166

12181167
const mkspiffs = getMkSpiffs(target, espToolsPath);
1168+
const python = getPythonExecutable(espToolsPath);
12191169

12201170
// --- Ready to get down to business ---
12211171

@@ -1224,7 +1174,7 @@ async function _executeSpiffs(command) {
12241174
if (isIP(port))
12251175
uploadSpiffsOta(getEspotaPy(espPackagePath), port, spiffsImage);
12261176
else
1227-
uploadSpiffsEspTool(getEspTool(target, espToolsPath), port, spiffsImage, spiffsOptions, target);
1177+
uploadSpiffsEspToolPy(python, getEspTool(target, espPackageToolPath), port, spiffsImage, spiffsOptions, target);
12281178

12291179
break;
12301180
}

0 commit comments

Comments
 (0)