From a40d4126cb42e28d2ef5ddde51f673661b6dffce Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Wed, 15 Sep 2021 21:09:28 +0200 Subject: [PATCH] Add small ledstrip blocky editor --- .gitignore | 6 +- editor/LedBlocks.ts | 170 +++++++++++++++++++++++++++++++++++++++++++ editor/Utils.ts | 17 +++++ editor/tsconfig.json | 18 +++++ test.lua | 2 + 5 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 editor/LedBlocks.ts create mode 100644 editor/Utils.ts create mode 100644 editor/tsconfig.json diff --git a/.gitignore b/.gitignore index d163863..6718b4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -build/ \ No newline at end of file +.build/ +editor/.idea/ +editor/.cache +editor/dist +editor/node_modules diff --git a/editor/LedBlocks.ts b/editor/LedBlocks.ts new file mode 100644 index 0000000..0714fa6 --- /dev/null +++ b/editor/LedBlocks.ts @@ -0,0 +1,170 @@ +import Blockly from "blockly"; +import lua from "blockly/lua" + +export default class LedBlocks { + + static AddBlocks() { + + this.add_delay_block() + this.add_waitframes_block() + this.add_wled_block() + this.add_led_amount(); + } + private static add_wled_block() { + const delay_json = { + "type": "led", + "message0": "%1 %2 %3 %4 %5", + "previousStatement": null, + "nextStatement": null, + "args0": [ + { + "type": "field_label_serializable", + "name": "NAME", + "text": "led" + }, + { + "type": "input_value", + "name": "index", + "check": "Number" + }, + { + "type": "input_value", + "name": "red", + "check": "Number" + }, + { + "type": "input_value", + "name": "green", + "check": "Number" + }, + { + "type": "input_value", + "name": "blue", + "check": "Number" + } + ], + "colour": 345, + "tooltip": "Sets the led at index 'index' to the specified RGB-value ", + "helpUrl": "" + } + + Blockly.Blocks['led'] = { + init: function () { + this.jsonInit(delay_json); + } + }; + + lua['led'] = function (block) { + const value = lua.valueToCode(block, 'index', lua["ORDER_ATOMIC"]); + const r = lua.valueToCode(block, 'red', lua["ORDER_ATOMIC"]); + const g = lua.valueToCode(block, 'green', lua["ORDER_ATOMIC"]); + const b = lua.valueToCode(block, 'blue', lua["ORDER_ATOMIC"]); + return `led(${value},${r},${g},${b}) +`; + }; + + } + + private static add_waitframes_block() { + const delay_json = { + "type": "waitframes", + "previousStatement": null, + "nextStatement": null, + "message0": "%1 %2", + "args0": [ + { + "type": "field_label_serializable", + "name": "NAME", + "text": "waitframes" + }, + { + "type": "input_value", + "name": "framewait", + "check": "Number" + } + ], + "colour": 345, + "tooltip": "Waits the given amount of frames", + "helpUrl": "" + } + + Blockly.Blocks['waitframes'] = { + init: function () { + this.jsonInit(delay_json); + } + }; + + lua['waitframes'] = function (block) { + // @ts-ignore + const value = lua.valueToCode(block, 'framewait', lua.ORDER_ATOMIC); + return 'waitframes(' + value + ')\n'; + }; + + } + + private static add_led_amount(){ + const def = { + "type": "ledamount", + "message0": "%1", + "args0": [ + { + "type": "field_label_serializable", + "name": "NAME", + "text": "ledamount in strip" + }], + "output": null, + "colour": 345, + "tooltip": "Gives the number of leds in the ledstrip", + "helpUrl": "" + } + + Blockly.Blocks['ledamount'] = { + init: function () { + this.jsonInit(def); + } + }; + + lua['ledamount'] = function(block) { + // @ts-ignore + return ["ledamount()", lua.ORDER_NONE]; + }; + } + + private static add_delay_block() { + const delay_json = { + "type": "delay", + "message0": "%1 %2", + "previousStatement": null, + "nextStatement": null, + "args0": [ + { + "type": "field_label_serializable", + "name": "NAME", + "text": "delay (ms)" + }, + { + "type": "input_value", + "name": "wait_time_millisecs", + "check": "Number" + } + ], + "colour": 345, + "tooltip": "Waits the given amount of time in milliseconds", + "helpUrl": "" + } + + Blockly.Blocks['delay'] = { + init: function () { + this.jsonInit(delay_json); + } + }; + + lua['delay'] = function (block) { + // @ts-ignore + const value = lua.valueToCode(block, 'wait_time_millisecs', lua.ORDER_ATOMIC); + return 'delay(' + value + ')\n'; + }; + + } + +} \ No newline at end of file diff --git a/editor/Utils.ts b/editor/Utils.ts new file mode 100644 index 0000000..b50d6ee --- /dev/null +++ b/editor/Utils.ts @@ -0,0 +1,17 @@ +export default class Utils{ + public static offerContentsAsDownloadableFile(contents: string | Blob, fileName: string = "download.txt", + options?: { mimetype: string }) { + const element = document.createElement("a"); + let file; + if (typeof (contents) === "string") { + file = new Blob([contents], {type: options?.mimetype ?? 'text/plain'}); + } else { + file = contents; + } + element.href = URL.createObjectURL(file); + element.download = fileName; + document.body.appendChild(element); // Required for this to work in FireFox + element.click(); + } + +} \ No newline at end of file diff --git a/editor/tsconfig.json b/editor/tsconfig.json new file mode 100644 index 0000000..671d699 --- /dev/null +++ b/editor/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "sourceMap": true, + "resolveJsonModule": true, + "esModuleInterop": true, + "lib": [ + "DOM", + "es5", + "scripthost", + "es2015.collection" + ] + }, + "exclude": [ + "node_modules" + ] +} \ No newline at end of file diff --git a/test.lua b/test.lua index 1dd465b..597e7c7 100644 --- a/test.lua +++ b/test.lua @@ -1,3 +1,5 @@ print("hello world") led(1, 255, 255, 3) print("There are " .. tostring(ledamount()) .. " leds") +delay(500) -- Waits X millissecs +waitframes(5) -- waits X frames, where a 'frame' is defined in full refreshes