*-----------------------------------------------------------------------------
*  Read MP data - table MP_Data.tab
*  Read machine configuration - MP_config.tab
*-----------------------------------------------------------------------------
*  Machine parameter values (previously MP4210.x ..) for PLC access are saved
*  in the file PLC:\MP\MP_Data.tab.
*  Besides the real value (column: Value) also the unit will be evaluated
*  (column: Unit).
*
*  Machine configurations (previously MP4310.x) for PLC access are saved
*  PLC:\MP\MP_Config.tab.
*
*  There are two arrays DG_MP_data and MG_MP_config in (GLB_TCMB.def), which
*  defines the numbers of values are read.
*
*  An identification in the last line serves a check of the tables .
*
*  Table editor can only be displayed in the machine mode.
*  For this case a key simulation will be done by request (key change screen)
*
*
*-----------------------------------------------------------------------------

#define /s    KL_offset_title_pop_up_window            K +30
#define /s    KL_file_access_read_locked               K %010
#define /s    KL_table_test                            K +807667123
#define /s    KL_edit_mode_in_background               K +2
#define /s    KL_machine_mode_in_background            K +3
#define /s    KL_MP_edit_active                        K +3
#define /s    KL_status_current_SK_menu                K -1
#define /s    KL_SK_vertical                           K +1
#define /s    KL_mode_machine_edit                     K +2
#define /s    KL_number_current_SK_menu                K +0
#define /s    KL_reserved                              K +0
#define /s    KL_active                                K +1
#define /s    KL_inactive                              K -1
#define /s    KL_line_1                                K +1
#define /s    KL_1_line                                K +1
#define /s    KL_2_lines                               K +2
#define /s    KL_D_address_divisible_by_4              K +4
#define /s    KL_entire_file                           K -1
#define /s    KL_edit_original_file                    K %010
#define /s    KL_reject_changes                        K +0
#define /s    KL_save_changes                          K +1
#define /s    KL_soft_key_MP_data_not_pressed          K +0
#define /s    KL_incorrect_operating_mode              K +8
#define /s    KL_identifier_soft_key_range             K -10000
#define /s    KL_number_soft_key_sub_menu              K +2
#define /s    KL_maximale_number_of_soft_keys          K +18
#define /s    KL_modus_soft_keys_yes_no                K +2
#define /s    KL_soft_key_yes                          K +2
#define /s    KL_soft_key_no                           K +3

#define /s    ML_MP_editor_active                      M
#define /s    ML_MP_editor_has_been_active             M
#define /s    ML_soft_keys_MP_edit_on                  M
#define /s    ML_soft_keys_MP_edit_off                 M
#define /s    ML_soft_keys_MP_data_save                M
#define /s    ML_soft_keys_MP_data_reject              M
#define /s    ML_MP_data_save_active                   M
#define /s    ML_MP_data_reject_active                 M
#define /s    ML_submit_started                        M

#define /s    BL_identifier                            B
#define /s    BL_soft_key_index                        B
#define /s    BL_stack_synchronize                     B
#define /s    BL_staus_SUBM                            B
#define /s    BL_soft_key_code                         B
#define /s    BL_error_code                            B

#define /s    WL_soft_key_range                        W[18]

#define /s    WL_table_end                             W
#define /s    WL_index_MP_data                         W
#define /s    WL_index_axis_data                       W
#define /s    WL_index_config_data                     W
#define /s    WL_soft_key_MP_data                      W
#define /s    WL_soft_key_MP_data_old                  W

#define /s    DL_file_handle                           D
#define /s    DL_MP_unit                               D
#define /s    DL_recources_handle                      D
#define /s    DL_status_information                    D
#define /s    DL_spawn_pop_up_window                   D

#include Mac_Lib.inc

GLOBAL MP_data_tab_reading
GLOBAL MP_config_tab_reading
GLOBAL MP_table_edit

;----------------------------
LBL MP_table_edit
;----------------------------

  L      NP_M4173_1st_PLC_run_after_compile
  CMT    Keys_unlock

  PS     KL_edit_mode_in_background
  CM     9035
  PLW
  ==     KL_MP_edit_active
  =      ML_MP_editor_active

  L      NP_D276_code_number_entered_via_MOD
  ==     K+123
  R      ML_MP_editor_active

  L      ML_MP_editor_active
  AN     ML_MP_editor_has_been_active
  S      ML_soft_keys_MP_edit_on

  LN     ML_MP_editor_active
  A      ML_MP_editor_has_been_active
  S      ML_soft_keys_MP_edit_off

  L      ML_MP_editor_active
  =      ML_MP_editor_has_been_active

  L      MG_SK_save_MP_data
  AN     ML_MP_data_save_active
  S      ML_soft_keys_MP_data_save

  L      MG_SK_save_MP_data
  =      ML_MP_data_save_active

  L      MG_SK_reject_MP_data
  AN     ML_MP_data_reject_active
  S      ML_soft_keys_MP_data_reject

  L      MG_SK_reject_MP_data
  =      ML_MP_data_reject_active

  L      WG_soft_key_MP_data
  <>     WL_soft_key_MP_data_old
  IFT
    L      WG_soft_key_MP_data
    <      KL_soft_key_MP_data_not_pressed
    IFT
      L      WL_soft_key_MP_data_old
      =      WL_soft_key_MP_data
    ENDI
  ENDI

  L      WG_soft_key_MP_data
  =      WL_soft_key_MP_data_old

  L      WL_soft_key_MP_data
  <=     KL_soft_key_MP_data_not_pressed
  AN     ML_soft_keys_MP_data_save
  AN     ML_soft_keys_MP_data_reject
  AN     ML_soft_keys_MP_edit_on
  AN     ML_soft_keys_MP_edit_off
  A[
    LN     NP_M4159_key_END_during_tab_editor_pressed
    ON     ML_MP_editor_active
  ]
  EMT

  RPLY   BL_identifier
  <>     K+0
  EMT

  SUBM   Soft_key_MP_on_off
  =      BL_identifier
  ==     K+0
  S      PN_error_submit_spawn_queue_full

EM

;----------------------------
LBL Soft_key_MP_on_off

  L      ML_soft_keys_MP_edit_on
  R      ML_soft_keys_MP_edit_on
  CMT    Soft_key_MP_on

  L      ML_soft_keys_MP_edit_off
  R      ML_soft_keys_MP_edit_off
  CMT    Soft_key_MP_off

*$WS vor speichern testen ob Daten zulässig sind
  L      ML_soft_keys_MP_data_save
  R      ML_soft_keys_MP_data_save
  IFT
    CM     Table_save

    L      KL_soft_key_MP_data_not_pressed
    =      WL_soft_key_MP_data
  ENDI

  L      ML_soft_keys_MP_data_reject
  R      ML_soft_keys_MP_data_reject
  IFT
    CM     Table_reject

    L      KL_soft_key_MP_data_not_pressed
    =      WL_soft_key_MP_data
  ENDI

  L      NP_M4159_key_END_during_tab_editor_pressed
  R      NP_M4159_key_END_during_tab_editor_pressed
  IFT
    CM     Save_yes_no
  ENDI

  L      WL_soft_key_MP_data
  >=     SK_MP_config_total
  A[
    L      WL_soft_key_MP_data
    <=     SK_MP_data_user_M           ;*DC
  ]
  CMT    Table_edit

  LN     MG_commissioning
  EMT
  CM     9019
  PL     BG_bytes_on_stack
  M_display_module_error

  L      BG_bytes_on_stack
  >      K+0
  S      PN_error_stack_not_empty

EM

;----------------------------
LBL Key_screen_change

  PS     KG_key_screen_change
  CM     9180
  PL     BL_stack_synchronize

  M_display_module_error

EM

;----------------------------
LBL Keys_unlock

  PS     KG_key_edit_mode
  CM     9182
  PL     BL_error_code
  M_display_module_error

  PS     KG_key_test_mode
  CM     9182
  PL     BL_error_code
  M_display_module_error

  PS     KG_key_screen_change
  CM     9182
  PL     BL_error_code
  M_display_module_error

EM

;----------------------------
LBL Keys_lock

  PS     KG_key_edit_mode
  CM     9181
  PL     BL_error_code
  M_display_module_error

  PS     KG_key_test_mode
  CM     9181
  PL     BL_error_code
  M_display_module_error

  PS     KG_key_screen_change
  CM     9181
  PL     BL_error_code
  M_display_module_error

EM

;----------------------------
LBL Table_edit

  L      WL_soft_key_MP_data
  ==     SK_MP_config_total
  IFT
    PS     S">OEM.MP_CONFIG_TAB"
*   PS     S"PLC:\MP\MP_Config.TAB"
    PS     S"Comment on/off Default"
    PS     KL_line_1
    PS     K +&MG_table_check - &MG_MP_config - &KL_1_line
  ELSE
    PS     S">OEM.MP_DATA_TAB"
*   PS     S"PLC:\MP\MP_Data.TAB"
    PS     S"Comment Value Default"

    L      WL_soft_key_MP_data
    <=     SK_MP_data_total
    IFT
      PS     KL_line_1
      L      K +&DG_table_test - &DG_MP_data
      /      KL_D_address_divisible_by_4
      -      KL_1_line
      PSW
    ELSE
      L      WL_soft_key_MP_data
      -      SK_MP_data_total
      /[
        L      KL_number_soft_key_sub_menu
        +      K+1
      ]
      =X
      DECX
      PS     WL_soft_key_range[X]
      INCX
      L      WL_soft_key_range[X]
      -      KL_2_lines
      PSW
    ENDI
  ENDI

  PS     KL_edit_original_file
  CM     9250                          ;Start table editor

  L      NP_W1022_module_error_status
  ==     KL_incorrect_operating_mode
  A      NP_M4203_error_module_9xxx
  =      PN_error_table_edit_active
  IFT
    CM     Display_MP_soft_key

    L      KL_soft_key_MP_data_not_pressed
    =      WL_soft_key_MP_data
EM
  ELSE
  M_display_module_error
  ENDI

  L      KL_soft_key_MP_data_not_pressed
  =      WL_soft_key_MP_data

  L      MG_edit_mode
  CMT    Key_screen_change

  CM     Keys_lock

EM

;----------------------------
LBL Save_yes_no

  SPAWN  Message_window
  =      DL_spawn_pop_up_window
  ==     K-1
  S      PN_error_submit_spawn_queue_full

EM

;----------------------------
LBL Message_window

  L      KL_offset_title_pop_up_window
  =X

  L      S#D0[X]
  =      SG_title_pop_up_menu

  PS     K +&SG_title_pop_up_menu

  PS     S"MP_Edit.a"

  PS     KL_modus_soft_keys_yes_no
  CM     9217
  PL     BL_soft_key_code
  M_display_module_error

  L      BL_soft_key_code
  ==     KL_soft_key_yes
  CMT    Table_save

  L      BL_soft_key_code
  ==     KL_soft_key_no
  CMT    Table_reject

  CM     Display_MP_soft_key

EM

;----------------------------
LBL Table_reject

  PS     KL_reject_changes
  CM     9251
  M_display_module_error

  CM     Keys_unlock

  LN     MG_edit_mode
  CMT    Key_screen_change

EM

;----------------------------
LBL Table_save

  PS     KL_save_changes
  CM     9251
  M_display_module_error

  LN     NP_M4203_error_module_9xxx
  R      MG_machine_parameter_are_read

  CM     Keys_unlock

  LN     MG_edit_mode
  CMT    Key_screen_change

EM

;----------------------------
LBL Soft_key_MP_on

;Detect current soft key menu

  PS     DL_recources_handle
  PS     S""
  PS     KL_status_current_SK_menu
  PS     KL_SK_vertical
  PS     KL_mode_machine_edit
  CM     9203                          ;Activate soft key menu
  PL     DL_recources_handle
    M_display_module_error

  PS     DL_recources_handle
  PS     KL_number_current_SK_menu
  PS     KL_reserved
  CM     9208                          ;Status information PLC soft keys
  PL     DL_status_information
    M_display_module_error

LBL Display_MP_soft_key

;Display soft key menu MP edit

  PS     DL_recources_handle
  PS     S""
  PS     MP_Edit
  PS     KL_SK_vertical
  PS     KL_mode_machine_edit
  CM     9203                          ;Activate soft key menu
  PL     DL_recources_handle
    M_display_module_error

  PS     DL_recources_handle           ;Refresh soft key row
  CM     9204
    M_display_module_error

EM

;----------------------------
LBL Soft_key_MP_off

;Display previous current soft key menu

  PS     DL_recources_handle
  PS     S""
  PS     DL_status_information
  PS     KL_SK_vertical
  PS     KL_mode_machine_edit
  CM     9203                          ;Activate soft key menu
  PL     DL_recources_handle
    M_display_module_error

  PS     DL_recources_handle           ;Refresh soft key row
  CM     9204
    M_display_module_error

EM

;----------------------------
LBL MP_data_tab_reading
;----------------------------

  PS     KL_file_access_read_locked
* PS     S"PLC:\MP\MP_Data.TAB"
  PS     S">OEM.MP_DATA_TAB"
  CM     9240                          ;File open
  PL     DL_file_handle
  M_module_error_display(MG_win_file_open)

  L      K-1
  =      WL_index_MP_data

  L      K+0
  =      BL_soft_key_index

  REPEAT
    INC    WL_index_MP_data

    PS     DL_file_handle
    PS     WL_index_MP_data
    PS     S"Unit"
    CM     9255                        ;Table read
    PL     DL_MP_unit
    M_modul_error_table(WL_index_MP_data,PLC:\MP\MP_Data_xxx.TAB)

    L      NP_M4203_error_module_9xxx
    S      PN_error_check_MP_data

    LN     PN_error_check_MP_data
    IFT
      PS     DL_file_handle
      PS     WL_index_MP_data
      PS     S"Value"
      CM     9255                      ;Table read
      PLW
      /      DL_MP_unit
      =      DG_MP_data[WL_index_MP_data]
*$WS S">OEM.name" not yet supported by module 9084
*    M_module_error_table(WL_index_MP_data,>OEM.MP_DATA_TAB)
      M_modul_error_table(WL_index_MP_data,PLC:\MP\MP_Data_xxx.TAB)

      L      NP_M4203_error_module_9xxx
      S      PN_error_check_MP_data
    ENDI

    L      DL_MP_unit
    ==     KL_identifier_soft_key_range
    AN     PN_error_check_MP_data
    IFT
      L      WL_index_MP_data
      =      WL_soft_key_range[BL_soft_key_index]
      =      WL_table_end

      INC    BL_soft_key_index

      L      BL_soft_key_index
      >      KL_maximale_number_of_soft_keys
      S      PN_error_check_MP_data
    ENDI

    L      WL_index_MP_data
    >=[
      L      K &DG_table_test - &DG_MP_data
      /      K+4
    ]
    O      PN_error_check_MP_data
  UNTILT

  L      K-1
  =      WL_index_axis_data

  REPEAT
    INC    WL_index_axis_data

    L      DG_TC_pos_window_X[WL_index_axis_data]
    <      DG_MP1030_pos_window_X[WL_index_axis_data]
    IFT
      L      DG_MP1030_pos_window_X[WL_index_axis_data]
      =      DG_TC_pos_window_X[WL_index_axis_data]
    ENDI

    L      WL_index_axis_data
    >=     KG_axis_9
  UNTILT

  L      DG_table_test
  <>     KL_table_test
  O      NP_M4203_error_module_9xxx
  S      PN_error_check_MP_data

EM

;----------------------------
LBL MP_config_tab_reading
;----------------------------

  PS     KL_file_access_read_locked
  PS     S">OEM.MP_CONFIG_TAB"
*  PS     S"PLC:\MP\MP_CONFIG.TAB"
  CM     9240                          ;File open
  PL     DL_file_handle
  M_module_error_display(MG_win_file_open)

  L      K-1
  =      WL_index_config_data

  REPEAT
    INC    WL_index_config_data

    PS     DL_file_handle
    PS     WL_index_config_data
    PS     S"on/off"
    CM     9255                        ;Table read
    PLW
    /      K+10000
    ==     KL_active
    =      MG_MP_config[WL_index_config_data]
*$WS S">OEM.name" not yet supported by module 9084
*   M_module_error_table(WL_index_config_data,>OEM.MP_CONFIG_TAB)
    M_modul_error_table(WL_index_config_data,PLC:\MP\MP_CONFIG.TAB)

    L      NP_M4203_error_module_9xxx
    S      PN_error_MP_config_table_check

    L      WL_index_config_data
    >=     K &MG_table_check - &MG_MP_config
    O      PN_error_MP_config_table_check
  UNTILT

  LN     PN_error_MP_config_table_check
  IFT
    PS     DL_file_handle
    PS     WL_index_config_data
    PS     S"on/off"
    CM     9255                        ;Table read
    PLW
    =      DG_table_test
    M_module_error_display(MG_win_read_table)
  ENDI

  L      DG_table_test
  <>     KL_table_test
  O      NP_M4203_error_module_9xxx
  S      PN_error_MP_config_table_check

  L      MG_power_fail_inactive
  =      MG_power_fail_inactive_remanent

EM

