diff --git a/klippy/extras/display/display.py b/klippy/extras/display/display.py index b9e42094..7ad1243e 100644 --- a/klippy/extras/display/display.py +++ b/klippy/extras/display/display.py @@ -1,6 +1,6 @@ # Basic LCD display support # -# Copyright (C) 2018-2020 Kevin O'Connor +# Copyright (C) 2018-2022 Kevin O'Connor # Copyright (C) 2018 Aleph Objects, Inc # Copyright (C) 2018 Eric Callahan # @@ -83,47 +83,20 @@ class DisplayGroup: display.draw_text(row, col, text.replace('\n', ''), eventtime) context.clear() # Remove circular references for better gc -class PrinterLCD: +# Global cache of DisplayTemplate, DisplayGroup, and glyphs +class PrinterDisplayTemplate: def __init__(self, config): self.printer = config.get_printer() - self.reactor = self.printer.get_reactor() - # Load low-level lcd handler - self.lcd_chip = config.getchoice('lcd_type', LCD_chips)(config) - # Load menu and display_status - self.menu = None - name = config.get_name() - if name == 'display': - # only load menu for primary display - self.menu = menu.MenuManager(config, self) - self.printer.load_object(config, "display_status") - # Configurable display self.display_templates = {} self.display_data_groups = {} + self.display_glyphs = {} self.load_config(config) - dgroup = "_default_16x4" - if self.lcd_chip.get_dimensions()[0] == 20: - dgroup = "_default_20x4" - dgroup = config.get('display_group', dgroup) - self.show_data_group = self.display_data_groups.get(dgroup) - if self.show_data_group is None: - raise config.error("Unknown display_data group '%s'" % (dgroup,)) - # Screen updating - self.printer.register_event_handler("klippy:ready", self.handle_ready) - self.screen_update_timer = self.reactor.register_timer( - self.screen_update_event) - self.redraw_request_pending = False - self.redraw_time = 0. - # Register g-code commands - gcode = self.printer.lookup_object("gcode") - gcode.register_mux_command('SET_DISPLAY_GROUP', 'DISPLAY', name, - self.cmd_SET_DISPLAY_GROUP, - desc=self.cmd_SET_DISPLAY_GROUP_help) - if name == 'display': - gcode.register_mux_command('SET_DISPLAY_GROUP', 'DISPLAY', None, - self.cmd_SET_DISPLAY_GROUP) - def get_dimensions(self): - return self.lcd_chip.get_dimensions() - # Configurable display + def get_display_templates(self): + return self.display_templates + def get_display_data_groups(self): + return self.display_data_groups + def get_display_glyphs(self): + return self.display_glyphs def _parse_glyph(self, config, glyph_name, data, width, height): glyph_data = [] for line in data.split('\n'): @@ -170,7 +143,7 @@ class PrinterLCD: self.display_data_groups[group_name] = dg # Load display glyphs dg_prefix = 'display_glyph ' - icons = {} + self.display_glyphs = icons = {} dg_main = config.get_prefix_sections(dg_prefix) dg_main_names = {c.get_name(): 1 for c in dg_main} dg_def = [c for c in dconfig.get_prefix_sections(dg_prefix) @@ -188,8 +161,56 @@ class PrinterLCD: slot = dg.getint('hd44780_slot', minval=0, maxval=7) idata = self._parse_glyph(config, glyph_name, data, 5, 8) icons.setdefault(glyph_name, {})['icon5x8'] = (slot, idata) - self.lcd_chip.set_glyphs(icons) - # Initialization + +def lookup_display_templates(config): + printer = config.get_printer() + dt = printer.lookup_object("display_template", None) + if dt is None: + dt = PrinterDisplayTemplate(config) + printer.add_object("display_template", dt) + return dt + +class PrinterLCD: + def __init__(self, config): + self.printer = config.get_printer() + self.reactor = self.printer.get_reactor() + # Load low-level lcd handler + self.lcd_chip = config.getchoice('lcd_type', LCD_chips)(config) + # Load menu and display_status + self.menu = None + name = config.get_name() + if name == 'display': + # only load menu for primary display + self.menu = menu.MenuManager(config, self) + self.printer.load_object(config, "display_status") + # Configurable display + templates = lookup_display_templates(config) + self.display_templates = templates.get_display_templates() + self.display_data_groups = templates.get_display_data_groups() + self.lcd_chip.set_glyphs(templates.get_display_glyphs()) + dgroup = "_default_16x4" + if self.lcd_chip.get_dimensions()[0] == 20: + dgroup = "_default_20x4" + dgroup = config.get('display_group', dgroup) + self.show_data_group = self.display_data_groups.get(dgroup) + if self.show_data_group is None: + raise config.error("Unknown display_data group '%s'" % (dgroup,)) + # Screen updating + self.printer.register_event_handler("klippy:ready", self.handle_ready) + self.screen_update_timer = self.reactor.register_timer( + self.screen_update_event) + self.redraw_request_pending = False + self.redraw_time = 0. + # Register g-code commands + gcode = self.printer.lookup_object("gcode") + gcode.register_mux_command('SET_DISPLAY_GROUP', 'DISPLAY', name, + self.cmd_SET_DISPLAY_GROUP, + desc=self.cmd_SET_DISPLAY_GROUP_help) + if name == 'display': + gcode.register_mux_command('SET_DISPLAY_GROUP', 'DISPLAY', None, + self.cmd_SET_DISPLAY_GROUP) + def get_dimensions(self): + return self.lcd_chip.get_dimensions() def handle_ready(self): self.lcd_chip.init() # Start screen update timer