forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
96 lines (78 loc) · 2.05 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Serial } from "./upload/serial.js"
import { transfer } from "./upload/ymodem.js"
function delay(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
const imports = `
const {
/* sprite interactions */ setSolids, setPushables,
/* see also: sprite.x +=, sprite.y += */
/* art */ setLegend, setBackground,
/* text */ addText, clearText,
/* spawn sprites */ setMap, addSprite,
/* despawn sprites */ clearTile, /* sprite.remove() */
/* tile queries */ getGrid, getTile, getFirst, getAll, tilesWith,
/* see also: sprite.type */
/* map dimensions */ width, height,
/* constructors */ bitmap, tune, map,
/* input handling */ onInput, afterInput,
/* how much sprite has moved since last onInput: sprite.dx, sprite.dy */
playTune,
} = require("engine");
`;
const startupSound = `
playTune(\`
150: c5~150 + g4~150 + e5^150,
150,
150: g5^150,
150,
150: b4~150 + d5^150 + g4~150,
150,
150: e5^150,
150,
150: a4~150 + f4~150 + c5^150,
150,
150: g4~150 + b4~150 + d5^150,
150,
150: g4~150 + e4~150 + c4~150 + e5^150 + c5~150 + g5~150,
2850
\`);
`;
async function uploadToSerial(serial, code) {
let respStr;
serial.on("data", t => {
for (let c of t) {
if (c == 10) return;
if (c == 13) {
console.log(respStr);
respStr = '';
}
else respStr += String.fromCharCode(c);
}
})
serial.write("\r.hi\r");
await serial.write("\r");
await serial.write(".flash -w\r");
await delay(500);
const result = await transfer(
serial,
"code",
new TextEncoder().encode(imports + startupSound + code)
);
console.log('ymodem transfer result: ', result);
await delay(500);
serial.write("\r.load\r");
alert("upload complete! you may need to unplug and replug your device.");
}
export async function upload(code) {
const port = await navigator.serial.requestPort();
const serial = new Serial(port);
await serial.open({ baudRate: 115200 });
uploadToSerial(serial, code)
.catch(console.error)
.finally(() => serial.close());
}