# This file contains common pin mappings for the Geeetech Spark E3 board

# To use this config, the firmware should be compiled for the
# STM32F103 with a "32KiB bootloader" and USB communication.
# Select "Disable SWD at startup (for GigaDevice stmf32f103 clones)"

# The "make flash" command does not work on the Geeetech Spark E3 board. Instead,
# after running "make", copy the generated "out/klipper.bin" file to a
# file named "GTM32Source.bin" on an SD card and then restart the board with that SD card.

# Rename the file to printer.cfg

##################################################################
# Printer
##################################################################

[mcu]
#obtain your MCU id using ls /dev/serial/by-path/*
#serial: /dev/serial/by-id/usb-1a86_USB_Serial-if00-port0
serial: /dev/ttyUSB0
restart_method: command

[printer]
kinematics: cartesian
max_velocity: 200
max_accel: 3000
max_z_velocity: 5
max_z_accel: 100

#[static_digital_output usb_pullup_enable]
#pins: !PA14

[bltouch]
sensor_pin: ^PC4
control_pin: PA8
x_offset: -45
y_offset: -10
z_offset: 3
#pin_up_touch_mode_reports_triggered: FALSE #needed bc of the bltouch clone used by sunlu

[safe_z_home]
home_xy_position: 110,110
speed: 50
z_hop: 10
z_hop_speed: 5

[bed_mesh]
speed: 50
horizontal_move_z: 6
mesh_min: 10, 10
mesh_max: 170, 200
probe_count: 3,3
fade_start: 1
fade_end: 10

[bed_screws]
#for BED_SCREWS_ADJUST
screw1: 31,38 #X,Y Position
screw1_name: Front Left
screw2: 201,38 #X,Y Position
screw2_name: Front Right
screw3: 201,204 #X,Y Position
screw3_name: Rear Right
screw4: 31,204 #X,Y Position
screw4_name: Rear Left

##IMPORTANT. If using the filament sensor add CLEAR_PAUSE to your slicer's start gcode or to your print start macro.##
##The act of loading and unloading filament will trigger a paused state##
[filament_motion_sensor Filament_Sensor]
detection_length: 7.0
extruder: extruder
switch_pin: !PA11
pause_on_runout: FALSE
runout_gcode: PAUSE

#########################################################
# Motion Axis
#########################################################

[stepper_x]
step_pin: PC3
dir_pin: !PC14    ;E3 !
enable_pin: !PA1
microsteps: 16
rotation_distance: 40
endstop_pin: PA7
position_endstop: 0
position_max: 220
homing_speed: 25

[stepper_y]
step_pin: PE6
dir_pin: !PE5
enable_pin: !PC13
microsteps: 16
rotation_distance: 40
#rotation_distance: 26.66666666666667
endstop_pin: PC15
position_endstop: 0
position_max: 220
homing_speed: 30

[stepper_z]
step_pin: PE0
dir_pin: PB9
enable_pin: !PE4   
microsteps: 16
rotation_distance: 8
position_max: 260
position_endstop: 0
#endstop_pin: probe:z_virtual_endstop
endstop_pin: PB5
position_min: -1.2

###################################################
# Heaters
###################################################

[extruder]
step_pin: PB7
dir_pin: !PB6
enable_pin: !PB8
microsteps: 16

instantaneous_corner_velocity: 0.500
max_extrude_only_velocity:35
max_extrude_only_accel:3500

rotation_distance: 7      # BMG Extruder
#rotation_distance: 23.18840579710145    # MK8 Extruder
#rotation_distance = <full_steps_per_rotation> * <microsteps> / <steps_per_mm> or
#rotation_distance = <full_steps_per_rotation> * <microsteps> * <step_distance>


nozzle_diameter: 0.400
filament_diameter: 1.750
heater_pin: PA0
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC1
control: pid
pid_Kp: 19.479
pid_Ki: 1.073
pid_Kd: 88.385
min_temp: 0
max_temp: 250
pressure_advance: 0.2
pressure_advance_smooth_time: 0.040


[heater_bed]
heater_pin: PC2
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PC0
control: pid
pid_Kp: 62.673
pid_Ki: 1.530
pid_Kd: 641.619
min_temp: 0
max_temp: 130

#########################################
# Fans
#########################################

[heater_fan Hotend]
heater: extruder
pin: PD3
heater_temp: 50.0

[fan]
pin: PE1
#pin: PD6


###############################################
# Stock Screen
###############################################

[board_pins]
aliases:
    # EXP1 header
    #EXP1_1=PA15,  EXP1_3=PA3,   EXP1_5=PA2, EXP1_7=PA3,  EXP1_9=<GND>,
    #EXP1_2=PB0, EXP1_4=<RST>, EXP1_6=PA2,  EXP1_8=PB15, EXP1_10=<5V>

[display]
lcd_type: st7920
cs_pin: PA5                #EXP1_7
sclk_pin: PB1             #EXP1_6
sid_pin: PA6             #EXP1_8
encoder_pins:  ^PA3, ^PA2 #^EXP1_5, ^EXP1_3
click_pin: ^!PB0         #^!EXP1_2

[output_pin beeper]
pin: PA15    #EXP1_1
pwm: True
value: 0
shutdown_value: 0
cycle_time: 0.001
scale: 1
[gcode_macro M300]
gcode:
  {% set S = params.S|default(1000)|int %} ; S sets the tone frequency
  {% set P = params.P|default(100)|int %} ; P sets the tone duration
  {% set L = 0.5 %} ; L varies the PWM on time, close to 0 or 1 the tone gets a bit quieter. 0.5 is a symmetric waveform
  {% if S <= 0 %} ; dont divide through zero
  {% set F = 1 %}
  {% set L = 0 %}
  {% elif S >= 10000 %} ;max frequency set to 10kHz
  {% set F = 0 %}
  {% else %}
  {% set F = 1/S %} ;convert frequency to seconds
  {% endif %}
    SET_PIN PIN=beeper VALUE={L} CYCLE_TIME={F} ;Play tone
  G4 P{P} ;tone duration
    SET_PIN PIN=beeper VALUE=0

[virtual_sdcard]
path: /home/mks/printer_data/gcodes

[pause_resume]

[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: PAUSE_BASE
# change this if you need more or less extrusion
variable_extrude: 1.0
gcode:
  ##### read E from pause macro #####
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  ##### set park positon for x and y #####
  # default is your max posion 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 %}
  ##### calculate save lift position #####
  {% set max_z = printer.toolhead.axis_maximum.z|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% if act_z < (max_z - 2.0) %}
      {% set z_safe = 2.0 %}
  {% else %}
      {% set z_safe = max_z - act_z %}
  {% endif %}
  ##### end of definitions #####
  PAUSE_BASE
  G91
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G1 E-{E} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    G1 Z{z_safe} F900
    G90
    G1 X{x_park} Y{y_park} F6000
  {% else %}
    {action_respond_info("Printer not homed")}
  {% endif %} 

[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: RESUME_BASE
gcode:
  ##### read E from pause macro #####
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  #### get VELOCITY parameter if specified ####
  {% if 'VELOCITY' in params|upper %}
    {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
  {%else %}
    {% set get_params = "" %}
  {% endif %}
  ##### end of definitions #####
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G91
    G1 E{E} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}  
  RESUME_BASE {get_params}

[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: CANCEL_PRINT_BASE
gcode:
  TURN_OFF_HEATERS
  CANCEL_PRINT_BASE
  G91
  G1 Z10 F3000
  G90

[led GT_led]
green_pin:PE2
cycle_time: 1
initial_GREEN: 0.5
   
[input_shaper]
shaper_freq_x: 53  # frequency for the X mark of the test model
shaper_freq_y: 45  # frequency for the Y mark of the test model
shaper_type: mzv

[gcode_macro PRINT_START]
#   Use PRINT_START for the slicer starting script - please customise for your slicer of choice
gcode:
    G32                            ; home all axes
    G1 Z10 F3000                   ; move nozzle away from bed

[gcode_macro G32]
gcode:
    BED_MESH_CLEAR
    G28
    BED_MESH_CALIBRATE

[gcode_macro G29]
gcode:
    G28
    BED_MESH_PROFILE LOAD="default"

#*# <---------------------- SAVE_CONFIG ---------------------->
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
#*#
#*# [bed_mesh default]
#*# version = 1
#*# points =
#*# 	  -0.790000, -1.132500, -1.317500
#*# 	  -0.730000, -1.025000, -1.277500
#*# 	  -0.782500, -1.070000, -1.325000
#*# x_count = 3
#*# y_count = 3
#*# mesh_x_pps = 2
#*# mesh_y_pps = 2
#*# algo = lagrange
#*# tension = 0.2
#*# min_x = 10.0
#*# max_x = 170.0
#*# min_y = 10.0
#*# max_y = 200.0
