-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support loading gcode_macro templates from files
Currently, writing a python macro is somewhat painful, requiring preficing each line with a single `!` to make preserve indentation. This also heavily limits editor support. This PR adds support for `!!include path/to/macro.py` (and technically `!!include macro.notpy` resolves as jinja2 gcode templates). For python macros, I've also added type stubs in `klippy/macro.pyi` and added `TYPE_CHECKING` to the environment. This allows for basic typing support in macro definitions. In the future I would love to be able to generate a bespoke type stub including Printer objects, but thats long term. ``` if TYPE_CHECKING: from klippy.macro import * ```
- Loading branch information
Showing
11 changed files
with
268 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import typing | ||
import math | ||
|
||
rawparams: str | ||
params: dict[str, str] | ||
own_vars: dict[str, typing.Any] | ||
|
||
printer: dict[str, dict[str, typing.Any]] | ||
|
||
def emit(gcode: str) -> None: | ||
"Run a G-Code" | ||
|
||
def wait_while(condition: typing.Callable[[], bool]) -> None: | ||
"Wait while a condition is True" | ||
|
||
def wait_until(condition: typing.Callable[[], bool]) -> None: | ||
"Wait until a condition is True" | ||
|
||
def wait_moves() -> None: | ||
"Wait until all moves are completed" | ||
|
||
def blocking(function: typing.Callable[[], typing._T]) -> typing._T: | ||
"Run a blocking task in a thread, waiting for the result" | ||
|
||
def sleep(timeout: float) -> None: | ||
"Wait a given number of seconds" | ||
|
||
def set_gcode_variable(macro: str, variable: str, value: typing.Any) -> None: | ||
"Save a variable to a gcode_macro" | ||
|
||
def action_log(msg: str) -> typing.Literal[""]: | ||
"Log a message to klippy.log" | ||
|
||
def action_emergency_stop( | ||
msg: str = "action_emergency_stop", | ||
) -> typing.Literal[""]: | ||
"Immediately shutdown Kalico" | ||
|
||
def action_respond_info(msg: str) -> typing.Literal[""]: | ||
"Send a message to the console" | ||
|
||
def action_raise_error(msg) -> None: | ||
"Raise a G-Code command error" | ||
|
||
def action_call_remote_method(method: str, **kwargs) -> typing.Literal[""]: | ||
"Call a Kalico webhooks method" | ||
|
||
emergency_stop = action_emergency_stop | ||
respond_info = action_respond_info | ||
raise_error = action_raise_error | ||
call_remote_method = action_call_remote_method | ||
|
||
TYPE_CHECKING: False | ||
|
||
__all__ = ( | ||
"params", | ||
"rawparams", | ||
"own_vars", | ||
"printer", | ||
"emit", | ||
"wait_while", | ||
"wait_until", | ||
"wait_moves", | ||
"blocking", | ||
"sleep", | ||
"set_gcode_variable", | ||
"emergency_stop", | ||
"respond_info", | ||
"raise_error", | ||
"call_remote_method", | ||
"action_call_remote_method", | ||
"action_emergency_stop", | ||
"action_log", | ||
"action_raise_error", | ||
"action_respond_info", | ||
"math", | ||
"TYPE_CHECKING", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Test config for gcode python | ||
[stepper_x] | ||
step_pin: PF0 | ||
dir_pin: PF1 | ||
enable_pin: !PD7 | ||
microsteps: 16 | ||
rotation_distance: 40 | ||
endstop_pin: ^PE5 | ||
position_endstop: 0 | ||
position_max: 200 | ||
homing_speed: 50 | ||
|
||
[stepper_y] | ||
step_pin: PF6 | ||
dir_pin: !PF7 | ||
enable_pin: !PF2 | ||
microsteps: 16 | ||
rotation_distance: 40 | ||
endstop_pin: ^PJ1 | ||
position_endstop: 0 | ||
position_max: 200 | ||
homing_speed: 50 | ||
|
||
[stepper_z] | ||
step_pin: PL3 | ||
dir_pin: PL1 | ||
enable_pin: !PK0 | ||
microsteps: 16 | ||
rotation_distance: 8 | ||
endstop_pin: probe:z_virtual_endstop | ||
position_max: 200 | ||
|
||
[extruder] | ||
step_pin: PA4 | ||
dir_pin: PA6 | ||
enable_pin: !PA2 | ||
microsteps: 16 | ||
rotation_distance: 33.5 | ||
nozzle_diameter: 0.400 | ||
filament_diameter: 1.750 | ||
heater_pin: PB4 | ||
sensor_type: EPCOS 100K B57560G104F | ||
sensor_pin: PK5 | ||
control: pid | ||
pid_Kp: 22.2 | ||
pid_Ki: 1.08 | ||
pid_Kd: 114 | ||
min_temp: 0 | ||
max_temp: 250 | ||
|
||
[heater_bed] | ||
heater_pin: PH5 | ||
sensor_type: EPCOS 100K B57560G104F | ||
sensor_pin: PK6 | ||
control: watermark | ||
min_temp: 0 | ||
max_temp: 130 | ||
|
||
[probe] | ||
pin: PH6 | ||
z_offset: 1.15 | ||
drop_first_result: true | ||
|
||
[bed_mesh] | ||
mesh_min: 10,10 | ||
mesh_max: 180,180 | ||
|
||
[mcu] | ||
serial: /dev/ttyACM0 | ||
|
||
[printer] | ||
kinematics: cartesian | ||
max_velocity: 300 | ||
max_accel: 3000 | ||
max_z_velocity: 5 | ||
max_z_accel: 100 | ||
|
||
[gcode_macro EXTERNAL_GCODE] | ||
gcode: !!include macro_loading/macro.gcode.j2 | ||
|
||
[gcode_macro EXTERNAL_PYTHON] | ||
gcode: !!include macro_loading/macro.py | ||
|
||
# Use the include here to test the include-relative imports work | ||
[include macro_loading/include.cfg] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Test case for python gcode | ||
CONFIG macro_loading.cfg | ||
DICTIONARY atmega2560.dict | ||
|
||
EXTERNAL_GCODE | ||
EXTERNAL_PYTHON | ||
INCLUDE_GCODE | ||
INCLUDE_PYTHON |
Oops, something went wrong.