(author: Chris P)
(version: 0.5)
(date: 06/27/24)

(load tool in spindle safety macro)
; This is called form the ATC page
; Parameter #3991 is used to track the current tool loaded it in the spindle (persistently)
; Parameters #4001 to #4024 are used to track which tool is in which pocket (persistently)
; Parameter #4000 is not populated just used in the maths to calculate the above numbers
; #<number_of_pockets>: The number of pockets the ATC platter has, This is user set in the INI file via #<_ini[atc]pockets>

o<load_spindle_safety> sub
(PRINT, o<load_spindle_safety>)

#<load_spindle_tool_number> = #1 ; this is the value form the ATC tab
#<probe_tool_number> = #3014

; default to a 12 pocket ATC (matching DynATC Widget behaviour), then update based on INI settings
#<number_of_pockets> = 12
o101 if [EXISTS[#<_ini[atc]pockets>]]
    #<number_of_pockets> = #<_ini[atc]pockets>
o101 endif

#10 = 0
#11 = 0 ; 1 if tool is already stored in carousel

o110 while [#10 LT #<number_of_pockets>]
    #10 = [#10+1]
    o120 if [#<load_spindle_tool_number> EQ #[4000 + #10]]
        (DEBUG, the tool you are trying to load is already stored in carousel pocket %d#10)
        #11 = 1
    o120 endif
o110 endwhile

o130 if [#11 EQ 0]
    M61 Q#<load_spindle_tool_number> G43 H#<load_spindle_tool_number>
    #3991 = #<load_spindle_tool_number>
o130 endif

o140 if [#<load_spindle_tool_number> EQ #<probe_tool_number>]
    S0 M5
o140 endif

o<program_coolant> call

o<load_spindle_safety> endsub

M2 (end program)
