diff --git a/index.html b/index.html index 86b9d16..0811a30 100644 --- a/index.html +++ b/index.html @@ -30,6 +30,8 @@ background-color: #f8f8f8; border: darkslategrey 3px dashed; border-radius: 1cqw; + display: flex; + align-items: center; } #displayDiv embed { @@ -37,6 +39,11 @@ width: 7cqw } + #displayDiv #restart img { + height: 3cqmax; + width: 3cqmax; + } + #connect-circle { width: 1cqmax; height: 1cqmax; @@ -62,6 +69,9 @@ + @@ -84,15 +94,15 @@ }; 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(function (event) { - if (event.type === Blockly.Events.BLOCK_CHANGE) { + if (CHANGE_EVENTS.has(event.type)) { segm14StartScript() } } ) //todo display stuff led //todo save/restore - //todo rerun document.getElementById("digit0").src = led14Svg document.getElementById("digit1").src = led14Svg document.getElementById("digit2").src = led14Svg @@ -108,6 +118,9 @@ MqttClient.save(button.info.file_key, JSON.stringify(state)) }) EXAMPLES.init(workspace) + document.getElementById("restart").addEventListener("click", function() { + segm14StartScript() + }); // MqttClient.start() diff --git a/led_control.js b/led_control.js index 8d9dd10..87d5551 100644 --- a/led_control.js +++ b/led_control.js @@ -99,31 +99,31 @@ const font = { }; -var stopMe = false -var runningTimeout = null - -function segm14Stop() -{ - stopMe = true - clearTimeout(runningTimeout) -} function segm14StartScript() { + if(this!==window) { + segm14StartScript.call(window) + return + } + window.stopMe = true + clearTimeout(window.runningTimeout) const js = javascript.javascriptGenerator.workspaceToCode(workspace) - segm14Stop() segm14Text("\s\s\s\s") - const f = (async ()=>{}).constructor(js) + //Tricky stuff: here we create an async function using browser internal constructor + const f = (async () => { + }).constructor(js) setTimeout(() => { - stopMe = false + window.stopMe = false f() - }) + },100) } + // noinspection JSUnusedGlobalSymbols function segm14Text(text) { - if(stopMe) { - return - } for (let i = 0; i < 4; i++) { + if (window.stopMe) { + return + } const shape = font[text.charAt(i)] segm14Output(i, shape === undefined ? "" : shape) } @@ -131,9 +131,11 @@ function segm14Text(text) { // noinspection JSUnusedGlobalSymbols function segm14Delay(ms) { - if(stopMe) { + if (window.stopMe) { return null } - return new Promise(resolve => runningTimeout = setTimeout(resolve, ms * 1000.0)); + return new Promise(resolve => { + if (!window.stopMe) window.runningTimeout = setTimeout(resolve, ms * 1000.0) + }); } diff --git a/restart-svgrepo-com.svg b/restart-svgrepo-com.svg new file mode 100644 index 0000000..7b874ca --- /dev/null +++ b/restart-svgrepo-com.svg @@ -0,0 +1,6 @@ + + + +restart + + \ No newline at end of file