Update: 更新web ampy

This commit is contained in:
王立帮
2025-03-21 17:10:28 +08:00
parent 449ba1f793
commit 883dbb336d
6 changed files with 73 additions and 96 deletions

View File

@@ -35,8 +35,8 @@ class AmpyExt extends Ampy {
#receiveTemp_ = []; #receiveTemp_ = [];
#writeBuffer_ = true; #writeBuffer_ = true;
#active_ = false; #active_ = false;
#dataLength_ = 32; #dataLength_ = 256;
constructor(device, writeBuffer = true, dataLength = 32) { constructor(device, writeBuffer = true, dataLength = 256) {
super(); super();
this.#device_ = device; this.#device_ = device;
this.#writeBuffer_ = writeBuffer; this.#writeBuffer_ = writeBuffer;
@@ -93,23 +93,25 @@ class AmpyExt extends Ampy {
} }
} }
async interrupt(timeout = 5000) { async interrupt(timeout = 1000) {
// 中断两次 for (let i = 0; i < 5; i++) {
await this.#device_.sendBuffer([3, 3]); // 中断两次
await this.#device_.sleep(100); await this.#device_.sendBuffer([3, 3]);
let succeed = false; await this.#device_.sleep(100);
if (await this.readUntil('>>>', true, timeout)) { if (await this.readUntil('>>>', true, timeout)) {
succeed = true; return true;
}
} }
return succeed; return false;
} }
async enterRawREPL(timeout = 5000) { async enterRawREPL(timeout = 1000) {
await this.#device_.sendBuffer([1]); for (let i = 0; i < 5; i++) {
await this.#device_.sleep(100); await this.#device_.sendBuffer([1]);
let succeed = false; await this.#device_.sleep(100);
if (await this.readUntil('raw repl; ctrl-b to exit', true, timeout)) { if (await this.readUntil('raw repl; ctrl-b to exit', true, timeout)) {
return true; return true;
}
} }
return false; return false;
} }
@@ -134,7 +136,7 @@ class AmpyExt extends Ampy {
return succeed; return succeed;
} }
async exec(str) { async exec(str, timeout = 5000) {
if (this.#writeBuffer_) { if (this.#writeBuffer_) {
const buffer = this.#device_.encode(str); const buffer = this.#device_.encode(str);
const len = Math.ceil(buffer.length / this.#dataLength_); const len = Math.ceil(buffer.length / this.#dataLength_);
@@ -155,6 +157,25 @@ class AmpyExt extends Ampy {
} }
} }
await this.#device_.sendBuffer([4]); 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() { async enter() {
@@ -202,29 +223,22 @@ class AmpyExt extends Ampy {
const code = Mustache.render(AmpyExt.GET, { const code = Mustache.render(AmpyExt.GET, {
path: filename path: filename
}); });
await this.exec(code); const { data, dataError } = await this.exec(code, timeout);
await this.#device_.sleep(100); if (dataError) {
if (!await this.readUntil('ok', true, timeout)) { return '';
throw new Error(Msg.Lang['ampy.executePythonCodeFailed']);
} }
let str = await this.readUntil('>', false, timeout); return this.#device_.decode(this.unhexlify(data));
str = str.replace('\x04\x04', '');
if (str.indexOf('OSError') === -1) {
str = this.#device_.decode(this.unhexlify(str));
return str;
}
return false;
} }
async put(filename, code) { async put(filename, code, timeout = 5000) {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error(Msg.Lang['ampy.portIsNotOpen']); 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 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++) { 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 = ''; let writeStr = '';
for (let num of writeBuffer) { for (let num of writeBuffer) {
let numStr = num.toString(16); let numStr = num.toString(16);
@@ -233,10 +247,9 @@ class AmpyExt extends Ampy {
} }
writeStr += '\\x' + numStr; writeStr += '\\x' + numStr;
} }
str += `file.write(b'${writeStr}')\n`; await this.exec(`file.write(b'${writeStr}')`, timeout);
} }
str += `file.close()\n`; await this.exec(`file.close()`, timeout);
await this.exec(str);
} }
async ls(directory = '/', longFormat = true, recursive = false, timeout = 5000) { async ls(directory = '/', longFormat = true, recursive = false, timeout = 5000) {
@@ -257,18 +270,11 @@ class AmpyExt extends Ampy {
path: directory path: directory
}); });
} }
await this.exec(code); const { data, dataError } = await this.exec(code, timeout);
await this.#device_.sleep(100); if (dataError) {
if (!await this.readUntil('ok', true, timeout)) { return '[]';
throw new Error(Msg.Lang['ampy.executePythonCodeFailed']);
} }
let str = await this.readUntil('>', false, timeout); return JSON.parse(data.replaceAll('\'', '\"'));
let info = null;
str = str.replace('\x04\x04', '');
str = str.replaceAll('\'', '\"');
str = str.substring(0, str.lastIndexOf(']') + 1);
info = JSON.parse(str);
return info;
} }
async mkdir(directory, timeout = 5000) { async mkdir(directory, timeout = 5000) {
@@ -278,16 +284,8 @@ class AmpyExt extends Ampy {
const code = Mustache.render(AmpyExt.MKDIR, { const code = Mustache.render(AmpyExt.MKDIR, {
path: directory path: directory
}); });
await this.exec(code); const { dataError } = await this.exec(code, timeout);
await this.#device_.sleep(100); return !dataError;
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;
} }
async mkfile(file, timeout = 5000) { async mkfile(file, timeout = 5000) {
@@ -297,16 +295,8 @@ class AmpyExt extends Ampy {
const code = Mustache.render(AmpyExt.MKFILE, { const code = Mustache.render(AmpyExt.MKFILE, {
path: file path: file
}); });
await this.exec(code); const { dataError } = await this.exec(code, timeout);
await this.#device_.sleep(100); return !dataError;
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;
} }
async rename(oldname, newname, timeout = 5000) { async rename(oldname, newname, timeout = 5000) {
@@ -317,16 +307,9 @@ class AmpyExt extends Ampy {
oldPath: oldname, oldPath: oldname,
newPath: newname newPath: newname
}); });
await this.exec(code); const result = await this.exec(code, timeout);
await this.#device_.sleep(100); const { dataError } = await this.exec(code, timeout);
if (!await this.readUntil('ok', true, timeout)) { return !dataError;
throw new Error(Msg.Lang['ampy.executePythonCodeFailed']);
}
let str = await this.readUntil('>', false, timeout);
if (str.indexOf('OSError') === -1) {
return true;
}
return false;
} }
async rm(filename, timeout = 5000) { async rm(filename, timeout = 5000) {
@@ -337,15 +320,9 @@ class AmpyExt extends Ampy {
path: filename path: filename
}); });
await this.exec(code); await this.exec(code);
await this.#device_.sleep(100); const result = await this.exec(code, timeout);
if (!await this.readUntil('ok', true, timeout)) { const { dataError } = await this.exec(code, timeout);
throw new Error(Msg.Lang['ampy.executePythonCodeFailed']); return !dataError;
}
let str = await this.readUntil('>', false, timeout);
if (str.indexOf('OSError') === -1) {
return true;
}
return false;
} }
async rmdir(directory, timeout = 5000) { async rmdir(directory, timeout = 5000) {
@@ -355,16 +332,9 @@ class AmpyExt extends Ampy {
const code = Mustache.render(AmpyExt.RMDIR, { const code = Mustache.render(AmpyExt.RMDIR, {
path: directory path: directory
}); });
await this.exec(code); const result = await this.exec(code, timeout);
await this.#device_.sleep(100); const { dataError } = await this.exec(code, timeout);
if (!await this.readUntil('ok', true, timeout)) { return !dataError;
throw new Error(Msg.Lang['ampy.executePythonCodeFailed']);
}
let str = await this.readUntil('>', false, timeout);
if (str.indexOf('OSError') === -1) {
return true;
}
return false;
} }
getDevice() { getDevice() {

View File

@@ -121,7 +121,7 @@ class WebSerialPort extends Serial {
try { try {
while (true) { while (true) {
const { value, done } = await this.#reader_.read(); const { value, done } = await this.#reader_.read();
value && this.onBuffer(value); value !== undefined && this.onBuffer(value);
if (done) { if (done) {
break; break;
} }

View File

@@ -320,6 +320,8 @@
"ampy.portIsNotOpen": "Serial port is not open", "ampy.portIsNotOpen": "Serial port is not open",
"ampy.executePythonCodeFailed": "Unable to execute python code", "ampy.executePythonCodeFailed": "Unable to execute python code",
"ampy.dataReadInterrupt": "Data reading interrupted", "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.cut": "Cut",
"editor.contextMenu.copy": "Copy", "editor.contextMenu.copy": "Copy",
"editor.contextMenu.paste": "Paste", "editor.contextMenu.paste": "Paste",

View File

@@ -320,6 +320,8 @@
"ampy.portIsNotOpen": "串口未打开", "ampy.portIsNotOpen": "串口未打开",
"ampy.executePythonCodeFailed": "无法执行python代码", "ampy.executePythonCodeFailed": "无法执行python代码",
"ampy.dataReadInterrupt": "数据读取中断", "ampy.dataReadInterrupt": "数据读取中断",
"ampy.waitingFirstEOFTimeout": "等待第一个EOF超时",
"ampy.waitingSecondEOFTimeout": "等待第二个EOF超时",
"editor.contextMenu.cut": "剪切", "editor.contextMenu.cut": "剪切",
"editor.contextMenu.copy": "复制", "editor.contextMenu.copy": "复制",
"editor.contextMenu.paste": "粘贴", "editor.contextMenu.paste": "粘贴",

View File

@@ -320,6 +320,8 @@
"ampy.portIsNotOpen": "串列埠未開啟", "ampy.portIsNotOpen": "串列埠未開啟",
"ampy.executePythonCodeFailed": "無法執行python程式碼", "ampy.executePythonCodeFailed": "無法執行python程式碼",
"ampy.dataReadInterrupt": "資料讀取中斷", "ampy.dataReadInterrupt": "資料讀取中斷",
"ampy.waitingFirstEOFTimeout": "等待第一個EOF超時",
"ampy.waitingSecondEOFTimeout": "等待第二個EOF超時",
"editor.contextMenu.cut": "剪下", "editor.contextMenu.cut": "剪下",
"editor.contextMenu.copy": "複製", "editor.contextMenu.copy": "複製",
"editor.contextMenu.paste": "貼上", "editor.contextMenu.paste": "貼上",

View File

@@ -19,4 +19,5 @@ for f in listdir('{{&path}}'):
except: except:
size = os.size(f) size = os.size(f)
r.append([f, size]) r.append([f, size])
print(r) print(r)