Update: 更新ampy

This commit is contained in:
王立帮
2024-08-17 23:59:17 +08:00
parent 29f15dd7d4
commit edce8ed5bc
8 changed files with 46 additions and 34 deletions

View File

@@ -5,6 +5,17 @@ goog.provide('Mixly.Ampy');
class Ampy { class Ampy {
constructor() {} 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 get() {}
async ls() {} async ls() {}
async mkdir() {} async mkdir() {}

View File

@@ -1529,6 +1529,7 @@
"Mustache", "Mustache",
"Mixly.Env", "Mixly.Env",
"Mixly.Msg", "Mixly.Msg",
"Mixly.Ampy",
"Mixly.Web" "Mixly.Web"
], ],
"provide": [ "provide": [

View File

@@ -130,7 +130,7 @@ class AmpyFS extends FS {
let stdout = [], error = null; let stdout = [], error = null;
try { try {
const output = await this.#ampy_.ls(this.#port_, this.#baud_, folderPath); 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) { for (let i in dirs) {
if (!dirs[i]) { if (!dirs[i]) {
continue; continue;

View File

@@ -329,12 +329,10 @@ ArduShell.upload = (boardType, port) => {
} }
ArduShell.runCmd(layerNum, 'upload', cmdStr, ArduShell.runCmd(layerNum, 'upload', cmdStr,
function () { function () {
setTimeout(() => {
mainStatusBarTabs.add('serial', port); mainStatusBarTabs.add('serial', port);
mainStatusBarTabs.changeTo(port); mainStatusBarTabs.changeTo(port);
const statusBarSerial = mainStatusBarTabs.getStatusBarById(port); const statusBarSerial = mainStatusBarTabs.getStatusBarById(port);
statusBarSerial.open(); statusBarSerial.open();
}, 1000);
} }
); );
} }

View File

@@ -170,7 +170,7 @@ class AmpyFS extends FS {
} }
async readDirectory(folderPath) { async readDirectory(folderPath) {
let stdout = '', error = null, ampy = null; let stdout = [], error = null, ampy = null;
try { try {
ampy = await this.getAmpy(); ampy = await this.getAmpy();
await ampy.enter(); await ampy.enter();

View File

@@ -4,13 +4,19 @@ goog.require('path');
goog.require('Mustache'); goog.require('Mustache');
goog.require('Mixly.Env'); goog.require('Mixly.Env');
goog.require('Mixly.Msg'); goog.require('Mixly.Msg');
goog.require('Mixly.Ampy');
goog.require('Mixly.Web'); goog.require('Mixly.Web');
goog.provide('Mixly.Web.Ampy'); goog.provide('Mixly.Web.Ampy');
const { Env, Msg, Web } = Mixly; const {
Env,
Msg,
Ampy,
Web
} = Mixly;
class Ampy { class AmpyExt extends Ampy {
static { static {
this.LS = goog.get(path.join(Env.templatePath, 'python/ls.py')); this.LS = goog.get(path.join(Env.templatePath, 'python/ls.py'));
this.LS_RECURSIVE = goog.get(path.join(Env.templatePath, 'python/ls-recursive.py')); this.LS_RECURSIVE = goog.get(path.join(Env.templatePath, 'python/ls-recursive.py'));
@@ -28,6 +34,7 @@ class Ampy {
#writeBuffer_ = false; #writeBuffer_ = false;
#active_ = false; #active_ = false;
constructor(device) { constructor(device) {
super();
this.#device_ = device; this.#device_ = device;
this.#addEventsListener_(); 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() { isActive() {
return this.#active_; return this.#active_;
} }
@@ -186,7 +182,7 @@ class Ampy {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error('串口未打开'); throw new Error('串口未打开');
} }
const code = Mustache.render(Ampy.GET, { const code = Mustache.render(AmpyExt.GET, {
path: filename path: filename
}); });
await this.exec(code); await this.exec(code);
@@ -197,7 +193,7 @@ class Ampy {
let str = await this.readUntil('>', false, timeout); let str = await this.readUntil('>', false, timeout);
str = str.replace('\x04\x04', ''); str = str.replace('\x04\x04', '');
if (str.indexOf('OSError') === -1) { if (str.indexOf('OSError') === -1) {
str = this.#device_.decode(this.#unhexlify_(str)); str = this.#device_.decode(this.unhexlify(str));
return str; return str;
} }
return false; return false;
@@ -232,15 +228,15 @@ class Ampy {
} }
let code = ''; let code = '';
if (longFormat) { if (longFormat) {
code = Mustache.render(Ampy.LS_LONG_FORMAT, { code = Mustache.render(AmpyExt.LS_LONG_FORMAT, {
path: directory path: directory
}); });
} else if (recursive) { } else if (recursive) {
code = Mustache.render(Ampy.LS_RECURSIVE, { code = Mustache.render(AmpyExt.LS_RECURSIVE, {
path: directory path: directory
}); });
} else { } else {
code = Mustache.render(Ampy.LS, { code = Mustache.render(AmpyExt.LS, {
path: directory path: directory
}); });
} }
@@ -262,7 +258,7 @@ class Ampy {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error('串口未打开'); throw new Error('串口未打开');
} }
const code = Mustache.render(Ampy.MKDIR, { const code = Mustache.render(AmpyExt.MKDIR, {
path: directory path: directory
}); });
await this.exec(code); await this.exec(code);
@@ -281,7 +277,7 @@ class Ampy {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error('串口未打开'); throw new Error('串口未打开');
} }
const code = Mustache.render(Ampy.MKFILE, { const code = Mustache.render(AmpyExt.MKFILE, {
path: file path: file
}); });
await this.exec(code); await this.exec(code);
@@ -300,7 +296,7 @@ class Ampy {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error('串口未打开'); throw new Error('串口未打开');
} }
const code = Mustache.render(Ampy.RENAME, { const code = Mustache.render(AmpyExt.RENAME, {
oldPath: oldname, oldPath: oldname,
newPath: newname newPath: newname
}); });
@@ -320,7 +316,7 @@ class Ampy {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error('串口未打开'); throw new Error('串口未打开');
} }
const code = Mustache.render(Ampy.RM, { const code = Mustache.render(AmpyExt.RM, {
path: filename path: filename
}); });
await this.exec(code); await this.exec(code);
@@ -339,7 +335,7 @@ class Ampy {
if (!this.isActive()) { if (!this.isActive()) {
throw new Error('串口未打开'); throw new Error('串口未打开');
} }
const code = Mustache.render(Ampy.RMDIR, { const code = Mustache.render(AmpyExt.RMDIR, {
path: directory path: directory
}); });
await this.exec(code); await this.exec(code);
@@ -365,6 +361,6 @@ class Ampy {
} }
} }
Web.Ampy = Ampy; Web.Ampy = AmpyExt;
}); });

View File

@@ -31,7 +31,10 @@ def listdir(directory):
dirs = sorted([directory + '/' + f for f in os.listdir(directory)]) dirs = sorted([directory + '/' + f for f in os.listdir(directory)])
for dir in dirs: for dir in dirs:
output.append([dir, check_path(dir)]) info = check_path(dir)
if info == 'none':
continue
output.append([dir, info])
return output return output
print(listdir('{{&path}}')) print(listdir('{{&path}}'))

View File

@@ -170,7 +170,10 @@ class Files(object):
dirs = sorted([directory + '/' + f for f in os.listdir(directory)]) dirs = sorted([directory + '/' + f for f in os.listdir(directory)])
for dir in dirs: 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""" return output\n"""
# Execute os.listdir() command on the board. # Execute os.listdir() command on the board.