esrf

Beamline Instrument Software Support
SPEC Macro documentation: [ Macro Index | BCU Home ]

#%NAME% PCAPCSPEC.MAC 
#
#%DESCRIPTION%
# This file contains all macros for communication between PC and SPEC
#
# Last Modifications : %BR% 
#
#%BR% 18/11/98 (MP) Fix error: user_precount() -> user_prepcount()
#%BR% 09/11/98 (MP) Suppress erasing screen when not in interactive mode
#%BR% 08/13/98 (Denis Gautherot) Initial Revision
#
#%UU%
#%MDESC%
#  This macro do all the configuration
#
def pcasetup '{

  # ****           Differents variables for setup and all the application          ****
  # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  global PcaScreenX
  global PcaScreenY
  global PcaDeviceServer

  # ****   PcaTabCounters contains all mnemonic counters : Ypos, Zpos, Intensity   ****
  # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  global PcaNbCounters
  global PcaTabCounters
  global PcaCounterFlag

  # **** PcaTabMotorsMne contains all the motors mnemonic between 0 to PcaNbMotors ****
  # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  global PcaNbMotors
  global PcaTabMotorsMne2Ind
  global PcaTabMotorsInd2Mne

  # ****                             Locals Variables                              ****
  # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  local Pca_aNewMotor
  local Pca_aNewCounter
  local Pca_TabParam
  local Pca_NbParameters
  local Pca_DSOk
  local Pca_AllParam
  local Pca_indice
  local Pca_Str
    
	# ****               Init globals variables                 ****
	# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
	PcaScreenX			= 79
	PcaScreenY 			= 24
	PcaDeviceServer			= ""

	PcaNbMotors			= 0
	PcaTabMotorsMne2Ind[0]		= 0
	PcaTabMotorsInd2Mne[0]		= -1
	PcaMotorInd2Move		= -1

	PcaNbCounters 			= 0
	PcaTabCounters[0] 		= ""
	PcaCounterFlag[0] 		= 0

	# ****          Check if paramters in command line          ****
	# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
	Pca_NbParameters 	= $#
	Pca_TabParam[0] 	= ""
	Pca_AllParam 		= "$*"
	Pca_NextParamInd	= 0
	
	if ((Pca_NbParameters > 0) && (Pca_NbParameters < 14)) {
		# ****				Begin of Mode 1		    ****
		# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
	
		if (split(Pca_AllParam, Pca_TabParam) >= 10) {
			# ****                Get PcaDeviceServer                   ****
			# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
			if (Pca_TabParam[Pca_NextParamInd] != "") {
				PcaDeviceServer = Pca_TabParam[Pca_NextParamInd]
				
			} else {
				printf ("ERROR : Bad parameter %d \n", Pca_NextParamInd + 1)
				exit
			}
			Pca_NextParamInd++

			# ****                Get all counters                      ****
			# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
			for (Pca_indice = 0; Pca_indice < 3; Pca_indice++) {
				if (Pca_TabParam[Pca_NextParamInd] == 1) {
					Pca_NextParamInd++
					if (Pca_TabParam[Pca_NextParamInd] != "") {
						PcaTabCounters[PcaNbCounters] = cnt_num(Pca_TabParam[Pca_NextParamInd])
						PcaCounterFlag[Pca_indice] = 1
						PcaNbCounters++
					} else {
						printf ("ERROR : Bad parameter %d \n", Pca_NextParamInd + 1)
						exit
					}
				} else 	if (Pca_TabParam[Pca_NextParamInd] != 0) {
					printf ("ERROR : Bad parameter %d \n", Pca_NextParamInd + 1)
					exit
				}
				Pca_NextParamInd++
			}
			
			# ****                Get all 6 motors                      ****
			# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
			for (Pca_indice = 0; Pca_indice < 6; Pca_indice++) {
				if (Pca_TabParam[Pca_NextParamInd] != "") {
					PcaTabMotorsInd2Mne[PcaNbMotors] = motor_num(Pca_TabParam[Pca_NextParamInd])
					PcaTabMotorsMne2Ind[motor_num(Pca_TabParam[Pca_NextParamInd])] = 0
					PcaNbMotors ++
				} else {
					printf ("ERROR : Bad parameter %d \n", Pca_NextParamInd + 1)
					exit
				}
				Pca_NextParamInd++
			}
		} else {
			# Not enougth parameters..........
			printf ("USAGE : pcasetup <param1> ... <param13>\n")
			# A completer plus tard.....
			exit
		}
		# End of Mode 1	
	} else
	if (Pca_NbParameters == 0) {
		# ****				Begin of Mode 2		    ****
		# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

		# **************  Get the name of the Device Server ************
		# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
		# Clear the screen
		clscreen()
		tty_move (15, 1, "===================")
		tty_move (15, 2, "   DEVICE SERVER   ")
		tty_move (15, 3, "===================")
		tty_move (2, 5, " ==> Need informations about the device serve name !")
		Pca_DSOk = 0
		while (!Pca_DSOk) {
			tty_move (2, 7, "Enter the Device Server Name : ")
			PcaDeviceServer = input()
			tty_move (2, 9)
			Choice = yesno ("Are you sure about the Device Server Name", 1)
			if (Choice == 0) {
					tty_move (1, 7)
				tty_cntl ("cd")
			} else
				Pca_DSOk = 1
		} # end while

		# *****************  Get all counters mnemonic  ****************
		# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
		# Clear the screen
		clscreen()
		tty_move (15, 1, "==========================================")
		tty_move (15, 2, "   VISUALISATION COUNTER FOR Y-POSITION   ")
		tty_move (15, 3, "==========================================")
		tty_move (2, 5, " ==> Need informations about the counters to be used, when scanning !")

		Pca_aNewCounter = pcaNewCounterXY(2, 7, "Do you want to use a counter for Yposition")
		if (Pca_aNewCounter != -1) {			
			PcaTabCounters[PcaNbCounters] = cnt_num(Pca_aNewCounter)
			PcaNbCounters++
			PcaCounterFlag[0] = 1
		}

	 	# Clear the screen 
		clscreen()
		tty_move (15, 1, "==========================================")
		tty_move (15, 2, "   VISUALISATION COUNTER FOR Z-POSITION   ")
		tty_move (15, 3, "==========================================")
		tty_move (2, 5, " ==> Need informations about the counters to be used, when scanning !")

		Pca_aNewCounter = pcaNewCounterXY(2, 7, "Do you want to use a counter for Zposition")
		if (Pca_aNewCounter != -1) {			
			PcaTabCounters[PcaNbCounters] = cnt_num(Pca_aNewCounter)
			PcaNbCounters++
			PcaCounterFlag[1] = 1
		}

		# Clear the screen
		clscreen()
		tty_move (15, 1, "=========================================")
		tty_move (15, 2, "   VISUALISATION COUNTER FOR INTENSITY   ")
		tty_move (15, 3, "=========================================")
		tty_move (2, 5, " ==> Need informations about the counters to be used, when scanning !")
		Pca_aNewCounter = pcaNewCounterXY(2, 7, "Do you want to use a counter for Intensity")
		if (Pca_aNewCounter != -1) {			
			PcaTabCounters[PcaNbCounters] = cnt_num(Pca_aNewCounter)
			PcaNbCounters++
			PcaCounterFlag[2] = 1
		}

		# *****************  Get all motors mnemonic  *******************
		# And generate the table of operations allowed for each motor
		# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
		# Clear the screen
		clscreen()
		tty_move (15, 1, "===================")
		tty_move (15, 2, "   VERTICAL SLIT   ")
		tty_move (15, 3, "===================")
		tty_move (2, 5, " ==> Need informations about Y-SCAN Motor !")
		Pca_aNewMotor = pcaNewMotorXY(2, 7, "What is the motor mnemonic for Y-Scan ? ")
		PcaTabMotorsInd2Mne[PcaNbMotors] = motor_num(Pca_aNewMotor)
		PcaTabMotorsMne2Ind[motor_num(Pca_aNewMotor)] = 0
		PcaNbMotors ++

		# Clear the screen
		clscreen()
		tty_move (15, 1, "===================")
		tty_move (15, 2, "  HORIZONTAL SLIT  ")
		tty_move (15, 3, "===================")
		tty_move (2, 5, " ==> Need informations about Z-SCAN Motor !")	
		Pca_aNewMotor = pcaNewMotorXY(2, 7, "What is the motor mnemonic for Z-Scan ? ")
		PcaTabMotorsInd2Mne[PcaNbMotors] = motor_num(Pca_aNewMotor)
		PcaTabMotorsMne2Ind[motor_num(Pca_aNewMotor)] = PcaNbMotors
		PcaNbMotors ++

		# Clear the screen
		clscreen()
		tty_move (15, 1, "=====================")
		tty_move (15, 2, "  HORIZONTAL BENDER  ")
		tty_move (15, 3, "=====================")
		tty_move (2, 5, " ==> Need informations about Horizontal Bender Motors.")	
		for (Pca_indice = 0; Pca_indice < 2; Pca_indice++) {
			Pca_Str = sprintf ("What is the motor no %d mnemonic ? ", Pca_indice + 1)
			Pca_aNewMotor = pcaNewMotorXY(2, 7, Pca_Str)
			PcaTabMotorsInd2Mne[PcaNbMotors] = motor_num(Pca_aNewMotor)
			PcaTabMotorsMne2Ind[motor_num(Pca_aNewMotor)] = PcaNbMotors
			PcaNbMotors ++
		}
		
		# Clear the screen
		clscreen()
		tty_move (15, 1, "=====================")
		tty_move (15, 2, "   VERTICAL BENDER   ")
		tty_move (15, 3, "=====================")
		tty_move (2, 5, " ==> Need informations about Vertical Bender Motors.")	
		for (Pca_indice = 0; Pca_indice < 2; Pca_indice++) {
			Pca_Str = sprintf ("What is the motor no %d mnemonic ? ", Pca_indice + 1)
			Pca_aNewMotor = pcaNewMotorXY(2, 7, Pca_Str)
			PcaTabMotorsInd2Mne[PcaNbMotors] = motor_num(Pca_aNewMotor)
			PcaTabMotorsMne2Ind[motor_num(Pca_aNewMotor)] = PcaNbMotors
			PcaNbMotors ++
		}
		
		# Clear the screen
		clscreen()
		tty_move (1, 1, " --------------------------------------------------------")
		tty_move (1, 2, "                 FINISHING INSTALLATION...               ")
		tty_move (1, 3, " --------------------------------------------------------")


		# Clear the screen
		clscreen()
		tty_move (1, 1, " --------------------------------------------------------")
		tty_move (1, 2, "                   SETUP COMPLETED !!!!                  ")
		tty_move (1, 3, " --------------------------------------------------------")	
	} # End of Mode 2

	pcaon
}'

def pcaunsetup 'pcaoff;'

# --------------------------------------------------------------------
# This macro send a message or a command to the PC via RS232 port
# pca_com must be a string , for example : "koide9docteur"
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
# --------------------------------------------------------------------
#%IU% (<message>)
#%MDESC%
#  This macro sends a message to the PC
#
def pcasend(pca_Message) '{
  local Pca_Code
 
	Pca_Code = sprintf("%s\r", pca_Message)
	ESRF_ERR = -1
	esrf_io(PcaDeviceServer, "DevSerWriteString", Pca_Code)
}'

# --------------------------------------------------------------------
# This macro receive a message or a command to the PC via RS232 port
# The result is inserted into pca_Message
# This macro wait 6 seconds
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
# --------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro wait for a message from the PC, and
#  returns it or ERROR if no message from PC.
#
def pcareceive() '{
  local Pca_Message
  local Pca_Counter

	Pca_Message = ""
	Pca_Counter = 0
	ESRF_ERR = -1
	while ((ESRF_ERR != 0) && (Pca_Counter < 3)) {
		ESRF_ERR = -1
		Pca_Message = esrf_io(PcaDeviceServer, "DevSerReadString", 2)
		Pca_Counter ++
	}

	if (Pca_Counter >= 3)
		Pca_Message = "ERROR"

	return(Pca_Message)
}'

# --------------------------------------------------------------------
# This macro flushes the PcaDeviceServer buffer
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
# --------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro flush the buffer of the Device Server.
#
def pcaflush '{
	while (esrf_io(PcaDeviceServer, "DevSerReadString", 2) != "") {
	}
}'

#------------------------------------------------------------------
# Macro pcastop : send the PCA_STOP command to the PC
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro tell the PC to stop what he is doing.
#
def pcastop '{
	# Send the request to the PC
	pcasend("PCA_STOP")
}'

#------------------------------------------------------------------
# Macro pcastart : send the PCA_POS command to the PC
# The PC answers "PCA_REP_POS FAULT" if he is already calculating
# Otherwise he answers "PCA_REP_POS STARTED"
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro sends to the PC a message which contains the position of the motor
#  used for the scan, in order to make the PC begin calcs.
#  It is called when ascan command is entered.
#
def pcastart '{
  local Pca_Start_res
  local Pca_Start_MessageReceived
  local Pca_Start_codeRet
  local Pca_Start_Msg2Send
  local Pca_indice
  
	# Send the request to the PC
	Pca_Start_Msg2Send = sprintf("PCA_POS %d %d %f", _n1, NPTS, A[_m[0]])
	pcasend(Pca_Start_Msg2Send)
	
	# Wait 6 seconds to receive the result
	Pca_Start_MessageReceived = pcareceive()
	
	if (Pca_Start_MessageReceived != "ERROR") {
		# Recuperation des resultats attendus
		Pca_Start_codeRet = sscanf (Pca_Start_MessageReceived, "%s%s", Pca_Start_res[0], Pca_Start_res[1])
		if (Pca_Start_res[0] == "PCA_REP_POS") {
			if (Pca_Start_res[1] == "FAULT") {
				# printf ("The PC is already calculating....\n")
			} else {
				# Initialisation des resultats dans les differents compteurs
				for (Pca_indice = 0; Pca_indice < 3; Pca_indice++) { 
					if (PcaCounterFlag[Pca_indice] = 1) { S[PcaTabCounters[Pca_indice]] = 0 }
				}
			}
		} else {
			# printf ("ERROR : Bad response from the PC (START)...\n")
		}
	}
	else {
		printf ("--> TIME OUT, Retry...\n")
		printf ("Please verify the light is red at the bottom right corner on the PC software.\n")
		printf ("If not, press the \"START PC<->SPEC\" button.\n")
		exit
	}
}'

#------------------------------------------------------------------
# Macro pcastatus : send to the PC the command 'PCA_STATUS' to
# get the current mode of the computer : still calc or waiting for
# commands.
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro returns 1 if the PC is ready, 0 otherwise.
#  It is called when ascan command is entered.
#
def pcastatus() '{
  local Pca_Stat_res
  local Pca_Stat_MessageReceived
  local Pca_Stat_codeRet

	# Send the request to the PC
	pcasend("PCA_STATUS")

	# Wait 6 seconds to receive the result
	Pca_Stat_MessageReceived = pcareceive()
	
	if (Pca_Stat_MessageReceived != "ERROR") {
		# Recuperation des resultats attendus
		Pca_Stat_codeRet = sscanf (Pca_Stat_MessageReceived, "%s%d", Pca_Stat_res[0], Pca_Stat_res[1])
		if (Pca_Stat_res[0] == "PCA_REP_STATUS") {
			return (Pca_Stat_res[1])
		} else {
			# printf ("ERROR : Bad response from PC. (STATUS) \n")
		}
	}
	else {
		printf ("--> TIME OUT, Retry...\n")
		printf ("Please verify the light is red at the bottom right corner on the PC software.\n")
		printf ("If not, press the \"START PC<->SPEC\" button.\n")
		exit
	}
}'

#------------------------------------------------------------------
# Macro pcaread : send to the PC the command 'PCA_READVALUE' to
# get the position of the blob , the flux, and more...
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro fill the counters used for displaying Ypos, Zpos and Luminosity.
#  It is called when ascan command is entered.
#
def pcaread '{
  local Pca_Read_res
  local Pca_Read_MessageReceived
  local Pca_Read_codeRet
  local Pca_indice
  
	# Send the request to the PC
	pcasend("PCA_READVALUE")

	# Wait 6 seconds to receive the result
	Pca_Read_MessageReceived = pcareceive()

	if(Pca_Read_MessageReceived != "ERROR") {
		# Recuperation des resultats attendus
		Pca_Read_codeRet = sscanf (Pca_Read_MessageReceived, "%s%f%f%f", Pca_Read_res[0], Pca_Read_res[1], Pca_Read_res[2], Pca_Read_res[3])
		if (Pca_Read_res[0] == "PCA_REP_READVALUE") {
			for (Pca_indice = 0; Pca_indice < 3; Pca_indice++) { 
				if (PcaCounterFlag[Pca_indice] = 1) { S[PcaTabCounters[Pca_indice]] = Pca_Read_res[Pca_indice + 1] }
			}
		} else
			# printf ("ERROR : Bad response from the PC (READ)...\n")
	}
	else {
		printf ("--> TIME OUT, Retry...\n")
		printf ("Please verify the light is red at the bottom right corner on the PC software.\n")
		printf ("If not, press the \"START PC<->SPEC\" button.\n")
		exit
	}
}'

#------------------------------------------------------------------
# Macro pcagetcorrection : send to the PC the command 
# 'PCA_GETCORRECT' to get the correction suggested by MatLab...
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro get the suggested correction from the PC and asks the user
#  if he wants to apply it.
#
def pcagetcorrection '{
  local Pca_Corr_res
  local Pca_Corr_MessageReceived
  local Pca_Corr_codeRet
  local Pca_Corr_Choice
  local Pca_Corr_2Do
  local Pca_MotorInd2Move
  
	# Send the request to the PC
	pcasend("PCA_GETCORRECT")

	# Wait 6 seconds to receive the result
	Pca_Corr_MessageReceived = pcareceive()

	Pca_Corr_2Do = 0
	if(Pca_Corr_MessageReceived != "ERROR") {
		# Recuperation des resultats attendus
		Pca_Corr_codeRet = sscanf (Pca_Corr_MessageReceived, "%s%d%f%f", Pca_Corr_res[0], Pca_Corr_res[1], Pca_Corr_res[2], Pca_Corr_res[3])
		if (Pca_Corr_res[0] == "PCA_REP_GETCORRECT") {
			printf ("Suggested Correction : Type = %d / Pico1 = %5.4f / Pico2 = %5.4f\n", Pca_Corr_res[1], Pca_Corr_res[2], Pca_Corr_res[3])
			if (Pca_Corr_res[1] == 1) {
				printf ("That correction applies to the HORIZONTAL BENDER.\n")
			} else	printf ("That correction applies to the VERTICAL BENDER.\n")

			Pca_Corr_Choice = yesno("--> Do you want to make this correction ", 0)
			if (Pca_Corr_Choice == 1) {
				printf ("Doing the correction, please wait... \n")
				Pca_Corr_2Do = 1
			} else {
				printf ("Correction aborted by user !! \n")
				Pca_Corr_2Do = 0
			}
			if (Pca_Corr_2Do == 1) {
				# On fait un mv sur les deux moteurs du bender correspondant
				Pca_MotorMneInd2Move = Pca_Corr_res[1] * 2
				waitmove; get_angles
				A[PcaTabMotorsInd2Mne[Pca_MotorMneInd2Move]] += Pca_Corr_res[2]
				A[PcaTabMotorsInd2Mne[Pca_MotorMneInd2Move+1]] += Pca_Corr_res[3]
        			move_em
        			waitmove; get_angles
			}
		}
		else
			printf ("ERROR : Bad answer from the PC.")
	}
	else {
		printf ("--> TIME OUT, Retry...\n")
		printf ("Please verify the light is red at the bottom right corner on the PC software.\n")
		printf ("If not, press the \"START PC<->SPEC\" button.\n")
	}
}'

#---------------------------------------------------------------------------
# Macro pcashow
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
#%UU%
#%MDESC%
#  This macro shows the current configuration.
#
def pcashow '{
  local Pca_Raw
  local Pca_Column
  
  	printf ("\n ---------------------------------------------------- \n")
	printf (" Current Device Server Name : %s\n", PcaDeviceServer)
	printf (" ---------------------------------------------------- \n")
  	
  	if (PcaNbCounters != 0) {
  		printf (" Here are the counters used for displaying Ypos, Zpos and Intensity\n")
		for (Pca_Raw = 0; Pca_Raw < PcaNbCounters; Pca_Raw++) {
			printf ("\t %d - %s\t <->\t %s \n", Pca_Raw, cnt_mne(PcaTabCounters[Pca_Raw]), cnt_name(PcaTabCounters[Pca_Raw]))
		}
	} else {
		printf (" There is no counters attached to Ypos, Zpos, and intensity\n")
	}
	printf (" ---------------------------------------------------- \n")
	
	if (PcaNbMotors != 0) {
		printf (" Here are the motors used for the scan and for bender\n")
		for (Pca_Raw = 0; Pca_Raw < PcaNbMotors; Pca_Raw++) {
			printf ("\t %d - %s\t <->\t %s \n", Pca_Raw, motor_mne(PcaTabMotorsInd2Mne[Pca_Raw]), motor_name(PcaTabMotorsInd2Mne[Pca_Raw]))
		}
	} else {
		printf (" There is no motor defined for scans or benders\n")
	}
	printf (" ---------------------------------------------------- \n")	
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaon '{
	cdef ("user_prepcount", "pcastart\n", "DenisG")
	cdef ("user_countersrun", "if (pcastatus() == 0) return(1)\n", "DenisG")
	cdef ("user_getcounts", "pcaread\n", "DenisG")
	cdef ("user_cleanup2", "pcastop\n", "DenisG")
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaoff '{
	cdef ("user_prepcount", "", "DenisG", "delete")
	cdef ("user_countersrun", "", "DenisG", "delete")
	cdef ("user_getcounts", "", "DenisG", "delete")
	cdef ("user_cleanup2", "", "DenisG", "delete")
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaNewMotorXY(Xstr, Ystr, AskStr) '{
  local Pca_AMotor
  local Pca_MotorIsGood
  local Pca_MotorOk
  
	Pca_MotorOk = 0
	while (!Pca_MotorOk) {
	  	Pca_MotorIsGood = 0
  		while (!Pca_MotorIsGood) {
			tty_move (Xstr, Ystr)
			tty_cntl ("ce")
  			tty_move (Xstr, Ystr, AskStr)
		  	Pca_AMotor = input ()
		  	if (motor_num(Pca_AMotor) == -1) {
	  			tty_move (1, PcaScreenY - 2)
	  			tty_cntl ("cd")
		  		tty_move (2, PcaScreenY - 2, "!! ERROR !! The new motor you entered does not exist. Re-enter another one.")
		  	} else {
	  			tty_move (1, PcaScreenY - 2)
	  			tty_cntl ("cd")
		  		Pca_MotorIsGood = 1
		  	}
		}
		if (pcaMotorIsAlreadyUsed(Pca_AMotor)) {
			tty_move (5, PcaScreenY - 2, "ERROR !! This motor is already used by another scan or bender.")
			tty_move (5, PcaScreenY - 1, "Please re-type another one.")
		} else Pca_MotorOk = 1
	} # end while

	return (Pca_AMotor)
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaMotorIsAlreadyUsed(aMotor) '{
  local Pca_IsGood
  local Pca_indice
  
  	Pca_IsGood = 1
  	if (PcaNbMotors >= 1) {
		for (Pca_indice = 0; Pca_indice < PcaNbMotors; Pca_indice++) {
  			if (motor_mne(PcaTabMotorsInd2Mne[Pca_indice]) == aMotor) {
  				Pca_IsGood = 0
  				break
	  		}
		}
	}
	return (!Pca_IsGood)
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaNewCounterXY(Xstr, Ystr, AskStr) '{
  local Pca_ACounter
  local Pca_CounterIsGood
  local Pca_CounterOk
  local Pca_Choice
  local Pca_ChoiceOk
  local Pca_ReChoice
  local Pca_Code
  
	Pca_CounterOk = 0
	Pca_Choice = 1
	while ((!Pca_CounterOk) && (Pca_Choice)) {
		tty_move (Xstr, Ystr)
		Pca_ChoiceOk = 0
		Pca_Choice = yesno (AskStr, Pca_Choice)
		Pca_Code = 0
		if (Pca_Choice == 1) {
			Pca_ReChoice = 1
			while (!Pca_CounterOk) {
  				Pca_CounterIsGood = 0
			  	while (!Pca_CounterIsGood) {
					tty_move (Xstr, Ystr + 2)
					tty_cntl ("ce")
			  		tty_move (Xstr, Ystr + 2, "Enter the mnemonic of this counter : ")
				  	Pca_ACounter = input ()
				  	if (cnt_num(Pca_ACounter) == -1) {
				  		tty_move (1, PcaScreenY - 2)
				  		tty_cntl ("cd")
				  		tty_move (2, PcaScreenY - 2, "!! ERROR !! The new counter you entered does not exist. Re-enter another one.")
				  	} else {
				  		tty_move (1, PcaScreenY - 2)
				  		tty_cntl ("cd")
				  		Pca_CounterIsGood = 1
				  	}
				}
				if (pcaCounterIsAlreadyUsed(Pca_ACounter)) {
					tty_move (5, PcaScreenY - 2, "ERROR !! This counter is already used.")
					tty_move (5, PcaScreenY - 1, "Please re-type another one.")
				} else {
					tty_move (Xstr, Ystr + 4)
					Pca_ReChoice = yesno ("Are you sure about the counter mnemonic", 1)
					if (Pca_ReChoice == 0) {
						tty_move (1, Ystr + 2)
						tty_cntl ("cd")
					} else {
						Pca_Code = 1
						Pca_CounterOk = 1
					}
				}
			} # end while
		}
	} # end while

	if (Pca_Code == 1) return (Pca_ACounter)
	else return (-1)
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaCounterIsAlreadyUsed(aCounter) '{
  local Pca_IsGood
  local Pca_indice
  
  	Pca_IsGood = 1
  	if (PcaNbCounters >= 1) {
		for (Pca_indice = 0; Pca_indice < PcaNbCounters; Pca_indice++) {
  			if (cnt_mne(PcaTabCounters[Pca_indice]) == aCounter) {
  				Pca_IsGood = 0
  				break
	  		}
		}
	}
	return (!Pca_IsGood)
}'

#---------------------------------------------------------------------------
# Last modified : 08/13/98 (Denis GAUTHEROT --> ESRF)
#---------------------------------------------------------------------------
def pcaunsetup '{

	pcaoff
	
	# **** Free all globals variables ****
	# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
	unglobal PcaScreenX
	unglobal PcaScreenY
	unglobal PcaDeviceServer

	unglobal PcaNbCounters
	unglobal PcaTabCounters
	unglobal PcaCounterFlag

	unglobal PcaNbMotors
	unglobal PcaTabMotorsMne2Ind
	unglobal PcaTabMotorsInd2Mne
}'

#%MACROS%
#%IMACROS%
#%AUTHORS% D. Gautherot, Manuel Perez