-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmuff.py
728 lines (664 loc) · 26.9 KB
/
smuff.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
#---------------------------------------------------------------------------------------------
# SMuFF Klipper Module
#---------------------------------------------------------------------------------------------
#
# Copyright (C) 2022-2024 Technik Gegg <[email protected]>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
#
#
# This version implements the following GCodes which can be accessed from
# the Klipper console:
#
# SMUFF_CONN - Connect to the SMuFF via serial interface
# SMUFF_DISC - Disconnect from the SMuFF
# SMUFF_CONNECTED - Show current connection status (see also: SMUFF_STATUS)
# SMUFF_CUT - Cut filament (if Filament-Cutter is configured)
# SMUFF_WIPE - Wipe nozzle (if Wiper is installed)
# SMUFF_LID_OPEN - Open lid servo
# SMUFF_LID_CLOSE - Close lid servo
# SMUFF_SET_SERVO - Position a servo
# SMUFF_TOOL_CHANGE - Change the current tool (mainly called from Tn GCodes)
# SMUFF_INFO - Query the firmware info from SMuFF
# SMUFF_STATUS - Query the status from SMuFF
# SMUFF_SEND - Send GCode to the SMuFF
# SMUFF_PARAM - Send configuration parameters to the SMuFF
# SMUFF_MATERIALS - Query the materials configured on the SMuFF
# SMUFF_SWAPS - Query the tool swaps configured on the SMuFF
# SMUFF_LIDMAPPINGS - Query the lid servo mappings configured on the SMuFF
# SMUFF_LOAD - Load filament on active tool on the SMuFF
# SMUFF_UNLOAD - Unload filament from active tool on the SMuFF
# SMUFF_HOME - Home Selector (and Revolver if available)
# SMUFF_MOTORS_OFF - Turn stepper motors on the SMuFF off
# SMUFF_CLEAR_JAM - Resets the Feeder Jammed flag on the SMuFF
# SMUFF_RESET - Resets the SMuFF
# SMUFF_VERSION - Query the SMuFF module version
# SMUFF_RESET_AVG - Resets the tool change statistics
# SMUFF_INSTANCE - Sets the active instance on an IDEX machine
#
# During runtime you have the option to query the following values from within
# scripts or macros:
#
# printer.smuff.tools (int) The number of tools available on the SMuFF
# printer.smuff.activetool (int) The active tool number, -1 if Selector is homed
# printer.smuff.pendingtool (int) The new tool number requested by a tool change, -1 if no tool change is pending
# printer.smuff.selector (bool) True = triggered
# printer.smuff.revolver (bool) True = triggered
# printer.smuff.feeder (bool) True = triggered
# printer.smuff.feeder2 (bool) True = triggered
# printer.smuff.fwinfo (string) Same as with M115 GCode command
# printer.smuff.isbusy (bool) True if SMuFF is doing stuff
# printer.smuff.iserror (bool) True if the last command processed did fail
# printer.smuff.isprocessing (bool) True while processing stuff
# printer.smuff.isconnected (bool) True when connected through serial port
# printer.smuff.isidle (bool) True if SMuFF is in idle state
# printer.smuff.sdstate (bool) True if SD-Card on SMuFF is inserted
# printer.smuff.lidstate (bool) True if Lid is closed
# printer.smuff.hascutter (bool) True if Filament-Cutter is configured (see below)
# printer.smuff.haswiper (bool) True if Wiper is configured (see below)
# printer.smuff.device (string) Name of the SMuFF
# printer.smuff.materials (array) Two dimensional array of type materials
# printer.smuff.swaps (array) Array of tool swaps
# printer.smuff.lidmappings (array) Array of lid mappings
# printer.smuff.version (float) Module version info
# printer.smuff.fwversion (string) The SMuFF firmware version
# printer.smuff.fwmode (string) The configured SMuFF mode
# printer.smuff.fwoptions (string) The configured SMuFF options
# printer.smuff.loadstate (int) The current tool filament load state
# printer.smuff.isdde (bool) True if DDE is configured
# printer.smuff.hassplitter (bool) True if Splitter Option is configured
#
#
# The configuration takes place in 'smuff.cfg', which contains the configuration for this
# module, as well as the tool change GCodes, as the following example shows.
# Add a [include smuff.cfg] line into your 'printer.cfg' to use this module.
#
# [smuff]
# serial=/dev/serial/by-id/... or /dev/ttySMuFF if it has been set up accordingly
# baudrate=115200
# serialTimeout=10
# autoConnectSerial=yes
# commandTimeout=25
# toolchangeTimeout=90
# watchdogTimeout=30
# autoLoad=yes
# hasCutter=yes
# hasWiper=no
# debug=no
#
#
# PRE_TOOLCHANGE and POST_TOOLCHANGE will be called from within
# the module in order to prepare the printer for a tool change /
# resume printing.
#
# Basically it's pausing/resuming the print but you can put in
# different commands, such like wiping, purging or ramming,
# if needed.
#
# PARK_TOOLHEAD macro sets the toolhead to a save position for
# the tool change. Modify the positions to suite your printer
# if needed.
#
# [gcode_macro PRE_TOOLCHANGE]
# description: Macro called from SMUFF_TOOL_CHANGE for pre-processing. Don't call it via console or script!
# gcode:
# # the SMuFF module will provide a parameter T for the current tool in case
# # you want to evalute those before pausing the print
# # { action_respond_info("Tool change to 'T%s' pending" % params.T|int) }
# # pause the print first
# PAUSE_BASE
# PARK_TOOLHEAD
# # then switch off extruder motor (it's mandatory!)
# SET_STEPPER_ENABLE STEPPER=extruder ENABLE=0
# [gcode_macro POST_TOOLCHANGE]
# description: Macro called from SMUFF_TOOL_CHANGE for post-processing. Don't call it via console or script!
# gcode:
# # the SMuFF module will provide two parameters, P for the previous tool, T for the current tool
# # in case you want to evalute those before resuming
# # { action_respond_info("Tool change from 'T%s' to 'T%s' done" % params.P|int, params.T|int) }
# # switch extruder motor back on
# SET_STEPPER_ENABLE STEPPER=extruder ENABLE=1
# # then resume printing
# RESUME_BASE
#
# [gcode_macro PARK_TOOLHEAD]
# description: Parks the toolhead before a tool change.
# gcode:
# # set park position for x and y; default is the max. position
# # taken from your printer.cfg
# {% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
# {% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
# # z position is 10 mm above the print
# {% set z_park_lift = 10.0 %}
# # validate the lift position
# {% set max_z = printer.toolhead.axis_maximum.z|float %}
# {% set act_z = printer.toolhead.position.z|float %}
# {% if (act_z + z_park_lift) <= max_z %}
# {% set z_safe = act_z + z_park_lift %}
# {% else %}
# {% set z_safe = max_z %}
# {% endif %}
# # park the toolhead at the calculated positions (if all axes are homed)
# {% if "xyz" in printer.toolhead.homed_axes %}
# # move in absolute coordinates
# G90
# G1 Z{z_safe} F2000
# G1 X{x_park} Y{y_park} F12000
# {% if printer.gcode_move.absolute_coordinates|lower == 'false' %}
# G91
# {% endif %}
# {% else %}
# {action_respond_info("Printer is not homed!")}
# {% endif %}
#
# Tool change macros (T0-T11) macros will call the internal function SMUFF_TOOL_CHANGE with
# the according tool as parameter. Keep in mind, you'll need as many macros as you have tools
# on your SMuFF. Although, it doesn't hurt configuring the max. number of tools, which in
# this case is 12 (0-11).
#
# [gcode_macro T0]
# gcode:
# SMUFF_TOOL_CHANGE T=0
# ...
#
# [gcode_macro T11]
# gcode:
# SMUFF_TOOL_CHANGE T=11
#---------------------------------------------------------------------------------------------
import logging
from logging.handlers import TimedRotatingFileHandler
from . import smuff_core # main functions are located in this module
IS_KLIPPER = True # flag has to be set to True for Klipper, False for OctoPrint
# Wrapper class for logging with a prefix
class SLogger:
def __init__(self, prefix):
self._prefix = prefix
self._logger = None
if self._prefix == None: # create separate logfile
logHandler = logging.handlers.TimedRotatingFileHandler(filename="/var/tmp/smuff.log", when='H', interval=1, backupCount=5, encoding="utf-8")
logHandler.setLevel(logging.DEBUG)
logHandler.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s: %(message)s"))
self._logger = logging.getLogger("SMuFF")
self._logger.propagate = False
self._logger.addHandler(logHandler)
def info(self, message):
if self._logger != None:
self._logger.info(message)
def error(self, message):
if self._logger != None:
self._logger.error(message)
def debug(self, message):
if self._logger != None:
self._logger.debug(message)
class SMuFF:
def __init__(self, config, logger):
self._log = logger
self.SCA = smuff_core.SmuffCore(logger, IS_KLIPPER, self.smuffStatusCallbackA, self.smuffResponseCallbackA, config)
self.SCB = smuff_core.SmuffCore(logger, IS_KLIPPER, self.smuffStatusCallbackB, self.smuffResponseCallbackB, config)
self._activeInstance = "A"
self._instance = self.SCA
self._hasIDEX = False
self._printer = config.get_printer()
self._reactor = self._printer.get_reactor()
self.gcode = self._printer.lookup_object("gcode")
self._lastActiveA = False
self._lastActiveB = False
try:
self.pause_resume = self._printer.load_object(config, "pause_resume")
except config.error:
raise self._printer.config_error(smuff_core.T_CFG_GCODE_ERR.format("PAUSE_RESUME"))
# get configuration
self._ignoreDebug = config.get("ignoreDebug", default=smuff_core.T_NO).upper() == smuff_core.T_YES
self._hasIDEX = config.get("hasIDEX", default=smuff_core.T_NO).upper() == smuff_core.T_YES
serialPort = config.get("serial")
if not serialPort:
raise config.error(smuff_core.T_CFG_ERR)
self.SCA.serialPort = serialPort
self.SCA.baudrate = config.getint("baudrate", default=115200)
self.SCA.timeout = config.getfloat("serialTimeout", default=5.0)
self.SCA.cmdTimeout = config.getfloat("commandTimeout", default=20.0)
self.SCA.tcTimeout = config.getfloat("toolchangeTimeout", default=90.0)
self.SCA.autoConnect = config.get("autoConnectSerial", default=smuff_core.T_YES).upper() == smuff_core.T_YES
self.SCA.hasCutter = config.get("hasCutter", default=smuff_core.T_YES).upper() == smuff_core.T_YES # will be eventually overwritten by the SMuFF config
self.SCA.hasWiper = config.get("hasWiper", default=smuff_core.T_NO).upper() == smuff_core.T_YES
self.SCA.dumpRawData = config.get("debug", default=smuff_core.T_NO).upper() == smuff_core.T_YES
self.SCA.wdTimeout = config.getfloat("watchdogTimeout", default=60)
self.SCA.ignoreDebug = self._ignoreDebug
# 2nd serial port gets only handled if hasIDEX is set
if self._hasIDEX:
serialPortB = config.get("serialB")
if serialPortB:
self.SCB.serialPort = serialPortB
self.SCB.baudrate = config.getint("baudrateB", default=115200)
self.SCB.timeout = config.getfloat("serialTimeoutB", default=5.0)
self.SCB.cmdTimeout = config.getfloat("commandTimeoutB", default=20.0)
self.SCB.tcTimeout = config.getfloat("toolchangeTimeoutB", default=90.0)
self.SCB.autoConnect = config.get("autoConnectSerialB", default=smuff_core.T_YES).upper() == smuff_core.T_YES
self.SCB.hasCutter = config.get("hasCutterB", default=smuff_core.T_YES).upper() == smuff_core.T_YES # will be eventually overwritten by the SMuFF config
self.SCB.hasWiper = config.get("hasWiperB", default=smuff_core.T_NO).upper() == smuff_core.T_YES
self.SCB.dumpRawData = self.SCA.dumpRawData
self.SCB.wdTimeout = config.getfloat("watchdogTimeoutB", default=60)
self.SCB.ignoreDebug = self._ignoreDebug
# register Klipper event handlers
self._printer.register_event_handler("klippy:disconnect", self.event_disconnect)
self._printer.register_event_handler("klippy:connect", self.event_connect)
self._printer.register_event_handler("klippy:ready", self.event_ready)
self._printer.register_event_handler("klippy:shutdown", self.event_shutdown)
# register GCodes for SMuFF
self.gcode.register_command("SMUFF_CONN", self.cmd_connect, smuff_core.T_HELP_CONN)
self.gcode.register_command("SMUFF_DISC", self.cmd_disconnect, smuff_core.T_HELP_DISC)
self.gcode.register_command("SMUFF_CONNECTED", self.cmd_connected, smuff_core.T_HELP_CONNECTED)
self.gcode.register_command("SMUFF_CUT", self.cmd_cut, smuff_core.T_HELP_CUT)
self.gcode.register_command("SMUFF_WIPE", self.cmd_wipe, smuff_core.T_HELP_WIPE)
self.gcode.register_command("SMUFF_LID_OPEN", self.cmd_lid_open, smuff_core.T_HELP_LID_OPEN)
self.gcode.register_command("SMUFF_LID_CLOSE", self.cmd_lid_close, smuff_core.T_HELP_LID_CLOSE)
self.gcode.register_command("SMUFF_SET_SERVO", self.cmd_servo_pos, smuff_core.T_HELP_SET_SERVO)
self.gcode.register_command("SMUFF_TOOL_CHANGE", self.cmd_tool_change, smuff_core.T_HELP_TOOL_CHANGE)
self.gcode.register_command("SMUFF_INFO", self.cmd_fw_info, smuff_core.T_HELP_INFO)
self.gcode.register_command("SMUFF_STATUS", self.cmd_get_states, smuff_core.T_HELP_STATUS)
self.gcode.register_command("SMUFF_SEND", self.cmd_gcode, smuff_core.T_HELP_SEND)
self.gcode.register_command("SMUFF_PARAM", self.cmd_param, smuff_core.T_HELP_PARAM)
self.gcode.register_command("SMUFF_MATERIALS", self.cmd_materials, smuff_core.T_HELP_MATERIALS)
self.gcode.register_command("SMUFF_SWAPS", self.cmd_swaps, smuff_core.T_HELP_SWAPS)
self.gcode.register_command("SMUFF_LIDMAPPINGS", self.cmd_lidmappings, smuff_core.T_HELP_LIDMAPPINGS)
self.gcode.register_command("SMUFF_LOAD", self.cmd_load, smuff_core.T_HELP_LOAD)
self.gcode.register_command("SMUFF_UNLOAD", self.cmd_unload, smuff_core.T_HELP_UNLOAD)
self.gcode.register_command("SMUFF_HOME", self.cmd_home, smuff_core.T_HELP_HOME)
self.gcode.register_command("SMUFF_MOTORS_OFF", self.cmd_motors_off, smuff_core.T_HELP_MOTORS_OFF)
self.gcode.register_command("SMUFF_CLEAR_JAM", self.cmd_clear_jam, smuff_core.T_HELP_CLEAR_JAM)
self.gcode.register_command("SMUFF_RESET", self.cmd_reset, smuff_core.T_HELP_RESET)
self.gcode.register_command("SMUFF_VERSION", self.cmd_version, smuff_core.T_HELP_VERSION)
self.gcode.register_command("SMUFF_RESET_AVG", self.cmd_reset_avg, smuff_core.T_HELP_RESET_AVG)
self.gcode.register_command("SMUFF_DUMP_RAW", self.cmd_dump_raw, smuff_core.T_HELP_DUMP_RAW)
self.gcode.register_command("SMUFF_DEBUG", self.cmd_dump_raw, smuff_core.T_HELP_DUMP_RAW)
self.gcode.register_command("SMUFF_INSTANCE", self.cmd_instance, smuff_core.T_HELP_INSTANCE)
self.gcode.register_command("SMUFF_GETINSTANCE", self.cmd_getinstance, smuff_core.T_HELP_GETINSTANCE)
self.gcode.register_command("SMUFF_TEST", self.cmd_test, "")
def __del__(self):
self._log.info("Destructor has been called...")
pass
def autoConnect(self):
if self.SCA.autoConnect:
self._log.info("Auto connecting SMuFF A...")
self.SCA.connect_SMuFF()
if self._hasIDEX and self.SCB.autoConnect:
self._log.info("Auto connecting SMuFF B...")
self.SCB.connect_SMuFF()
def smuffStatusCallbackA(self, active):
if self._lastActiveA != active:
self._log.info("[ A ] active state switched from {0} to {1}".format(self._lastActiveA, active))
self._lastActiveA = active
if active == False:
self._setResponse("Serial reader has shut down", False, self.SCA)
def smuffStatusCallbackB(self, active):
if self._lastActiveB != active:
self._log.info("[ B ] active state switched from {0} to {1}".format(self._lastActiveB, active))
self._lastActiveB = active
if active == False:
self._setResponse("Serial reader has shut down", False, self.SCB)
def smuffResponseCallbackA(self, message):
self._setResponse(message, False, self.SCA)
def smuffResponseCallbackB(self, message):
self._setResponse(message, False, self.SCB)
#
# Send a response (text) to Klipper GCode panel
#
def _setResponse(self, response, addPrefix = False, instance = None):
fromInst = ""
if self._hasIDEX and not instance == None:
fromInst = " [ A ] " if instance == self.SCA else " [ B ] "
if response != "":
self.gcode.respond_info(fromInst + response)
def _reset(self):
self._log.info("Resetting module")
#
# Klippy disconnect handler
#
def event_disconnect(self):
# make sure to clean up ressources (i.e. serial reader thread) otherwise
# it'll interfere with the new instance after firmware restart
self._log.info("Klippy has disconnected, closing serial communication to SMuFF")
self.SCA.close_serial()
self.SCB.close_serial()
#
# Klippy connect handler
#
def event_connect(self):
# no further action in here
self._log.info("Klippy has connected")
#
# Klippy ready handler
#
def event_ready(self):
# check presence of PRE/POST_TOOLCHANGE macros
self._log.info("Klippy is ready")
if not smuff_core.PRE_TC in self.gcode.ready_gcode_handlers:
raise self._printer.config_error(smuff_core.T_CFG_GCODE_ERR.format("gcode_macro '{0}'".format(smuff_core.PRE_TC)))
else:
self._log.info("gcode_macro '{0}' check OK".format(smuff_core.PRE_TC))
if not smuff_core.POST_TC in self.gcode.ready_gcode_handlers:
raise self._printer.config_error(smuff_core.T_CFG_GCODE_ERR.format("gcode_macro '{0}'".format(smuff_core.POST_TC)))
else:
self._log.info("gcode_macro '{0}' check OK".format(smuff_core.POST_TC))
#
# Klippy shutdown handler
#
def event_shutdown(self):
# make sure to clean up ressources (i.e. serial reader thread) otherwise
# it'll interfere with the new instance after restart
self._log.info("Klippy has shut down, closing serial communication to SMuFF")
self.SCA.close_serial()
self.SCB.close_serial()
def get_instance(self, gcmd=None):
if gcmd:
device = gcmd.get(smuff_core.P_DEVICE, default="A").upper()
if device == "A" or device == "B":
self._activeInstance = device
else:
self.gcode.respond_info(smuff_core.T_INVALID_DEVICE)
else:
self._activeInstance = "A" # make the first device the default
self._instance = self.SCA if self._activeInstance == "A" else self.SCB
def set_instance(self, inst):
self._activeInstance = inst
def chk_connection(self, gcmd=None):
self.get_instance(gcmd)
if not self._instance.isConnected:
self._setResponse(smuff_core.T_NOT_CONN, False, self._instance)
return False
return True
#
# SMUFF_CONN
#
def cmd_connect(self, gcmd=None, autoConnect=None):
self.get_instance(gcmd)
if self._instance.isConnected:
self._setResponse(smuff_core.T_ALDY_CONN, False, self._instance)
return True
status = self._instance.connect_SMuFF(gcmd)
if status:
self._setResponse(smuff_core.T_CONN, False, self._instance)
return True
else:
self._setResponse(smuff_core.T_NOT_CONN, False, self._instance)
return False
#
# SMUFF_DISC
#
def cmd_disconnect(self, gcmd=None):
self.get_instance(gcmd)
self._instance.close_serial()
#
# SMUFF_CONNECTED
#
def cmd_connected(self, gcmd):
self.get_instance(gcmd)
if self._instance.isConnected:
self._setResponse(smuff_core.T_IS_CONN.format(self._instance.serialPort), False, self._instance)
else:
self._setResponse(smuff_core.T_ISNT_CONN, False, self._instance)
#
# SMUFF_CUT
#
def cmd_cut(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._setResponse(smuff_core.T_CUTTING, False, self._instance)
self._instance.send_SMuFF(smuff_core.CUT)
#
# SMUFF_WIPE
#
def cmd_wipe(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._setResponse(smuff_core.T_WIPING, False, self._instance)
self._instance.send_SMuFF(smuff_core.WIPE)
#
# SMUFF_LID_OPEN
#
def cmd_lid_open(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._setResponse(smuff_core.T_OPENING_LID, False, self._instance)
self._instance.send_SMuFF(smuff_core.LIDOPEN)
#
# SMUFF_LID_CLOSE
#
def cmd_lid_close(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._setResponse(smuff_core.T_CLOSING_LID, False, self._instance)
self._instance.send_SMuFF(smuff_core.LIDCLOSE)
#
# SMUFF_SET_SERVO
#
def cmd_servo_pos(self, gcmd=None):
if not self.chk_connection(gcmd):
return
if gcmd:
servo = gcmd.get_int(smuff_core.P_SERVO, default=1) # Lid servo by default
pos = gcmd.get_int(smuff_core.P_ANGLE, default=90)
if pos < 0 or pos > 180:
gcmd.respond_info(smuff_core.T_ERR_SERVOPOS)
return
self._setResponse(smuff_core.T_POSITIONING.format(servo, pos), False, self._instance)
self._instance.send_SMuFF(smuff_core.SETSERVO.format(servo, pos))
#
# SMUFF_TOOL_CHANGE
#
def cmd_tool_change(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._instance.klipper_change_tool(gcmd)
#
# SMUFF_INFO
#
def cmd_fw_info(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._setResponse(smuff_core.T_FW_INFO.format(self._instance.get_fw_info()), False, self._instance)
#
# SMUFF_STATUS
#
def cmd_get_states(self, gcmd=None):
self.get_instance(gcmd)
connStat = self._instance.get_states(gcmd)
self._setResponse(connStat, False, self._instance)
#
# SMUFF_SEND
#
def cmd_gcode(self, gcmd=None):
if not self.chk_connection(gcmd):
return
if gcmd == None:
return
gcode = gcmd.get(smuff_core.P_GCODE)
if not gcode:
gcmd.respond_info(smuff_core.T_NO_PARAM.format(smuff_core.P_GCODE))
return
if gcode.upper() == smuff_core.RESET:
self._instance.send_SMuFF(gcode)
else:
response = self._instance.send_SMuFF_and_wait(gcode)
if response:
gcmd.respond_info(smuff_core.T_SMUFF_RESPONSE.format(response))
#
# SMUFF_PARAM
#
def cmd_param(self, gcmd=None):
if not self.chk_connection(gcmd):
return
if gcmd == None:
return
param = gcmd.get(smuff_core.P_PARAM)
paramVal = gcmd.get(smuff_core.P_PARAMVAL)
if not param:
gcmd.respond_info(smuff_core.T_NO_PARAM.format(smuff_core.P_PARAM))
return
if not paramVal:
gcmd.respond_info(smuff_core.T_NO_VALUE.format(smuff_core.P_PARAMVAL))
return
response = self._instance.send_SMuFF_and_wait(smuff_core.SETPARAM.format(param, paramVal))
if self._instance.isError:
gcmd.respond_info(smuff_core.T_SMUFF_ERR.format(response))
#
# SMUFF_MATERIALS
#
def cmd_materials(self, gcmd=None):
if not self.chk_connection(gcmd):
return
inst = self._instance
response = inst.send_SMuFF_and_wait(smuff_core.GETCONFIG.format(smuff_core.CFG_MATERIALS))
# print materials to console
if response:
for i in range(inst.toolCount):
material = inst.materials[i]
self._setResponse("Tool {0} is '{2} {1}' with a purge factor of {3}%".format(i, material[0], material[1], material[2]), False, inst)
#
# SMUFF_SWAPS
#
def cmd_swaps(self, gcmd=None):
if not self.chk_connection(gcmd):
return
inst = self._instance
response = inst.send_SMuFF_and_wait(smuff_core.GETCONFIG.format(smuff_core.CFG_SWAPS))
# print swaps to console
if response:
for i in range(inst.toolCount):
slot = inst.swaps[i]
self._setResponse("Tool {0} is assigned to slot {1}".format(i, slot), False, inst)
#
# SMUFF_LIDMAPPINGS
#
def cmd_lidmappings(self, gcmd=None):
if not self.chk_connection(gcmd):
return
inst = self._instance
response = inst.send_SMuFF_and_wait(smuff_core.GETCONFIG.format(smuff_core.CFG_SERVOMAPS))
# print lid mappings to console
if response:
for i in range(inst.toolCount):
servoMap = inst.servoMaps[i]
self._setResponse("Tool {0} is closed @ {1} deg.".format(i, servoMap), False, inst)
#
# SMUFF_LOAD
#
def cmd_load(self, gcmd=None):
if not self.chk_connection(gcmd):
return
if not self._instance._okTimer is None:
self._setResponse(smuff_core.T_NOT_READY, False, self._instance)
return
activeTool = self._instance.get_active_tool()
if activeTool != -1:
self._instance.send_SMuFF(smuff_core.LOADFIL)
self._instance._okTimer = self._reactor.register_timer(self._instance.wait_for_ok, self._reactor.NOW + 5.0)
#
# SMUFF_UNLOAD
#
def cmd_unload(self, gcmd=None):
if not self.chk_connection(gcmd):
return
if not self._instance._okTimer is None:
self._setResponse(smuff_core.T_NOT_READY, False, self._instance)
return
activeTool = self._instance.get_active_tool()
if activeTool != -1:
self._instance.send_SMuFF(smuff_core.UNLOADFIL)
self._instance._okTimer = self._reactor.register_timer(self._instance.wait_for_ok, self._reactor.NOW + 5.0)
#
# SMUFF_HOME
#
def cmd_home(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._instance.send_SMuFF(smuff_core.HOME)
#
# SMUFF_MOTORS_OFF
#
def cmd_motors_off(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._instance.send_SMuFF(smuff_core.MOTORSOFF)
#
# SMUFF_CLEAR_JAM
#
def cmd_clear_jam(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._instance.send_SMuFF(smuff_core.UNJAM)
#
# SMUFF_RESET
#
def cmd_reset(self, gcmd=None):
if not self.chk_connection(gcmd):
return
self._instance.send_SMuFF(smuff_core.RESET)
#
# SMUFF_VERSION
#
def cmd_version(self, gcmd=None):
self.get_instance(gcmd)
self._setResponse(smuff_core.VERSION_STRING.format(smuff_core.VERSION_NUMBER, smuff_core.VERSION_DATE), False, self._instance)
#
# SMUFF_RESET_AVG
#
def cmd_reset_avg(self, gcmd=None):
self.get_instance(gcmd)
self._instance.reset_avg()
self._setResponse(smuff_core.T_OK, False, self._instance)
#
# SMUFF_DUMP_RAW
#
def cmd_dump_raw(self, gcmd=None):
self.SCA.dumpRawData = not self.SCA.dumpRawData
self.SCB.dumpRawData = not self.SCB.dumpRawData
self.gcode.respond_info(smuff_core.T_DUMP_RAW.format(smuff_core.T_ON if self.SCA.dumpRawData else smuff_core.T_OFF))
#
# SMUFF_INSTANCE
#
def cmd_instance(self, gcmd=None):
if self._hasIDEX == False:
self.gcode.respond_info(smuff_core.T_NO_IDEX)
return
if gcmd == None:
return
param = gcmd.get(smuff_core.P_PARAMVAL).upper()
if param == "A" or param == "B":
self.set_instance(param)
else:
self.gcode.respond_info(smuff_core.T_INVALID_DEVICE)
return
self.gcode.respond_info(smuff_core.T_OK)
#
# SMUFF_GETINSTANCE
#
def cmd_getinstance(self, gcmd=None):
self.gcode.respond_info(smuff_core.T_ACTIVE_INSTANCE.format(self._activeInstance))
#
# SMUFF_TEST
#
def cmd_test(self, gcmd=None):
self.gcode.respond_info(smuff_core.T_OK)
#
# Set status values to be used within Klipper (scripts, GCode)
#
def get_status(self, eventtime=None):
#self._log.info("get_status being called. M:{0} S:{1} ")
self.get_instance(None)
return self._instance.get_status()
#
# Main entry point; Creates a new instance of this module.
#
def load_config(config):
#logger = SLogger("SMuFF: {0}")
logger = SLogger(None)
try:
instance = SMuFF(config, logger)
logger.info("Module instance successfully created")
if instance:
instance.autoConnect()
return instance
except Exception as err:
logger.error("Unable to create module instance.\n\t{0}".format(err))
return None