Skip to content

Commit 1b3ea8e

Browse files
committed
Connected amu_manager with printer and mainwindow and lowercase AMUFolder and implement simple parts of AMU backend
1 parent 0edfc1f commit 1b3ea8e

5 files changed

Lines changed: 52 additions & 12 deletions

File tree

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ def __init__(self, parent: QtCore.QObject | None = None) -> None:
4343
self.__setup_configfile()
4444

4545
def __setup_configfile(self) -> None:
46-
"""
47-
Sets up local configfile variable
48-
49-
Raises:
50-
FileNotFoundError: File Not Found
51-
"""
52-
self._config_filename = CONFIG_PATH
46+
"""Sets up local configfile variable"""
47+
self._config_filename: Path | None = CONFIG_PATH
5348
if not self._config_filename.exists():
54-
raise FileNotFoundError(("Config file not found %s", self._config_filename))
49+
logger.warning("Config file not found %s", self._config_filename)
50+
self._config_filename = None
5551

5652
def _apply_patterns(self, state: bool) -> bool:
5753
"""Method that comments/uncomments the AMU_FILES from the printer.cfg according with state value
@@ -62,6 +58,10 @@ def _apply_patterns(self, state: bool) -> bool:
6258
Returns:
6359
bool: True: Success, False: Failed
6460
"""
61+
if self._config_filename is None:
62+
logger.warning("_apply_patterns called but no config file available")
63+
return False
64+
6565
if self._amu_state == state:
6666
return False
6767

@@ -122,7 +122,9 @@ def set_gate_info(
122122
color (str): Filament color as hex string, e.g. ``"ff56e0"``.
123123
spool_id (int): Spoolman spool ID, or -1 if not tracked.
124124
"""
125-
...
125+
self.run_gcode_signal.emit(
126+
f"MMU_GATE_MAP gate={gate} MATERIAL={material} COLOR={color} SPOOLID={spool_id}"
127+
)
126128

127129
def set_gate_material(self, gate: int, material: str) -> None:
128130
"""Set the `material` at the gate `gate`
@@ -131,6 +133,7 @@ def set_gate_material(self, gate: int, material: str) -> None:
131133
gate (int): Gate index (0-based).
132134
material (str): Filament material name, e.g. ``"PLA"``.
133135
"""
136+
self.run_gcode_signal.emit(f"MMU_GATE_MAP gate={gate} MATERIAL={material}")
134137

135138
def set_gate_color(self, gate: int, color: str) -> None:
136139
"""Set the `color` at the gate `gate`
@@ -139,6 +142,7 @@ def set_gate_color(self, gate: int, color: str) -> None:
139142
gate (int): Gate index (0-based).
140143
color (str): Filament color, e.g. ``"ff56e0"``.
141144
"""
145+
self.run_gcode_signal.emit(f"MMU_GATE_MAP gate={gate} COLOR={color}")
142146

143147
def set_gate_spool(self, gate: int, spool_id: int) -> None:
144148
"""Set the `spool_id` at the gate `gate`
@@ -147,31 +151,53 @@ def set_gate_spool(self, gate: int, spool_id: int) -> None:
147151
gate (int): Gate index (0-based).
148152
spool_id (int): Spoolman spool ID, or -1 to clear.
149153
"""
154+
self.run_gcode_signal.emit(f"MMU_GATE_MAP gate={gate} SPOOLID={spool_id}")
150155

151156
def home_mmu(self) -> None:
152157
"""Home the MMU selector by sending MMU_HOME."""
158+
self.run_gcode_signal.emit("MMU_HOME")
153159

154160
def reset_mmu(self) -> None:
155161
"""Reset the MMU and clear any pause or error state by sending MMU_RESET."""
162+
self.run_gcode_signal.emit("MMU_RESET")
156163

157164
def load_gate(self, gate: int) -> None:
158165
"""Load filament from the specified gate by sending MMU_LOAD
159166
160167
Args:
161168
gate (int): Gate index to select (0-based)
162169
"""
170+
self.run_gcode_signal.emit(f"MMU_SELECT gate={gate}\nMMU_LOAD")
163171

164172
def unload(self) -> None:
165173
"""Unload the currently loaded filament by sending MMU_UNLOAD."""
166174
...
175+
self.run_gcode_signal.emit("MMU_UNLOAD")
176+
177+
def eject_gate(self, gate: int) -> None:
178+
"""Fully eject filament from gate, releasing from MMU gear.
179+
180+
Args:
181+
gate: Gate index to eject from, on None to use currently selected gate.
182+
"""
183+
self.run_gcode_signal.emit(f"MMU_EJECT GATE={gate}")
184+
185+
def eject_all_gates(self, num_gates: int) -> None:
186+
"""Fully eject filament from all gates sequentially(amu_manager.get_state().num_gates)
187+
188+
Args:
189+
num_gates: Total number of gates(from MMUState.num_gates)
190+
"""
191+
cmd = "\n".join(f"MMU_EJECT GATE={i}" for i in range(num_gates))
192+
self.run_gcode_signal.emit(cmd)
167193

168194
def select_tool(self, tool: int) -> None:
169195
"""Select a tool, triggering a filament change if needed.
170196
171197
Args:
172198
tool (int): Tool index to select (0-based).
173199
"""
174-
...
200+
self.run_gcode_signal.emit(f"MMU_CHANGE_TOOL TOOL={tool}")
175201

176202
@QtCore.pyqtSlot(dict, name="update_mmu_state")
177203
def update_mmu_state(self, data: dict) -> None:
@@ -184,7 +210,11 @@ def update_mmu_state(self, data: dict) -> None:
184210
Args:
185211
data (dict): Raw MMU status or diff dict from Moonraker
186212
"""
187-
...
213+
if self._mmu_state is None:
214+
self._mmu_state = MMUState.from_status(data)
215+
else:
216+
self._mmu_state = self._mmu_state.apply_diff(data)
217+
self.mmu_state_changed.emit(self._mmu_state)
188218

189219

190220
if __name__ == "__main__":

BlocksScreen/lib/panels/mainWindow.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import events
66
from configfile import BlocksScreenConfig, get_configparser
7+
from devices.amu.manager import AMUManager
78
from devices.storage import USBManager
89
from lib.files import Files
910
from lib.machine import MachineControl
@@ -118,8 +119,8 @@ def __init__(self):
118119
gdir = None
119120
if usb_config:
120121
gdir = usb_config.get("gcodes_dir", default=None)
121-
122122
self.usb_manager: USBManager = USBManager(parent=self, gcodes_dir=gdir)
123+
self.amu_manager: AMUManager = AMUManager(parent=self)
123124
self.ws = MoonWebSocket(self)
124125
self.notiPage = NotificationPage(self)
125126
self.mc = MachineControl(self)
@@ -200,6 +201,8 @@ def __init__(self):
200201
self.query_object_list.connect(self.utilitiesPanel.on_object_list)
201202
self.printer.extruder_update.connect(self.on_extruder_update)
202203
self.printer.heater_bed_update.connect(self.on_heater_bed_update)
204+
self.printer.mmu_updated.connect(self.amu_manager.update_mmu_state)
205+
self.amu_manager.run_gcode_signal.connect(self.ws.api.run_gcode)
203206
self.run_gcode_signal.connect(self.ws.api.run_gcode)
204207

205208
self.ui.main_content_widget.currentChanged.connect(slot=self.reset_tab_indexes)

BlocksScreen/lib/printer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class Printer(QtCore.QObject):
7979
z_tilt_update: typing.ClassVar[QtCore.pyqtSignal] = QtCore.pyqtSignal(
8080
str, bool, name="z_tilt_update"
8181
)
82+
mmu_updated: typing.ClassVar[QtCore.pyqtSignal] = QtCore.pyqtSignal(
83+
object, name="mmu-updated"
84+
)
85+
8286
config_subscription: typing.ClassVar[QtCore.pyqtSignal] = QtCore.pyqtSignal(
8387
[dict],
8488
[list],
@@ -740,3 +744,6 @@ def _unload_filament_object_updated(self, values: dict, name: str) -> None:
740744
def _load_filament_object_updated(self, values: dict, name: str) -> None:
741745
if "state" in values.keys():
742746
self.load_filament_update[bool].emit(values["state"])
747+
748+
def _mmu_object_updated(self, values: dict, name: str = "") -> None:
749+
self.mmu_updated.emit(values)

0 commit comments

Comments
 (0)