%%%
  VERSION:1
  LANGUAGE:ENGLISH
%%%

MODULE TPU_MMI (SYSMODULE)
  !######################################################
  !# PalletWare module TPU_MMI				#
  !# This module holds the MMI communication routines	#
  !# for the teach pendant.				#
  !# 							#
  !######################################################
  CONST string tpu_mmi_rev:="$Revision: 1.3 $";
  
  !######################################################
  !#							#
  !# 		G  L O B A L   D A T A	  		#
  !#							#
  !######################################################
  !Text resource handling
  PERS num PalDlgTextResNo:=0;
    
  !######################################################
  !#							#
  !# 		L O C A L   D A T A	  		#
  !#							#
  !######################################################
  
  !######################################################
  !#							#
  !# 		P U B L I C   R O U T I N E S  		#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	PalInitDlgText
  !	descr:	Initiates dialog text resource file.
  !	arg:	none
  !------------------------------------------------------ 
  PROC PalInitDlgText()
   
    PalDlgTextResNo:=TextTabGet("dialog_texts");
  ERROR
    RAISE;
  ENDPROC
  
  !######################################################
  !#							#
  !# 		  User Request Dialogues 		#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	GetCxtToEdit
  !	descr:	This func communicates with the TPU to
  !		get the context number to edit.
  !		It uses text files to get the text to
  !		display.
  !	arg:	none
  !	ret:	context of type num
  !------------------------------------------------------
  FUNC num GetCxtToEdit()
    
    VAR num context;
    
    TPErase;
    !Select context to edit
    TPWrite TextGet(PalDlgTextResNo,101);
    !Display and read choice 1 to 5
    TPReadFK context,"?","1","2","3","4","5";
    RETURN context;
  ERROR
    RAISE;
  ENDFUNC
  
  !------------------------------------------------------
  !	name:	GetEditChoice
  !	descr:	This func communicates with the TPU to
  !		get the edit option of following:
  !		1 - Empty line and change pallet type
  !		2 - Finish current pallet and change pallet type
  !		3 - Change pallet type immediately
  !		4 - Evacuate current pallet
  !		5 - Continue
  !		It uses text files to get the
  !		text to display.
  !	arg:	context of type num	
  !	ret:	choice of type num
  !------------------------------------------------------
  FUNC num GetEditChoice(num context)
  
    VAR num choice;
    
    TPErase;
    TPWrite TextGet(PalDlgTextResNo,110)+" "+ValToStr(context); 
    !Display options according to procedure head
    TPWrite TextGet(PalDlgTextResNo,111);
    TPWrite TextGet(PalDlgTextResNo,112);
    TPWrite TextGet(PalDlgTextResNo,113);
    TPWrite TextGet(PalDlgTextResNo,114);
    TPWrite TextGet(PalDlgTextResNo,115); 
    !Display and read choice 1 to 5
    TPReadFK choice,"?","1","2","3","4","5";
    RETURN choice;
  ERROR
    RAISE;
  ENDFUNC 
   
  !------------------------------------------------------
  !	name:	ChkEditMoreCxt
  !	descr:	This func communicates with the TPU to
  !		get the choice of what to do after edit
  !		a context. It uses text files to get the
  !		text to display.
  !		The alternatives to return are:
  !		TRUE if choice is 4
  !		FALSE otherwise
  !	arg:	context of type num	
  !	ret:	choice of type bool
  !------------------------------------------------------
  FUNC bool ChkEditMoreCxt()
    
    VAR num choice;
    
    TPErase;
    !Edit more or start
    TPReadFK choice,TextGet(PalDlgTextResNo,130),stEmpty,stEmpty,stEmpty,
      TextGet(PalDlgTextResNo,131),
      TextGet(PalDlgTextResNo,132);
    !Read choice 4 (edit) or 5 (start)
    IF choice=4 THEN 
      RETURN TRUE;
    ELSE
      RETURN FALSE;
    ENDIF
  ERROR
    RAISE;
  ENDFUNC
  
  !######################################################
  !#							#
  !# 		  Error Handling Dialogues 		#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	PalGetErrChoice
  !	descr:	This func display error dialog text on
  !		the TPU. 
  !		It uses a text file to retrieve the text.
  !		The text to display is:
  !		1 - Repeat PPC
  !		2 - Take next PPC
  !		3 - Take next pallet
  !		4 - Attend after current PPC
  !		5 - Continue
  !	arg:	none
  !	ret:	answer of type num	
  !------------------------------------------------------
  FUNC num PalGetErrChoice()
    
    VAR num answer;
    
    TPErase;
    !Display text according to procedure head
    TPWrite TextGet(PalDlgTextResNo,201);
    TPWrite TextGet(PalDlgTextResNo,202);
    TPWrite TextGet(PalDlgTextResNo,203);
    TPWrite TextGet(PalDlgTextResNo,204);
    TPWrite TextGet(PalDlgTextResNo,205);
    !Display and read choice 1 to 5
    TPReadFK answer,"?","1","2","3","4","5";
    RETURN answer;
  ERROR
    RAISE;
  ENDFUNC
  
  !------------------------------------------------------
  !	name:	PalGetAckChoice
  !	descr:	This func display ack dialog text on
  !		the TPU. 
  !		It uses a text file to retrieve the text.
  !		The text to display is:
  !		Select choice when ready
  !	arg:	none
  !	ret:	choice of type num	
  !------------------------------------------------------
  FUNC num PalGetAckChoice()
  
    VAR num choice;
    
    TPErase;
    !Stop or continue
    TPReadFK choice,TextGet(PalDlgTextResNo,210),stEmpty,stEmpty,stEmpty,
      TextGet(PalDlgTextResNo,211),
      TextGet(PalDlgTextResNo,212);
    RETURN choice;
  ERROR
    RAISE;
  ENDFUNC
    
    
    
    
  !######################################################
  !#							#
  !# 		Change Pallet Cycle Dialogues 		#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	GetNewPalCycNo
  !	descr:	This function displays information on the
  !		TPU with alternatives as follows.
  !		First info of active pallet cycle.
  !		Then a question if user want to change.
  !		If answer is YES a list of all pallet
  !		cycles is presented and the user can 
  !		select a number.	 
  !	arg:	context of type num.
  !		current pallet cycle number of type num.
  !	ret:	new pallet cycle number of type num	
  !------------------------------------------------------
  FUNC num GetNewPalCycNo(
    num context,
    num palcyc_no)
  
    VAR num n;
    VAR num choice;
    
    TPErase;
    IF palcyc_no=0 THEN
      TPWrite TextGet(PalDlgTextResNo,120)+" "+ValToStr(context);
    ELSE
    TPWrite TextGet(PalDlgTextResNo,121)+" "+ValToStr(context);
    TPWrite TextGet(PalDlgTextResNo,122)+" "+ValToStr(palcyc_no)+" - "+PalCyc_names{palcyc_no};
    ENDIF
    TPReadFK n,TextGet(PalDlgTextResNo,123),stEmpty,stEmpty,stEmpty,
      TextGet(PalDlgTextResNo,124),
      TextGet(PalDlgTextResNo,125);
    IF n=5 RETURN palcyc_no;
    choice:=0;
    WHILE choice=0 DO
      FOR i FROM 1 TO (NBPALCYC-1) DIV 5+1 DO
        IF choice=0 THEN 
          TPErase;
          n:=5;
          IF 5*i>NBPALCYC n:=NBPALCYC-5*(i-1);
          FOR j FROM 1 TO n DO
            TPWrite ValToStr(j+5*(i-1))+" - "+PalCyc_names{j+5*(i-1)};
          ENDFOR
          TPWrite " ";
          TPReadNum choice,TextGet(PalDlgTextResNo,126)+" "+TextGet(PalDlgTextResNo,127);
          IF choice=-1 THEN
            palcyc_no:=0;
          ELSEIF choice>0 THEN
            palcyc_no:=choice;
            PalcycleDefault context\name:=PalCyc_names{palcyc_no}
              \palStation:=CfgCxtToPalStat{context}\feeder:=CfgCxtToFeeder{context};
          ENDIF
        ENDIF
      ENDFOR
    ENDWHILE
    RETURN palcyc_no;
  ERROR
    RAISE;
  ENDFUNC
  
  !######################################################
  !#							#
  !# 		  	Misc Dialogues 			#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	ChkGripToolEmpty
  !	descr:	This proc display ack dialog text on
  !		the TPU. 
  !		It uses a text file to retrieve the text.
  !		The text to display is:
  !		Check that gripper is empty
  !	arg:	none	
  !------------------------------------------------------
  PROC ChkGripToolEmpty()
    
    VAR num ack;
    
    TPErase;
    !Wait for operator ack that gripper is empty
    TPReadFK ack,TextGet(PalDlgTextResNo,220),stEmpty,stEmpty,stEmpty,
      stEmpty,TextGet(PalDlgTextResNo,221);
  ERROR
    RAISE;
  ENDPROC
  		       
ENDMODULE
