*-----------------------------------------------------------------------------
*  TC Magazine loading
*-----------------------------------------------------------------------------
*  Module Description
*
*-----------------------------------------------------------------------------

#define /s    KL_line_0                                K +0
#define /s    KL_tab_editor_active_line                K +1000
#define /s    KL_TC_magazine_1                         K +1
#define /s    KL_TC_magazine_2                         K +2
#define /s    KL_TC_mag_1_selection                    K +0

#define /s    ML_soft_key_TC_mag_load_has_been_active  M
#define /s    ML_softkey_T_load_has_been_on            M
#define /s    ML_key_pressed                           M
#define /s    ML_key_TC_mag_load                       M
#define /s    ML_TC_mag_guard_close                    M
#define /s    ML_pulse_actual_pocket_shift             M     ;20120613
#define /s    ML_MAG_display_enable                    M     ;20120613
#define /s    ML_clock_pocket_count                    M     ;20120613
#define /s    ML_data_pocket_count                     M     ;20120613
#define /s    ML_mark_data_pocket_count                M     ;20120613
#define /s    ML_pulse_data_counting                   M     ;20120613
#define /s    ML_mark_clock_pocket_count               M     ;20120613
#define /s    ML_pulse_clock_counting                  M     ;20120613
#define /s    ML_pulse_25_ms                           M     ;20120613
#define /s    ML_pulse_data_25_ms                      M     ;20120613

#define /s /c ML_T_in_MAG                              M[16] ;20120613
#define /s /c ML_T_in_MAG_bit0                         M &ML_T_in_MAG + 0
#define /s /c ML_T_in_MAG_bit1                         M &ML_T_in_MAG + 1
#define /s /c ML_T_in_MAG_bit2                         M &ML_T_in_MAG + 2
#define /s /c ML_T_in_MAG_bit3                         M &ML_T_in_MAG + 3
#define /s /c ML_T_in_MAG_bit4                         M &ML_T_in_MAG + 4
#define /s /c ML_T_in_MAG_bit5                         M &ML_T_in_MAG + 5
#define /s /c ML_T_in_MAG_bit6                         M &ML_T_in_MAG + 6
#define /s /c ML_T_in_MAG_bit7                         M &ML_T_in_MAG + 7
#define /s /c ML_T_in_MAG_bit8                         M &ML_T_in_MAG + 8
#define /s /c ML_T_in_MAG_bit9                         M &ML_T_in_MAG + 9
#define /s /c ML_T_in_MAG_bit10                        M &ML_T_in_MAG + 10
#define /s /c ML_T_in_MAG_bit11                        M &ML_T_in_MAG + 11
#define /s /c ML_T_in_MAG_bit12                        M &ML_T_in_MAG + 12
#define /s /c ML_T_in_MAG_bit13                        M &ML_T_in_MAG + 13
#define /s /c ML_T_in_MAG_bit14                        M &ML_T_in_MAG + 14
#define /s /c ML_T_in_MAG_bit15                        M &ML_T_in_MAG + 15

#define /s    BL_identifier                            B

#define /s    KL_File_type_pocket_table                K+2   ;20120613
#define /s    KL_element_no_of_tool                    K+0   ;20120613

#define /s    WL_tab_editor_active_line                W
#define /s    WL_soft_key_TC_mag_n_selection           W
#define /s    WL_Tool_no_on_actual_pocket              W     ;20120613
#define /s    WL_stack_synchronize                     W     ;20120613
#define /s    WL_TC_magazine_Memory_pocket             W     ;20120613
#define /s    WL_display_counting                      W     ;20120613

EXTERN TC_mag_cw_turn
EXTERN TC_mag_ccw_turn
GLOBAL TC_mag_loading

#include Mac_Lib.inc

;----------------------------
LBL TC_mag_loading
;----------------------------

  LN     MG_TC_auto_active
  R      MG_SK_enable_TC_mag_load_active
  R      MG_SK_enable_T_load_unload
  EMT

  CM     Tool_no_in_pocket             ;20120613
  CM     NO_T_Display                  ;20120613

  L      MG_key_TC_mag_load_active
  AN     ML_key_TC_mag_load
  XO     MG_SK_TC_mag_load_active
  =      MG_SK_TC_mag_load_active

  L      MG_key_TC_mag_load_active
  =      ML_key_TC_mag_load

  L      MG_SK_TC_mag_load_active
  IFT
    L      WG_TC_pocket_number
    <>     K-1
    A[
      L      WG_tool_in_spindle
      <>     K+0
    ]
    A      MG_TC_mag_T0_necessary_before_loading
    A      MG_manual_mode
    S      PN_error_spindle_not_empty
    R      MG_SK_TC_mag_load_active
  ENDI

  LN     I_TC_magazine_guard_locked_closed
  AN     MG_SK_TC_mag_load_active
  A      MG_power_delayed_on
  =      PN_error_TC_magazine_guard_open

  L      MG_edit_pocket_table
  O      MG_manual_mode
  AN     MG_TC_magazine_n_start_active
  =      MG_SK_enable_TC_mag_load_active

  LN     MG_edit_pocket_table
  AN     MG_manual_mode
  AN     MG_TC_magazine_n_start_active
  R      MG_SK_TC_mag_load_active

  L      MG_SK_TC_mag_load_active
  XO     ML_soft_key_TC_mag_load_has_been_active
  S      MG_standby_tool_status_reading

  L      MG_SK_TC_mag_load_active
  =      ML_soft_key_TC_mag_load_has_been_active

  L      MG_SK_TC_mag_load_active
  =      PN_error_TC_magazine_loading
  =      O_lamp_TC_pocket_loading
  RN     O_TC_magazine_load_position_unlock
  RN     MG_SK_enable_T_load_unload
  EMF

  L      NP_M4074_strobe_TOOL_DEF
  S      PN_M4094_quit_TOOL_DEF

  L      I_TC_magazine_guard_closed
  AN     ML_TC_mag_guard_close
  R      O_TC_magazine_guard_unlock

  L      I_TC_magazine_guard_closed
  =      ML_TC_mag_guard_close

  L      MG_key_TC_magazine_guard_unlock
  AN     MG_TC_magazine_n_start_active
  S      O_TC_magazine_guard_unlock

  LN     I_TC_magazine_guard_locked_closed
  O      MG_TC_magazine_n_start_active
  IFT
    L      MG_clock_1_s
    =      O_lamp_TC_pocket_loading
  ENDI

  L      I_key_TC_mag_load_unclamp_foot_switch
  =      O_TC_magazine_load_position_unlock
  EMT

  LN     I_TC_magazine_guard_locked_closed
*  ON     MG_servo_drive_release_2nd_axes_group
  EMT

  L      MG_key_TC_magazine_n_select
  AN     ML_key_pressed
  IFT
    INC    WG_soft_key_TC_mag_n_select

    L      WG_soft_key_TC_mag_n_select
    >=     KG_TC_magazine_max_count
    IFT
      L      KG_TC_magazine_1_index
      =      WG_soft_key_TC_mag_n_select
    ENDI
  ENDI

  L      WG_soft_key_TC_mag_n_select
  <>     WL_soft_key_TC_mag_n_selection
  IFT
    S      MG_standby_tool_status_reading

    L      WG_soft_key_TC_mag_n_select
    =      WL_soft_key_TC_mag_n_selection
    =      BG_TC_mag_index_Tool_Call
    INCW
    =      NP_W268_tool_magazine_number
  ENDI

  L      MG_key_TC_magazine_n_select
  =      ML_key_pressed

  L      MG_key_TC_mag_turn_cw
  O      MG_SK_TC_mag_right_cw
  A      MG_TC_standby_position
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_O_TC_magazine_ccw
  CMT    TC_mag_cw_turn

  L      MG_key_TC_mag_turn_ccw
  O      MG_SK_TC_mag_left_ccw
  A      MG_TC_standby_position
  AN     STG_TC_mag[BG_TC_mag_index_Tool_Call].MG_O_TC_magazine_cw
  CMT    TC_mag_ccw_turn

  L      MG_SK_TC_mag_load_active
  A      MG_pocket_table_selected
  =      MG_SK_enable_T_load_unload
  IFT
    L      MG_SK_T_load_unload
    AN     ML_softkey_T_load_has_been_on
    S      MG_T_load_unload

    L      MG_SK_T_load_unload
    =      ML_softkey_T_load_has_been_on

    L      MG_T_load_unload
    CMT    TC_mag_load_unload
  ENDI

EM

;----------------------------
LBL TC_mag_load_unload

  RPLY   BL_identifier
  <>     K+0
  EMT

  SUBM   Tab_active_line
  =      BL_identifier
  ==     K+0
  S      PN_error_submit_spawn_queue_full

EM

;----------------------------
LBL Tab_active_line

  PS     KL_tab_editor_active_line
  CM     9035
  PL     WL_tab_editor_active_line
  M_display_module_error

*$WS vereinfachen Line in Magazin über Modul
  L      WL_tab_editor_active_line
  <=     STG_TC_mag[KG_TC_magazine_1_index].WG_MP7261_TC_magazine_max_pocket
  A[
    L      WL_tab_editor_active_line
    >      KL_line_0
  ]
  IFT
    L      WL_tab_editor_active_line
    +      STG_TC_mag[KG_TC_magazine_1_index].WG_MP7261_TC_magazine_max_pocket
    -      DG_TC_mag_1_offset_load_position
    -      K+1
    MOD    STG_TC_mag[KG_TC_magazine_1_index].WG_MP7261_TC_magazine_max_pocket
    +      K+1
    =      STG_TC_mag[KG_TC_magazine_1_index].WG_TC_mag_load_nominal_pocket

    LN     NP_M4203_error_module_9xxx
    S      STG_TC_mag[KG_TC_magazine_1_index].MG_WZ_mag_load_unload_start
  ELSE
    L      WL_tab_editor_active_line
    <=[
      L      STG_TC_mag[KG_TC_magazine_1_index].WG_MP7261_TC_magazine_max_pocket
      +      STG_TC_mag[KG_TC_magazine_2_index].WG_MP7261_TC_magazine_max_pocket
    ]
    IFT
      L      WL_tab_editor_active_line
      -      STG_TC_mag[KG_TC_magazine_1_index].WG_MP7261_TC_magazine_max_pocket
      +      STG_TC_mag[KG_TC_magazine_2_index].WG_MP7261_TC_magazine_max_pocket
      -      DG_TC_mag_2_offset_load_position
      -      K+1
      MOD    STG_TC_mag[KG_TC_magazine_2_index].WG_MP7261_TC_magazine_max_pocket
      +      K+1
      =      STG_TC_mag[KG_TC_magazine_2_index].WG_TC_mag_load_nominal_pocket

      LN     NP_M4203_error_module_9xxx
      S      STG_TC_mag[KG_TC_magazine_2_index].MG_WZ_mag_load_unload_start
    ELSE
      L      MG_marker_one
      R      MG_SK_T_load_unload
    ENDI
  ENDI

  L      MG_marker_one
  R      MG_T_load_unload

EM

;----------------------------
LBL Tool_no_in_pocket                  ;20120613
;----------------------------

  RPLY   BL_identifier
  <>     K+0
  EMT

  SUBM   T_in_actual_pocket
  =      BL_identifier
  ==     K+0
  S      PN_error_submit_spawn_queue_full

EM

;----------------------------
LBL T_in_actual_pocket                 ;20120613
;----------------------------

  PS     KL_File_type_pocket_table
;  PS     WG_TC_magazine_actual_pocket
  PS     STG_TC_mag[BG_TC_mag_index_Tool_Call].WG_TC_magazine_actual_pocket
  PS     KL_element_no_of_tool
  CM     9093
  PL     WL_Tool_no_on_actual_pocket
  PL     WL_stack_synchronize
  M_display_module_error(KG_error_module_9093)

EM

;----------------------------
LBL NO_T_Display                       ;20120613
;----------------------------

  LN     O_TC_magazine_1_cw
  AN     O_TC_magazine_1_ccw
  IFT
    L      WL_Tool_no_on_actual_pocket
    <>     WL_TC_magazine_Memory_pocket
    =      ML_pulse_actual_pocket_shift
  ELSE
    L      MG_marker_one
    =      ML_pulse_actual_pocket_shift
  ENDI

  L      WL_Tool_no_on_actual_pocket
  =      WL_TC_magazine_Memory_pocket

  L      ML_pulse_actual_pocket_shift
  IFT
    L      K-1
    =      WL_display_counting
  ENDI

  L      ML_pulse_actual_pocket_shift
  IFT
;    M_start_time(TR_Hex_to_DEC,K+0)
;    L      K&TR_Hex_to_DEC
;    +      K+0
;    PSW
    PS     TR_Hex_to_DEC
    PS     K-1
    CM     9006
  ENDI

  L      ML_pulse_actual_pocket_shift
  S      ML_MAG_display_enable

  L      ML_pulse_actual_pocket_shift
  O      TR_Hex_to_DEC
  IFT
    PS     WL_Tool_no_on_actual_pocket ;Value to be written
    CM     Dec_Hex_BCD
    PLW
;    W=     ML_T_in_MAG_bit0
    W=     ML_T_in_MAG
 ENDI

  L      ML_MAG_display_enable
  AN     TR_Hex_to_DEC
  IFT
    L      ML_clock_pocket_count
    =      ML_data_pocket_count

    L      ML_data_pocket_count
    XO     MG_pulse_data_25_ms
    =      ML_data_pocket_count
  ELSE
    L      MG_marker_zero
    =      ML_data_pocket_count
  ENDI

  L      ML_MAG_display_enable
  AN     TR_Hex_to_DEC
  IFT
    LN     TR_pulse_25_ms
    =      TS_pulse_25_ms
    =      MG_pulse_25_ms

    L      ML_clock_pocket_count
    XO     MG_pulse_25_ms
    =      ML_clock_pocket_count
  ELSE
    L      MG_marker_zero
    =      ML_clock_pocket_count
  ENDI

  L      ML_MAG_display_enable
  AN     TR_Hex_to_DEC
  =      O_MAG_display_enable          ;sent

  L      O_MAG_display_enable
  AN     TR_Hex_to_DEC
  A      ML_clock_pocket_count
  =      O_MAG_display_clock

  L      ML_clock_pocket_count         ;clock up edge
  AN     ML_mark_clock_pocket_count
  =      ML_pulse_clock_counting

  L      ML_clock_pocket_count
  =      ML_mark_clock_pocket_count

  L      ML_data_pocket_count          ;data up edge
  AN     ML_mark_data_pocket_count
  =      ML_pulse_data_counting

  L      ML_data_pocket_count
  =      ML_mark_data_pocket_count

  L      ML_data_pocket_count
  IFT
    LW     ML_T_in_MAG
    BT     WL_display_counting
    =      O_MAG_display_data
  ELSE
    L      MG_marker_zero
    =      O_MAG_display_data
  ENDI

  L      ML_pulse_clock_counting
  A      ML_MAG_display_enable         ;counting depend on clock up edge
  A      O_MAG_display_enable
  IFT
    INC    WL_display_counting
  ENDI

  L      WL_display_counting
  >      K+16
  IFT
    L      K+0
    =      WL_display_counting

    L      MG_marker_one
    R      ML_MAG_display_enable
  ENDI

EM

;----------------------------
LBL Dec_Hex_BCD                        ;20120613
;----------------------------

  PLW
  PSW
  /      K+10
  X      K+16
  +[
    PLW
    MOD    K+10
  ]
  PSW

EM
