-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
467 lines (397 loc) · 15.7 KB
/
index.html
File metadata and controls
467 lines (397 loc) · 15.7 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>R2P2-ESP32 Web Installer</title>
<script type="module">
import './js/esp-web-tools/install-button.js';
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #CC342D 0%, #8B1A1A 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
max-width: 960px;
width: 100%;
padding: 40px;
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 28px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 14px;
}
.info-box {
background: #f8f9fa;
border-left: 4px solid #CC342D;
padding: 15px;
margin-bottom: 25px;
border-radius: 4px;
}
.info-box h3 {
color: #333;
margin-bottom: 8px;
font-size: 16px;
}
.info-box ul {
margin-left: 20px;
color: #666;
font-size: 14px;
line-height: 1.6;
}
.firmware-select-section {
margin-bottom: 25px;
padding: 20px;
background: #f8f9fa;
border-radius: 8px;
border: 1px solid #e0e0e0;
}
.firmware-select-section h2 {
color: #333;
margin-bottom: 15px;
font-size: 20px;
}
.release-status {
font-size: 13px;
margin-bottom: 15px;
min-height: 20px;
}
.release-status.loading {
color: #666;
}
.release-status.loaded {
color: #155724;
}
.release-status.error {
color: #721c24;
}
.select-area {
display: none;
}
.select-row {
display: flex;
align-items: center;
gap: 15px;
margin-bottom: 12px;
}
.select-row label {
min-width: 140px;
font-size: 14px;
color: #333;
font-weight: 500;
}
.select-row select {
flex: 1;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
background: white;
cursor: pointer;
max-width: 400px;
}
.select-row select:focus {
outline: none;
border-color: #CC342D;
}
.flash-section {
margin-bottom: 30px;
}
.flash-section h2 {
color: #333;
margin-bottom: 15px;
font-size: 20px;
}
esp-web-install-button {
display: block;
margin: 20px 0;
}
button {
background: #CC342D;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background 0.3s;
}
button:hover {
background: #A52A2A;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.footer {
margin-top: 30px;
padding-top: 20px;
border-top: 2px solid #eee;
text-align: center;
color: #666;
font-size: 12px;
}
.footer a {
color: #CC342D;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>R2P2-ESP32 Web Installer</h1>
<p class="subtitle">Flash your ESP32 device directly from your browser</p>
<div class="info-box">
<h3>Before you start:</h3>
<ul>
<li>Connect your ESP32 device via USB</li>
<li>Make sure you're using Chrome, Edge, or Opera browser</li>
<li>This tool requires Web Serial API support</li>
</ul>
</div>
<div class="firmware-select-section">
<h2>Select Firmware</h2>
<div id="releaseStatus" class="release-status loading">Loading latest release info...</div>
<div id="selectArea" class="select-area">
<div class="select-row">
<label for="chipSelect">Board:</label>
<select id="chipSelect"></select>
</div>
<div class="select-row">
<label for="rubySelect">Ruby Variant:</label>
<select id="rubySelect"></select>
</div>
</div>
</div>
<div class="flash-section">
<h2>Flash Firmware</h2>
<esp-web-install-button id="installButton">
<button slot="activate">Connect and Flash</button>
</esp-web-install-button>
</div>
<div class="footer">
<p>Powered by <a href="https://esphome.github.io/esp-web-tools/" target="_blank">ESP Web Tools</a></p>
<p>R2P2-ESP32 - PicoRuby on ESP32</p>
</div>
</div>
<script>
// ---- Firmware selection ----
const GITHUB_RELEASES_API = 'https://api.github.com/repos/picoruby/R2P2-ESP32/releases/latest';
const CHIP_FAMILY_MAP = {
esp32: 'ESP32',
esp32c3: 'ESP32-C3',
esp32s3: 'ESP32-S3',
};
// ESP32 (original) uses bootloader offset 0x1000; all other chips use 0x0000
const BOOTLOADER_OFFSET_MAP = {
esp32: 4096,
esp32c3: 0,
esp32s3: 0,
};
const CHIP_LABEL_MAP = {
esp32: 'ESP32',
esp32c3: 'ESP32-C3',
esp32s3: 'ESP32-S3',
esp32s3_usb_console: 'ESP32-S3 (USB Console)',
};
const RUBY_LABEL_MAP = {
femtoruby: 'FemtoRuby',
picoruby: 'PicoRuby',
};
let releaseInfo = null;
let parsedAssets = [];
let currentManifestUrl = null;
function parseAssets(assets) {
return assets
.filter(a => a.name.endsWith('.bin'))
.map(a => {
// Pattern: R2P2-ESP32-{chip}-{variant?}-{ruby}.bin
const base = a.name.replace(/^R2P2-ESP32-/, '').replace(/\.bin$/, '');
const parts = base.split('-');
const ruby = parts[parts.length - 1];
const chip = parts[0];
const variantParts = parts.slice(1, -1);
const variant = variantParts.length > 0 ? variantParts.join('_') : null;
const chipKey = variant ? `${chip}_${variant}` : chip;
return { chip, variant, chipKey, ruby, url: a.browser_download_url };
})
.filter(a => a.chipKey in CHIP_LABEL_MAP && a.ruby in RUBY_LABEL_MAP);
}
function getBaseUrl() {
return window.location.href.replace(/[^/]*$/, '');
}
function generateManifest(asset, version) {
const baseUrl = getBaseUrl();
const chipFamily = CHIP_FAMILY_MAP[asset.chip];
const chipDir = asset.chip + (asset.variant ? '-' + asset.variant : '');
const appBin = baseUrl + `firmware/${chipDir}/${asset.ruby}.bin`;
const bootloaderOffset = BOOTLOADER_OFFSET_MAP[asset.chip] ?? 4096;
const parts = [
{ path: baseUrl + `firmware/${chipDir}/bootloader.bin`, offset: bootloaderOffset },
{ path: baseUrl + `firmware/${chipDir}/partition-table.bin`, offset: 32768 },
{ path: appBin, offset: 65536 },
{ path: baseUrl + 'firmware/storage.bin', offset: 2162688 },
];
const manifest = {
name: 'R2P2-ESP32',
version: version,
new_install_prompt_erase: true,
builds: [{ chipFamily, improv: false, parts }],
};
return manifest;
}
function updateManifest() {
if (!releaseInfo || parsedAssets.length === 0) return;
const chipKey = document.getElementById('chipSelect').value;
const ruby = document.getElementById('rubySelect').value;
const asset = parsedAssets.find(a => a.chipKey === chipKey && a.ruby === ruby);
if (!asset) return;
const manifest = generateManifest(asset, releaseInfo.tag_name);
const blob = new Blob([JSON.stringify(manifest)], { type: 'application/json' });
if (currentManifestUrl) URL.revokeObjectURL(currentManifestUrl);
currentManifestUrl = URL.createObjectURL(blob);
document.getElementById('installButton').setAttribute('manifest', currentManifestUrl);
}
async function fetchReleaseInfo() {
const statusEl = document.getElementById('releaseStatus');
const selectArea = document.getElementById('selectArea');
try {
const res = await fetch(GITHUB_RELEASES_API);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
releaseInfo = await res.json();
parsedAssets = parseAssets(releaseInfo.assets);
// Build chip options (unique chipKeys)
const chipKeys = [...new Map(parsedAssets.map(a => [a.chipKey, a])).keys()];
const chipSelect = document.getElementById('chipSelect');
chipSelect.innerHTML = chipKeys.map(key =>
`<option value="${key}">${CHIP_LABEL_MAP[key] || key}</option>`
).join('');
const rubySelect = document.getElementById('rubySelect');
function updateRubyOptions(chipKey) {
const rubies = [...new Set(parsedAssets.filter(a => a.chipKey === chipKey).map(a => a.ruby))];
rubySelect.innerHTML = rubies.map(r =>
`<option value="${r}">${RUBY_LABEL_MAP[r] || r}</option>`
).join('');
}
chipSelect.addEventListener('change', () => {
updateRubyOptions(chipSelect.value);
updateManifest();
});
rubySelect.addEventListener('change', updateManifest);
updateRubyOptions(chipKeys[0]);
statusEl.textContent = `Latest release: ${releaseInfo.tag_name}`;
statusEl.className = 'release-status loaded';
selectArea.style.display = 'block';
updateManifest();
} catch (err) {
statusEl.textContent = `Failed to load release info: ${err.message}`;
statusEl.className = 'release-status error';
}
}
fetchReleaseInfo();
// ---- Intercept installation completion ----
// Watch for ewt-install-dialog being added to document.body
new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeName === 'EWT-INSTALL-DIALOG') {
setupInstallDialogInterception(node);
}
}
}
}).observe(document.body, { childList: true });
function setupInstallDialogInterception(dialog) {
function tryInjectCompletionUI() {
if (!dialog.shadowRoot) return;
// label="Installation complete!" is set as a DOM attribute by Lit
// (no leading dot in the template = attribute binding, not property binding)
const completeMsg = dialog.shadowRoot.querySelector(
'ewt-page-message[label="Installation complete!"]'
);
if (!completeMsg) return;
// Find the actions div — may not exist yet if Lit hasn't rendered it
const actionsDiv = dialog.shadowRoot.querySelector('div[slot="actions"]');
if (!actionsDiv) return;
// Already injected (guard against Lit re-renders)
if (actionsDiv.querySelector('.r2p2-completion')) return;
// Replace "Next" button with our completion content
actionsDiv.innerHTML = '';
const container = document.createElement('div');
container.className = 'r2p2-completion';
container.style.cssText = [
'display:flex',
'flex-direction:column',
'align-items:center',
'gap:14px',
'padding:4px 16px 4px',
].join(';');
const link = document.createElement('a');
link.href = 'https://picoruby.org/terminal';
link.target = '_blank';
link.rel = 'noopener noreferrer';
link.textContent = 'Open PicoRuby Terminal →';
link.style.cssText = [
'color:#CC342D',
'font-size:15px',
'font-weight:500',
'text-decoration:none',
].join(';');
link.addEventListener('mouseover', () => { link.style.textDecoration = 'underline'; });
link.addEventListener('mouseout', () => { link.style.textDecoration = 'none'; });
const okBtn = document.createElement('button');
okBtn.textContent = 'OK';
okBtn.style.cssText = [
'background:#CC342D',
'color:#fff',
'border:none',
'padding:10px 32px',
'border-radius:4px',
'cursor:pointer',
'font-size:14px',
'font-family:inherit',
].join(';');
okBtn.addEventListener('mouseover', () => { okBtn.style.background = '#A52A2A'; });
okBtn.addEventListener('mouseout', () => { okBtn.style.background = '#CC342D'; });
okBtn.addEventListener('click', () => {
// Close the ew-dialog inside the shadow DOM; ewt-install-dialog
// will handle cleanup (port close, self-removal) via its own listener.
const ewDialog = dialog.shadowRoot.querySelector('ew-dialog');
if (ewDialog) ewDialog.close();
});
container.appendChild(link);
container.appendChild(okBtn);
actionsDiv.appendChild(container);
}
// Wait for the shadow root to be ready, then observe mutations
const waitId = setInterval(() => {
if (!dialog.shadowRoot) return;
clearInterval(waitId);
new MutationObserver(tryInjectCompletionUI)
.observe(dialog.shadowRoot, { childList: true, subtree: true });
}, 50);
}
</script>
</body>
</html>