-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-sync.html
More file actions
570 lines (530 loc) · 20.5 KB
/
web-sync.html
File metadata and controls
570 lines (530 loc) · 20.5 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-3PSNCFVT76"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-3PSNCFVT76');
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M5Capsule Pomodoro Sync</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🍅</text></svg>">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #0d1117;
color: #c9d1d9;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
background: #161b22;
border: 1px solid #30363d;
border-radius: 12px;
padding: 32px;
max-width: 420px;
width: 100%;
}
h1 {
font-size: 24px;
margin-bottom: 8px;
color: #58a6ff;
}
.subtitle {
color: #8b949e;
font-size: 14px;
margin-bottom: 24px;
}
.status {
padding: 12px 16px;
border-radius: 8px;
margin-bottom: 24px;
font-size: 14px;
}
.status.disconnected {
background: #3d1100;
color: #ff7b72;
}
.status.connected {
background: #0d2d0d;
color: #7ee787;
}
.status.waiting {
background: #1e3a5f;
color: #79c0ff;
}
.form-group {
margin-bottom: 16px;
}
label {
display: block;
font-size: 14px;
color: #8b949e;
margin-bottom: 6px;
}
input[type="number"] {
width: 100%;
padding: 10px 12px;
background: #0d1117;
border: 1px solid #30363d;
border-radius: 6px;
color: #c9d1d9;
font-size: 16px;
}
input[type="number"]:focus, input[type="range"]:focus {
outline: none;
border-color: #58a6ff;
}
input[type="range"] {
width: 100%;
height: 8px;
background: #0d1117;
border: 1px solid #30363d;
border-radius: 4px;
-webkit-appearance: none;
appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 18px;
height: 18px;
background: #58a6ff;
border-radius: 50%;
cursor: pointer;
}
.slider-value {
display: inline-block;
min-width: 40px;
text-align: right;
color: #58a6ff;
font-weight: 500;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.checkbox-group {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 24px;
}
input[type="checkbox"] {
width: 18px;
height: 18px;
accent-color: #58a6ff;
}
button {
width: 100%;
padding: 12px;
background: #238636;
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
transition: background 0.2s;
}
button:hover:not(:disabled) {
background: #2ea043;
}
button.secondary {
background: #1f6feb;
margin-top: 8px;
}
button.secondary:hover:not(:disabled) {
background: #388bfd;
}
button:disabled {
background: #30363d;
color: #8b949e;
cursor: not-allowed;
}
.log {
margin-top: 24px;
padding: 12px;
background: #0d1117;
border-radius: 6px;
font-family: monospace;
font-size: 12px;
color: #8b949e;
max-height: 200px;
overflow-y: auto;
}
.instructions {
margin-top: 24px;
padding: 16px;
background: #0d1117;
border-radius: 8px;
font-size: 13px;
line-height: 1.6;
color: #8b949e;
}
.instructions ol {
margin-left: 20px;
}
.instructions li {
margin-bottom: 8px;
}
.legend {
margin-top: 16px;
padding: 12px;
background: #0d1117;
border-radius: 8px;
font-size: 12px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
}
.dot {
width: 12px;
height: 12px;
border-radius: 50%;
}
.dot.work { background: #f85149; }
.dot.break { background: #3fb950; }
.dot.long { background: #58a6ff; }
.dot.sync { background: #ff5000; animation: pulse 1s infinite; }
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
</style>
</head>
<body>
<div class="container">
<h1>☄️ M5Capsule Sync</h1>
<p class="subtitle">Sync settings with your Pomodoro Timer</p>
<div id="status" class="status disconnected">
Not connected. Double-click button during 5s boot window to enter sync mode.
<br><small>Requires Chrome/Edge with Web Serial API support.</small>
</div>
<div class="form-row">
<div class="form-group">
<label for="workMinutes">Work (min)</label>
<input type="number" id="workMinutes" min="1" max="60" value="25">
</div>
<div class="form-group">
<label for="breakMinutes">Short Break (min)</label>
<input type="number" id="breakMinutes" min="1" max="30" value="5">
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="longBreakMinutes">Long Break (min)</label>
<input type="number" id="longBreakMinutes" min="5" max="60" value="15">
</div>
<div class="form-group">
<label for="workSessions">Sessions before Long</label>
<input type="number" id="workSessions" min="2" max="10" value="4">
</div>
</div>
<div class="checkbox-group">
<input type="checkbox" id="soundEnabled" checked>
<label for="soundEnabled">Enable buzzer sounds</label>
</div>
<div class="form-group">
<label for="ledBrightness">LED Brightness: <span class="slider-value" id="ledValue">16</span></label>
<input type="range" id="ledBrightness" min="0" max="255" value="16">
</div>
<div class="form-group">
<label for="buzzerVolume">Buzzer Volume: <span class="slider-value" id="volValue">24</span></label>
<input type="range" id="buzzerVolume" min="0" max="255" value="24">
</div>
<button id="connectBtn">Connect</button>
<button id="syncBtn" class="secondary" disabled>Send Settings (SYNC)</button>
<button id="getBtn" class="secondary" disabled>Load from Device (GET)</button>
<div class="legend">
<strong>LED Colors:</strong>
<div class="legend-item"><span class="dot work"></span> Red = Work</div>
<div class="legend-item"><span class="dot break"></span> Green = Short Break</div>
<div class="legend-item"><span class="dot long"></span> Blue = Long Break</div>
<div class="legend-item"><span class="dot sync"></span> Orange = Sync Mode</div>
<div class="legend-item" style="margin-top:4px; font-size:11px; color:#8b949e;">
White = Boot mode (5s window for sync)
</div>
</div>
<div id="log" class="log"></div>
<div class="instructions">
<strong>How to sync:</strong>
<ol>
<li>Double-click the button during the 5s white LED boot window</li>
<li>Device enters SYNC mode (blue LED)</li>
<li>Click "Connect" above (requires Chrome/Edge)</li>
<li>Adjust settings and click "Send Settings"</li>
<li>Click "Done" when finished to exit sync mode</li>
</ol>
<p><strong>Protocol:</strong> PING → PONG → [SYNC/GET] → PONG (exit)</p>
<p><strong>Commands:</strong> PING, PONG, GET, SYNC:key=val,..., RESET</p>
</div>
</div>
<script>
let port = null;
let reader = null;
let writer = null;
let connected = false;
let pingSent = false;
const statusEl = document.getElementById('status');
const connectBtn = document.getElementById('connectBtn');
const syncBtn = document.getElementById('syncBtn');
const getBtn = document.getElementById('getBtn');
const logEl = document.getElementById('log');
function log(msg) {
const line = document.createElement('div');
line.textContent = `[${new Date().toLocaleTimeString()}] ${msg}`;
logEl.appendChild(line);
logEl.scrollTop = logEl.scrollHeight;
}
// Slider value display updates
document.getElementById('ledBrightness').addEventListener('input', function() {
document.getElementById('ledValue').textContent = this.value;
});
document.getElementById('buzzerVolume').addEventListener('input', function() {
document.getElementById('volValue').textContent = this.value;
});
async function connect() {
try {
port = await navigator.serial.requestPort({
filters: [{ usbVendorId: 0x303A }] // ESP32-S3 native USB VID
});
await port.open({ baudRate: 115200 });
reader = port.readable.getReader();
writer = port.writable.getWriter();
connected = true;
statusEl.textContent = 'Connected! Sending PING...';
statusEl.className = 'status waiting';
log('USB connected');
// Start reading
readLoop();
// Start protocol: Send PING
await sendCommand('PING');
pingSent = true;
} catch (err) {
log('Error: ' + err.message);
statusEl.textContent = 'Connection failed: ' + err.message;
statusEl.className = 'status disconnected';
connected = false;
resetButtons();
}
}
async function disconnect() {
if (reader) {
await reader.cancel();
reader = null;
}
if (writer) {
await writer.close();
writer = null;
}
if (port) {
await port.close();
port = null;
}
connected = false;
pingSent = false;
statusEl.textContent = 'Disconnected. Double-click button during boot to sync again.';
statusEl.className = 'status disconnected';
connectBtn.textContent = 'Connect';
connectBtn.disabled = false;
syncBtn.disabled = true;
getBtn.disabled = true;
log('Disconnected');
}
function resetButtons() {
connectBtn.textContent = 'Connect';
connectBtn.disabled = false;
syncBtn.disabled = true;
getBtn.disabled = true;
}
async function sendCommand(cmd) {
if (!writer) return;
const encoder = new TextEncoder();
await writer.write(encoder.encode(cmd + '\n'));
log('→ ' + cmd);
}
async function sendSync() {
const work = document.getElementById('workMinutes').value;
const brk = document.getElementById('breakMinutes').value;
const longBrk = document.getElementById('longBreakMinutes').value;
const sessions = document.getElementById('workSessions').value;
const sound = document.getElementById('soundEnabled').checked ? 1 : 0;
const ledBright = document.getElementById('ledBrightness').value;
const buzzVol = document.getElementById('buzzerVolume').value;
const command = `SYNC:work=${work},break=${brk},longBreak=${longBrk},sessions=${sessions},sound=${sound},ledBright=${ledBright},buzzVol=${buzzVol}`;
await sendCommand(command);
statusEl.textContent = 'Settings sent. Device stays in sync mode.';
}
async function sendGet() {
await sendCommand('GET');
statusEl.textContent = 'Requesting settings from device...';
}
async function sendDone() {
await sendCommand('PONG');
statusEl.textContent = 'Exiting sync mode...';
log('Sent PONG - device should exit to TIMER MODE');
// Disable all buttons until we get confirmation or timeout
connectBtn.disabled = true;
syncBtn.disabled = true;
getBtn.disabled = true;
}
async function readLoop() {
const decoder = new TextDecoder();
let buffer = '';
while (port && port.readable) {
try {
const { value, done } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop(); // Keep incomplete line
for (const line of lines) {
const trimmed = line.trim();
if (trimmed) {
log('← ' + trimmed);
await handleMessage(trimmed);
}
}
} catch (err) {
log('Read error: ' + err.message);
break;
}
}
// Connection closed
if (connected) {
await disconnect();
}
}
async function handleMessage(msg) {
if (msg === 'PONG') {
if (pingSent) {
// This is response to our PING
pingSent = false;
statusEl.textContent = 'Device ready. You can now SYNC, GET, or click Done to exit.';
statusEl.className = 'status connected';
connectBtn.textContent = 'Done (PONG)';
syncBtn.disabled = false;
getBtn.disabled = false;
log('Handshake complete - ready for commands');
} else {
// This is response to our final PONG - device is exiting
statusEl.textContent = 'Sync complete. Device returned to TIMER MODE.';
statusEl.className = 'status disconnected';
log('Device exited sync mode');
// Properly disconnect and cleanup
await disconnect();
}
} else if (msg.startsWith('SETTINGS:')) {
// Parse settings from device
const settingsStr = msg.substring(9);
log('Received settings: ' + settingsStr);
parseSettings(settingsStr);
statusEl.textContent = 'Settings loaded from device.';
} else if (msg === 'SYNC received') {
statusEl.textContent = 'Settings synced. Device stays in sync mode.';
} else if (msg === 'Settings reset to defaults') {
statusEl.textContent = 'Settings reset to defaults.';
} else if (msg.startsWith('USB CMD:')) {
// Echo from device, ignore
} else if (msg.startsWith('PING received')) {
// Status message from device
} else {
// Other status messages from device
log('Device: ' + msg);
}
}
function parseSettings(str) {
// Parse work=25,break=5,longBreak=15,sessions=4,sound=1,ledBright=24,buzzVol=64
const parts = str.split(',');
for (const part of parts) {
const [key, value] = part.split('=');
let parsedValue;
switch(key) {
case 'work':
parsedValue = parseInt(value);
if (parsedValue >= 1 && parsedValue <= 60) {
document.getElementById('workMinutes').value = parsedValue;
} else {
log('Invalid work value: ' + value);
}
break;
case 'break':
parsedValue = parseInt(value);
if (parsedValue >= 1 && parsedValue <= 30) {
document.getElementById('breakMinutes').value = parsedValue;
} else {
log('Invalid break value: ' + value);
}
break;
case 'longBreak':
parsedValue = parseInt(value);
if (parsedValue >= 5 && parsedValue <= 60) {
document.getElementById('longBreakMinutes').value = parsedValue;
} else {
log('Invalid longBreak value: ' + value);
}
break;
case 'sessions':
parsedValue = parseInt(value);
if (parsedValue >= 2 && parsedValue <= 10) {
document.getElementById('workSessions').value = parsedValue;
} else {
log('Invalid sessions value: ' + value);
}
break;
case 'sound':
parsedValue = parseInt(value);
document.getElementById('soundEnabled').checked = parsedValue !== 0;
break;
case 'ledBright':
parsedValue = parseInt(value);
if (parsedValue >= 0 && parsedValue <= 255) {
document.getElementById('ledBrightness').value = parsedValue;
document.getElementById('ledValue').textContent = parsedValue;
} else {
log('Invalid ledBright value: ' + value);
}
break;
case 'buzzVol':
parsedValue = parseInt(value);
if (parsedValue >= 0 && parsedValue <= 255) {
document.getElementById('buzzerVolume').value = parsedValue;
document.getElementById('volValue').textContent = parsedValue;
} else {
log('Invalid buzzVol value: ' + value);
}
break;
}
}
}
connectBtn.addEventListener('click', () => {
if (!connected) {
connect();
} else {
sendDone();
}
});
syncBtn.addEventListener('click', sendSync);
getBtn.addEventListener('click', sendGet);
// Check for Web Serial API support
if (!('serial' in navigator)) {
statusEl.textContent = 'Web Serial not supported. Use Chrome/Edge on desktop.';
connectBtn.disabled = true;
}
</script>
</body>
</html>