33 lines
726 B
JavaScript
33 lines
726 B
JavaScript
|
|
import { writeString } from './connection.js';
|
||
|
|
|
||
|
|
export async function enterRawRepl() {
|
||
|
|
await writeString('\x03\x03');
|
||
|
|
await sleep(100);
|
||
|
|
await writeString('\x01');
|
||
|
|
await sleep(100);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function executeCode(code) {
|
||
|
|
await enterRawRepl();
|
||
|
|
await writeString(code);
|
||
|
|
await writeString('\x04');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function stopExecution() {
|
||
|
|
await writeString('\x03\x03');
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function saveToDevice(code, filename = 'main.py') {
|
||
|
|
const script = [
|
||
|
|
`f = open('${filename}', 'w')`,
|
||
|
|
`f.write(${JSON.stringify(code)})`,
|
||
|
|
`f.close()`,
|
||
|
|
`print('Saved to ${filename}')`,
|
||
|
|
].join('\n');
|
||
|
|
await executeCode(script);
|
||
|
|
}
|
||
|
|
|
||
|
|
function sleep(ms) {
|
||
|
|
return new Promise((r) => setTimeout(r, ms));
|
||
|
|
}
|