*-----------------------------------------------------------------------------
*  OEM specifc functions
*-----------------------------------------------------------------------------
*  Module Description
*    - M function M22..29 and M900..999 reserved for OEMs
*    - Error 150..199, 800..999 reserved for OEMs (ERROR.A, ERR_TAB.PET)
*    - Error 800..999 can be activated by option Error_messages_additional
*      in config.def
*
*-----------------------------------------------------------------------------

#define /s    KL_runtime_from_MP                       K -1
#define /s    ML_TC_tool_lock_open                     M
#define /s    ML_TC_tool_lock_close                    M
#define /s    ML_chiller_fault                         M     ;20120803

GLOBAL OEM_function_begin
GLOBAL OEM_function_end

#include Mac_Lib.inc

;----------------------------
LBL OEM_function_begin
;----------------------------

*  LN     I_ref_end_posit_X                        I4
*  =      I_not_ref_end_posit_X                    M

  CM     Input_switchover
  CM     Other_messages
  CM     TC_tool_lock
  CM     TC_tool_lock_monitoring

EM

;----------------------------
LBL OEM_function_end
;----------------------------

*  LN     O_unclamping_4                           M
*  =      O_clamping_4                             O14

  L      O221
  =      O5

  L      O_servo_drive_release_S1
  =      O_spindle_air_seal

#if CFG_magazine_tool_lock = "active"
  L      MG_marker_one
  S      MG_pocket_up_always_after_TC
#endif

EM

;----------------------------
LBL Input_switchover
;----------------------------

#if &I_lubricant_level_low < 1000
  L      I_lubricant_level_low
  =N     I_lubricant_level_ok
#endif

#if &I_S1_tool_clamped_with_tool < 1000
  LN     I_S1_tool_clamped_with_tool
  A      I_S1_tool_clamped
  =      I_S1_tool_clamped_wo_tool
#endif

  L      I_S1_tool_clamped_wo_tool
  =N     I_S1_tool_located_in_spindle

  L      I_circuit_breaker_tripped
  =N     I_circuit_breaker_ok

  L      I_TC_mag_counter
  =      I_TC_mag_1_counter
  =      I_TC_mag_1_exact_position

#if CFG_Tool_magazine_system = "Mag_type_04"
#if &I_TC_arm_middle_pos > 1000
  L      I_TC_arm_middle_pos_inverse
  =N     I_TC_arm_middle_pos
#endif
#if &I_TC_arm_basic_pos > 1000
  L      I_TC_arm_basic_pos_inverse
  =N     I_TC_arm_basic_pos
#endif
#if &I_TC_arm_spindle_pos > 1000
  L      I_TC_arm_spindle_pos_inverse
  =N     I_TC_arm_spindle_pos
#endif
#else
#if &I_TC_arm_in_pos > 1000
  L      I_TC_arm_in_pos_inverse
  =N     I_TC_arm_in_pos
#endif
#if &I_TC_arm_clamp_pos > 1000
  L      I_TC_arm_clamp_pos_inverse
  =N     I_TC_arm_clamp_pos
#endif
#if &I_TC_arm_unclamp_pos > 1000
  L      I_TC_arm_unclamp_pos_inverse
  =N     I_TC_arm_unclamp_pos
#endif
#endif
#if &I_TC_arm_brake_pos > 1000
  L      I_TC_arm_brake_pos_inverse
  =N     I_TC_arm_brake_pos
#endif

EM

;----------------------------
LBL Other_messages
;----------------------------

  LN     MG_spindle_chiller_inactive
  IFT
;    L      I_emergency_button_unlocked
    L      I_ESA_control_operational
;  AN     MG_spindle_chiller_inactive
    =      O_S_chiller_on

    L      I_CBT_chiller_unit          ;20120803
    =      TS_chiller_dalay

    LN     TS_chiller_dalay            ;20120803
    AN     TR_chiller_dalay
    =      ML_chiller_fault

;  LN     I_CBT_chiller_unit            ;20120803
;  A      O_servo_drive_release_S1      ;20120803
    L      O_S_chiller_on
    A      ML_chiller_fault
    A      MG_power_delayed_on
    S      PN_error_chiller_unit
;  R      PN_M4005_S_M03_nominal_and_status       ;20120803
;  R      PN_M4006_S_M04_nominal_and_status       ;20120803
;  S      PN_M4007_S_M05_nominal_0_and_status     ;20120803
  ENDI

  L      O_servo_drive_release_S1
  AN     O_S1_tool_unclamping
  AN     MG_TC_automatic
  AN     MG_TC_help
  A      I_S1_tool_clamped_wo_tool
  S      PN_error_S_without_tool

  L      I_cabinet_door_open
  S      PN_error_cabinet_opened

;  L      NP_M4176_control_in_operation_on_or_flashes
;  AN     NP_M4175_control_in_operation_flashes
;  AN     I_guard_1_closed_locked
;  AN     MG_guard_inactive
;  A      MG_power_delayed_on
;  S      PN_error_guard_error

  L      MG_spindle_chiller_inactive
  O      MG_pneumatic_source_inactive
  =      PN_error_test_mode_on

EM

;----------------------------
LBL TC_tool_lock
;----------------------------

#if CFG_magazine_tool_lock = "inactive"
  L      MG_M77_TC_tool_lock_open
  O      MG_M78_TC_tool_lock_close
  S      PN_M4092_quit_M_function
#else
  L      MG_TC_automatic
  O      MG_TC_help
  IFT
    L      MG_M77_TC_tool_lock_open
    O      MG_M78_TC_tool_lock_close
    S      PN_M4092_quit_M_function

    L      MG_marker_one
    R      MG_tool_lock_open_by_M_code
    R      MG_tool_lock_close_by_M_code
  ELSE
    L      MG_M77_TC_tool_lock_open
    S      MG_tool_lock_open_by_M_code
    R      MG_tool_lock_close_by_M_code
    S      O_tool_lock_open
    R      O_tool_lock_close

    L      MG_M77_TC_tool_lock_open
    A      I_tool_lock_open
    AN     I_tool_lock_close
    S      PN_M4092_quit_M_function

    L      MG_M78_TC_tool_lock_close
    S      MG_tool_lock_close_by_M_code
    R      MG_tool_lock_open_by_M_code
    R      O_tool_lock_open
    S      O_tool_lock_close

    L      MG_M78_TC_tool_lock_close
    AN     I_tool_lock_open
    A      I_tool_lock_close
    S      PN_M4092_quit_M_function
    R      MG_tool_lock_close_by_M_code
  ENDI
#endif
EM

;----------------------------
LBL TC_tool_lock_monitoring
;----------------------------
#if CFG_magazine_tool_lock = "active"
   L      O_tool_lock_open
   AN     ML_TC_tool_lock_open
   IFT
     PS     K&TR_TC_tool_lock_open
     PS     KL_runtime_from_MP
     CM     9006

     L      MG_marker_one
     S      ML_TC_tool_lock_open
   ENDI

   L      O_tool_lock_close
   AN     ML_TC_tool_lock_close
   IFT
     PS     K&TR_TC_tool_lock_close
     PS     KL_runtime_from_MP
     CM     9006

     L      MG_marker_one
     S      ML_TC_tool_lock_close
   ENDI

   LN     O_tool_lock_open
   R      ML_TC_tool_lock_open

   LN     O_tool_lock_close
   R      ML_TC_tool_lock_close

   LN     O_tool_lock_open
   AN     O_tool_lock_close
   AN     I_tool_lock_open
   AN     I_tool_lock_close
   O[
     L    O_tool_lock_open
     AN   I_tool_lock_open
     A    ML_TC_tool_lock_open
     AN   TR_TC_tool_lock_open
   ]
   O[
     L    O_tool_lock_close
     AN   I_tool_lock_close
     A    ML_TC_tool_lock_close
     AN   TR_TC_tool_lock_close
   ]
   O[
     L    I_tool_lock_open
     A    I_tool_lock_close
   ]
   S      PN_error_tool_lock_error
#endif
EM

