forked from wolfmanjm/kivy-smoothie-host
/
main.py
1331 lines (1109 loc) · 46.8 KB
/
main.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.widget import Widget
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.actionbar import ActionButton
from kivy.properties import NumericProperty, StringProperty, ObjectProperty, ListProperty, BooleanProperty
from kivy.vector import Vector
from kivy.clock import Clock, mainthread
from kivy.factory import Factory
from kivy.logger import Logger
from kivy.core.window import Window
from kivy.config import ConfigParser
from kivy.metrics import dp, Metrics
from native_file_chooser import NativeFileChooser
from mpg_knob import Knob
from comms import Comms
from message_box import MessageBox
from input_box import InputBox
from selection_box import SelectionBox
from file_dialog import FileDialog
from viewer import GcodeViewerScreen
from web_server import ProgressServer
from camera_screen import CameraScreen
from spindle_camera import SpindleCamera
from config_editor import ConfigEditor
from gcode_help import GcodeHelp
from text_editor import TextEditor
from tool_scripts import ToolScripts
import subprocess
import traceback
import queue
import math
import os
import sys
import datetime
import configparser
from functools import partial
import collections
import importlib
import signal
Window.softinput_mode = 'below_target'
class NumericInput(TextInput):
"""Text input that shows a numeric keypad"""
def __init__(self, **kwargs):
super(NumericInput, self).__init__(**kwargs)
def on_focus(self, i, v):
if v:
self._last = self.text
self.text = ""
if App.get_running_app().is_desktop == 0:
self.show_keyboard()
if self.keyboard and self.keyboard.widget:
self.keyboard.widget.layout = "numeric.json"
self.m_keyboard = self.keyboard.widget
else:
if self.text == "":
self.text = self._last
if App.get_running_app().is_desktop == 0:
self.hide_keyboard()
if self.keyboard and self.keyboard.widget:
self.m_keyboard.layout = "qwerty"
def on_parent(self, widget, parent):
if App.get_running_app().is_desktop != 0:
return
self.keyboard_mode = 'managed'
class DROWidget(RelativeLayout):
"""DROWidget Shows realtime information in a DRO style"""
curwcs = StringProperty('')
def __init__(self, **kwargs):
super(DROWidget, self).__init__(**kwargs)
self.app = App.get_running_app()
def enter_wpos(self, axis, v):
i = ord(axis) - ord('x')
v = v.strip()
if v.startswith('/'):
# we divide current value by this
try:
d = float(v[1:])
v = str(self.app.wpos[i] / d)
except Exception:
Logger.warning("DROWidget: cannot divide by: {}".format(v))
self.app.wpos[i] = self.app.wpos[i]
return
try:
# needed because the filter does not allow -ive numbers WTF!!!
f = float(v.strip())
except Exception:
Logger.warning("DROWidget: invalid float input: {}".format(v))
# set the display back to what it was, this looks odd but it forces the display to update
self.app.wpos[i] = self.app.wpos[i]
return
Logger.debug("DROWidget: Set axis {} wpos to {}".format(axis, f))
self.app.comms.write('G10 L20 P0 {}{}\n'.format(axis.upper(), f))
self.app.wpos[i] = f
def select_wcs(self, v):
self.app.comms.write('{}\n'.format(v))
def reset_axis(self, a):
# only used for ABC axis
self.app.comms.write('G92 {}0\n'.format(a))
def update_buttons(self):
return "$I\n"
def _on_curwcs(self):
# foreach WCS button see if it is active or not
for i in self.ids.wcs_buts.children:
if i.text == self.curwcs:
i.state = 'down'
else:
i.state = 'normal'
class MPGWidget(RelativeLayout):
last_pos = NumericProperty(0)
selected_axis = StringProperty('X')
selected_index = NumericProperty(0)
def __init__(self, **kwargs):
super(MPGWidget, self).__init__(**kwargs)
self.app = App.get_running_app()
def handle_action(self):
# Run button pressed
if self.selected_index == -1:
# change feed override
self.app.comms.write('M220 S{}'.format(round(self.last_pos, 1)))
return
if self.app.main_window.is_printing and not self.app.main_window.is_suspended:
return
# if in non MPG mode then issue G0 in abs or rel depending on settings
# if in MPG mode then issue $J commands when they occur
if not self.ids.mpg_mode_tb.state == 'down':
# normal mode
cmd = 'G90' if self.ids.abs_mode_tb.state == 'down' else 'G91'
# print('{} {}{} {}'.format(cmd1, self.selected_axis, round(self.last_pos, 3), cmd2))
self.app.comms.write('M120 {} G0 {}{} M121\n'.format(cmd, self.selected_axis, round(self.last_pos, 3)))
def handle_change(self, ticks):
if self.selected_index == -1:
# change feed override
self.last_pos += ticks
if self.last_pos < 1:
self.last_pos = 1
return
if self.app.main_window.is_printing and not self.app.main_window.is_suspended:
return
# change an axis
if self.ids.x01.active:
d = 0.01 * ticks
elif self.ids.x001.active:
d = 0.001 * ticks
else:
d = 0.1 * ticks
self.last_pos = self.last_pos + d
axis = self.selected_axis
# MPG mode
if self.ids.mpg_mode_tb.state == 'down':
self.app.comms.write('$J {}{}\n'.format(self.selected_axis.upper(), d))
def index_changed(self, i):
if i == -1:
# Feedrate overide
self.last_pos = self.app.fro
elif self.ids.abs_mode_tb.state == 'down':
# show current axis position
self.last_pos = self.app.wpos[i]
else:
# relative mode zero it
self.last_pos = 0
def abs_mode_changed(self):
self.index_changed(self.selected_index)
class CircularButton(ButtonBehavior, Widget):
text = StringProperty()
def collide_point(self, x, y):
return Vector(x, y).distance(self.center) <= self.width / 2
class ArrowButton(ButtonBehavior, Widget):
text = StringProperty()
angle = NumericProperty()
# def collide_point(self, x, y):
# bmin = Vector(self.center) - Vector(25, 25)
# bmax = Vector(self.center) + Vector(25, 25)
# return Vector.in_bbox((x, y), bmin, bmax)
class JogRoseWidget(BoxLayout):
xy_feedrate = StringProperty()
abc_sel = StringProperty('Z')
def __init__(self, **kwargs):
super(JogRoseWidget, self).__init__(**kwargs)
self.app = App.get_running_app()
self.xy_feedrate = self.app.config.get('Jog', 'xy_feedrate')
def handle_action(self, axis, v):
if self.app.main_window.is_printing and not self.app.main_window.is_suspended:
self.app.main_window.display("NOTE: Cannot jog while printing")
return
x10 = self.ids.x10cb.active
if x10:
v *= 10
fr = self.xy_feedrate
if axis == 'O':
self.app.comms.write('M120 G21 G90 G0 X0 Y0 F{} M121\n'.format(fr))
elif axis == 'H':
self.app.comms.write('$H\n')
else:
self.app.comms.write('M120 G21 G91 G0 {}{} F{} M121\n'.format(axis, v, fr))
def update_xy_feedrate(self):
fr = self.ids.xy_feedrate.text
self.app.config.set('Jog', 'xy_feedrate', fr)
self.app.config.write()
self.xy_feedrate = fr
def motors_off(self):
self.app.comms.write('M18\n')
class KbdWidget(GridLayout):
def __init__(self, **kwargs):
super(KbdWidget, self).__init__(**kwargs)
self.app = App.get_running_app()
self.last_command = ""
def _add_line_to_log(self, s):
self.app.main_window.display(s)
def do_action(self, key):
if key == 'Send':
# Logger.debug("KbdWidget: Sending {}".format(self.display.text))
if self.display.text.strip():
self._add_line_to_log('<< {}'.format(self.display.text))
self.app.comms.write('{}\n'.format(self.display.text))
self.last_command = self.display.text
self.display.text = ''
elif key == 'Repeat':
self.display.text = self.last_command
elif key == 'BS':
self.display.text = self.display.text[:-1]
elif key == '?':
self.handle_input('?')
else:
self.display.text += key
def handle_input(self, s):
self.app.command_input(s)
self.display.text = ''
class MainWindow(BoxLayout):
status = StringProperty('Idle')
wpos = ListProperty([0, 0, 0])
eta = StringProperty('--:--:--')
is_printing = BooleanProperty(False)
is_suspended = BooleanProperty(False)
def __init__(self, **kwargs):
super(MainWindow, self).__init__(**kwargs)
self.app = App.get_running_app()
self._trigger = Clock.create_trigger(self.async_get_display_data)
self._q = queue.Queue()
self.config = self.app.config
self.last_path = self.config.get('General', 'last_gcode_path')
self.paused = False
self.last_line = 0
# print('font size: {}'.format(self.ids.log_window.font_size))
# Clock.schedule_once(self.my_callback, 2) # hack to overcome the page layout not laying out initially
def on_touch_down(self, touch):
if self.ids.log_window.collide_point(touch.x, touch.y):
if touch.is_triple_tap:
self.ids.log_window.data = []
return True
return super(MainWindow, self).on_touch_down(touch)
def add_line_to_log(self, s, overwrite=False):
''' Add lines to the log window, which is trimmed to the last 200 lines '''
max_lines = 200 # TODO needs to be configurable
n = len(self.ids.log_window.data)
if overwrite:
self.ids.log_window.data[n - 1] = ({'text': s})
return
self.ids.log_window.data.append({'text': s})
# we use some hysterysis here so we don't truncate every line added over max_lines
n = n - max_lines # how many lines over our max
if n > 10:
# truncate log to last max_lines, we delete the oldest 10 or so lines
del self.ids.log_window.data[0:n]
def connect(self):
if self.app.is_connected:
if self.is_printing:
mb = MessageBox(text='Cannot Disconnect while printing - Abort first, then wait')
mb.open()
else:
self._disconnect()
else:
port = self.config.get('General', 'serial_port') if not self.app.use_com_port else self.app.use_com_port
self.add_line_to_log("Connecting to {}...".format(port))
self.app.comms.connect(port)
def _disconnect(self, b=True):
if b:
self.add_line_to_log("Disconnect...")
self.app.comms.disconnect()
def display(self, data):
self.add_line_to_log(data)
# we have to do this as @mainthread was getting stuff out of order
def async_display(self, data):
''' called from external thread to display incoming data '''
# puts the data onto queue and triggers an event to read it in the Kivy thread
self._q.put(data)
self._trigger()
def async_get_display_data(self, *largs):
''' fetches data from the Queue and displays it, triggered by incoming data '''
while not self._q.empty():
# we need this loop until q is empty as trigger only triggers once per frame
data = self._q.get(False)
if data.endswith('\r'):
self.add_line_to_log(data[0:-1], True)
else:
self.add_line_to_log(data)
@mainthread
def connected(self):
Logger.debug("MainWindow: Connected...")
self.add_line_to_log("...Connected")
self.app.is_connected = True
self.ids.connect_button.state = 'down'
self.ids.connect_button.text = "Disconnect"
self.ids.print_but.text = 'Run'
self.paused = False
self.is_printing = False
@mainthread
def disconnected(self):
Logger.debug("MainWindow: Disconnected...")
self.app.is_connected = False
self.is_printing = False
self.ids.connect_button.state = 'normal'
self.ids.connect_button.text = "Connect"
self.add_line_to_log("...Disconnected")
@mainthread
def update_status(self, stat, d):
self.status = stat
self.app.status = stat
if 'WPos' in d:
self.wpos = d['WPos']
self.app.wpos = self.wpos
if 'MPos' in d:
self.app.mpos = d['MPos']
if 'F' in d:
self.app.fr = d['F'][0]
if len(d['F']) == 2:
self.app.fro = d['F'][1]
self.app.frr = d['F'][0]
elif len(d['F']) == 3:
self.app.frr = d['F'][1]
self.app.fro = d['F'][2]
if 'S' in d:
self.app.sr = d['S'][0]
if 'L' in d:
self.app.lp = d['L'][0]
if not self.app.is_cnc:
# extract temperature readings and update the extruder property
# We only want to update once per query
t = {}
if 'T' in d:
t['hotend0'] = (d['T'][0], d['T'][1])
if 'T1' in d:
t['hotend1'] = (d['T1'][0], d['T1'][1])
if 'B' in d:
t['bed'] = (d['B'][0], d['B'][1])
if t:
self.ids.extruder.update_temp(t)
@mainthread
def update_state(self, a):
if not self.app.is_cnc:
self.ids.extruder.curtool = int(a[9][1])
self.ids.dro_widget.curwcs = a[1]
self.app.is_inch = a[3] == 'G20'
self.app.is_abs = a[4] == 'G90'
self.app.is_spindle_on = a[7] == 'M3'
@mainthread
def alarm_state(self, s):
''' called when smoothie is in Alarm state and it is sent a gcode '''
self.add_line_to_log("! Alarm state: {}".format(s))
def ask_exit(self, restart=False):
# are you sure?
if restart:
mb = MessageBox(text='Restart - Are you Sure?', cb=self._do_restart)
else:
mb = MessageBox(text='Exit - Are you Sure?', cb=self._do_exit)
mb.open()
def _do_exit(self, ok):
if ok:
self.app.stop()
def _do_restart(self, ok):
if ok:
self.app.stop()
os.execl(sys.executable, sys.executable, *sys.argv)
def ask_shutdown(self):
# are you sure?
mb = MessageBox(text='Shutdown - Are you Sure?', cb=self._do_shutdown)
mb.open()
def _do_shutdown(self, ok):
if ok:
os.system('/sbin/shutdown -h now')
self._do_exit(True)
def change_port(self):
ll = self.app.comms.get_ports()
ports = [self.config.get('General', 'serial_port')] # current port is first in list
for p in ll:
ports.append('serial://{}'.format(p.device))
ports.append('network...')
sb = SelectionBox(title='Select port', text='Select the port to open from drop down', values=ports, cb=self._change_port)
sb.open()
def _change_port(self, s):
if s:
Logger.info('MainWindow: Selected port {}'.format(s))
if s.startswith('network'):
mb = InputBox(title='Network address', text='Enter network address as "ipaddress[:port]"', cb=self._new_network_port)
mb.open()
else:
self.config.set('General', 'serial_port', s)
self.config.write()
def _new_network_port(self, s):
if s:
self.config.set('General', 'serial_port', 'net://{}'.format(s))
self.config.write()
def abort_print(self):
# are you sure?
mb = MessageBox(text='Abort - Are you Sure?', cb=self._abort_print)
mb.open()
def _abort_print(self, ok):
if ok:
self.app.comms.stream_pause(False, True)
@mainthread
def action_paused(self, paused, suspended=False):
# comms layer is telling us we paused or unpaused
self.ids.print_but.text = 'Resume' if paused else 'Pause'
self.paused = paused
self.is_suspended = suspended
if paused:
if suspended:
self.add_line_to_log(">>> Streaming Suspended, Resume or KILL as needed")
else:
self.add_line_to_log(">>> Streaming Paused, Abort or Continue as needed")
def start_print(self):
if self.is_printing:
if not self.paused:
# we clicked the pause button
self.app.comms.stream_pause(True)
else:
# we clicked the resume button
if self.is_suspended:
# as we were suspended we need to resume the suspend first
# we let Smoothie tell us to resume from pause though
self.app.comms.write('resume\n')
else:
self.app.comms.stream_pause(False)
self.is_suspended = False
else:
# get file to print
f = Factory.filechooser()
f.open(self.last_path, cb=self._start_print)
def _start_print(self, file_path=None, directory=None):
# start comms thread to stream the file
# set comms.ping_pong to False for fast stream mode
if file_path is None:
file_path = self.app.gcode_file
if directory is None:
directory = self.last_path
Logger.info('MainWindow: printing file: {}'.format(file_path))
try:
self.nlines = Comms.file_len(file_path) # get number of lines so we can do progress and ETA
Logger.debug('MainWindow: number of lines: {}'.format(self.nlines))
except Exception:
Logger.warning('MainWindow: exception in file_len: {}'.format(traceback.format_exc()))
self.nlines = None
self.start_print_time = datetime.datetime.now()
self.display('>>> Running file: {}, {} lines'.format(file_path, self.nlines))
if self.app.comms.stream_gcode(file_path, progress=lambda x: self.display_progress(x)):
self.display('>>> Run started at: {}'.format(self.start_print_time.strftime('%x %X')))
else:
self.display('WARNING Unable to start print')
return
self.set_last_file(directory, file_path)
self.ids.print_but.text = 'Pause'
self.is_printing = True
self.paused = False
def set_last_file(self, directory, file_path):
if directory != self.last_path:
self.last_path = directory
self.config.set('General', 'last_gcode_path', directory)
self.app.gcode_file = file_path
self.config.set('General', 'last_print_file', file_path)
self.config.write()
def reprint(self):
# are you sure?
mb = MessageBox(text='ReRun {}?'.format(self.app.gcode_file), cb=self._reprint)
mb.open()
def _reprint(self, ok):
if ok:
self._start_print()
@mainthread
def start_last_file(self):
if self.app.gcode_file:
self._start_print()
def review(self):
self._show_viewer(self.app.gcode_file, self.last_path)
@mainthread
def stream_finished(self, ok):
''' called when streaming gcode has finished, ok is True if it completed '''
self.ids.print_but.text = 'Run'
self.is_printing = False
now = datetime.datetime.now()
self.display('>>> Run finished {}'.format('ok' if ok else 'abnormally'))
self.display(">>> Run ended at : {}, Last line: {}".format(now.strftime('%x %X'), self.last_line))
et = datetime.timedelta(seconds=int((now - self.start_print_time).seconds))
self.display(">>> Elapsed time: {}".format(et))
self.eta = '--:--:--'
@mainthread
def display_progress(self, n):
if self.nlines and n <= self.nlines:
now = datetime.datetime.now()
d = (now - self.start_print_time).seconds
if n > 10 and d > 10:
# we have to wait a bit to get reasonable estimates
lps = n / d
eta = (self.nlines - n) / lps
else:
eta = 0
# print("progress: {}/{} {:.1%} ETA {}".format(n, nlines, n/nlines, et))
self.eta = '{} | {:.1%} | L{}'.format("Paused" if self.paused else datetime.timedelta(seconds=int(eta)), n / self.nlines, n)
self.last_line = n
def list_sdcard(self):
if self.app.comms.list_sdcard(self._list_sdcard_results):
# TODO open waiting dialog
pass
@mainthread
def _list_sdcard_results(self, l):
# dismiss waiting dialog
fl = {}
for f in l:
f = '/sd/{}'.format(f)
if f.endswith('/'):
fl[f[:-1]] = {'size': 0, 'isdir': True}
else:
fl[f] = {'size': 0, 'isdir': False}
# get file to print
f = FileDialog()
f.open(title='SD File to print', file_list=fl, cb=self._start_sd_print)
def _start_sd_print(self, file_path, directory):
Logger.info("MainWindow: SDcard print: {}".format(file_path))
self.app.comms.write('play {}\n'.format(file_path))
def show_viewer(self):
if self.is_printing:
self._show_viewer(self.app.gcode_file, self.last_path)
else:
# get file to view
f = Factory.filechooser()
f.open(self.last_path, title='File to View', cb=self._show_viewer)
def _show_viewer(self, file_path, directory):
self.set_last_file(directory, file_path)
self.app.sm.current = 'viewer'
def do_kill(self):
if self.status == 'Alarm':
self.app.comms.write('$X\n')
else:
# are you sure?
mb = MessageBox(text='KILL - Are you Sure?', cb=self._do_kill)
mb.open()
def _do_kill(self, ok):
if ok:
self.app.comms.write('\x18')
def do_update(self):
try:
p = subprocess.Popen(['git', 'pull'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result, err = p.communicate()
if p.returncode != 0:
self.add_line_to_log(">>> Update: Failed to run git pull")
else:
need_update = True
str = result.decode('utf-8').splitlines()
self.add_line_to_log(">>> Update:")
for l in str:
self.add_line_to_log(l)
if "up-to-date" in l:
need_update = False
if need_update:
self.add_line_to_log(">>> Update: Restart may be required")
except Exception:
self.add_line_to_log(">>> Update: Error trying to update. See log")
Logger.error('MainWindow: {}'.format(traceback.format_exc()))
def show_camera_screen(self):
self.app.sm.current = 'camera'
@mainthread
def tool_change_prompt(self, l):
# Print is paused by gcode command M6, prompt for tool change
self.display("ACTION NEEDED: Manual Tool Change:\n Tool: {}\nWait for machine to stop, then you can jog around to change the tool.\n tap resume to continue".format(l))
@mainthread
def m0_dlg(self):
MessageBox(text='M0 Pause, click OK to continue', cb=self._m0_dlg).open()
def _m0_dlg(self, ok):
self.app.comms.release_m0()
# called by query timer in comms context, return strings for queries to send
def get_queries(self):
if not self.app.is_connected or self.is_printing:
return ""
if not self.app.is_v2 and self.status == 'Run':
# for v1 we do not send these commands when running as they clog up the USB serial channel
return ""
cmd = ""
if self.app.is_desktop == 0:
# we only send query for the tab we are on
current_tab = self.ids.tabs.current_tab.text
if current_tab == 'Macros': # macros screen
cmd += self.ids.macros.update_buttons()
# always need the $I
cmd += self.ids.dro_widget.update_buttons()
else:
# in desktop mode we need to poll for state changes for macros and DRO
cmd += self.ids.macros.update_buttons()
cmd += self.ids.dro_widget.update_buttons()
return cmd
def config_editor(self):
if self.app.is_v2:
MessageBox(text='Implemented for V1 config only').open()
return
self.app.config_editor.populate()
self.app.sm.current = 'config_editor'
def text_editor(self):
# get file to view
f = Factory.filechooser()
f.open(self.last_path, title='File to Edit', filters=['*'], cb=self._text_editor)
def _text_editor(self, file_path, directory):
self.app.text_editor.open(file_path)
self.app.sm.current = 'text_editor'
class MainScreen(Screen):
pass
class SmoothieHost(App):
is_connected = BooleanProperty(False)
status = StringProperty("Not Connected")
wpos = ListProperty([0, 0, 0])
mpos = ListProperty([0, 0, 0, 0, 0, 0])
fr = NumericProperty(0)
frr = NumericProperty(0)
fro = NumericProperty(100)
sr = NumericProperty(0)
lp = NumericProperty(0)
is_inch = BooleanProperty(False)
is_spindle_on = BooleanProperty(False)
is_abs = BooleanProperty(True)
is_desktop = NumericProperty(0)
is_cnc = BooleanProperty(False)
tab_top = BooleanProperty(False)
main_window = ObjectProperty()
gcode_file = StringProperty()
is_show_camera = BooleanProperty(False)
is_spindle_camera = BooleanProperty(False)
manual_tool_change = BooleanProperty(False)
is_v2 = BooleanProperty(True)
wait_on_m0 = BooleanProperty(False)
# Factory.register('Comms', cls=Comms)
def __init__(self, **kwargs):
super(SmoothieHost, self).__init__(**kwargs)
if len(sys.argv) > 1:
# override com port
self.use_com_port = sys.argv[1]
else:
self.use_com_port = None
self.webserver = False
self._blanked = False
self.blank_timeout = 0
self.last_touch_time = 0
self.camera_url = None
self.loaded_modules = []
self.secs = 0
self.fast_stream = False
self.last_probe = {'X': 0, 'Y': 0, 'Z': 0, 'status': False}
self.tool_scripts = ToolScripts()
self.desktop_changed = False
def build_config(self, config):
config.setdefaults('General', {
'last_gcode_path': os.path.expanduser("~"),
'last_print_file': '',
'serial_port': 'serial:///dev/ttyACM0',
'report_rate': '1.0',
'blank_timeout': '0',
'manual_tool_change': 'false',
'wait_on_m0': 'false',
'fast_stream': 'false',
'v2': 'false',
'is_spindle_camera': 'false'
})
config.setdefaults('UI', {
'display_type': "RPI Touch",
'cnc': 'false',
'tab_top': 'false',
'screen_size': 'auto',
'screen_pos': 'auto',
'filechooser': 'default'
})
config.setdefaults('Extruder', {
'last_bed_temp': '60',
'last_hotend_temp': '185',
'length': '20',
'speed': '300',
'hotend_presets': '185 (PLA), 230 (ABS)',
'bed_presets': '60 (PLA), 110 (ABS)'
})
config.setdefaults('Jog', {
'xy_feedrate': '3000'
})
config.setdefaults('Web', {
'webserver': 'false',
'show_video': 'false',
'camera_url': 'http://localhost:8080/?action=snapshot'
})
def build_settings(self, settings):
jsondata = """
[
{ "type": "title",
"title": "UI Settings" },
{ "type": "options",
"title": "Desktop Layout",
"desc": "Select Display layout, RPI is for 7in touch screen layout",
"section": "UI",
"key": "display_type",
"options": ["RPI Touch", "Small Desktop", "Large Desktop", "Wide Desktop", "RPI Full Screen"]
},
{ "type": "bool",
"title": "CNC layout",
"desc": "Turn on for a CNC layout, otherwise it is a 3D printer Layout",
"section": "UI",
"key": "cnc"
},
{ "type": "bool",
"title": "Tabs on top",
"desc": "TABS are on top of the screen",
"section": "UI",
"key": "tab_top"
},
{ "type": "options",
"title": "File Chooser",
"desc": "Which filechooser to use in desktop mode",
"section": "UI",
"key": "filechooser",
"options": ["default", "wx", "zenity", "kdialog"]
},
{ "type": "title",
"title": "General Settings" },
{ "type": "numeric",
"title": "Report rate",
"desc": "Rate in seconds to query for status from Smoothie",
"section": "General",
"key": "report_rate" },
{ "type": "numeric",
"title": "Blank Timeout",
"desc": "Inactive timeout in seconds before screen will blank",
"section": "General",
"key": "blank_timeout" },
{ "type": "bool",
"title": "Manual Tool change",
"desc": "On M6 let user do a manual tool change",
"section": "General",
"key": "manual_tool_change"
},
{ "type": "bool",
"title": "Wait on M0",
"desc": "On M0 popup a dialog and pause until it is dismissed",
"section": "General",
"key": "wait_on_m0"
},
{ "type": "bool",
"title": "Spindle Camera",
"desc": "Enable the spindle camera screen",
"section": "General",
"key": "is_spindle_camera"
},
{ "type": "bool",
"title": "Version 2 Smoothie",
"desc": "Select for version 2 smoothie",
"section": "General",
"key": "v2"
},
{ "type": "bool",
"title": "Fast Stream",
"desc": "Allow fast stream for laser over network",
"section": "General",
"key": "fast_stream"
},
{ "type": "title",
"title": "Web Settings" },
{ "type": "bool",
"title": "Web Server",
"desc": "Turn on Web server to remotely check progress",
"section": "Web",
"key": "webserver"
},
{ "type": "bool",
"title": "Show Video",
"desc": "Display mjpeg video in web progress",
"section": "Web",
"key": "show_video"
},
{ "type": "string",
"title": "Camera URL",
"desc": "URL for camera stream",
"section": "Web",
"key": "camera_url"
},
{ "type": "title",
"title": "Extruder Settings" },
{ "type": "string",
"title": "Hotend Presets",
"desc": "Set the comma separated presets for the hotend temps",
"section": "Extruder",
"key": "hotend_presets"
},
{ "type": "string",
"title": "Bed Presets",
"desc": "Set the comma separated presets for the bed temps",
"section": "Extruder",
"key": "bed_presets"
}
]
"""
settings.add_json_panel('SmooPie application', self.config, data=jsondata)
def on_config_change(self, config, section, key, value):
# print("config changed: {} - {}: {}".format(section, key, value))
token = (section, key)
if token == ('UI', 'cnc'):
self.is_cnc = value == "1"
elif token == ('UI', 'display_type'):
self.desktop_changed = True
self.main_window.display("NOTICE: Restart is needed")
elif token == ('UI', 'tab_top'):
self.tab_top = value == "1"
elif token == ('Extruder', 'hotend_presets'):
self.main_window.ids.extruder.ids.set_hotend_temp.values = value.split(',')
elif token == ('Extruder', 'bed_presets'):
self.main_window.ids.extruder.ids.set_bed_temp.values = value.split(',')
elif token == ('General', 'blank_timeout'):
self.blank_timeout = float(value)
elif token == ('General', 'manual_tool_change'):
self.manual_tool_change = value == '1'
elif token == ('General', 'wait_on_m0'):
self.wait_on_m0 = value == '1'
elif token == ('General', 'v2'):