-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemperature.py
575 lines (498 loc) · 25.5 KB
/
temperature.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import logging
import contextlib
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
from ks_includes.screen_panel import ScreenPanel
from ks_includes.widgets.heatergraph import HeaterGraph
from ks_includes.widgets.keypad import Keypad
def create_panel(*args):
return TemperaturePanel(*args)
class TemperaturePanel(ScreenPanel):
graph_update = None
active_heater = None
def __init__(self, screen, title):
super().__init__(screen, title)
self.popover_timeout = None
self.left_panel = None
self.popover_device = None
self.h = 1
self.tempdeltas = ["1", "5", "10", "25"]
self.tempdelta = self.tempdeltas[-2]
self.show_preheat = False
self.preheat_options = self._screen._config.get_preheat_options()
self.grid = self._gtk.HomogeneousGrid()
self._gtk.reset_temp_color()
self.grid.attach(self.create_left_panel(), 0, 0, 1, 1)
# When printing start in temp_delta mode and only select tools
state = self._printer.state
logging.info(state)
selection = []
if state not in ["printing", "paused"]:
for extruder in self._printer.get_tools():
selection.append(extruder)
self.show_preheat = True
selection.extend(self._printer.get_heaters())
else:
current_extruder = self._printer.get_stat("toolhead", "extruder")
if current_extruder:
selection.append(current_extruder)
# Select heaters
for h in selection:
if h.startswith("temperature_sensor "):
continue
name = h.split()[1] if len(h.split()) > 1 else h
# Support for hiding devices by name
if name.startswith("_"):
continue
if h not in self.active_heaters:
self.select_heater(None, h)
if self._screen.vertical_mode:
self.grid.attach(self.create_right_panel(), 0, 1, 1, 1)
else:
self.grid.attach(self.create_right_panel(), 1, 0, 1, 1)
self.content.add(self.grid)
def create_right_panel(self):
cooldown = self._gtk.Button('cool-down', _('Cooldown'), "color4", self.bts, Gtk.PositionType.LEFT, 1)
adjust = self._gtk.Button('fine-tune', None, "color3", self.bts * 1.4, Gtk.PositionType.LEFT, 1)
cooldown.connect("clicked", self.set_cooldown, "cooldown")
adjust.connect("clicked", self.switch_preheat_adjust)
right = self._gtk.HomogeneousGrid()
right.attach(cooldown, 0, 0, 2, 1)
right.attach(adjust, 2, 0, 1, 1)
if self.show_preheat:
right.attach(self.preheat(), 0, 1, 3, 3)
else:
right.attach(self.delta_adjust(), 0, 1, 3, 3)
return right
def switch_preheat_adjust(self, widget):
self.show_preheat ^= True
if self._screen.vertical_mode:
self.grid.remove_row(1)
self.grid.attach(self.create_right_panel(), 0, 1, 1, 1)
else:
self.grid.remove_column(1)
self.grid.attach(self.create_right_panel(), 1, 0, 1, 1)
self.grid.show_all()
def preheat(self):
self.labels["preheat_grid"] = self._gtk.HomogeneousGrid()
i = 0
preheat_config = ""
for option in self.preheat_options:
if option != "cooldown":
preheat_config = _('Extruder') + f":{int(self.preheat_options[option]['extruder'])}℃ " + _('Bed') + f":{int(self.preheat_options[option]['bed'])}℃"
self.labels[option] = self._gtk.Button(image_name=option, label= preheat_config, scale=2.25, style=f"color{(i % 4) + 1}")
self.labels[option].connect("clicked", self.set_temperature, option)
self.labels['preheat_grid'].attach(self.labels[option], (i % 2), int(i / 2), 1, 1)
i += 1
scroll = self._gtk.ScrolledWindow()
scroll.add(self.labels["preheat_grid"])
return scroll
def delta_adjust(self):
deltagrid = self._gtk.HomogeneousGrid()
self.labels["increase"] = self._gtk.Button("increase", None, "color1")
self.labels["increase"].connect("clicked", self.change_target_temp_incremental, "+")
self.labels["decrease"] = self._gtk.Button("decrease", None, "color3")
self.labels["decrease"].connect("clicked", self.change_target_temp_incremental, "-")
tempgrid = Gtk.Grid()
for j, i in enumerate(self.tempdeltas):
self.labels[f'deg{i}'] = self._gtk.Button(label=i)
self.labels[f'deg{i}'].connect("clicked", self.change_temp_delta, i)
ctx = self.labels[f'deg{i}'].get_style_context()
if j == 0:
ctx.add_class("distbutton_top")
elif j == len(self.tempdeltas) - 1:
ctx.add_class("distbutton_bottom")
else:
ctx.add_class("distbutton")
if i == self.tempdelta:
ctx.add_class("distbutton_active")
tempgrid.attach(self.labels[f'deg{i}'], j, 0, 1, 1)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
vbox.pack_start(Gtk.Label(_("Temperature") + " (°C)"), False, False, 8)
vbox.pack_end(tempgrid, True, True, 2)
vsize = 2 if self._screen.vertical_mode else 3
deltagrid.attach(self.labels["decrease"], 0, 0, 1, vsize)
deltagrid.attach(self.labels["increase"], 1, 0, 1, vsize)
deltagrid.attach(vbox, 0, vsize, 2, 2)
return deltagrid
def change_temp_delta(self, widget, tempdelta):
logging.info(f"### tempdelta {tempdelta}")
self.labels[f"deg{self.tempdelta}"].get_style_context().remove_class("distbutton_active")
self.labels[f"deg{tempdelta}"].get_style_context().add_class("distbutton_active")
self.tempdelta = tempdelta
def change_target_temp_incremental(self, widget, direction):
if len(self.active_heaters) == 0:
self._screen.show_popup_message(_("Nothing selected"))
else:
for heater in self.active_heaters:
target = self._printer.get_dev_stat(heater, "target")
name = heater.split()[1] if len(heater.split()) > 1 else heater
if direction == "+":
target += int(self.tempdelta)
max_temp = int(float(self._printer.get_config_section(heater)['max_temp'])) - 5
if target > max_temp:
target = max_temp
self._screen.show_popup_message(_("Can't set above the maximum:") + f' {target}')
else:
target -= int(self.tempdelta)
target = max(target, 0)
if heater.startswith('extruder'):
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(heater), target)
elif heater.startswith('heater_bed'):
self._screen._ws.klippy.set_bed_temp(target)
elif heater.startswith('heater_generic '):
self._screen._ws.klippy.set_heater_temp(name, target)
elif heater.startswith("temperature_fan "):
self._screen._ws.klippy.set_temp_fan_temp(name, target)
else:
logging.info(f"Unknown heater: {heater}")
self._screen.show_popup_message(_("Unknown Heater") + " " + heater)
self._printer.set_dev_stat(heater, "target", int(target))
logging.info(f"Setting {heater} to {target}")
def update_graph_visibility(self):
if not self._printer.get_temp_store_devices():
return
count = 0
for device in self.devices:
visible = self._config.get_config().getboolean(f"graph {self._screen.connected_printer}",
device, fallback=True)
self.devices[device]['visible'] = visible
self.labels['da'].set_showing(device, visible)
if visible:
count += 1
self.devices[device]['name'].get_style_context().add_class(self.devices[device]['class'])
self.devices[device]['name'].get_style_context().remove_class("graph_label_hidden")
else:
self.devices[device]['name'].get_style_context().add_class("graph_label_hidden")
self.devices[device]['name'].get_style_context().remove_class(self.devices[device]['class'])
if count > 0:
if self.labels['da'] not in self.left_panel:
self.left_panel.add(self.labels['da'])
self.labels['da'].queue_draw()
self.labels['da'].show()
elif self.labels['da'] in self.left_panel:
self.left_panel.remove(self.labels['da'])
def activate(self):
if self.graph_update is None:
# This has a high impact on load
self.graph_update = GLib.timeout_add_seconds(5, self.update_graph)
self.update_graph_visibility()
def deactivate(self):
if self.graph_update is not None:
GLib.source_remove(self.graph_update)
self.graph_update = None
if self.active_heater is not None:
self.hide_numpad()
def select_heater(self, widget, device):
if self.active_heater is None and device in self.devices and self.devices[device]["can_target"]:
if device in self.active_heaters:
self.active_heaters.pop(self.active_heaters.index(device))
self.devices[device]['name'].get_style_context().remove_class("button_active")
self.devices[device]['select'].set_label(_("Select"))
logging.info(f"Deselecting {device}")
return
self.active_heaters.append(device)
self.devices[device]['name'].get_style_context().add_class("button_active")
self.devices[device]['select'].set_label(_("Deselect"))
logging.info(f"Seselecting {device}")
return
def set_temperature(self, widget, setting):
if len(self.active_heaters) == 0:
self._screen.show_popup_message(_("Nothing selected"))
else:
for heater in self.active_heaters:
target = None
max_temp = float(self._printer.get_config_section(heater)['max_temp']) - 5
name = heater.split()[1] if len(heater.split()) > 1 else heater
with contextlib.suppress(KeyError):
for i in self.preheat_options[setting]:
logging.info(f"{self.preheat_options[setting]}")
if i == name:
# Assign the specific target if available
target = self.preheat_options[setting][name]
logging.info(f"name match {name}")
elif i == heater:
target = self.preheat_options[setting][heater]
logging.info(f"heater match {heater}")
if target is None and setting == "cooldown" and not heater.startswith('temperature_fan '):
target = 0
if heater.startswith('extruder'):
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(heater), target)
elif heater.startswith('heater_bed'):
if target is None:
with contextlib.suppress(KeyError):
target = self.preheat_options[setting]["bed"]
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_bed_temp(target)
elif heater.startswith('heater_generic '):
if target is None:
with contextlib.suppress(KeyError):
target = self.preheat_options[setting]["heater_generic"]
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_heater_temp(name, target)
elif heater.startswith('temperature_fan '):
if target is None:
with contextlib.suppress(KeyError):
target = self.preheat_options[setting]["temperature_fan"]
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_temp_fan_temp(name, target)
# This small delay is needed to properly update the target if the user configured something above
# and then changed the target again using preheat gcode
GLib.timeout_add(250, self.preheat_gcode, setting)
def set_cooldown(self, widget, setting):
if setting != "cooldown":
return
for heater in ['extruder', 'heater_bed']:
target = None
max_temp = float(self._printer.get_config_section(heater)['max_temp'])
name = heater.split()[1] if len(heater.split()) > 1 else heater
with contextlib.suppress(KeyError):
for i in self.preheat_options[setting]:
logging.info(f"{self.preheat_options[setting]}")
if i == name:
target = selg.preheat_options[setting][name]
logging.info(f"name match {name}")
elif i == heater:
target = self.preheat_options[setting][heater]
logging.info(f"heater match {heater}")
if target is None and setting == "cooldown" and not heater.startswith("temperature_fan"):
target = 0
if heater.startswith('extruder'):
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(heater), target)
elif heater.startswith('heater_bed'):
if target is None:
with contextlib.suppress(KeyError):
target = self.preheat_options[setting]["bed"]
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_bed_temp(target)
elif heater.startswith('temperature_fan '):
if target is None:
with contextlib.suppress(KeyError):
target = self.preheat_options[setting]["temperature_fan"]
if self.validate(heater, target, max_temp):
self._screen._ws.klippy.set_temp_fan_temp(name, target)
self._screen._ws.klippy.gcode_script("M106 S255")
self._printer.set_cooldown_flag(True)
def validate(self, heater, target=None, max_temp=None):
if target is not None and max_temp is not None:
if 0 <= target <= max_temp:
self._printer.set_dev_stat(heater, "target", target)
return True
elif target > max_temp:
self._screen.show_popup_message(_("Can't set above the maximum:") + f' {max_temp}')
return False
logging.debug(f"Invalid {heater} Target:{target}/{max_temp}")
return False
def preheat_gcode(self, setting):
with contextlib.suppress(KeyError):
self._screen._ws.klippy.gcode_script(self.preheat_options[setting]['gcode'])
return False
def add_device(self, device):
logging.info(f"Adding device: {device}")
temperature = self._printer.get_dev_stat(device, "temperature")
if temperature is None:
return False
devname = device.split()[1] if len(device.split()) > 1 else device
# Support for hiding devices by name
if devname.startswith("_"):
return False
if device.startswith("extruder"):
i = sum(d.startswith('extruder') for d in self.devices)
image = f"extruder-{i}" if self._printer.extrudercount > 1 else "extruder"
class_name = f"graph_label_{device}"
dev_type = "extruder"
elif device == "heater_bed":
image = "bed"
devname = "Heater Bed"
class_name = "graph_label_heater_bed"
dev_type = "bed"
elif device.startswith("heater_generic"):
self.h = sum("heater_generic" in d for d in self.devices)
image = "heater"
class_name = f"graph_label_sensor_{self.h}"
dev_type = "sensor"
elif device.startswith("temperature_fan"):
f = 1 + sum("temperature_fan" in d for d in self.devices)
image = "fan"
class_name = f"graph_label_fan_{f}"
dev_type = "fan"
elif self._config.get_main_config().getboolean("only_heaters", False):
return False
else:
self.h += sum("sensor" in d for d in self.devices)
image = "heat-up"
class_name = f"graph_label_sensor_{self.h}"
dev_type = "sensor"
rgb = self._gtk.get_temp_color(dev_type)
name = self._gtk.Button(image, devname.capitalize().replace("_", " "), None, self.bts, Gtk.PositionType.LEFT, 1)
name.set_alignment(0, .5)
visible = self._config.get_config().getboolean(f"graph {self._screen.connected_printer}", device, fallback=True)
if visible:
name.get_style_context().add_class(class_name)
else:
name.get_style_context().add_class("graph_label_hidden")
can_target = self._printer.device_has_target(device)
self.labels['da'].add_object(device, "temperatures", rgb, False, True)
if can_target:
self.labels['da'].add_object(device, "targets", rgb, True, False)
name.connect('button-press-event', self.name_pressed, device)
name.connect('button-release-event', self.name_released, device)
else:
name.connect("clicked", self.toggle_visibility, device)
self.labels['da'].set_showing(device, visible)
temp = self._gtk.Button(label="", lines=1)
if can_target:
temp.connect("clicked", self.show_numpad, device)
self.devices[device] = {
"class": class_name,
"name": name,
"temp": temp,
"can_target": can_target,
"visible": visible
}
if self.devices[device]["can_target"]:
self.devices[device]['select'] = self._gtk.Button(label=_("Select"))
self.devices[device]['select'].connect('clicked', self.select_heater, device)
devices = sorted(self.devices)
pos = devices.index(device) + 1
self.labels['devices'].insert_row(pos)
self.labels['devices'].attach(name, 0, pos, 1, 1)
self.labels['devices'].attach(temp, 1, pos, 1, 1)
self.labels['devices'].show_all()
return True
def name_pressed(self, widget, event, device):
self.popover_timeout = GLib.timeout_add_seconds(1, self.popover_popup, widget, device)
def name_released(self, widget, event, device):
if self.popover_timeout is not None:
GLib.source_remove(self.popover_timeout)
self.popover_timeout = None
if not self.popover_device:
self.select_heater(None, device)
def toggle_visibility(self, widget, device=None):
if device is None:
device = self.popover_device
self.devices[device]['visible'] ^= True
logging.info(f"Graph show {self.devices[device]['visible']}: {device}")
section = f"graph {self._screen.connected_printer}"
if section not in self._config.get_config().sections():
self._config.get_config().add_section(section)
self._config.set(section, f"{device}", f"{self.devices[device]['visible']}")
self._config.save_user_config_options()
self.update_graph_visibility()
if self.devices[device]['can_target']:
self.popover_populate_menu()
self.labels['popover'].show_all()
def change_target_temp(self, temp):
name = self.active_heater.split()[1] if len(self.active_heater.split()) > 1 else self.active_heater
max_temp = int(float(self._printer.get_config_section(self.active_heater)['max_temp'])) - 5
if temp > max_temp:
self._screen.show_popup_message(_("Can't set above the maximum:") + f' {max_temp}')
# return
temp = max_temp
temp = max(temp, 0)
if self.active_heater.startswith('extruder'):
self._screen._ws.klippy.set_tool_temp(self._printer.get_tool_number(self.active_heater), temp)
elif self.active_heater == "heater_bed":
self._screen._ws.klippy.set_bed_temp(temp)
elif self.active_heater.startswith('heater_generic '):
self._screen._ws.klippy.set_heater_temp(name, temp)
elif self.active_heater.startswith('temperature_fan '):
self._screen._ws.klippy.set_temp_fan_temp(name, temp)
else:
logging.info(f"Unknown heater: {self.active_heater}")
self._screen.show_popup_message(_("Unknown Heater") + " " + self.active_heater)
self._printer.set_dev_stat(self.active_heater, "target", temp)
def create_left_panel(self):
self.labels['devices'] = Gtk.Grid()
self.labels['devices'].get_style_context().add_class('heater-grid')
self.labels['devices'].set_vexpand(False)
name = Gtk.Label("")
temp = Gtk.Label(_("Temp (°C)"))
temp.get_style_context().add_class("heater-grid-temp")
self.labels['devices'].attach(name, 0, 0, 1, 1)
self.labels['devices'].attach(temp, 1, 0, 1, 1)
self.labels['da'] = HeaterGraph(self._printer, self._gtk.font_size)
self.labels['da'].set_vexpand(True)
scroll = self._gtk.ScrolledWindow()
scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
scroll.add(self.labels['devices'])
self.left_panel = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
self.left_panel.add(scroll)
self.labels['graph_settemp'] = self._gtk.Button(label=_("Set Temp"))
self.labels['graph_settemp'].connect("clicked", self.show_numpad)
self.labels['graph_hide'] = self._gtk.Button(label=_("Hide"))
self.labels['graph_hide'].connect("clicked", self.toggle_visibility)
self.labels['graph_show'] = self._gtk.Button(label=_("Show"))
self.labels['graph_show'].connect("clicked", self.toggle_visibility)
popover = Gtk.Popover()
self.labels['popover_vbox'] = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
popover.add(self.labels['popover_vbox'])
popover.set_position(Gtk.PositionType.BOTTOM)
popover.connect('closed', self.popover_closed)
self.labels['popover'] = popover
for d in (self._printer.get_tools() + self._printer.get_heaters()):
self.add_device(d)
return self.left_panel
def hide_numpad(self, widget=None):
self.devices[self.active_heater]['name'].get_style_context().remove_class("button_active")
self.active_heater = None
for d in self.active_heaters:
self.devices[d]['name'].get_style_context().add_class("button_active")
if self._screen.vertical_mode:
self.grid.remove_row(1)
self.grid.attach(self.create_right_panel(), 0, 1, 1, 1)
else:
self.grid.remove_column(1)
self.grid.attach(self.create_right_panel(), 1, 0, 1, 1)
self.grid.show_all()
def popover_closed(self, widget):
self.popover_device = None
def popover_popup(self, widget, device):
self.popover_device = device
po = self.labels['popover']
po.set_relative_to(widget)
self.popover_populate_menu()
po.show_all()
def popover_populate_menu(self):
pobox = self.labels['popover_vbox']
for child in pobox.get_children():
pobox.remove(child)
if self.labels['da'].is_showing(self.popover_device):
pobox.pack_start(self.labels['graph_hide'], True, True, 5)
else:
pobox.pack_start(self.labels['graph_show'], True, True, 5)
if self.devices[self.popover_device]["can_target"]:
pobox.pack_start(self.labels['graph_settemp'], True, True, 5)
pobox.pack_end(self.devices[self.popover_device]['select'], True, True, 5)
def process_update(self, action, data):
if action != "notify_status_update":
return
for x in (self._printer.get_tools() + self._printer.get_heaters()):
self.update_temp(
x,
self._printer.get_dev_stat(x, "temperature"),
self._printer.get_dev_stat(x, "target"),
self._printer.get_dev_stat(x, "power"),
)
def show_numpad(self, widget, device=None):
for d in self.active_heaters:
self.devices[d]['name'].get_style_context().remove_class("button_active")
self.active_heater = self.popover_device if device is None else device
self.devices[self.active_heater]['name'].get_style_context().add_class("button_active")
if "keypad" not in self.labels:
self.labels["keypad"] = Keypad(self._screen, self.change_target_temp, self.hide_numpad)
self.labels["keypad"].clear()
if self._screen.vertical_mode:
self.grid.remove_row(1)
self.grid.attach(self.labels["keypad"], 0, 1, 1, 1)
else:
self.grid.remove_column(1)
self.grid.attach(self.labels["keypad"], 1, 0, 1, 1)
self.grid.show_all()
self.labels['popover'].popdown()
def update_graph(self):
self.labels['da'].queue_draw()
return True