Skip to content

Commit ef8d728

Browse files
gmmcosta15HugoCLSC
andauthored
bugfix/z offset (#204)
* fix bug on save z-offset change and refactor babystep page * bugfix removal of wrong on last commit * bugfix on the permenant save of z_offset value and other bugfixes * Detect probe/endstop type at runtime * Remove unused variable * Delete duplicate variable * Format --------- Co-authored-by: HugoCLSC <hugo.santos.costa@gmail.com>
1 parent 4ee0abc commit ef8d728

2 files changed

Lines changed: 239 additions & 336 deletions

File tree

BlocksScreen/lib/panels/printTab.py

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ class PrintTab(QtWidgets.QStackedWidget):
6565
call_load_panel = QtCore.pyqtSignal(bool, str, name="call-load-panel")
6666

6767
call_cancel_panel = QtCore.pyqtSignal(bool, name="call-load-panel")
68-
_z_offset: float = 0.0
69-
_active_z_offset: float = 0.0
70-
_finish_print_handled: bool = False
7168

7269
def __init__(
7370
self,
@@ -77,6 +74,9 @@ def __init__(
7774
printer: Printer,
7875
) -> None:
7976
super().__init__(parent)
77+
self._active_z_offset: float = 0.0
78+
self._finish_print_handled: bool = False
79+
self._z_apply_command: str = "Z_OFFSET_APPLY_ENDSTOP"
8080

8181
self.setupMainPrintPage()
8282
self.ws: MoonWebSocket = ws
@@ -267,7 +267,6 @@ def __init__(
267267
self.confirmPage_widget.on_delete.connect(self.delete_file)
268268
self.change_page(self.indexOf(self.print_page)) # force set the initial page
269269
self.save_config_btn.clicked.connect(self.save_config)
270-
self.BasePopup_z_offset.accepted.connect(self.update_configuration_file)
271270

272271
@QtCore.pyqtSlot(str, dict, name="on_print_stats_update")
273272
@QtCore.pyqtSlot(str, float, name="on_print_stats_update")
@@ -321,34 +320,48 @@ def on_slidePage_request(
321320
@QtCore.pyqtSlot(str, str, name="delete_file")
322321
@QtCore.pyqtSlot(str, name="delete_file")
323322
def delete_file(self, filename: str, directory: str = "gcodes") -> None:
324-
"""Handle Delete file signal, shows confirmation dialog"""
323+
"""Handle Delete file signal, shows confirmation dialog."""
325324
self.BasePopup.set_message("Are you sure you want to delete this file?")
325+
try:
326+
self.BasePopup.accepted.disconnect()
327+
except (RuntimeError, TypeError):
328+
pass
326329
self.BasePopup.accepted.connect(
327330
lambda: self._on_delete_file_confirmed(filename, directory)
328331
)
329332
self.BasePopup.open()
330333

331334
def save_config(self) -> None:
332335
"""Handle Save configuration behaviour, shows confirmation dialog"""
333-
if self._finish_print_handled:
334-
self.run_gcode_signal.emit("Z_OFFSET_APPLY_PROBE")
335-
self._z_offset = self._active_z_offset
336-
self.babystepPage.bbp_z_offset_title_label.setText(
337-
f"Z: {self._z_offset:.3f}mm"
338-
)
336+
337+
self.babystepPage.bbp_z_offset_title_label.setText(
338+
f"Z: {self._active_z_offset:.3f}mm"
339+
)
339340
self.BasePopup_z_offset.set_message(
340-
f"The ZOffset is now {self._active_z_offset:.3f}mm.\n"
341+
f"The Z-Offset is now {self._active_z_offset:.3f} mm.\n"
341342
"Would you like to save this change permanently?\n"
342343
"The machine will restart."
343344
)
344345
self.BasePopup_z_offset.cancel_button_text("Later")
346+
try:
347+
self.BasePopup_z_offset.accepted.disconnect(self.update_configuration_file)
348+
except (RuntimeError, TypeError):
349+
pass
350+
self.BasePopup_z_offset.accepted.connect(self.update_configuration_file)
345351
self.BasePopup_z_offset.open()
346352

347-
def update_configuration_file(self):
348-
"""Runs the `SAVE_CONFIG` gcode"""
349-
self.run_gcode_signal.emit("Z_OFFSET_APPLY_PROBE")
353+
def update_configuration_file(self) -> None:
354+
"""Restore the captured offset, apply it to the probe config, then save."""
355+
try:
356+
self.BasePopup_z_offset.accepted.disconnect(self.update_configuration_file)
357+
except (RuntimeError, TypeError):
358+
pass
359+
self.run_gcode_signal.emit(
360+
f"SET_GCODE_OFFSET Z={self._active_z_offset:.3f} MOVE=0"
361+
)
362+
self.run_gcode_signal.emit(self._z_apply_command)
350363
self.run_gcode_signal.emit("SAVE_CONFIG")
351-
self.BasePopup_z_offset.disconnect()
364+
self.save_config_btn.setVisible(False)
352365

353366
@QtCore.pyqtSlot(str, list, name="activate_save_button")
354367
def activate_save_button(self, name: str, value: list) -> None:
@@ -357,15 +370,19 @@ def activate_save_button(self, name: str, value: list) -> None:
357370
return
358371

359372
if name == "homing_origin":
360-
self._active_z_offset = value[2]
361-
self.save_config_btn.setVisible(value[2] != 0)
373+
if len(value) > 2:
374+
self._active_z_offset = value[2]
375+
self.save_config_btn.setVisible(value[2] != 0)
362376

363377
def _on_delete_file_confirmed(self, filename: str, directory: str) -> None:
364-
"""Handle confirmed file deletion after user accepted the dialog"""
378+
"""Handle confirmed file deletion after user accepted the dialog."""
365379
self.file_data.on_request_delete_file(filename, directory)
366380
self.request_back.emit()
367381
self.filesPage_widget.reset_dir()
368-
self.BasePopup.disconnect()
382+
try:
383+
self.BasePopup.accepted.disconnect()
384+
except (RuntimeError, TypeError):
385+
pass
369386

370387
def setProperty(self, name: str, value: typing.Any) -> bool:
371388
"""Intercept the set property method
@@ -404,6 +421,17 @@ def klipper_ready_signal(self) -> None:
404421
"""React to klipper ready signal"""
405422
self.babystepPage.baby_stepchange = False
406423
self._finish_print_handled = False
424+
self.printer.on_subscribe_config("stepper_z", self._on_stepper_z_config)
425+
426+
def _on_stepper_z_config(self, config: dict | list) -> None:
427+
"""Select the correct Z-offset apply command based on endstop type."""
428+
if not isinstance(config, dict):
429+
return
430+
stepper_z = config.get("stepper_z", {})
431+
if stepper_z.get("endstop_pin") == "probe:z_virtual_endstop":
432+
self._z_apply_command = "Z_OFFSET_APPLY_PROBE"
433+
else:
434+
self._z_apply_command = "Z_OFFSET_APPLY_ENDSTOP"
407435

408436
@QtCore.pyqtSlot(name="finish_print_signal")
409437
def finish_print_signal(self) -> None:

0 commit comments

Comments
 (0)