-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
409 lines (379 loc) · 14.4 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="A 14-segment indicator toy page">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#317EFB">
<link rel="icon" href="pwa/favicon.svg">
<link rel="manifest" href="pwa/manifest.json">
<title>4*14-Segment Scratch Control</title>
<!-- Load Blockly core -->
<script src="https://unpkg.com/blockly/blockly_compressed.js"></script>
<!-- Load the default blocks -->
<script src="https://unpkg.com/blockly/blocks_compressed.js"></script>
<!-- Load generators -->
<script src="https://unpkg.com/blockly/javascript_compressed.js"></script>
<script src="https://unpkg.com/blockly/python_compressed.js"></script>
<!-- Load a message file -->
<script src="https://unpkg.com/blockly/msg/en.js"></script> <!-- Load Blockly core -->
<!-- MQTT CLIENT-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js"
type="text/javascript"></script>
<script src="led14_segment_blocks.js"></script>
<script src="toolbox.js"></script>
<script src="led_control.js"></script>
<script src="comm.js"></script>
<script src="examples.js"></script>
<style>
#blocklyDiv {
height: 95dvh
}
#indicatorsDiv {
position: absolute;
right: 5dvw;
top: 5dvh;
}
#indicatorsDiv img, embed {
height: 3dvmax;
width: 3dvmax;
}
#indicatorsDiv > div {
background-color: #f8f8f8;
border: darkslategrey 3px dashed;
border-radius: 1dvw;
padding: 2dvh;
display: flex;
user-select: none;
}
#connectDiv {
grid-gap: 0.6dvw;
align-items: start;
}
#connectDiv *:last-child {
margin-left: auto;
}
#displayDiv {
align-items: end;
}
#displayDiv embed {
height: 12dvh;
width: 7dvw;
user-select: none;
pointer-events: none;
}
#settings-board img {
height: 1dvmax;
width: 1dvmax;
}
/*noinspection CssUnusedSymbol*/
.LoadButton .blocklyFlyoutButtonBackground {
fill: #236123;
}
/*noinspection CssUnusedSymbol*/
.SaveButton .blocklyFlyoutButtonBackground {
fill: #682714;
}
dialog {
border-radius: 1em;
}
dialog * {
font-size: larger;
margin: 0 1em;
}
#dialog-mqtt-params div {
display: grid;
grid-template-columns: auto auto;
grid-row-gap: 1em;
}
#dialog-mqtt-params div *:first-child {
grid-column: span 2;
}
input:invalid {
box-shadow: 0 0 5px 1px red;
}
input:focus:invalid {
box-shadow: none;
}
</style>
</head>
<body>
<div id="blocklyDiv"></div>
<div id="indicatorsDiv">
<div id="displayDiv">
<embed type="image/svg+xml" id="digit0" src="led14.svg">
<embed type="image/svg+xml" id="digit1" src="led14.svg">
<embed type="image/svg+xml" id="digit2" src="led14_dots.svg">
<embed type="image/svg+xml" id="digit3" src="led14.svg">
<button id="restart" title="Restart">
<img src="restart-svgrepo-com.svg" alt="Restart">
</button>
</div>
<div id="connectDiv">
<svg id="connect-indicator" width="3dvmax" height="3dvmax" viewBox="0 0 32 32" fill="none">
<title>Connecting</title>
<path fill="#808080" d="M15.631,24.833l-3.649-3.717c-0.194-0.197-0.201-0.527,0.005-0.712C13.04,19.456,14.448,19,16,19
s2.96,0.456,4.014,1.405c0.206,0.185,0.198,0.514,0.005,0.712l-3.649,3.717C16.173,25.056,15.827,25.056,15.631,24.833z M16,13
c-3.06,0-5.845,1.074-7.944,2.982c-0.198,0.18-0.208,0.491-0.031,0.691l1.958,2.219c0.186,0.21,0.502,0.216,0.709,0.026
C12.093,17.635,13.955,17,16,17c2.045,0,3.907,0.635,5.309,1.918c0.207,0.189,0.523,0.184,0.709-0.026l1.958-2.219
c0.177-0.2,0.167-0.511-0.031-0.691C21.845,14.074,19.06,13,16,13z M4.125,12.253l1.953,2.213c0.182,0.206,0.492,0.218,0.697,0.033
C9.22,12.291,12.453,11,16,11s6.78,1.291,9.225,3.5c0.204,0.184,0.515,0.173,0.697-0.033l1.953-2.213
c0.179-0.203,0.166-0.518-0.035-0.699C24.7,8.729,20.556,7,16,7c-4.556,0-8.7,1.729-11.839,4.554
C3.96,11.735,3.946,12.05,4.125,12.253z"/>
</svg>
<button id="upload-board" title="Upload to Board">
<img src="upload-svgrepo-com.svg" alt="Upload to Board">
</button>
<button id="restart-board" title="Restart on Board">
<img src="restart-svgrepo-com.svg" alt="Restart on Board">
</button>
<div>
<button id="settings-board" title="Connection settings">
<img src="settings-cog-svgrepo-com.svg" alt="Connection settings">
</button>
<br>
<a href="https://github.com/elmot/esp32-blockly-display" title="Source repository" target="_blank">
<button>
<svg width="1dvmax" height="1dvmax" viewBox="0 0 24 24" fill="#000000">
<path d="M21 8A3.74 3.74 0 0 0 15.05 5.2a3.74 3.74 0 0 0 1.16 6.6 3 3 0 0 1-2.67 1.67h-3a4.46 4.46 0 0 0-3 1.17V7.4a3.74 3.74 0 1 0-1.5 0v9.12a3.78 3.78 0 1 0 1.8.1 3 3 0 0 1 2.67-1.67h3a4.48 4.48 0 0 0 4.22-3.04 3.74 3.74 0 0 0 3.25-3.67zM4.57 3.74a2.24 2.24 0 1 1 4.5 0 2.24 2.24 0 0 1-4.5 0zm4.45 16.4a2.2 2.2 0 1 1-4.5 0 2.24 2.24 0 0 1 4.5 0zm8.2-9.72a2.24 2.24 0 1 1 0-4.49 2.24 2.24 0 0 1 0 4.49z"/>
</svg>
</button>
</a>
</div>
</div>
</div>
<dialog id="dialog-alert">
<div id="dialog-alert-msg"></div>
<br>
<button id="dialog-alert-ok">OK</button>
</dialog>
<dialog id="dialog-confirm">
<div id="dialog-confirm-msg"></div>
<br>
<button id="dialog-confirm-ok">OK</button>
<button id="dialog-confirm-cancel">Cancel</button>
</dialog>
<dialog id="dialog-prompt">
<label for="dialog-prompt-input" id="dialog-prompt-msg"></label>
<br>
<br>
<input type="text" id="dialog-prompt-input">
<br>
<br>
<button id="dialog-prompt-ok">OK</button>
<button id="dialog-prompt-cancel">Cancel</button>
</dialog>
<dialog id="dialog-mqtt-params">
<div>
<h2>Please set MQTT connection params</h2>
<label for="dialog-mqtt-host">Broker host:</label>
<input type="text" id="dialog-mqtt-host" placeholder="blah.blah.hivemq.cloud" required>
<label for="dialog-mqtt-port">Broker Websocket/TLS port:</label>
<input type="number" id="dialog-mqtt-port" min="80" max="65535" required>
<label for="dialog-mqtt-path">Broker path</label>
<input type="text" id="dialog-mqtt-path" required>
<label for="dialog-mqtt-user">User</label>
<input type="text" id="dialog-mqtt-user" required>
<label for="dialog-mqtt-password">Password</label>
<input type="password" id="dialog-mqtt-password" required minlength="8">
<button id="dialog-mqtt-ok">OK</button>
<button id="dialog-mqtt-cancel">Cancel</button>
</div>
</dialog>
</body>
<script>
const config = {
grid:
{
spacing: 20,
length: 3,
colour: "#ccc",
snap: true
},
oneBasedIndex: false,
sounds: false,
toolbox: toolbox,
zoom: {controls: true}
};
const workspace = Blockly.inject("blocklyDiv", config);
const CHANGE_EVENTS = new Set([Blockly.Events.BLOCK_CHANGE, Blockly.Events.BLOCK_CREATE, Blockly.Events.BLOCK_DELETE, Blockly.Events.BLOCK_MOVE])
workspace.addChangeListener((event) => {
if (CHANGE_EVENTS.has(event.type)) {
window.workspace_changed = true
segm14StartScript()
}
}
)
workspace.registerButtonCallback("file-load", (button) => {
const js = window.localStorage.getItem(`local-file:${button.info.file_key}`)
if (js != null) {
if (window.workspace_changed) {
Blockly.dialog.confirm(`Unsaved workspace, load file ${button.info.file_key} anyway?`,
(q) => {
if (q) {
Blockly.serialization.workspaces.load(JSON.parse(js), workspace);
window.workspace_changed = false
}
})
}
} else {
Blockly.dialog.alert(`Nothing saved to ${button.info.file_key}`)
}
})
workspace.registerButtonCallback("file-save", (button) => {
Blockly.dialog.confirm(`Save blocks to ${button.info.file_key}?`,
(q) => {
if (q) {
const js = JSON.stringify(Blockly.serialization.workspaces.save(workspace));
window.localStorage.setItem(`local-file:${button.info.file_key}`, js)
window.workspace_changed = false
}
}
)
})
EXAMPLES.init(workspace)
{
const QUICKSAVE_KEY = "local-file:quicksave"
const js = window.localStorage.getItem(QUICKSAVE_KEY)
if (js != null) {
setTimeout(() => {
try {
Blockly.serialization.workspaces.load(JSON.parse(js), workspace);
} catch (e) {
console.warn(e)
}
}, 100)
}
setInterval(() => {
try {
const js = JSON.stringify(Blockly.serialization.workspaces.save(workspace));
window.localStorage.setItem(QUICKSAVE_KEY, js)
console.info("Quick save")
} catch (e) {
console.warn(e)
}
}, 30000)
}
window.addEventListener("beforeunload", () => {
MqttClient.disconnect()
})
document.getElementById("restart").addEventListener("click", () => {
segm14StartScript()
});
document.getElementById("upload-board").addEventListener("click", () => {
const js = python.pythonGenerator.workspaceToCode(workspace)
console.log(js)
MqttClient.executePython(js)
})
document.getElementById("restart-board").addEventListener("click", () => {
MqttClient.restartPython()
})
// Init dialogs
{
const dialog = document.querySelector("#dialog-alert")
dialog.querySelector("#dialog-alert-ok").addEventListener("click", () => {
dialog.close();
})
dialog.addEventListener("close", dialog._callback)
const dialogAlert = function (msg, callback) {
dialog.querySelector("#dialog-alert-msg").innerText = msg
dialog._callback = callback
dialog.showModal()
setTimeout(() => {
dialog.close()
}, 2500)
}
Blockly.dialog.setAlert(dialogAlert)
}
{
const dialog = document.querySelector("#dialog-confirm")
dialog.querySelector("#dialog-confirm-cancel").addEventListener("click", () => {
dialog.close();
})
dialog.querySelector("#dialog-confirm-ok").addEventListener("click", () => {
dialog.close('true');
})
dialog.addEventListener("close", (_) => {
dialog._callback(dialog.returnValue.toLowerCase() === 'true')
})
const dialogConfirm = function (msg, callback) {
dialog.querySelector("#dialog-confirm-msg").innerText = msg
dialog._callback = callback
dialog.showModal()
}
Blockly.dialog.setConfirm(dialogConfirm)
}
{
const dialog = document.querySelector("#dialog-prompt")
const input = dialog.querySelector("#dialog-prompt-input")
dialog.querySelector("#dialog-prompt-cancel").addEventListener("click", () => {
dialog.close()
})
dialog.querySelector("#dialog-prompt-ok").addEventListener("click", () => {
dialog.close(input.value)
})
dialog.addEventListener("close", (_) => {
dialog._callback(dialog.returnValue)
})
const dialogPrompt = function (msg, defaultValue, callback) {
dialog.querySelector("#dialog-prompt-msg").innerText = msg
input.value = defaultValue
dialog._callback = callback
dialog.showModal()
}
Blockly.dialog.setPrompt(dialogPrompt)
}
//MQTT parameters
{
const dialog = document.getElementById("dialog-mqtt-params")
const defParams = {
host: "",
port: "8884",
path: "/mqtt",
user: "",
password: "",
}
document.getElementById("settings-board").addEventListener("click", () => {
let params
try {
params = JSON.parse(window.localStorage.getItem(MqttClient.MQTT_PARAMS_KEY))
} catch (e) {
}
if (params == null) params = {}
for (const k in defParams) {
let value = params[k]
if (typeof value !== "string") value = defParams[k]
dialog.querySelector(`#dialog-mqtt-${k}`).value = value
}
dialog.showModal()
})
dialog.querySelector("#dialog-mqtt-cancel").addEventListener("click", () => {
dialog.close()
})
dialog.querySelector("#dialog-mqtt-ok").addEventListener("click", () => {
const params = {}
let valid = true
for (const k in defParams) {
let input = dialog.querySelector(`#dialog-mqtt-${k}`)
valid &&= input.checkValidity()
params[k] = input.value
}
if (!params.path.startsWith("/")) {
params.path = "/" + params.path
}
if (valid) {
dialog.close()
Blockly.dialog.confirm(`wss://${params.user}:***@${params.host}:${params.port}${params.path}\nSave these parameters?`,
(q) => {
if (q) {
localStorage.setItem(MqttClient.MQTT_PARAMS_KEY, JSON.stringify(params))
window.location.reload()
}
}
)
}
})
}
MqttClient.connect()
</script>
</html>