diff --git a/common/modules/mixly-modules/web/ampy.js b/common/modules/mixly-modules/web/ampy.js index 91155147..81f4618e 100644 --- a/common/modules/mixly-modules/web/ampy.js +++ b/common/modules/mixly-modules/web/ampy.js @@ -35,8 +35,8 @@ class AmpyExt extends Ampy { #receiveTemp_ = []; #writeBuffer_ = true; #active_ = false; - #dataLength_ = 32; - constructor(device, writeBuffer = true, dataLength = 32) { + #dataLength_ = 256; + constructor(device, writeBuffer = true, dataLength = 256) { super(); this.#device_ = device; this.#writeBuffer_ = writeBuffer; @@ -93,23 +93,25 @@ class AmpyExt extends Ampy { } } - async interrupt(timeout = 5000) { - // 中断两次 - await this.#device_.sendBuffer([3, 3]); - await this.#device_.sleep(100); - let succeed = false; - if (await this.readUntil('>>>', true, timeout)) { - succeed = true; + async interrupt(timeout = 1000) { + for (let i = 0; i < 5; i++) { + // 中断两次 + await this.#device_.sendBuffer([3, 3]); + await this.#device_.sleep(100); + if (await this.readUntil('>>>', true, timeout)) { + return true; + } } - return succeed; + return false; } - async enterRawREPL(timeout = 5000) { - await this.#device_.sendBuffer([1]); - await this.#device_.sleep(100); - let succeed = false; - if (await this.readUntil('raw repl; ctrl-b to exit', true, timeout)) { - return true; + async enterRawREPL(timeout = 1000) { + for (let i = 0; i < 5; i++) { + await this.#device_.sendBuffer([1]); + await this.#device_.sleep(100); + if (await this.readUntil('raw repl; ctrl-b to exit', true, timeout)) { + return true; + } } return false; } @@ -134,7 +136,7 @@ class AmpyExt extends Ampy { return succeed; } - async exec(str) { + async exec(str, timeout = 5000) { if (this.#writeBuffer_) { const buffer = this.#device_.encode(str); const len = Math.ceil(buffer.length / this.#dataLength_); @@ -155,6 +157,25 @@ class AmpyExt extends Ampy { } } await this.#device_.sendBuffer([4]); + return await this.follow(timeout); + } + + async follow(timeout = 1000) { + let data = await this.readUntil('\x04', true, timeout); + if (data.length < 1) { + throw new Error(Msg.Lang['ampy.waitingFirstEOFTimeout']); + } + let start = data.toLowerCase().indexOf('>ok'); + if (start === -1){ + start = 0; + } + data = data.substring(start + 3, data.length - 1); + let dataError = await this.readUntil('\x04', true, timeout); + if (dataError.length < 1) { + throw new Error(Msg.Lang['ampy.secondEOFTimeout']); + } + dataError = dataError.substring(0, dataError.length - 1); + return { data, dataError }; } async enter() { @@ -202,29 +223,22 @@ class AmpyExt extends Ampy { const code = Mustache.render(AmpyExt.GET, { path: filename }); - await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); + const { data, dataError } = await this.exec(code, timeout); + if (dataError) { + return ''; } - let str = await this.readUntil('>', false, timeout); - str = str.replace('\x04\x04', ''); - if (str.indexOf('OSError') === -1) { - str = this.#device_.decode(this.unhexlify(str)); - return str; - } - return false; + return this.#device_.decode(this.unhexlify(data)); } - async put(filename, code) { + async put(filename, code, timeout = 5000) { if (!this.isActive()) { throw new Error(Msg.Lang['ampy.portIsNotOpen']); } - let str = `file = open('${filename}', 'wb')\n`; + await this.exec(`file = open('${filename}', 'wb')`, timeout); const buffer = this.#device_.encode(code); - const len = Math.ceil(buffer.length / 500); + const len = Math.ceil(buffer.length / 64); for (let i = 0; i < len; i++) { - const writeBuffer = buffer.slice(i * 500, Math.min((i + 1) * 500, buffer.length)); + const writeBuffer = buffer.slice(i * 64, Math.min((i + 1) * 64, buffer.length)); let writeStr = ''; for (let num of writeBuffer) { let numStr = num.toString(16); @@ -233,10 +247,9 @@ class AmpyExt extends Ampy { } writeStr += '\\x' + numStr; } - str += `file.write(b'${writeStr}')\n`; + await this.exec(`file.write(b'${writeStr}')`, timeout); } - str += `file.close()\n`; - await this.exec(str); + await this.exec(`file.close()`, timeout); } async ls(directory = '/', longFormat = true, recursive = false, timeout = 5000) { @@ -257,18 +270,11 @@ class AmpyExt extends Ampy { path: directory }); } - await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); + const { data, dataError } = await this.exec(code, timeout); + if (dataError) { + return '[]'; } - let str = await this.readUntil('>', false, timeout); - let info = null; - str = str.replace('\x04\x04', ''); - str = str.replaceAll('\'', '\"'); - str = str.substring(0, str.lastIndexOf(']') + 1); - info = JSON.parse(str); - return info; + return JSON.parse(data.replaceAll('\'', '\"')); } async mkdir(directory, timeout = 5000) { @@ -278,16 +284,8 @@ class AmpyExt extends Ampy { const code = Mustache.render(AmpyExt.MKDIR, { path: directory }); - await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); - } - let str = await this.readUntil('>', false, timeout); - if (str.indexOf('OSError') === -1) { - return true; - } - return false; + const { dataError } = await this.exec(code, timeout); + return !dataError; } async mkfile(file, timeout = 5000) { @@ -297,16 +295,8 @@ class AmpyExt extends Ampy { const code = Mustache.render(AmpyExt.MKFILE, { path: file }); - await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); - } - let str = await this.readUntil('>', false, timeout); - if (str.indexOf('OSError') === -1) { - return true; - } - return false; + const { dataError } = await this.exec(code, timeout); + return !dataError; } async rename(oldname, newname, timeout = 5000) { @@ -317,16 +307,9 @@ class AmpyExt extends Ampy { oldPath: oldname, newPath: newname }); - await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); - } - let str = await this.readUntil('>', false, timeout); - if (str.indexOf('OSError') === -1) { - return true; - } - return false; + const result = await this.exec(code, timeout); + const { dataError } = await this.exec(code, timeout); + return !dataError; } async rm(filename, timeout = 5000) { @@ -337,15 +320,9 @@ class AmpyExt extends Ampy { path: filename }); await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); - } - let str = await this.readUntil('>', false, timeout); - if (str.indexOf('OSError') === -1) { - return true; - } - return false; + const result = await this.exec(code, timeout); + const { dataError } = await this.exec(code, timeout); + return !dataError; } async rmdir(directory, timeout = 5000) { @@ -355,16 +332,9 @@ class AmpyExt extends Ampy { const code = Mustache.render(AmpyExt.RMDIR, { path: directory }); - await this.exec(code); - await this.#device_.sleep(100); - if (!await this.readUntil('ok', true, timeout)) { - throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); - } - let str = await this.readUntil('>', false, timeout); - if (str.indexOf('OSError') === -1) { - return true; - } - return false; + const result = await this.exec(code, timeout); + const { dataError } = await this.exec(code, timeout); + return !dataError; } getDevice() { diff --git a/common/modules/mixly-modules/web/serialport.js b/common/modules/mixly-modules/web/serialport.js index 57c1c43b..546d7c89 100644 --- a/common/modules/mixly-modules/web/serialport.js +++ b/common/modules/mixly-modules/web/serialport.js @@ -121,7 +121,7 @@ class WebSerialPort extends Serial { try { while (true) { const { value, done } = await this.#reader_.read(); - value && this.onBuffer(value); + value !== undefined && this.onBuffer(value); if (done) { break; } diff --git a/common/msg/mixly/en.json b/common/msg/mixly/en.json index c3658ab0..d1f354b5 100644 --- a/common/msg/mixly/en.json +++ b/common/msg/mixly/en.json @@ -320,6 +320,8 @@ "ampy.portIsNotOpen": "Serial port is not open", "ampy.executePythonCodeFailed": "Unable to execute python code", "ampy.dataReadInterrupt": "Data reading interrupted", + "ampy.waitingFirstEOFTimeout": "timeout waiting for first EOF reception", + "ampy.waitingSecondEOFTimeout": "timeout waiting for second EOF reception", "editor.contextMenu.cut": "Cut", "editor.contextMenu.copy": "Copy", "editor.contextMenu.paste": "Paste", diff --git a/common/msg/mixly/zh-hans.json b/common/msg/mixly/zh-hans.json index 5a26c732..a684520b 100644 --- a/common/msg/mixly/zh-hans.json +++ b/common/msg/mixly/zh-hans.json @@ -320,6 +320,8 @@ "ampy.portIsNotOpen": "串口未打开", "ampy.executePythonCodeFailed": "无法执行python代码", "ampy.dataReadInterrupt": "数据读取中断", + "ampy.waitingFirstEOFTimeout": "等待第一个EOF超时", + "ampy.waitingSecondEOFTimeout": "等待第二个EOF超时", "editor.contextMenu.cut": "剪切", "editor.contextMenu.copy": "复制", "editor.contextMenu.paste": "粘贴", diff --git a/common/msg/mixly/zh-hant.json b/common/msg/mixly/zh-hant.json index ef15734f..2af577f0 100644 --- a/common/msg/mixly/zh-hant.json +++ b/common/msg/mixly/zh-hant.json @@ -320,6 +320,8 @@ "ampy.portIsNotOpen": "串列埠未開啟", "ampy.executePythonCodeFailed": "無法執行python程式碼", "ampy.dataReadInterrupt": "資料讀取中斷", + "ampy.waitingFirstEOFTimeout": "等待第一個EOF超時", + "ampy.waitingSecondEOFTimeout": "等待第二個EOF超時", "editor.contextMenu.cut": "剪下", "editor.contextMenu.copy": "複製", "editor.contextMenu.paste": "貼上", diff --git a/common/templates/python/ls-long-format.py b/common/templates/python/ls-long-format.py index fdb4f242..ba26ac47 100644 --- a/common/templates/python/ls-long-format.py +++ b/common/templates/python/ls-long-format.py @@ -19,4 +19,5 @@ for f in listdir('{{&path}}'): except: size = os.size(f) r.append([f, size]) + print(r) \ No newline at end of file