diff --git a/.github/workflows/build_pages.yaml b/.github/workflows/build_pages.yaml index 1cf4250..e955c7a 100644 --- a/.github/workflows/build_pages.yaml +++ b/.github/workflows/build_pages.yaml @@ -1,6 +1,8 @@ name: pages on: push: + branches: + - main env: PYTHON_VERSION: 3.x diff --git a/install-led_effect.sh b/install-led_effect.sh index 88a3d61..ac540a9 100755 --- a/install-led_effect.sh +++ b/install-led_effect.sh @@ -107,7 +107,7 @@ check_folders() link_extension() { echo -n "Linking extension to Klipper... " - ln -sf "${SRCDIR}/packages/led_effect/src/" "${KLIPPER_PATH}/klippy/extras/led_effect" + ln -sf "${SRCDIR}/packages/led_effect/led_effect/" "${KLIPPER_PATH}/klippy/extras/led_effect" echo "[OK]" } diff --git a/packages/klippermock/src/klippermock.py b/packages/klippermock/src/klippermock.py index 68579d1..3ebec4b 100644 --- a/packages/klippermock/src/klippermock.py +++ b/packages/klippermock/src/klippermock.py @@ -1,4 +1,4 @@ -from led_effect import ledEffect, ledFrameHandler +from led_effect.led_effect_plugin import ledEffect, ledFrameHandler class mockPrinter: NOW = 0 @@ -57,10 +57,10 @@ def register_adc(self, name, mcu): pass def lookup_heater(self, name): return self - def set_heater(self, min, max, temp): - self.led_effect.handler.heaterLast["bed"] = self.led_effect.handler.heaterCurrent["bed"] - self.led_effect.handler.heaterCurrent["bed"] = temp - self.led_effect.handler.heaterTarget["bed"] = max + def set_heater(self, min, max, temp, heater = "bed"): + self.led_effect.handler.heaterLast[heater] = self.led_effect.handler.heaterCurrent[heater] + self.led_effect.handler.heaterCurrent[heater] = temp + self.led_effect.handler.heaterTarget[heater] = max def set_progress (self, progress): self.led_effect.handler.printProgress=progress def set_analog(self, value): @@ -68,6 +68,8 @@ def set_analog(self, value): def load_template(self, config, name): self.template = config.get(name) return self + def config_error(self, msg): + raise Exception(msg) def render(self, context=None): return self.template def create_template_context(self): diff --git a/packages/led_effect/Makefile b/packages/led_effect/Makefile index f36c83f..c2d926b 100644 --- a/packages/led_effect/Makefile +++ b/packages/led_effect/Makefile @@ -1,2 +1,2 @@ -src/layer_parser_lark.py: src/layer_parser.lark +led_effect/layer_parser_lark.py: led_effect/layer_parser.lark poetry run python -m lark.tools.standalone -s line -o $@ $< \ No newline at end of file diff --git a/packages/led_effect/led_effect/__init__.py b/packages/led_effect/led_effect/__init__.py new file mode 100644 index 0000000..7eb0871 --- /dev/null +++ b/packages/led_effect/led_effect/__init__.py @@ -0,0 +1,7 @@ +from . import led_effect_plugin + +def load_config(config): + return led_effect_plugin.load_config(config) + +def load_config_prefix(config): + return led_effect_plugin.load_config_prefix(config) diff --git a/packages/led_effect/src/layer_parser.lark b/packages/led_effect/led_effect/layer_parser.lark similarity index 100% rename from packages/led_effect/src/layer_parser.lark rename to packages/led_effect/led_effect/layer_parser.lark diff --git a/packages/led_effect/src/layer_parser.py b/packages/led_effect/led_effect/layer_parser.py similarity index 100% rename from packages/led_effect/src/layer_parser.py rename to packages/led_effect/led_effect/layer_parser.py diff --git a/packages/led_effect/src/layer_parser_lark.py b/packages/led_effect/led_effect/layer_parser_lark.py similarity index 100% rename from packages/led_effect/src/layer_parser_lark.py rename to packages/led_effect/led_effect/layer_parser_lark.py diff --git a/packages/led_effect/src/led_effect.py b/packages/led_effect/led_effect/led_effect_plugin.py similarity index 100% rename from packages/led_effect/src/led_effect.py rename to packages/led_effect/led_effect/led_effect_plugin.py diff --git a/packages/led_effect/src/__init__.py b/packages/led_effect/src/__init__.py deleted file mode 100644 index 22224c5..0000000 --- a/packages/led_effect/src/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -from . import led_effect - -def load_config(config): - return led_effect.load_config(config) - -def load_config_prefix(config): - return led_effect.load_config_prefix(config) diff --git a/packages/led_effect/tests/layer_parser_test.py b/packages/led_effect/tests/layer_parser_test.py index ad19f9a..c89db1f 100644 --- a/packages/led_effect/tests/layer_parser_test.py +++ b/packages/led_effect/tests/layer_parser_test.py @@ -1,5 +1,5 @@ import pytest -import layer_parser +from led_effect import layer_parser legacy_format_data = [ ("breathing .5 0 screen (0,.1,1), (0,1,.5), (0, 1,1), (0,.1,.5)", "breathing", diff --git a/packages/led_effect/tests/led_effect_plugin_test.py b/packages/led_effect/tests/led_effect_plugin_test.py new file mode 100644 index 0000000..91af334 --- /dev/null +++ b/packages/led_effect/tests/led_effect_plugin_test.py @@ -0,0 +1,139 @@ +import pytest +from klippermock import * + + +def test_legacy_works(): + config = mockConfig() + config.setint("ledcount", 10) + config.set( + "layers", "gradient 2 3 top (1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0.0,1.0) ") + printer = mockPrinter(config) + printer._handle_ready() + layer = printer.led_effect.handler.effects[0].layers[0] + + assert layer.speed == 2 + assert layer.count == 3 + assert layer.blendingMode == "top" + assert layer.paletteColors == [1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0] + + +def test_new_params_work(): + config = mockConfig() + config.setint("ledcount", 10) + config.set( + "layers", "gradient(speed=2, count=3) top (1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0,1.0) ") + printer = mockPrinter(config) + printer._handle_ready() + layer = printer.led_effect.handler.effects[0].layers[0] + + assert layer.speed == 2 + assert layer.count == 3 + assert layer.blendingMode == "top" + assert layer.paletteColors == [1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0] + + +def test_missing_param_throws(): + config = mockConfig() + config.setint("ledcount", 10) + config.set( + "layers", "gradient(count=3) top (1.0,0.0,0.0),(0.0,1.0,0.0),(0.0,0,1.0) ") + with pytest.raises(Exception): + printer = mockPrinter(config) + printer._handle_ready() + + +def test_allow_layer_specific_heater(): + config = mockConfig() + config.setint("ledcount", 1) + config.set( + "layers", """ + heater(minTemp=10,disableOnceReached=0,heater=heater_bed) add (1.0,0.0,0.0) + heater(minTemp=10,disableOnceReached=0,heater=hotend) add (0,0.0,1.0) + """) + printer = mockPrinter(config) + printer._handle_ready() + printer.set_heater(None, 200, 100, "heater_bed") + assert printer.led_effect.getFrame(0) == ([1.0, 0.0, 0.0, 0.0], True) + printer.set_heater(None, 200, 100, "hotend") + assert printer.led_effect.getFrame(1) == ([1.0, 0.0, 1.0, 0.0], True) + + +def test_color_blending_colorspace_rgb_default(): + config = mockConfig() + config.setint("ledcount", 3) + config.set( + "layers", """ + static() top (1, 0, 0), (0, 0, 1) + """) + printer = mockPrinter(config) + printer._handle_ready() + assert printer.led_effect.getFrame(0) == ([ + 1.0, 0.0, 0.0, 0.0, + 0.5, 0.0, 0.5, 0.0, + 0.0, 0.0, 1.0, 0.0 + ], True) + + +def test_color_blending_colorspace_lab(): + config = mockConfig() + config.setint("ledcount", 3) + config.set( + "layers", """ + static(colorSpace=lab) top (1, 0, 0), (0, 0, 1) + """) + printer = mockPrinter(config) + printer._handle_ready() + assert printer.led_effect.getFrame(0) == ([ + 1.0, 0.0, 0.0, 0.0, + 0.7923588275927302, 0.0, 0.5387489625917922, 0.0, + 0.0, 0.0, 1.0, 0.0 + ], True) + + +def test_color_blending_colorspace_none(): + config = mockConfig() + config.setint("ledcount", 4) + config.set( + "layers", """ + static(colorSpace=none) top (1, 0, 0), (0, 0, 1) + """) + printer = mockPrinter(config) + printer._handle_ready() + assert printer.led_effect.getFrame(0) == ([ + 1.0, 0.0, 0.0, 0.0, + 1.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0, 0.0 + ], True) + + +def test_default_heater_gradient_length(): + config = mockConfig() + config.setint("ledcount", 1) + config.set( + "layers", """ + heater(heater=heater_bed,minTemp=0,disableOnceReached=0) top (1, 0, 0), (0, 0, 1), (0, 0, 1) + """) + printer = mockPrinter(config) + printer._handle_ready() + printer.set_heater(None, 100, 50, "heater_bed") + assert printer.led_effect.getFrame(0) == ([ + 0.4974874371859297, 0.0, 0.5025125628140703, 0.0 + ], True) + + +def test_changing_heater_gradient_length(): + config = mockConfig() + config.setint("ledcount", 1) + config.set( + "layers", """ + heater(heater=heater_bed,minTemp=0,disableOnceReached=0,gradientSteps=3) top (1, 0, 0), (0, 0, 1), (0, 0, 1) + """) + printer = mockPrinter(config) + printer._handle_ready() + printer.set_heater(None, 100, 50, "heater_bed") + assert printer.led_effect.getFrame(0) == ([ + 0.5, 0.0, 0.5, 0.0 + ], True) diff --git a/packages/web_simulator/package.json b/packages/web_simulator/package.json index b57de09..4108f08 100644 --- a/packages/web_simulator/package.json +++ b/packages/web_simulator/package.json @@ -9,9 +9,9 @@ "build:vite": "vite build", "preview": "vite preview", "check": "svelte-check --tsconfig ./tsconfig.json", - "build:whl": "rm src/*.whl || true; run-p build:whl:*", - "build:whl:klippermock": "cd ../klippermock; poetry build -f wheel -o ../web_simulator/src", - "build:whl:led_effect": "cd ../led_effect; poetry build -f wheel -o ../web_simulator/src" + "build:whl": "run-p build:whl:*", + "build:whl:klippermock": "rm src/klippermock*.whl || true; cd ../klippermock; poetry build -f wheel -o ../web_simulator/src", + "build:whl:led_effect": "rm src/led_effect*.whl || true; cd ../led_effect; poetry build -f wheel -o ../web_simulator/src" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^3.0.2", diff --git a/packages/web_simulator/src/App.svelte b/packages/web_simulator/src/App.svelte index 3b4897e..2fa08b0 100644 --- a/packages/web_simulator/src/App.svelte +++ b/packages/web_simulator/src/App.svelte @@ -1,5 +1,6 @@