Exemplo n.º 1
0
    def __init__(self, kernel, index=0, *args, **kwargs):
        Service.__init__(self, kernel, "rotary/{index}".format(index=index))
        self.index = index
        _ = kernel.translation

        @self.console_command(
            "rotary",
            help=_("Rotary base command"),
            output_type="rotary",
        )
        def rotary(command, channel, _, data=None, **kwargs):
            channel(
                "Rotary {index} set to scale: {x}, scale:{y}".format(
                    index=self.index, x=self.scale_x, y=self.scale_y
                )
            )
            return "rotary", None

        @self.console_command("rotaryscale", help=_("Rotary Scale selected elements"))
        def apply_rotary_scale(*args, **kwargs):
            sx = self.scale_x
            sy = self.scale_y
            x, y = self.device.current
            matrix = Matrix("scale(%f, %f, %f, %f)" % (sx, sy, x, y))
            for node in self.elements.elems():
                if hasattr(node, "rotary_scale"):
                    # This element is already scaled
                    return
                try:
                    node.rotary_scale = sx, sy
                    node.matrix *= matrix
                    node.modified()
                except AttributeError:
                    pass
Exemplo n.º 2
0
    def __init__(self, kernel, *args, **kwargs):
        Service.__init__(self, kernel, "keymap")
        self.keymap = {}
        self.triggered = {}

        _ = self._

        @self.console_command("bind", help=_("bind <key> <console command>"))
        def bind(command, channel, _, args=tuple(), **kwgs):
            """
            Binds a key to a given keyboard keystroke.
            """
            if len(args) == 0:
                channel(_("Binds:"))

                def keymap_index(key):
                    mods, key = key.rsplit("+", 1) if "+" in key else ("", key)
                    return (
                        mods,
                        len(key) if len(key) <= 2 else 3,
                        key,
                    )

                channel(_("    Key                    Command"))
                for i, key in enumerate(
                        sorted(self.keymap.keys(), key=keymap_index)):
                    value = self.keymap[key]
                    channel("%2d: %s %s" % (i, key.ljust(22), value))
                channel("----------")
                return
            key = args[0].lower()
            if key == "default":
                self.default_keymap()
                channel(_("Keymap set to default."))
                return
            command_line = " ".join(args[1:])
            f = command_line.find("bind")
            if f == -1:  # If bind value has a bind, do not evaluate.
                if "$x" in command_line:
                    x, y = self.device.current
                    command_line = command_line.replace("$x", str(x))
                if "$y" in command_line:
                    x, y = self.device.current
                    command_line = command_line.replace("$y", str(y))
            if len(command_line) != 0:
                self.keymap[key] = command_line
            elif key in self.keymap:
                del self.keymap[key]
                channel(_("Unbound %s") % key)
            return

        self.read_persistent_string_dict(self.keymap, suffix=True)
        if not len(self.keymap):
            self.default_keymap()
Exemplo n.º 3
0
 def __init__(self, kernel, *args, **kwargs):
     Service.__init__(self, kernel, "planner")
     self._plan = dict()
     self._default_plan = "0"
Exemplo n.º 4
0
    def __init__(self, kernel, path, *args, **kwargs):
        Service.__init__(self, kernel, path)
        self.name = "MoshiDevice"

        self.setting(bool, "opt_rapid_between", True)
        self.setting(int, "opt_jog_mode", 0)
        self.setting(int, "opt_jog_minimum", 256)

        self.setting(int, "usb_index", -1)
        self.setting(int, "usb_bus", -1)
        self.setting(int, "usb_address", -1)
        self.setting(int, "usb_version", -1)
        self.setting(bool, "mock", False)

        self.setting(bool, "home_right", False)
        self.setting(bool, "home_bottom", False)
        self.setting(str, "home_x", "0mm")
        self.setting(str, "home_y", "0mm")
        self.setting(bool, "enable_raster", True)

        self.setting(int, "packet_count", 0)
        self.setting(int, "rejected_count", 0)
        self.setting(int, "rapid_speed", 40)

        _ = self._
        choices = [
            {
                "attr": "label",
                "object": self,
                "default": path,
                "type": str,
                "label": _("Label"),
                "tip": _("What is this device called."),
            },
            {
                "attr": "bedwidth",
                "object": self,
                "default": "330mm",
                "type": str,
                "label": _("Width"),
                "tip": _("Width of the laser bed."),
            },
            {
                "attr": "bedheight",
                "object": self,
                "default": "210mm",
                "type": str,
                "label": _("Height"),
                "tip": _("Height of the laser bed."),
            },
            {
                "attr":
                "scale_x",
                "object":
                self,
                "default":
                1.000,
                "type":
                float,
                "label":
                _("X Scale Factor"),
                "tip":
                _("Scale factor for the X-axis. Board units to actual physical units."
                  ),
            },
            {
                "attr":
                "scale_y",
                "object":
                self,
                "default":
                1.000,
                "type":
                float,
                "label":
                _("Y Scale Factor"),
                "tip":
                _("Scale factor for the Y-axis. Board units to actual physical units."
                  ),
            },
            {
                "attr": "interpolate",
                "object": self,
                "default": 50,
                "type": int,
                "label": _("Curve Interpolation"),
                "tip": _("Distance of the curve interpolation in mils"),
            },
            {
                "attr":
                "mock",
                "object":
                self,
                "default":
                False,
                "type":
                bool,
                "label":
                _("Run mock-usb backend"),
                "tip":
                _("This starts connects to fake software laser rather than real one for debugging."
                  ),
            },
        ]
        self.register_choices("bed_dim", choices)
        ViewPort.__init__(
            self,
            self.bedwidth,
            self.bedheight,
            user_scale_x=self.scale_x,
            user_scale_y=self.scale_y,
            native_scale_x=UNITS_PER_MIL,
            native_scale_y=UNITS_PER_MIL,
            # flip_x=self.flip_x,
            # flip_y=self.flip_y,
            # swap_xy=self.swap_xy,
            origin_x=1.0 if self.home_right else 0.0,
            origin_y=1.0 if self.home_bottom else 0.0,
        )

        self.state = 0

        self.driver = MoshiDriver(self)
        self.add_service_delegate(self.driver)

        self.controller = MoshiController(self)
        self.add_service_delegate(self.controller)

        self.spooler = Spooler(self, driver=self.driver)
        self.add_service_delegate(self.spooler)

        _ = self.kernel.translation

        @self.console_command("usb_connect", help=_("Connect USB"))
        def usb_connect(command, channel, _, **kwargs):
            """
            Force USB Connection Event for Moshiboard
            """
            try:
                self.controller.open()
            except ConnectionRefusedError:
                channel("Connection Refused.")

        @self.console_command("usb_disconnect", help=_("Disconnect USB"))
        def usb_disconnect(command, channel, _, **kwargs):
            """
            Force USB Disconnect Event for Moshiboard
            """
            try:
                self.controller.close()
            except ConnectionError:
                channel("Usb is not connected.")

        @self.console_command("start", help=_("Start Pipe to Controller"))
        def pipe_start(command, channel, _, data=None, **kwargs):
            """
            Start output sending.
            """
            self.controller.update_state(STATE_ACTIVE)
            self.controller.start()
            channel("Moshi Channel Started.")

        @self.console_command("hold",
                              input_type="moshi",
                              help=_("Hold Controller"))
        def pipe_pause(command, channel, _, **kwargs):
            """
            Pause output sending.
            """
            self.controller.update_state(STATE_PAUSE)
            self.controller.pause()
            channel(_("Moshi Channel Paused."))

        @self.console_command("resume",
                              input_type="moshi",
                              help=_("Resume Controller"))
        def pipe_resume(command, channel, _, **kwargs):
            """
            Resume output sending.
            """
            self.controller.update_state(STATE_ACTIVE)
            self.controller.start()
            channel(_("Moshi Channel Resumed."))

        @self.console_command(("estop", "abort"), help=_("Abort Job"))
        def pipe_abort(command, channel, _, **kwargs):
            """
            Abort output job. Usually due to the functionality of Moshiboards this will do
            nothing as the job will have already sent to the backend.
            """
            self.controller.estop()
            channel(_("Moshi Channel Aborted."))

        @self.console_command(
            "status",
            input_type="moshi",
            help=_("Update moshiboard controller status"),
        )
        def realtime_status(channel, _, **kwargs):
            """
            Updates the CH341 Status information for the Moshiboard.
            """
            try:
                self.controller.update_status()
            except ConnectionError:
                channel(_("Could not check status, usb not connected."))

        @self.console_command(
            "continue",
            help=_("abort waiting process on the controller."),
        )
        def realtime_pause(**kwargs):
            """
            Abort the waiting process for Moshiboard. This is usually a wait from BUSY (207) state until the board
            reports its status as READY (205)
            """
            self.controller.abort_waiting = True
Exemplo n.º 5
0
    def __init__(self, kernel, *args, **kwargs):
        Service.__init__(self, kernel, "alias")
        self.aliases = {}
        _ = self._

        @self.console_argument("alias", type=str, help=_("alias command"))
        @self.console_command(
            "alias",
            help=_("alias <alias> <console commands[;console command]*>"))
        def alias(command, channel, _, alias=None, remainder=None, **kwgs):
            _ = self._
            if alias is None:
                reverse_keymap = {v: k for k, v in self.bind.keymap.items()}
                channel(_("Aliases (keybind)`:"))
                channel(_("    Alias                  Command(s)"))
                last = None
                i = -1
                for key in sorted(
                        self.aliases.keys(),
                        key=lambda x: x
                        if x[0] not in "+-" else "+" + x[1:] + x[0],
                ):
                    value = self.aliases[key]
                    keystroke = reverse_keymap[
                        key] if key in reverse_keymap else ""
                    if last is None or last[0] != "+" or key[0] != "-":
                        i += 1
                    if keystroke and len(key) + len(keystroke) < 18:
                        key += " (%s)" % keystroke
                        keystroke = ""
                    if keystroke:
                        channel("%2d: (%s)" % (i, keystroke))
                    if last and last[0] == "+" and key[0] == "-":
                        channel("    %s %s" % (key.ljust(22), value))
                    elif keystroke:
                        channel("    %s %s" % (key.ljust(22), value))
                    else:
                        channel("%2d: %s %s" % (i, key.ljust(22), value))
                    last = key

                channel("----------")
                return
            alias = alias.lower()
            if alias == "default":
                self.default_alias()
                channel(_("Aliases set to default."))
                return
            if remainder is None:
                if alias in self.aliases:
                    del self.aliases[alias]
                    channel(_("Alias %s unset.") % alias)
                else:
                    channel(_("No alias for %s was set.") % alias)
            else:
                self.aliases[alias] = remainder

        @self.console_command(".*", regex=True, hidden=True)
        def alias_execute(command, **kwgs):
            """
            Alias execution code. Checks value for matching alias and utilizes that.

            Aliases with ; delimit multipart commands
            """
            if command in self.aliases:
                aliased_command = self.aliases[command]
                for cmd in aliased_command.split(";"):
                    self(f"{cmd}\n")
            else:
                raise CommandMatchRejected(_("This is not an alias."))

        self.read_persistent_string_dict(self.aliases, suffix=True)
        if not len(self.aliases):
            self.default_alias()
Exemplo n.º 6
0
    def __init__(self, kernel, path, *args, **kwargs):
        Service.__init__(self, kernel, path)
        self.name = "Dummy Device"
        self.native_x = 0.0
        self.native_y = 0.0
        self.settings = dict()
        self.state = 0
        self.spooler = Spooler(self, "default")
        self.viewbuffer = ""
        self.label = "Dummy Device"

        _ = self.kernel.translation
        choices = [
            {
                "attr": "bedwidth",
                "object": self,
                "default": "320mm",
                "type": str,
                "label": _("Width"),
                "tip": _("Width of the laser bed."),
            },
            {
                "attr": "bedheight",
                "object": self,
                "default": "220mm",
                "type": str,
                "label": _("Height"),
                "tip": _("Height of the laser bed."),
            },
            {
                "attr":
                "scale_x",
                "object":
                self,
                "default":
                1.000,
                "type":
                float,
                "label":
                _("X Scale Factor"),
                "tip":
                _("Scale factor for the X-axis. Board units to actual physical units."
                  ),
            },
            {
                "attr":
                "scale_y",
                "object":
                self,
                "default":
                1.000,
                "type":
                float,
                "label":
                _("Y Scale Factor"),
                "tip":
                _("Scale factor for the Y-axis. Board units to actual physical units."
                  ),
            },
        ]
        self.register_choices("bed_dim", choices)
        ViewPort.__init__(
            self,
            width=self.bedwidth,
            height=self.bedheight,
            native_scale_x=UNITS_PER_MIL,
            native_scale_y=UNITS_PER_MIL,
            origin_x=0.0,
            origin_y=0.0,
        )

        @self.console_command(
            "spool",
            help=_("spool <command>"),
            regex=True,
            input_type=(None, "plan", "device"),
            output_type="spooler",
        )
        def spool(command, channel, _, data=None, remainder=None, **kwgs):
            spooler = self.spooler
            if data is not None:
                # If plan data is in data, then we copy that and move on to next step.
                spooler.laserjob(data.plan)
                channel(_("Spooled Plan."))
                self.signal("plan", data.name, 6)

            if remainder is None:
                channel(_("----------"))
                channel(_("Spoolers:"))
                for d, d_name in enumerate(self.match("device", suffix=True)):
                    channel("%d: %s" % (d, d_name))
                channel(_("----------"))
                channel(_("Spooler on device %s:" % str(self.label)))
                for s, op_name in enumerate(spooler.queue):
                    channel("%d: %s" % (s, op_name))
                channel(_("----------"))

            return "spooler", spooler
Exemplo n.º 7
0
    def __init__(self, kernel, camera_path, *args, **kwargs):
        Service.__init__(self, kernel, camera_path)
        self.uri = 0
        self.fisheye_k = None
        self.fisheye_d = None
        self.perspective_x1 = None
        self.perspective_y1 = None
        self.perspective_x2 = None
        self.perspective_y2 = None
        self.perspective_x3 = None
        self.perspective_y3 = None
        self.perspective_x4 = None
        self.perspective_y4 = None
        self.camera_job = None

        self.current_frame = None
        self.last_frame = None

        self.current_raw = None
        self.last_raw = None

        self.capture = None
        self.image_width = -1
        self.image_height = -1

        # Used during calibration.
        self._object_points = []  # 3d point in real world space
        self._image_points = []  # 2d points in image plane.

        self.camera_lock = threading.Lock()

        self.connection_attempts = 0
        self.frame_attempts = 0
        self.frame_index = 0
        self.quit_thread = False
        self.camera_thread = None
        self.max_tries_connect = 10
        self.max_tries_frame = 10
        self.setting(int, "width", 640)
        self.setting(int, "height", 480)
        self.setting(int, "fps", 1)
        self.setting(bool, "correction_fisheye", False)
        self.setting(bool, "correction_perspective", False)
        self.setting(str, "fisheye", "")
        self.setting(float, "perspective_x1", None)
        self.setting(float, "perspective_y1", None)
        self.setting(float, "perspective_x2", None)
        self.setting(float, "perspective_y2", None)
        self.setting(float, "perspective_x3", None)
        self.setting(float, "perspective_y3", None)
        self.setting(float, "perspective_x4", None)
        self.setting(float, "perspective_y4", None)
        self.setting(str, "uri", "0")
        self.setting(int, "index", 0)
        self.setting(bool, "autonormal", False)
        self.setting(bool, "aspect", False)
        self.setting(str, "preserve_aspect", "xMinYMin meet")

        # TODO: regex confirm fisheye
        if self.fisheye is not None and len(self.fisheye) != 0:
            self.fisheye_k, self.fisheye_d = eval(self.fisheye)
        try:
            self.uri = int(self.uri)  # URI is an index.
        except ValueError:
            pass