forked from hackclub/puzzlelab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.js
More file actions
185 lines (161 loc) · 4.61 KB
/
upload.js
File metadata and controls
185 lines (161 loc) · 4.61 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Serial } from "./upload/serial.js"
import { transfer } from "./upload/ymodem.js"
let serial;
function delay(ms) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
}
function fmtImageData(imgData) {
const pixels = [...imgData.data];
const { width: w, height: h } = imgData;
const bytes = new Uint8Array(w*h*2);
const color16 = (r, g, b) =>
((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
for (let x = 0; x < w; x++)
for (let y = 0; y < h; y++) {
let i = (y*w + x);
const [r, g, b, a] = pixels.slice(i*4, (i + 1)*4);
if (a < 255) continue;
const col = color16(Math.max(b, 5),
Math.max(g, 5),
Math.max(r, 5));
bytes[i*2+0] = col >> 8;
bytes[i*2+1] = col;
}
const data = new Uint16Array(bytes.buffer);
return `{
data: new Uint16Array([${[...data]}]),
width: ${w},
height: ${h}
}`;
}
let index;
const games = {
list: (() => {
const raw = JSON.parse(localStorage.getItem('beaker-games') ?? '[]');
const list = Array.isArray(raw) ? raw : [];
return [...Array(3)].map((x, i) => list[i] ?? {
name: "<empty>",
code: '',
});
})(),
save() {
localStorage.setItem('beaker-games', JSON.stringify(this.list));
},
add(code) {
do {
index = prompt(`
Your Beaker has 3 slots:
${this.list.map((x, i) => `[${i}] - ${x?.name}`).join("\n")}
Where shall we write your game? [0, 1, 2]
`)
if (index == null) return null;
} while(!["0", "1", "2"].includes(index));
index = +index;
let name;
while (!(name = prompt(`What do you call your game?`)));
alert(`Press upload again to write "${name}" to slot ${index}!`);
this.list[index] = { code, name };
this.save();
}
};
function flash() {
const textImg = (text, width=80, height=16) => {
const offscreen = document.createElement("canvas");
offscreen.width = width;
offscreen.height = height;
const octx = offscreen.getContext("2d");
octx.fillStyle = "white";
octx.fillRect(0, 0, 108, 108);
octx.fillStyle = "black";
octx.fillText(text, 2, 10);
return octx.getImageData(0, 0, width, height);
}
const t0 = textImg("maze game");
const t1 = textImg("suckyban");
return `
const games = [
${games.list.map(({ name, code }) => `{
code: \`${code.replaceAll("`", "\\`")}\`,
img: ${fmtImageData(textImg(name))},
}`).join(",\n")}
];
const cr = ${fmtImageData(textImg(">", 16, 16))};
/* input */
const { GPIO } = require("gpio");
const btn = pin => ({
last: 1,
pin: new GPIO(pin, INPUT_PULLUP),
read() {
const now = this.pin.read();
return (now != this.last && !(this.last = now));
}
});
const down = btn(0);
const open = btn(1);
/* drawin */
const width = 128, height = 160;
const pixels = new Uint16Array(width * height);
const { rfill, sprdraw } = global.require("native");
const { screen } = require("screen");
const gc = screen.getContext("buffer");
let ts = 0;
let i = 0;
const loop = (function loop() {
i = (i + down.read()) % games.length;
if (open.read()) {
if (games[i].code) {
eval(games[i].code);
return;
}
console.log("there's no game there!");
}
rfill(pixels, gc.color16(255, 255, 255), width*height);
let y = 20;
for (const { img } of games) {
sprdraw(img.data, img.width, img.height, pixels, 30, y);
y += 30
}
let t = Math.sin(ts++ / 5) * 4;
sprdraw(cr.data, cr.width, cr.height, pixels, 10+t, 20+30*i);
const pixels8 = new Uint8Array(pixels.buffer);
screen.fillImage(0, 0, width, height, pixels8);
setTimeout(loop, 1000/20);
})();
`;
}
export async function upload(code) {
if (index == undefined) {
games.add(code);
return;
}
games.list[index].code = code;
if (!serial) {
const port = await navigator.serial.requestPort();
serial = new Serial(port);
await serial.open({ baudRate: 115200 });
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(flash()));
console.log('ymodem transfer result: ', result);
await delay(500);
serial.write("\r.load\r");
}