11import enum
22from functools import partial
33
4-
4+ import logging
55from lib .printer import Printer
66from lib .filament import Filament
77from lib .ui .filamentStackedWidget_ui import Ui_filamentStackedWidget
8-
98from lib .panels .widgets .popupDialogWidget import Popup
109from PyQt6 import QtCore , QtGui , QtWidgets
1110
11+ logger = logging .getLogger (__name__ )
12+
13+
14+ class FilamentTypes (enum .Enum ):
15+ PLA = Filament (name = "PLA" , temperature = 220 )
16+ PETG = Filament (name = "PETG" , temperature = 240 )
17+ ABS = Filament (name = "ABS" , temperature = 250 )
18+ HIPS = Filament (name = "HIPS" , temperature = 250 )
19+ NYLON = Filament (name = "NYLON" , temperature = 270 )
20+ TPU = Filament (name = "TPU" , temperature = 230 )
21+ UNKNOWN = Filament (name = "UNKNOWN" , temperature = 250 )
22+
1223
1324class FilamentTab (QtWidgets .QStackedWidget ):
1425 request_filament_change_page = QtCore .pyqtSignal (name = "filament_change_page" )
1526 request_filament_load = QtCore .pyqtSignal (name = "filament_load_t1" )
1627 request_back = QtCore .pyqtSignal (name = "request_back" )
1728 request_change_page = QtCore .pyqtSignal (int , int , name = "request_change_page" )
29+ request_change_tab = QtCore .pyqtSignal (int , name = "request_change_tab" )
1830 request_toolhead_count = QtCore .pyqtSignal (int , name = "toolhead_number_received" )
1931 run_gcode = QtCore .pyqtSignal (str , name = "run_gcode" )
2032 call_load_panel = QtCore .pyqtSignal (bool , str , name = "call-load-panel" )
2133
22- class FilamentTypes (enum .Enum ):
23- PLA = Filament (name = "PLA" , temperature = 220 )
24-
2534 class FilamentStates (enum .Enum ):
2635 LOADED = enum .auto ()
2736 UNLOADED = enum .auto ()
@@ -43,37 +52,45 @@ def __init__(self, parent: QtWidgets.QWidget, printer: Printer, ws, /) -> None:
4352 self .popup = Popup (self )
4453 self .has_load_unload_objects = None
4554 self ._filament_state = self .FilamentStates .UNKNOWN
46- self ._sensor_states = {}
47- self .filament_type : Filament | None = None
55+ self .filament_type = FilamentTypes .UNKNOWN
56+
57+ cfg = parent .config
58+ if cfg .has_section ("filament_presence" ):
59+ i = cfg .get_section ("filament_presence" , None )
60+ self .filament_sensor = i .get ("name" , str , None )
61+ else :
62+ self .filament_sensor = None
4863 self .panel .filament_page_load_btn .clicked .connect (
4964 partial (self .change_page , self .indexOf (self .panel .load_page ))
5065 )
5166 self .panel .custom_filament_header_back_btn .clicked .connect (self .back_button )
5267 self .panel .load_custom_btn .hide ()
5368 self .panel .load_header_back_button .clicked .connect (self .back_button )
5469 self .panel .load_pla_btn .clicked .connect (
55- partial (self .load_filament , toolhead = 0 , temp = 220 )
70+ partial (self .load_filament , toolhead = 0 , filament = FilamentTypes . PLA )
5671 )
5772 self .panel .load_petg_btn .clicked .connect (
58- partial (self .load_filament , toolhead = 0 , temp = 240 )
73+ partial (self .load_filament , toolhead = 0 , filament = FilamentTypes . PETG )
5974 )
6075 self .panel .load_abs_btn .clicked .connect (
61- partial (self .load_filament , toolhead = 0 , temp = 250 )
76+ partial (self .load_filament , toolhead = 0 , filament = FilamentTypes . ABS )
6277 )
6378 self .panel .load_hips_btn .clicked .connect (
64- partial (self .load_filament , toolhead = 0 , temp = 250 )
79+ partial (self .load_filament , toolhead = 0 , filament = FilamentTypes . HIPS )
6580 )
6681 self .panel .load_nylon_btn .clicked .connect (
67- partial (self .load_filament , toolhead = 0 , temp = 270 )
82+ partial (self .load_filament , toolhead = 0 , filament = FilamentTypes . NYLON )
6883 )
6984 self .panel .load_tpu_btn .clicked .connect (
70- partial (self .load_filament , toolhead = 0 , temp = 230 )
85+ partial (self .load_filament , toolhead = 0 , filament = FilamentTypes . TPU )
7186 )
7287 self .panel .filament_page_unload_btn .clicked .connect (
7388 lambda : self .unload_filament (toolhead = 0 , temp = 250 )
7489 )
90+ self .panel .main_back_button .clicked .connect (
91+ lambda : self .request_change_tab .emit (0 )
92+ )
7593 self .run_gcode .connect (self .ws .api .run_gcode )
76- self .printer .extruder_update .connect (self .on_extruder_update )
7794 self .printer .unload_filament_update .connect (self .on_unload_filament )
7895 self .printer .load_filament_update .connect (self .on_load_filament )
7996 self .printer .filament_switch_sensor_update .connect (
@@ -84,8 +101,18 @@ def __init__(self, parent: QtWidgets.QWidget, printer: Printer, ws, /) -> None:
84101 self .printer .print_stats_update [str , dict ].connect (self .on_print_stats_update )
85102 self .printer .print_stats_update [str , float ].connect (self .on_print_stats_update )
86103
87- self .loadignore = True
88- self .unloadignore = True
104+ self .printer .save_variables_update .connect (self .on_save_variables_update )
105+ self .state = "standby"
106+
107+ def on_save_variables_update (self , save_variables : dict ):
108+ """Handle query response"""
109+ for i in FilamentTypes :
110+ if i .value .name in save_variables ["variables" ]["filament_type" ]:
111+ self .filament_type = i
112+ break
113+ else :
114+ self .filament_type = FilamentTypes .UNKNOWN
115+ self .panel .label_2 .setText (self .filament_type .value .name )
89116
90117 @QtCore .pyqtSlot (str , dict , name = "on_print_stats_update" )
91118 @QtCore .pyqtSlot (str , float , name = "on_print_stats_update" )
@@ -94,9 +121,23 @@ def on_print_stats_update(self, field: str, value: dict | float | str) -> None:
94121 """Handle print stats object update"""
95122 if isinstance (value , str ):
96123 if "state" in field :
124+ self .state = value
125+ if value in ("printing" , "pausing" , "paused" , "resuming" ):
126+ self .panel .main_back_button .show ()
127+ self .panel .spacerItem1 .changeSize (
128+ 60 ,
129+ 0 ,
130+ QtWidgets .QSizePolicy .Policy .Minimum ,
131+ QtWidgets .QSizePolicy .Policy .Minimum ,
132+ )
97133 if value in ("standby" ):
98- self .loadignore = True
99- self .unloadignore = True
134+ self .panel .main_back_button .hide ()
135+ self .panel .spacerItem1 .changeSize (
136+ 0 ,
137+ 0 ,
138+ QtWidgets .QSizePolicy .Policy .Minimum ,
139+ QtWidgets .QSizePolicy .Policy .Minimum ,
140+ )
100141
101142 @QtCore .pyqtSlot (str , str , bool , name = "on_filament_sensor_update" )
102143 def on_filament_sensor_update (self , sensor_name : str , parameter : str , value : bool ):
@@ -106,78 +147,46 @@ def on_filament_sensor_update(self, sensor_name: str, parameter: str, value: boo
106147 self ._filament_state = self .FilamentStates .UNKNOWN
107148 self .handle_filament_state ()
108149 return
109- self ._sensor_states [sensor_name ] = value
110- if not self ._sensor_states :
111- new_state = self .FilamentStates .UNKNOWN
112- elif all (self ._sensor_states .values ()):
113- new_state = self .FilamentStates .LOADED
114- else :
115- new_state = self .FilamentStates .UNLOADED
116- if self ._filament_state != new_state :
117- self ._filament_state = new_state
118- self .handle_filament_state ()
150+ if sensor_name == self .filament_sensor :
151+ if value :
152+ self ._filament_state = self .FilamentStates .LOADED
153+ else :
154+ self ._filament_state = self .FilamentStates .UNLOADED
155+ return
156+ self .handle_filament_state ()
119157
120- @QtCore .pyqtSlot (str , str , float , name = "on_extruder_update" )
121- def on_extruder_update (
122- self , extruder_name : str , field : str , new_value : float
123- ) -> None :
124- """Handle extruder update"""
125- if not self .isVisible :
126- return
127- if not self .loadignore or not self .unloadignore :
128- if self .target_temp != 0 :
129- if self .current_temp == self .target_temp :
130- if self .isVisible :
131- self .call_load_panel .emit (
132- True , "Extruder heated up \n Please wait"
133- )
134- return
135- if field == "temperature" :
136- self .current_temp = round (new_value , 0 )
137- if self .isVisible :
138- self .call_load_panel .emit (
139- True ,
140- f"Heating up ({ new_value } /{ self .target_temp } ) \n Please wait" ,
141- )
142- if field == "target" :
143- self .target_temp = round (new_value , 0 )
144- if self .isVisible :
145- self .call_load_panel .emit (True , "Heating up \n Please wait" )
146-
147- @QtCore .pyqtSlot (bool , name = "on_load_filament" )
148- def on_load_filament (self , status : bool ):
158+ @QtCore .pyqtSlot (dict , name = "on_load_filament" )
159+ def on_load_filament (self , status : dict ):
149160 """Handle load filament object updated"""
150- if not self .isVisible :
151- return
152- if self .loadignore :
153- return
154- if status :
155- self .call_load_panel .emit (True , "Loading Filament" )
156- else :
157- self .loadignore = True
158- self .target_temp = 0
159- self .call_load_panel .emit (False , "" )
160- self ._filament_state = self .FilamentStates .LOADED
161+ if "state" in status .keys ():
162+ if not status ["state" ]:
163+ self .target_temp = 0
164+ self .call_load_panel .emit (False , "" )
165+ if self .state == "paused" :
166+ self .request_change_tab .emit (0 )
167+ return
168+ self .call_load_panel .emit (
169+ True , f"Loading Filament\n { status ['step' ].capitalize ()} "
170+ )
161171 self .handle_filament_state ()
162172
163- @QtCore .pyqtSlot (bool , name = "on_unload_filament" )
164- def on_unload_filament (self , status : bool ):
173+ @QtCore .pyqtSlot (dict , name = "on_unload_filament" )
174+ def on_unload_filament (self , status : dict ):
165175 """Handle unload filament object updated"""
166- if not self .isVisible :
167- return
168- if self .unloadignore :
169- return
170- if status :
171- self .call_load_panel .emit (True , "Unloading Filament" )
172- else :
173- self .unloadignore = True
174- self .call_load_panel .emit (False , "" )
175- self .target_temp = 0
176- self ._filament_state = self .FilamentStates .UNLOADED
176+ if "state" in status .keys ():
177+ if not status ["state" ]:
178+ self .target_temp = 0
179+ self .call_load_panel .emit (False , "" )
180+ return
181+ self .call_load_panel .emit (
182+ True , f"Unloading Filament\n { status ['step' ].capitalize ()} "
183+ )
177184 self .handle_filament_state ()
178185
179186 @QtCore .pyqtSlot (int , int , name = "load_filament" )
180- def load_filament (self , toolhead : int = 0 , temp : int = 220 ) -> None :
187+ def load_filament (
188+ self , toolhead : int = 0 , filament : FilamentTypes = FilamentTypes .UNKNOWN
189+ ) -> None :
181190 """Handle load filament buttons clicked"""
182191 if not self .isVisible :
183192 return
@@ -194,9 +203,11 @@ def load_filament(self, toolhead: int = 0, temp: int = 220) -> None:
194203 message = "Filament is already loaded." ,
195204 )
196205 return
197- self .loadignore = False
198206 self .call_load_panel .emit (True , "Loading Filament" )
199- self .run_gcode .emit (f"LOAD_FILAMENT TOOLHEAD=load_toolhead TEMPERATURE={ temp } " )
207+ self .run_gcode .emit (
208+ f"""SAVE_VARIABLE VARIABLE=filament_type VALUE='"{ filament .value .name } "'"""
209+ )
210+ self .run_gcode .emit (f"LOAD_FILAMENT TEMPERATURE={ filament .value .temperature } " )
200211
201212 @QtCore .pyqtSlot (str , int , name = "unload_filament" )
202213 def unload_filament (self , toolhead : int = 0 , temp : int = 220 ) -> None :
@@ -218,21 +229,23 @@ def unload_filament(self, toolhead: int = 0, temp: int = 220) -> None:
218229 return
219230
220231 self .find_routine_objects ()
221- self .unloadignore = False
222232 self .call_load_panel .emit (True , "Unloading Filament" )
233+ self .run_gcode .emit (
234+ f"""SAVE_VARIABLE VARIABLE=filament_type VALUE='"{ FilamentTypes .UNKNOWN .value .name } "'"""
235+ )
223236 self .run_gcode .emit (f"UNLOAD_FILAMENT TEMPERATURE={ temp } " )
224237
225238 def handle_filament_state (self ):
226239 """Handle ui changes on filament states"""
227240 if self ._filament_state == self .FilamentStates .LOADED :
228- self .panel .filament_page_load_btn . setDisabled (True )
229- self .panel .filament_page_load_btn .setDisabled (False )
241+ self .panel .filament_page_unload_btn . setEnabled (True )
242+ self .panel .filament_page_load_btn .setEnabled (False )
230243 elif self ._filament_state == self .FilamentStates .UNLOADED :
231- self .panel .filament_page_unload_btn .setDisabled ( True )
232- self .panel .filament_page_unload_btn . setDisabled ( False )
244+ self .panel .filament_page_unload_btn .setEnabled ( False )
245+ self .panel .filament_page_load_btn . setEnabled ( True )
233246 else :
234- self .panel .filament_page_load_btn .setDisabled ( False )
235- self .panel .filament_page_unload_btn .setDisabled ( False )
247+ self .panel .filament_page_load_btn .setEnabled ( True )
248+ self .panel .filament_page_unload_btn .setEnabled ( True )
236249
237250 @property
238251 def filament_state (self ):
0 commit comments