From edce8ed5bc213344668b8abd91f67b4cf8535b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=AB=8B=E5=B8=AE?= <3294713004@qq.com> Date: Sat, 17 Aug 2024 23:59:17 +0800 Subject: [PATCH] =?UTF-8?q?Update:=20=E6=9B=B4=E6=96=B0ampy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/modules/mixly-modules/common/ampy.js | 11 +++++ common/modules/mixly-modules/deps.json | 1 + .../modules/mixly-modules/electron/ampy-fs.js | 2 +- .../mixly-modules/electron/arduino-shell.js | 10 ++--- common/modules/mixly-modules/web/ampy-fs.js | 2 +- common/modules/mixly-modules/web/ampy.js | 44 +++++++++---------- common/templates/python/ls.py | 5 ++- tools/python/ampy/files.py | 5 ++- 8 files changed, 46 insertions(+), 34 deletions(-) diff --git a/common/modules/mixly-modules/common/ampy.js b/common/modules/mixly-modules/common/ampy.js index dd321895..43ee75c8 100644 --- a/common/modules/mixly-modules/common/ampy.js +++ b/common/modules/mixly-modules/common/ampy.js @@ -5,6 +5,17 @@ goog.provide('Mixly.Ampy'); class Ampy { constructor() {} + unhexlify(hexString) { + if (hexString.length % 2 !== 0) { + hexString = hexString + '0'; + } + let bytes = []; + for (let c = 0; c < hexString.length; c += 2) { + bytes.push(parseInt(hexString.substr(c, 2), 16)); + } + return new Uint8Array(bytes); + } + async get() {} async ls() {} async mkdir() {} diff --git a/common/modules/mixly-modules/deps.json b/common/modules/mixly-modules/deps.json index e24a2513..37fb7e4e 100644 --- a/common/modules/mixly-modules/deps.json +++ b/common/modules/mixly-modules/deps.json @@ -1529,6 +1529,7 @@ "Mustache", "Mixly.Env", "Mixly.Msg", + "Mixly.Ampy", "Mixly.Web" ], "provide": [ diff --git a/common/modules/mixly-modules/electron/ampy-fs.js b/common/modules/mixly-modules/electron/ampy-fs.js index 6bcda559..39d4520c 100644 --- a/common/modules/mixly-modules/electron/ampy-fs.js +++ b/common/modules/mixly-modules/electron/ampy-fs.js @@ -130,7 +130,7 @@ class AmpyFS extends FS { let stdout = [], error = null; try { const output = await this.#ampy_.ls(this.#port_, this.#baud_, folderPath); - const dirs = Array.from(output.stdout.split('\r\n')); + const dirs = Array.from(new Set(output.stdout.split('\r\n'))); for (let i in dirs) { if (!dirs[i]) { continue; diff --git a/common/modules/mixly-modules/electron/arduino-shell.js b/common/modules/mixly-modules/electron/arduino-shell.js index dd2f25cf..ac15690a 100644 --- a/common/modules/mixly-modules/electron/arduino-shell.js +++ b/common/modules/mixly-modules/electron/arduino-shell.js @@ -329,12 +329,10 @@ ArduShell.upload = (boardType, port) => { } ArduShell.runCmd(layerNum, 'upload', cmdStr, function () { - setTimeout(() => { - mainStatusBarTabs.add('serial', port); - mainStatusBarTabs.changeTo(port); - const statusBarSerial = mainStatusBarTabs.getStatusBarById(port); - statusBarSerial.open(); - }, 1000); + mainStatusBarTabs.add('serial', port); + mainStatusBarTabs.changeTo(port); + const statusBarSerial = mainStatusBarTabs.getStatusBarById(port); + statusBarSerial.open(); } ); } diff --git a/common/modules/mixly-modules/web/ampy-fs.js b/common/modules/mixly-modules/web/ampy-fs.js index ff8c6c30..47339f02 100644 --- a/common/modules/mixly-modules/web/ampy-fs.js +++ b/common/modules/mixly-modules/web/ampy-fs.js @@ -170,7 +170,7 @@ class AmpyFS extends FS { } async readDirectory(folderPath) { - let stdout = '', error = null, ampy = null; + let stdout = [], error = null, ampy = null; try { ampy = await this.getAmpy(); await ampy.enter(); diff --git a/common/modules/mixly-modules/web/ampy.js b/common/modules/mixly-modules/web/ampy.js index e8878b0b..ae86f7cd 100644 --- a/common/modules/mixly-modules/web/ampy.js +++ b/common/modules/mixly-modules/web/ampy.js @@ -4,13 +4,19 @@ goog.require('path'); goog.require('Mustache'); goog.require('Mixly.Env'); goog.require('Mixly.Msg'); +goog.require('Mixly.Ampy'); goog.require('Mixly.Web'); goog.provide('Mixly.Web.Ampy'); -const { Env, Msg, Web } = Mixly; +const { + Env, + Msg, + Ampy, + Web +} = Mixly; -class Ampy { +class AmpyExt extends Ampy { static { this.LS = goog.get(path.join(Env.templatePath, 'python/ls.py')); this.LS_RECURSIVE = goog.get(path.join(Env.templatePath, 'python/ls-recursive.py')); @@ -28,6 +34,7 @@ class Ampy { #writeBuffer_ = false; #active_ = false; constructor(device) { + super(); this.#device_ = device; this.#addEventsListener_(); } @@ -43,17 +50,6 @@ class Ampy { }); } - #unhexlify_(hexString) { - if (hexString.length % 2 !== 0) { - hexString = hexString + '0'; - } - let bytes = []; - for (let c = 0; c < hexString.length; c += 2) { - bytes.push(parseInt(hexString.substr(c, 2), 16)); - } - return new Uint8Array(bytes); - } - isActive() { return this.#active_; } @@ -186,7 +182,7 @@ class Ampy { if (!this.isActive()) { throw new Error('串口未打开'); } - const code = Mustache.render(Ampy.GET, { + const code = Mustache.render(AmpyExt.GET, { path: filename }); await this.exec(code); @@ -197,7 +193,7 @@ class Ampy { let str = await this.readUntil('>', false, timeout); str = str.replace('\x04\x04', ''); if (str.indexOf('OSError') === -1) { - str = this.#device_.decode(this.#unhexlify_(str)); + str = this.#device_.decode(this.unhexlify(str)); return str; } return false; @@ -232,15 +228,15 @@ class Ampy { } let code = ''; if (longFormat) { - code = Mustache.render(Ampy.LS_LONG_FORMAT, { + code = Mustache.render(AmpyExt.LS_LONG_FORMAT, { path: directory }); } else if (recursive) { - code = Mustache.render(Ampy.LS_RECURSIVE, { + code = Mustache.render(AmpyExt.LS_RECURSIVE, { path: directory }); } else { - code = Mustache.render(Ampy.LS, { + code = Mustache.render(AmpyExt.LS, { path: directory }); } @@ -262,7 +258,7 @@ class Ampy { if (!this.isActive()) { throw new Error('串口未打开'); } - const code = Mustache.render(Ampy.MKDIR, { + const code = Mustache.render(AmpyExt.MKDIR, { path: directory }); await this.exec(code); @@ -281,7 +277,7 @@ class Ampy { if (!this.isActive()) { throw new Error('串口未打开'); } - const code = Mustache.render(Ampy.MKFILE, { + const code = Mustache.render(AmpyExt.MKFILE, { path: file }); await this.exec(code); @@ -300,7 +296,7 @@ class Ampy { if (!this.isActive()) { throw new Error('串口未打开'); } - const code = Mustache.render(Ampy.RENAME, { + const code = Mustache.render(AmpyExt.RENAME, { oldPath: oldname, newPath: newname }); @@ -320,7 +316,7 @@ class Ampy { if (!this.isActive()) { throw new Error('串口未打开'); } - const code = Mustache.render(Ampy.RM, { + const code = Mustache.render(AmpyExt.RM, { path: filename }); await this.exec(code); @@ -339,7 +335,7 @@ class Ampy { if (!this.isActive()) { throw new Error('串口未打开'); } - const code = Mustache.render(Ampy.RMDIR, { + const code = Mustache.render(AmpyExt.RMDIR, { path: directory }); await this.exec(code); @@ -365,6 +361,6 @@ class Ampy { } } -Web.Ampy = Ampy; +Web.Ampy = AmpyExt; }); \ No newline at end of file diff --git a/common/templates/python/ls.py b/common/templates/python/ls.py index d237d3a9..2fa2b75b 100644 --- a/common/templates/python/ls.py +++ b/common/templates/python/ls.py @@ -31,7 +31,10 @@ def listdir(directory): dirs = sorted([directory + '/' + f for f in os.listdir(directory)]) for dir in dirs: - output.append([dir, check_path(dir)]) + info = check_path(dir) + if info == 'none': + continue + output.append([dir, info]) return output print(listdir('{{&path}}')) \ No newline at end of file diff --git a/tools/python/ampy/files.py b/tools/python/ampy/files.py index ab48c27c..32aea722 100644 --- a/tools/python/ampy/files.py +++ b/tools/python/ampy/files.py @@ -170,7 +170,10 @@ class Files(object): dirs = sorted([directory + '/' + f for f in os.listdir(directory)]) for dir in dirs: - output.append([dir, check_path(dir)]) + info = check_path(dir) + if info == 'none': + continue + output.append([dir, info]) return output\n""" # Execute os.listdir() command on the board.