* feature: add leds panel * Various refactors and improvements * lint fixes * led: use more columns for the selector * leds: name in the title * mainsail presets * change sliders to horizontal to avoid visual bug, simplify defaults * led: add a color preview and simplify methods to fix some issues * simplify presets move all the logic to the panel * led: change presets from names to previews * preview label shows color, add vertical mode * led: colored previews as rectangles instead of circles --------- Co-authored-by: alfrix <alfredomonclus@gmail.com>
This commit is contained in:
1 parent
584e4fe902
commit
87d09036ee
5 files changed
+269
-1
No files matched your search
@@ -39,3 +39,11 @@ class KlippyGcodes:
|
||||
@staticmethod
|
||||
def bed_mesh_save(profile):
|
||||
return f"BED_MESH_PROFILE SAVE='{profile}'"
|
||||
|
||||
@staticmethod
|
||||
def set_led_color(led, color):
|
||||
return (
|
||||
f'SET_LED LED="{led}" '
|
||||
f'RED={color[0]} GREEN={color[1]} BLUE={color[2]} WHITE={color[3]} '
|
||||
f'SYNC=0 TRANSMIT=1'
|
||||
)
|
||||
@@ -125,6 +125,12 @@ icon: fan
|
||||
panel: fan
|
||||
enable: {{ printer.fans.count > 0 }}
|
||||
|
||||
[menu __main more led]
|
||||
name: {{ gettext('Leds') }}
|
||||
icon: light
|
||||
panel: led
|
||||
enable: {{ printer.leds.count > 0 }}
|
||||
|
||||
[menu __main more macros]
|
||||
name: {{ gettext('Macros') }}
|
||||
icon: custom-script
|
||||
@@ -218,6 +224,12 @@ icon: shutdown
|
||||
panel: power
|
||||
enable: {{ printer.power_devices.count > 0 }}
|
||||
|
||||
[menu __print led]
|
||||
name: {{ gettext('Leds') }}
|
||||
icon: light
|
||||
panel: led
|
||||
enable: {{ printer.leds.count > 0 }}
|
||||
|
||||
[menu __print macros]
|
||||
name: {{ gettext('Macros') }}
|
||||
icon: custom-script
|
||||
|
||||
+39
-1
@@ -18,6 +18,7 @@ class Printer:
|
||||
self.extrudercount = 0
|
||||
self.tempdevcount = 0
|
||||
self.fancount = 0
|
||||
self.ledcount = 0
|
||||
self.output_pin_count = 0
|
||||
self.store_timeout = None
|
||||
self.tempstore = {}
|
||||
@@ -36,6 +37,7 @@ class Printer:
|
||||
self.extrudercount = 0
|
||||
self.tempdevcount = 0
|
||||
self.fancount = 0
|
||||
self.ledcount = 0
|
||||
self.output_pin_count = 0
|
||||
self.tempstore = {}
|
||||
self.busy = False
|
||||
@@ -88,6 +90,14 @@ class Printer:
|
||||
r['points'] = [[float(j.strip()) for j in i.split(",")] for i in r['points'].strip().split("\n")]
|
||||
except KeyError:
|
||||
logging.debug(f"Couldn't load mesh {x}: {self.config[x]}")
|
||||
if x.startswith('led') \
|
||||
or x.startswith('neopixel ') \
|
||||
or x.startswith('dotstar ') \
|
||||
or x.startswith('pca9533 ') \
|
||||
or x.startswith('pca9632 '):
|
||||
name = x.split()[1] if len(x.split()) > 1 else x
|
||||
if not name.startswith("_"):
|
||||
self.ledcount += 1
|
||||
self.process_update(data)
|
||||
|
||||
logging.info(f"Klipper version: {printer_info['software_version']}")
|
||||
@@ -95,6 +105,7 @@ class Printer:
|
||||
logging.info(f"# Temperature devices: {self.tempdevcount}")
|
||||
logging.info(f"# Fans: {self.fancount}")
|
||||
logging.info(f"# Output pins: {self.output_pin_count}")
|
||||
logging.info(f"# Leds: {self.ledcount}")
|
||||
|
||||
def process_update(self, data):
|
||||
if self.data is None:
|
||||
@@ -245,7 +256,8 @@ class Printer:
|
||||
"pause_resume": {"is_paused": self.state == "paused"},
|
||||
"power_devices": {"count": len(self.get_power_devices())},
|
||||
"cameras": {"count": len(self.cameras)},
|
||||
"spoolman": self.spoolman
|
||||
"spoolman": self.spoolman,
|
||||
"leds": {"count": self.ledcount},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,6 +272,32 @@ class Printer:
|
||||
|
||||
return data
|
||||
|
||||
def get_leds(self):
|
||||
leds = []
|
||||
led_types = ["dotstar", "led", "neopixel", "pca9533", "pca9632"]
|
||||
for led_type in led_types:
|
||||
leds.extend(iter(self.get_config_section_list(f"{led_type} ")))
|
||||
return leds
|
||||
|
||||
def get_led_color_order(self, led):
|
||||
if led not in self.config or led not in self.data:
|
||||
logging.debug(f"Error getting {led} config")
|
||||
return None
|
||||
elif "color_order" in self.config[led]:
|
||||
return self.config[led]["color_order"]
|
||||
colors = ''
|
||||
for option in self.config[led]:
|
||||
if option in ("red_pin", 'initial_RED') and 'R' not in colors:
|
||||
colors += 'R'
|
||||
elif option in ("green_pin", 'initial_GREEN') and 'G' not in colors:
|
||||
colors += 'G'
|
||||
elif option in ("blue_pin", 'initial_BLUE') and 'B' not in colors:
|
||||
colors += 'B'
|
||||
elif option in ("white_pin", 'initial_WHITE') and 'W' not in colors:
|
||||
colors += 'W'
|
||||
logging.debug(f"Colors in led: {colors}")
|
||||
return colors
|
||||
|
||||
def get_power_devices(self):
|
||||
return list(self.power_devices)
|
||||
|
||||
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
import logging
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk, GLib
|
||||
from math import pi
|
||||
from ks_includes.KlippyGcodes import KlippyGcodes
|
||||
from ks_includes.screen_panel import ScreenPanel
|
||||
|
||||
|
||||
class Panel(ScreenPanel):
|
||||
|
||||
def __init__(self, screen, title):
|
||||
super().__init__(screen, title)
|
||||
self.preview = Gtk.DrawingArea()
|
||||
self.preview.connect("draw", self.on_draw)
|
||||
self.preview_label = Gtk.Label(label='')
|
||||
self.preset_list = self._gtk.HomogeneousGrid()
|
||||
self.color_data = [0, 0, 0, 0]
|
||||
self.color_order = 'RGBW'
|
||||
self.presets = {
|
||||
"on": [1.0, 1.0, 1.0, 1.0],
|
||||
"off": [0.0, 0.0, 0.0, 0.0]
|
||||
}
|
||||
self.scales = {}
|
||||
self.buttons = []
|
||||
self.leds = self._printer.get_leds()
|
||||
self.current_led = self.leds[0] if len(self.leds) == 1 else None
|
||||
self.open_selector(None, self.current_led)
|
||||
|
||||
def color_available(self, idx):
|
||||
return (
|
||||
(idx == 0 and 'R' in self.color_order)
|
||||
or (idx == 1 and 'G' in self.color_order)
|
||||
or (idx == 2 and 'B' in self.color_order)
|
||||
or (idx == 3 and 'W' in self.color_order)
|
||||
)
|
||||
|
||||
def activate(self):
|
||||
if self.current_led is not None:
|
||||
self.set_title(f"{self.current_led}")
|
||||
|
||||
def set_title(self, title):
|
||||
self._screen.base_panel.set_title(self.prettify(title))
|
||||
|
||||
def back(self):
|
||||
if len(self.leds) > 1 and self.current_led:
|
||||
self.set_title(self._screen.panels[self._screen._cur_panels[-1]].title)
|
||||
self.open_selector(led=None)
|
||||
return True
|
||||
return False
|
||||
|
||||
def open_selector(self, widget=None, led=None):
|
||||
for child in self.content.get_children():
|
||||
self.content.remove(child)
|
||||
if led is None:
|
||||
self.content.add(self.led_selector())
|
||||
else:
|
||||
self.content.add(self.color_selector(led))
|
||||
self.content.show_all()
|
||||
|
||||
def led_selector(self):
|
||||
self.current_led = None
|
||||
columns = 3 if self._screen.vertical_mode else 4
|
||||
grid = self._gtk.HomogeneousGrid()
|
||||
for i, led in enumerate(self.leds):
|
||||
name = led.split()[1] if len(led.split()) > 1 else led
|
||||
button = self._gtk.Button(None, name.upper(), style=f"color{(i % 4) + 1}")
|
||||
button.connect("clicked", self.open_selector, led)
|
||||
grid.attach(button, (i % columns), int(i / columns), 1, 1)
|
||||
scroll = self._gtk.ScrolledWindow()
|
||||
scroll.add(grid)
|
||||
return scroll
|
||||
|
||||
def color_selector(self, led):
|
||||
logging.info(led)
|
||||
self.current_led = led
|
||||
self.set_title(f"{self.current_led}")
|
||||
grid = self._gtk.HomogeneousGrid()
|
||||
self.color_order = self._printer.get_led_color_order(led)
|
||||
if self.color_order is None:
|
||||
self.back()
|
||||
return
|
||||
scale_grid = self._gtk.HomogeneousGrid()
|
||||
da_size = self._gtk.img_scale * 2.75
|
||||
for idx, col_value in enumerate(self.color_data):
|
||||
if not self.color_available(idx):
|
||||
continue
|
||||
color = [0, 0, 0, 0]
|
||||
color[idx] = 1
|
||||
button = self._gtk.Button()
|
||||
preview = Gtk.DrawingArea(width_request=da_size, height_request=da_size)
|
||||
preview.connect("draw", self.on_draw, color)
|
||||
button.set_image(preview)
|
||||
button.connect("clicked", self.apply_preset, color)
|
||||
button.set_hexpand(False)
|
||||
scale = Gtk.Scale.new_with_range(orientation=Gtk.Orientation.HORIZONTAL, min=0, max=255, step=1)
|
||||
scale.set_value(round(col_value * 255))
|
||||
scale.set_digits(0)
|
||||
scale.set_hexpand(True)
|
||||
scale.set_has_origin(True)
|
||||
scale.get_style_context().add_class("fan_slider")
|
||||
scale.connect("button-release-event", self.apply_scales)
|
||||
scale.connect("value_changed", self.update_preview)
|
||||
self.scales[idx] = scale
|
||||
scale_grid.attach(button, 1, idx, 1, 1)
|
||||
scale_grid.attach(scale, 2, idx, 3, 1)
|
||||
grid.attach(scale_grid, 0, 0, 2, 1)
|
||||
|
||||
columns = 3 if self._screen.vertical_mode else 2
|
||||
self.preset_list.attach(self.preview_label, 0, 0, 1, 1)
|
||||
self.preset_list.attach(self.preview, 1, 0, columns - 1, 1)
|
||||
data_misc = self._screen.apiclient.send_request(
|
||||
"server/database/item?namespace=mainsail&key=miscellaneous.entries")
|
||||
if data_misc:
|
||||
presets_data = data_misc['result']['value'][next(iter(data_misc["result"]["value"]))]['presets']
|
||||
if presets_data:
|
||||
self.presets.update(self.parse_presets(presets_data))
|
||||
for i, key in enumerate(self.presets):
|
||||
logging.info(f'Adding preset: {key}')
|
||||
preview = Gtk.DrawingArea(width_request=da_size, height_request=da_size)
|
||||
preview.connect("draw", self.on_draw, self.presets[key])
|
||||
button = self._gtk.Button()
|
||||
button.set_image(preview)
|
||||
button.connect("clicked", self.apply_preset, self.presets[key])
|
||||
self.preset_list.attach(button, i % columns, int(i / columns) + 1, 1, 1)
|
||||
|
||||
scroll = self._gtk.ScrolledWindow()
|
||||
scroll.add(self.preset_list)
|
||||
if self._screen.vertical_mode:
|
||||
grid.attach(scroll, 0, 1, 2, 1)
|
||||
else:
|
||||
grid.attach(scroll, 2, 0, 1, 1)
|
||||
return grid
|
||||
|
||||
def on_draw(self, da, ctx, color=None):
|
||||
if color is None:
|
||||
color = self.color_data
|
||||
ctx.set_source_rgb(color[0], color[1], color[2])
|
||||
# Set the size of the rectangle
|
||||
width = height = da.get_allocated_width() * .9
|
||||
x = da.get_allocated_width() * .05
|
||||
# Set the radius of the corners
|
||||
radius = width / 2 * 0.2
|
||||
ctx.arc(x + radius, radius, radius, pi, 3 * pi / 2)
|
||||
ctx.arc(x + width - radius, radius, radius, 3 * pi / 2, 0)
|
||||
ctx.arc(x + width - radius, height - radius, radius, 0, pi / 2)
|
||||
ctx.arc(x + radius, height - radius, radius, pi / 2, pi)
|
||||
ctx.close_path()
|
||||
ctx.fill()
|
||||
|
||||
def update_preview(self, args):
|
||||
self.update_color_data()
|
||||
self.preview.queue_draw()
|
||||
self.preview_label.set_label(self.rgbw_to_hex(self.color_data))
|
||||
|
||||
def process_update(self, action, data):
|
||||
if action != 'notify_status_update':
|
||||
return
|
||||
if self.current_led in data and "color_data" in data[self.current_led]:
|
||||
self.update_scales(data[self.current_led]["color_data"][0])
|
||||
self.preview.queue_draw()
|
||||
|
||||
def update_scales(self, color_data):
|
||||
for idx in self.scales:
|
||||
self.scales[idx].set_value(int(color_data[idx] * 255))
|
||||
self.color_data[idx] = color_data[idx]
|
||||
|
||||
def update_color_data(self):
|
||||
for idx in self.scales:
|
||||
self.color_data[idx] = round(self.scales[idx].get_value() / 255, 4)
|
||||
|
||||
def apply_preset(self, widget, color_data):
|
||||
self.update_scales(color_data)
|
||||
self.apply_scales()
|
||||
|
||||
def apply_scales(self, *args):
|
||||
self.update_color_data()
|
||||
self.set_led_color(self.color_data)
|
||||
|
||||
def set_led_color(self, color_data):
|
||||
name = self.current_led.split()[1] if len(self.current_led.split()) > 1 else self.current_led
|
||||
self._screen._send_action(None, "printer.gcode.script",
|
||||
{"script": KlippyGcodes.set_led_color(name, color_data)})
|
||||
|
||||
@staticmethod
|
||||
def parse_presets(presets_data) -> {}:
|
||||
parsed = {}
|
||||
for i, preset in enumerate(presets_data.values()):
|
||||
name = i if preset["name"] == '' else preset["name"].lower()
|
||||
parsed[name] = []
|
||||
for color in ["red", "green", "blue", "white"]:
|
||||
if color not in preset or preset[color] is None:
|
||||
parsed[name].append(0)
|
||||
continue
|
||||
parsed[name].append(round(preset[color] / 255, 4))
|
||||
return parsed
|
||||
|
||||
@staticmethod
|
||||
def rgbw_to_hex(color):
|
||||
hex_color = '#'
|
||||
for value in color[:3]:
|
||||
int_value = round(value * 255)
|
||||
hex_color += hex(int_value)[2:].zfill(2)
|
||||
alpha = round(color[3] * 255)
|
||||
hex_color += hex(alpha)[2:].zfill(2)
|
||||
return hex_color.upper()
|
||||
@@ -261,6 +261,8 @@ class KlipperScreen(Gtk.Window):
|
||||
requested_updates['objects'][f] = ["enabled", "filament_detected"]
|
||||
for p in self.printer.get_output_pins():
|
||||
requested_updates['objects'][p] = ["value"]
|
||||
for led in self.printer.get_leds():
|
||||
requested_updates['objects'][led] = ["color_data"]
|
||||
|
||||
self._ws.klippy.object_subscription(requested_updates)
|
||||
|
||||
@@ -904,6 +906,7 @@ class KlipperScreen(Gtk.Window):
|
||||
+ self.printer.get_fans()
|
||||
+ self.printer.get_filament_sensors()
|
||||
+ self.printer.get_output_pins()
|
||||
+ self.printer.get_leds()
|
||||
)
|
||||
|
||||
data = self.apiclient.send_request("printer/objects/query?" + "&".join(PRINTER_BASE_STATUS_OBJECTS +
|
||||
|
||||
Reference in new issue
Block a user