' ------------------------------
'  TEST KEY-UP DELAY
' ------------------------------

Stop

v1 = DestX
v2 = DestY
SequenceFileName = ""
Jog_Active = 0
Print DestX

Slot Slot_MotorA = 0
Slot 998 = 0

If v1 < SoundLevelMin
	s1 = s1 + "ERROR"
EndIf

For DCV1 = 1 To 10
Next

For v1 = 1 To 10
Next

Label TEST1
	Beep
Return



' ----------------------------------------- MOTORS ASSIGNEMENTS
POWER_IN       = 363
POWER_OUT      = 750
USB3_IN        = 203
USB3_OUT       = 620
USBC_IN        = 355
USBC_OUT       = 650
HDMI_IN        = 908
HDMI_OUT       = 620
USBPWR_IN      = 228
USBPWR_OUT     = 576
ONOFF_IN       = 540
ONOFF_OUT      = 309
VOLUME_IN_UP   = 532
VOLUME_OUT     = 560
VOLUME_IN_DW   = 604
MEMORY_IN      = 300
MEMORY_OUT     = 604
HEARSET_IN     = 688
HEARSET_OUT    = 355
PEN_DOWN       = 600
PEN_UP         = 830

' ----------------------------------------- STEPPER SLOTS
Slot_MotorA        =  1			'1_TabTsterXY pin  1 STEPPER
Slot_MotorB        =  3			'1_TabTsterXY pin  3 STEPPER
Slot_SensorX       =  7			'1_TabTsterXY pin  7 DIG INPU
Slot_SensorY       =  8			'1_TabTsterXY pin  8 DIG INPU
Slot_EnableMotors  = 10			'1_TabTsterXY pin 10 DIG OUT

' ----------------------------------------- DIGITAL MOTOR SLOTS
SLOT_USB3         = 13 			'2_TabTsterMot pin 1 PWM16
SLOT_USBC         = 14          '2_TabTsterMot pin 2 PWM16
SLOT_HDMI         = 15			'2_TabTsterMot pin 3 PWM16
SLOT_USBPWR       = 16			'2_TabTsterMot pin 4 PWM16
SLOT_ONOFF        = 17			'2_TabTsterMot pin 5 PWM16
SLOT_POWER 		  = 18			'2_TabTsterMot pin 6 software control SERVO16
SLOT_VOLUME       = 25			'3_TabTsterMot pin 1 PWM16
SLOT_MEMORY       = 26			'3_TabTsterMot pin 2 PWM16
SLOT_HEARSET      = 27			'3_TabTsterMot pin 3 PWM16
SLOT_PEN          = 29			'3_TasterMot pin 4 PWM16
SLOT_ROTAT	      = 28			'3_TabTsterMot pin 5 PWM16

' ----------------------------------------- ROTATION H/V
Slot_RotHV_Drive      = 28     '30		' slot
Slot_RotHV_Current    = 60     '31		' slot
Slot_RotHV_MaxCurrent = 6000   'Period for the max current

' ----------------------------------------- SLOTS FOR SOUND TESTS
Slot_RunScope      = 37
Slot_SoundLevel    = 41
Slot_SoundLevel2ndHarm = 43
Slot_SoundLevel3rdHarm = 45

' ----------------------------------------- JOG
Jog_RangeX        = 256        '288
Jog_RangeY        = 144
Jog_Enable_Slot   = 34

' ----------------------------------------- COLOR RECOGNITION
Slot_S0   =  24
Slot_S1   =  23
Slot_S2   =  21
Slot_S3   =  20
Slot_Freq =  22

' ----------------------------------------- FILE SET UP
Variable String FileName
FileName = "Test_Results.txt"

' ----------------------------------------- LOAD APPS
Load ..\Theremino_HAL\Theremino_HAL.exe
Load ..\Theremino_HAL\Theremino_SlotViewer.exe
Load ..\Theremino_MicroVideo\Theremino_MicroVideo.exe
'Load ..\Theremino_AudioGenerator\Theremino_AudioGenerator.exe
'Load ..\Theremino_AudioInput\Theremino_AudioInput.exe
'Load ..\Theremino_HAL\Theremino_SignalScope.exe



Key Right		Gosub MoveRight
Key Left 		Gosub MoveLeft
Key Up 			Gosub MoveUp
Key Down 		Gosub MoveDown
Key Pageup		Gosub "PEN UP"
Key Pagedown	Gosub "PEN DOWN"
Key End     	Gosub "PEN SINGLE"

' ----------------------------------------- START
Load Toc.wav
Option Speed 9

Slot Slot_S0 = 1000
Slot Slot_S1 = 1000

'Window MinSize

COM Open 11 9600

s1 = COM_Status
Print s1

DestX = v1
DestY = v2

Gosub "PEN UP"
Gosub "MAIN MENU"

Stop

Label "MAIN MENU"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "DISPLAY"
	Button 1 Color SkyBlue
	Button 2 Text "TOUCH"
	Button 2 Color LightSteelBlue
	Button 3 Text "AUDIO"
	Button 3 Color LightGreen
	Button 4 Text "PLUG"
	Button 4 Color MediumAquamarine
	Button 5 Text "Wi-Fi"
	Button 5 Color HotPink
	Button 6 Text "BLUETOOTH"
	Button 6 Color RoyalBlue
	Button 7 Text "POWER"
	Button 7 Color Chocolate
	
	Button 9 Text "MECHANICAL"
	Button 9 Color Gainsboro
	Button 10 Text "MISCELLANEA"
	Button 10 Color DarkCyan
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "DISPLAY"
	Gosub "CLEAR BUTTONS"
	Button 1  Text "LUMINOSITY"
	Button 1  Color SkyBlue
	Button 2  Text "RED"
	Button 2  Color Red
	Button 3  Text "GREEN"
	Button 3  Color Green
	Button 4  Text "BLU"
	Button 4  Color Blue
	Button 5  Text "WHITE"
	Button 5  Color White
	Button 6  Text "BITMAP"
	Button 6  Color SkyBlue
	Button 7  Text "PHOTO"
	Button 7  Color SkyBlue
	Button 8  Text "VIDEO"
	Button 8  Color SkyBlue
	Button 9  Text "NO COLOR"
	Button 9  Color SkyBlue
	Button 10 Text "COLOR TEST"
	Button 10 Color SkyBlue
	Button 11 Text "--------"
	Button 11 Color SkyBlue
	Button 12 Text "TOUCH TEST"
	Button 12 Color SkyBlue
	Button 13 Text "COLOR RECOGNITION"
	Button 13 Color SkyBlue
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "TOUCH"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "PEN UP"
	Button 1 Color LightSteelBlue
	Button 2 Text "PEN DOWN"
	Button 2 Color LightSteelBlue
	Button 3 Text "PEN SINGLE"
	Button 3 Color LightSteelBlue
	Button 4 Text "PEN DOUBLE"
	Button 4 Color LightSteelBlue
	Button 5 Text "SLIDE"
	Button 5 Color LightSteelBlue
	Button 6 Text "Slide PSW"
	Button 6 Color LightSteelBlue
	Button 7 Text "TOUCH TEST"
	Button 7 Color LightSteelBlue
	Button 8 Text "JOG with Mouse"
	Button 8 Color LightSteelBlue
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "AUDIO"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "HEARSET IN"
	Button 1 Color LightGreen
	Button 2 Text "HEARSET OUT"
	Button 2 Color LightGreen
	Button 3 Text "VOLUME OUT"
	Button 3 Color LightGreen
	Button 4 Text "VOLUME UP"
	Button 4 Color LightGreen
	Button 5 Text "VOLUME DW"
	Button 5 Color LightGreen
	Button 6 Text "VOLUME TEST"
	Button 6 Color LightGreen
	Button 7 Text "SPEAK.DIST."
	Button 7 Color LightGreen
	Button 8 Text "TestHearset"
	Button 8 Color LightGreen
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "PLUG"
	Gosub "CLEAR BUTTONS"
	Button 1  Text "MEMORY IN"
	Button 1  Color MediumAquamarine
	Button 2  Text "MEMORY OUT"
	Button 2  Color MediumAquamarine
	Button 3  Text "USB3 IN"
	Button 3  Color MediumAquamarine
	Button 4  Text "USB3 OUT"
	Button 4  Color MediumAquamarine
	Button 5  Text "USBC IN"
	Button 5  Color MediumAquamarine
	Button 6  Text "USBC OUT"
	Button 6  Color MediumAquamarine
	Button 7  Text "HDMI IN"
	Button 7  Color MediumAquamarine
	Button 8  Text "HDMI OUT"
	Button 8  Color MediumAquamarine
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "Wi-Fi"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "EMPTY 1"
	Button 1 Color HotPink
	Button 2 Text "EMPTY 2"
	Button 2 Color HotPink
	Button 3 Text "EMPTY 3"
	Button 3 Color HotPink
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "BLUETOOTH"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "EMPTY 1"
	Button 1 Color RoyalBlue
	Button 2 Text "EMPTY 2"
	Button 2 Color RoyalBlue
	Button 3 Text "EMPTY 3"
	Button 3 Color RoyalBlue
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "POWER"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "POWER IN"
	Button 1 Color Chocolate
	Button 2 Text "POWER OUT"
	Button 2 Color Chocolate
	Button 4 Text "POWER CYCLE"
	Button 4 Color Red
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "MECHANICAL"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "ZERO"
	Button 1 Color Gainsboro
	Button 2 Text "ENAB.MOTORS"
	Button 2 Color Gainsboro
	Button 3 Text "DISAB.MOTORS"
	Button 3 Color Gainsboro
	Button 4 Text "ROTATE VERT"
	Button 4 Color Gainsboro
	Button 5 Text "ROTATE HORZ"
	Button 5 Color Gainsboro
	Button 6 Text "ROTATE MIDL"
	Button 6 Color Gainsboro
	Button 7 Text "BUTTON FAST"
	Button 7 Color Gainsboro
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label "MISCELLANEA"
	Gosub "CLEAR BUTTONS"
	Button 1 Text "STAND BY"
	Button 1 Color DarkCyan
	Button 2 Text "SAFE REMOVE"
	Button 2 Color DarkCyan
	Button 3 Text "ALL IN"
	Button 3 Color DarkCyan
	Button 4 Text "ALL OUT"
	Button 4 Color DarkCyan
	Button 5 Text "V KEYBOARD"
	Button 5 Color DarkCyan
	Button 6 Text "JOG with Mouse"
	Button 6 Color DarkCyan
	Button 7 Text "CALIBRATE"
	Button 7 Color DarkCyan
	Button 8 Text "USBPWR IN"
	Button 8 Color DarkCyan
	Button 9 Text "USBPWR OUT"
	Button 9 Color DarkCyan
	
	Button 14 Text "MAIN MENU"
	Button 14 Color Yellow
	Button 15 Text "CLOSE ALL"
	Button 15 Color Red
Return

Label Program1
' ----------------------------------------- BUTTONS
	Button 1 Text "RUN TEST"
	
Return

Label "LUMINOSITY"
	Button 12 Color Flashing8 Magenta2 Gray5
	Beep
	Wait Key("Space")
	SequenceFileName = "SEQUENCES\Sequence_LuminosityBigPen.seq"
	Gosub ExecSequence
	Button 12 Color Magenta9
Return

Label "V KEYBOARD"
	Beep
	Wait Key("Space")
	Button 29 Color Flashing8 Blue9  Cyan9
	SequenceFileName = "SEQUENCES\Sequence_VirtualKeyboard.seq"
	Gosub ExecSequence
	Wait Seconds 2
	Button 29 Color MediumBlue
Return

Label "RED"
	COM WriteString  "RED" + CRLF
Return

Label "GREEN"
	COM WriteString  "GREEN" + CRLF
Return

Label "BLU"
	COM WriteString "BLU" + CRLF
Return

Label "WHITE"
	COM WriteString  "WHITE" + CRLF
Return

Label "NO COLOR"
	COM WriteString "NO COLOR" + CRLF
Return

Label "BITMAP"
	COM WriteString "BITMAP" + CRLF
Return

Label "PHOTO"
	COM WriteString "PHOTO" + CRLF
Return

Label "VIDEO"
	COM WriteString "VIDEO" + CRLF
Return

Label "STAND BY"
	Gosub "ZERO"
	SequenceFileName = "SEQUENCES\Sequence_SafeRemove.seq"
	Gosub ExecSequence
	Gosub "POWER IN"
	Gosub "USB3 IN"
	Gosub "MEMORY IN"
	Gosub "USBC OUT"
	Gosub "HDMI OUT"
	Gosub "USBPWR OUT"
	Gosub "HEARSET OUT"
	Gosub "ONOFF OUT"
	Gosub "VOLUME OUT"
	Gosub "DISAB.MOTORS"
Return

Label "ALL IN"
	Gosub "POWER IN"
	Gosub "USB3 IN"
	Gosub "USBC IN"
	Gosub "HDMI IN"
	Gosub "USBPWR IN"
	Gosub "HEARSET IN"
	Gosub "MEMORY IN"
	'Gosub "ONOFF IN"
	'Gosub "VOLUME IN_UP"
	'Wait Seconds 0.5
	Gosub "VOLUME OUT"
Return

Label "ALL OUT"
	Gosub "POWER OUT"
	Gosub "USB3 OUT"
	Gosub "USBC OUT"
	Gosub "HDMI OUT"
	Gosub "USBPWR OUT"
	Gosub "HEARSET OUT"
	Gosub "MEMORY OUT"
	Gosub "ONOFF OUT"
	'Gosub "VOLUME IN_DW"
	'Wait Seconds 0.5
	Gosub "VOLUME OUT"
Return

Label MULTI
	v2 = Input "How many loops? "
	If v2 = 0
		v2 = 10
	EndIf
	For v1 = 1 To v2
		s1 = Now
		s1 = s1 + "     Cycle in progress = " + v1
		s1 = s1 + "     Cycle to do = " + v2
		Print s1
		Gosub RUN TEST
	Next
Return

Label "SAFE REMOVE"
	Gosub "ZERO"
	Gosub "POWER OUT"
	Gosub "USB3 OUT"
	Gosub "USBC OUT"
	Gosub "HDMI OUT"
	Gosub "USBPWR OUT"
	Gosub "HEARSET OUT"
	Gosub "ONOFF OUT"
	Gosub "VOLUME OUT"
	Gosub "MEMORY OUT"
	SequenceFileName = "SEQUENCES\Sequence_SafeRemove.seq"
	Gosub ExecSequence
Return

Label "BUTTON FAST"
	SequenceFileName = "SEQUENCES\Sequence_ButtonFast.seq"
	Gosub ExecSequence
Return

Label "CALIBRATE"
	'Beep
	'Wait Key("Space")
	Gosub "ZERO"
	'SequenceFileName = "SEQUENCES\Sequence_Calibrate.seq"
	SequenceFileName = "SEQUENCES\Sequence_SpeedTest.seq"
	Gosub ExecSequence
Return

' ===========================================================
'   AUDIO TESTS
' ===========================================================
Variable Numeric Slot_RunScope
Variable Numeric Slot_SoundLevel
Variable Numeric Slot_SoundLevel2ndHarm
Variable Numeric Slot_SoundLevel3rdHarm
Variable Numeric SoundLevelMin
Variable Numeric SoundNoiseMax
Variable Numeric SoundDist2ndHarmMax ' Max level for the 2nd harmonics
Variable Numeric SoundDist3rdHarmMax ' Max level for the 3rd harmonics

Label "SPEAK.DIST."
	Window MinSize
	Wait Key("Space")
	Button 25 Color Flashing8 Gold DarkGreen
	SoundLevelMin = 650
	SoundNoiseMax = 350
	SoundDist2ndHarmMax = 400
	SoundDist3rdHarmMax = 400
	Gosub AudioTest
	Window Sizable
	Button 25 Color LightSteelBlue
	'Gosub EventStop
Return

Label "TestHearset"
	Window MinSize
	Button 26 Color Flashing8 Gold DarkGreen
	SoundLevelMin = 50
	SoundNoiseMax = 30
	Gosub AudioTest
	Window Sizable
	Button 26 Color LightSteelBlue
	Gosub EventStop
Return

Label AudioTest
	'Gosub "ZERO"
	Slot 39 = 1000              '<<<<<<<<<<<<<< manual sound start
    'Beep
	'wait Seconds 2
	'Wait Key("Space")
	s1 = ""
	'---------------------- test audio left
	DestX = 014
	DestY = 108.8
	Gosub CoreXY_MoveToDestXY_AndWait
	Gosub "PEN SINGLE"
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.5
	v1 = Slot(Slot_SoundLevel)
	If v1 < SoundLevelMin
		s1 = s1 + "ERROR: Left level too low" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel2ndHarm)
	If v1 > SoundDist2ndHarmMax
		s1 = s1 + "ERROR: Left speaker. Too much distortion on the 2nd harmonics" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel3rdHarm)
	If v1 > SoundDist3rdHarmMax
		s1 = s1 + "ERROR: Left speaker. Too much distortion on the 3rd harmonics" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	
	'---------------------- test audio right
	DestX = 014
	DestY = 102.5
	Gosub CoreXY_MoveToDestXY_AndWait
	Gosub "PEN SINGLE"
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.5
	v1 = Slot(Slot_SoundLevel)
	If v1 < SoundLevelMin
		s1 = s1 + "ERROR: Right level too low" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel2ndHarm)
	If v1 > SoundDist2ndHarmMax
		s1 = s1 + "ERROR: Right speaker. Too much distortion on the 2nd harmonics" + CRLF
	EndIf
	v1 = Slot(Slot_SoundLevel3rdHarm)
	If v1 > SoundDist3rdHarmMax
		s1 = s1 + "ERROR: Right speaker. Too much distortion on the 3rd harmonics" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	
	Slot 39 = 0                 '<<<<<<<<<<<<<< manual sound stop
	
	'---------------------- test audio no sound left/right
	DestX = 014
	DestY = 096.2
	Gosub CoreXY_MoveToDestXY_AndWait
	Gosub "PEN SINGLE"
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.5
	v1 = Slot(Slot_SoundLevel)
	If v1 > SoundNoiseMax
		s1 = s1 + "ERROR: Too much noise" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	
	If s1 = ""
		s1 = "TEST PASSED OK"
	EndIf
	Print s1
Return


Label "VOLUME TEST"
	'Gosub "ZERO"
	Window MinSize
	Button 24 Color Flashing8 LightSteelBlue DarkGreen
	SoundLevelMin = 650
	SoundNoiseMax = 350
	Gosub VolumeTest
	Window Sizable
	Button 24 Color LightSteelBlue
	'SequenceFileName = "SEQUENCES\Sequence_SafeRemove.seq"
	'Gosub ExecSequence
	Gosub "STAND BY"
	Gosub EventStop
Return

Label VolumeTest
	'Beep
	'wait Seconds 2
	'Wait Key("Space")
	s1 = ""
	'---------------------- test audio volume level high
	Gosub "VOLUME UP"
	Wait Seconds 4
	Gosub "VOLUME OUT"
	Slot 39 = 1000              '<<<<<<<<<<<<<< manual sound start
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.23
	v1 = Slot(Slot_SoundLevel)
	If v1 < SoundLevelMin
		s1 = s1 + "ERROR: Left level too low" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	'Slot 39 = 0                 '<<<<<<<<<<<<<< manual sound stop
	
	'---------------------- test audio no sound left/right
	Gosub "VOLUME DW"
	Wait Seconds 4
	Gosub "VOLUME OUT"
	'Slot 39 = 1000              '<<<<<<<<<<<<<< manual sound start
	Wait Seconds 0.8
	Slot Slot_RunScope = 1000
	Wait Seconds 0.23
	v1 = Slot(Slot_SoundLevel)
	If v1 > SoundNoiseMax
		s1 = s1 + "ERROR: Too much noise" + CRLF
	EndIf
	Slot Slot_RunScope = 0
	Slot 39 = 0                 '<<<<<<<<<<<<<< manual sound stop
	If s1 = ""
		s1 = "TEST PASSED OK"
	EndIf
	Print s1
Return

' ' ----------------------------------------- COLOR RECOGNITION
Variable Numeric Slot_S0
Variable Numeric Slot_S1
Variable Numeric Slot_S2
Variable Numeric Slot_S3
Variable Numeric Slot_Freq

Label "COLOR TEST"
	FileHeaderTestName = "COLOR TEST"
	Gosub WriteFileHeader
	Beep
	'Wait Key("Space")
	Button 10 Color Flashing8 Red SkyBlue
	
	Gosub RED
	Wait Seconds 1
	Gosub "COLOR RECOGNITION"
	If s1 = "RED"
		s1 = "Red color = Passed"
	Else
		s1 = "Red color = Fail"
	EndIf
	Save StringToFile s1 FileName
	
	Gosub GREEN
	Wait Seconds 1
	Gosub "COLOR RECOGNITION"
	If s1 = "GREEN"
		s1 = "Green color = Passed"
	Else
		s1 = "Green color = Fail"
	EndIf
	Save StringToFile s1 FileName
	
	Gosub BLU
	Wait Seconds 1
	Gosub "COLOR RECOGNITION"
	If s1 = "BLU"
		s1 = "Blu color = Passed"
	Else
		s1 = "Blu color = Fail"
	EndIf
	Save StringToFile s1 FileName
	
	Button 10 Color SkyBlue
Return

Label "COLOR RECOGNITION"
	' ------------------------- RED filter
	Slot Slot_S2 = 0
	Slot Slot_S3 = 0
	Wait Seconds 0.1
	v1 = Slot(Slot_Freq)
	' ------------------------- GREEN filter
	Slot Slot_S2 = 1000
	Slot Slot_S3 = 1000
	Wait Seconds 0.1
	v2 = Slot(Slot_Freq)
	' ------------------------- BLU filter
	Slot Slot_S2 = 0
	Slot Slot_S3 = 1000
	Wait Seconds 0.1
	v3 = Slot(Slot_Freq)
	'' ------------------------- CLEAR filter
	'Slot Slot_S2 = 1000
	'Slot Slot_S3 = 0
	'Wait Seconds 0.2
	'v4 = Slot(Slot_Freq)
    ' ------------------------- RECOGNIZE COLOR
	If v1 > v2 And v1 > v3
		s1 = "RED"
	Else
		If v2 > v1 And v2 > v3
			s1 = "GREEN"
		Else
			If v3 > v1 And v3 > v2
				s1 = "BLU"
			EndIf
		EndIf
	EndIf
	Print v1 + " " + v2 + " " + v3 + " " + s1
Return

Label "TOUCH TEST"
	Window MinSize
	'Beep
	Wait Key("Space")
	SequenceFileName = "SEQUENCES\Sequence_TouchTest.seq"
	Gosub ExecSequence
Return

Label "Slide PSW"
	SequenceFileName = "SEQUENCES\Sequence_SlidePsw.seq"
	Gosub ExecSequence
Return

Variable String FileHeaderTestName
Label WriteFileHeader
	s1 = "-------------------------" + CRLF
	s1 = s1 + FileHeaderTestName + CRLF
	s1 = s1 + Format(Now, "dd/MMM/yyyy HH:mm:ss") + CRLF
	s1 = s1 + "-------------------------"
	Save StringToFile s1 FileName
Return

' =====================================================================
'  SEQUENCE LOADING AND EXECUTION
' =====================================================================
Variable String SequenceFileName
Variable String Sequence
Variable String SequenceCmd
Variable Numeric SequenceIndex
Variable Numeric SequenceLength

Label ExecSequence
	Load Sequence SequenceFileName
	If Len(Sequence) = 0
		Controls OpenTextBox
		Print CRLF + " Sequence file not found: " + SequenceFileName
		Beep
		Stop
	EndIf
	' ------------------------------------------
	Variable Numeric ExecSequenceStartTime
	ExecSequenceStartTime = ElapsedTime
	' ------------------------------------------
	SequenceLength = GetSeparatedStringCount(Sequence, CRLF)
	For SequenceIndex = 0 To SequenceLength-1
		SequenceCmd = GetSeparatedString(Sequence, SequenceIndex, CRLF)
		SequenceCmd = Replace(SequenceCmd, Chr(9), " ") 'TABS to SPACES
		SequenceCmd = RemoveComments(SequenceCmd)
		SequenceCmd = Trim(SequenceCmd)
		SequenceCmd = UCase(SequenceCmd)
		'
		Print " Executing command: " + SequenceCmd
		'
		Select SequenceCmd
            ' ------------------- exit from the sequence -------------
			Case "ENDSEQ"
				SequenceIndex = SequenceLength-1
			' ------------------- special multiple actions -----------
			Case "SLIDE"		     ' slide before insert PSW
				Gosub "SLIDE"
			Case "ZERO"              ' move pen to X=0 and Y=0 position
				Gosub "ZERO"
			Case "ALL OUT"           ' all plug out
				Gosub "ALL OUT"
			Case "ALL IN"            ' all plug in
				Gosub "ALL IN"
		   ' -----------------------------------------------
			Case "POWER IN"          ' power plug in
				Gosub "POWER IN"
			Case "POWER OUT"         ' power plug out
				Gosub "POWER OUT"
			Case "ENABLE MOTORS"     'enable movement motors
				Gosub "ENAB.MOTORS"
			Case "DISABLE MOTORS"    ' disable movement motors
				Gosub "DISAB.MOTORS"
			Case "PEN UP"            ' move pen up
				Gosub "PEN UP"
			Case "PEN DOWN"          ' move pen down
				Gosub "PEN DOWN"
			Case "PEN SINGLE"        ' single click
				Gosub "PEN SINGLE"
			Case "PEN DOUBLE"        ' double click
				Gosub "PEN DOUBLE"
			Case "POWER CYCLE"       ' push power button, wait then release
				Gosub "POWER CYCLE"
			Case "ROTATE HORZ"       ' rotate the tablet horizontally
				Gosub "ROTATE HORZ"
			Case "ROTATE MIDL"       ' rotate the tablet 60 degree
				Gosub "ROTATE MIDL"
			Case "ROTATE VERT"       ' rotate the tablet vertically
				Gosub "ROTATE VERT"
            ' ----------------------------------------------
			Case "HDMI IN"
				Gosub "HDMI IN"
			Case "HDMI OUT"
				Gosub "HDMI OUT"
			Case "USB3 IN"
				Gosub "USB3 IN"
			Case "USB3 OUT"
				Gosub "USB3 OUT"
			Case "USBC IN"
				Gosub "USBC IN"
			Case "USBC OUT"
				Gosub "USBC OUT"
			Case "USBPWR IN"
				Gosub "USBPWR IN"
			Case "USBPWR OUT"
				Gosub "USBPWR OUT"
			Case "MEMORY IN"
				Gosub "MEMORY IN"
			Case "MEMORY OUT"
				Gosub "MEMORY OUT"
			Case "HEARSET IN"
				Gosub "HEARSET IN"
			Case "HEARSET OUT"
				Gosub "HEARSET OUT"
			Case "CLOSE ALL"
				Gosub "CLOSE ALL"
			' ----------------------------------
			Case "BITMAP"
				Gosub "BITMAP"
			Case "PHOTO"
				Gosub "PHOTO"
			Case "VIDEO"
				Gosub "VIDEO"
			Case "RED"
				Gosub "RED"
			Case "GREEN"
				Gosub "GREEN"
			Case "BLU"
				Gosub "BLU"
			Case "NO COLOR"
				Gosub "NO COLOR"
			' ----------------------------------
			Case "WAIT MOTORS"
				Gosub WaitMotors
            ' ----------------------------------
			CaseElse
				s1 = GetSeparatedString(SequenceCmd, 0)
				Select s1
					Case "MOVE"
						DestX = GetSeparatedString(SequenceCmd, 1)
						DestY = GetSeparatedString(SequenceCmd, 2)
						Gosub CoreXY_MoveToDestXY_AndWait
					Case "MOVESLOW"
						MoveSlowXY_DestX = GetSeparatedString(SequenceCmd, 1)
						MoveSlowXY_DestY = GetSeparatedString(SequenceCmd, 2)
						MoveSlowXY_Speed = GetSeparatedString(SequenceCmd, 3)
						Gosub MoveSlowXY
					Case "DRAWCIRCLE"
						DrawCircle_X = GetSeparatedString(SequenceCmd, 1)
						DrawCircle_Y = GetSeparatedString(SequenceCmd, 2)
						DrawCircle_Radius = GetSeparatedString(SequenceCmd, 3)
						DrawCircle_Speed = GetSeparatedString(SequenceCmd, 4)
						Gosub DrawCircle
					Case "PAUSE"
						Wait Seconds GetSeparatedString(SequenceCmd, 1)
					Case "PENTYPING"
						PEN_TOUCH_TIME = GetSeparatedString(SequenceCmd, 1)
						Gosub "PEN TYPING"
					CaseElse
						If s1 <> ""
							Controls OpenTextBox
							Print CRLF + " Unrecognized command: " + SequenceCmd
							Beep
							Stop
						EndIf
				EndSelect
		EndSelect
	Next
	SequenceFileName = Replace(SequenceFileName,".seq","")
	Print SequenceFileName + " executed in " + Format(ElapsedTime - ExecSequenceStartTime, "0.00") + " seconds"
Return

' =====================================================================
'  CORE XY
' =====================================================================
Variable Numeric DestX				' destination in mm coords
Variable Numeric DestY				' destination in mm coords

Variable Numeric Slot_MotorA
Variable Numeric Slot_MotorB
Variable Numeric Slot_EnableMotors
Variable Numeric Slot_SensorX
Variable Numeric Slot_SensorY

Label CoreXY_MoveToDestXY_AndWait ' move to destX & destY and wait stop motors
	Gosub CoreXY_MoveToDestXY
	Gosub WaitMotors
Return

Label CoreXY_MoveToDestXY			' move to destX & destY
	Gosub CoreXY_PrepareCoreAB
	Slot(Slot_MotorA) = CoreA
	Slot(Slot_MotorB) = CoreB
Return

Label CoreXY_SetNewZero
    ' ------------------------- Reset axis X and Y
	Slot(Slot_MotorA) = Reset
	Slot(Slot_MotorB) = Reset
	Wait Seconds 0.15
	' ------------------------- Set reset values with CoreXY correction
	DestX = 0
	DestY = 0
	Gosub CoreXY_MoveToDestXY
	Gosub WaitMotors
Return

Variable Numeric CoreA
Variable Numeric CoreB

Label CoreXY_PrepareCoreAB
	CoreA = (DestX - DestY) / 2
	CoreB = (DestX + DestY) / 2
Return

' =====================================================================
'  HOME
' =====================================================================
Variable Numeric HomeAxis
Variable Numeric SlotSensor
Variable Numeric DetachMM
Variable Numeric SpeedSearch
Variable Numeric SpeedReturn
Variable Numeric InitialPosition

Label "ZERO"
	Gosub CoreXY_SetNewZero
	Gosub "ENAB.MOTORS"
	Gosub "PEN UP"
	Gosub HomeX
	Gosub HomeY
	Gosub CoreXY_SetNewZero
	Print Format(DestX, "0.000") + " " + Format(DestY, "0.000")
	'Gosub "DISAB.MOTORS"
Return

Label HomeX
	HomeAxis = 0        	  ' 0 = Axis X
	SlotSensor = Slot_SensorX ' home sensor slot
	DetachMM = 1			  ' initial detach mm (neg or pos)
	SpeedSearch = -0.3		  ' direction and speed
	SpeedReturn = +0.05		  ' Detach direction and speed
	InitialPosition = 5		  ' move to MM
	Gosub "Home"
Return

Label HomeY
	HomeAxis = 1        	  ' 1 = Axis Y
	SlotSensor = Slot_SensorY ' home sensor slot
	DetachMM = 1			  ' initial detach mm (neg or pos)
	SpeedSearch = -0.3		  ' direction and speed
	SpeedReturn = +0.05		  ' Detach direction and speed
	InitialPosition = 2		  ' move to MM
	Gosub Home
Return

Label Home
	If Slot(SlotSensor) > 500 ' detach if sensor active
		If HomeAxis = 0
			DestX = DestX + DetachMM
		Else
			DestY = DestY + DetachMM
		EndIf
		Gosub CoreXY_MoveToDestXY
		Gosub WaitMotors
	EndIf
'
Label SearchHome
	If HomeAxis = 0
		DestX = DestX + SpeedSearch
	Else
		DestY = DestY + SpeedSearch
	EndIf
	Gosub CoreXY_MoveToDestXY
	'
	Wait Seconds 0.005
	If Slot(SlotSensor) > 500
		Gosub WaitMotors
		Goto DetachSensor
	EndIf
Goto SearchHome
'
Label DetachSensor
	If HomeAxis = 0
		DestX = DestX + SpeedReturn
	Else
		DestY = DestY + SpeedReturn
	EndIf
	Gosub CoreXY_MoveToDestXY
	'
	Wait Seconds 0.005
	If Slot (SlotSensor) < 500
		Gosub WaitMotors
		Goto MoveToInitialPosition
	EndIf
Goto DetachSensor
'
Label MoveToInitialPosition
	' ------------------------------- Reset
	Gosub CoreXY_SetNewZero
	' ------------------------------- Initial position
	If HomeAxis = 0
		DestX = InitialPosition
	Else
		DestY = InitialPosition
	EndIf
	Gosub CoreXY_MoveToDestXY
	Gosub WaitMotors
Return

' =====================================================================
'  WAIT until the motors A and B are stopped
' =====================================================================
Label WaitMotors
	Wait Seconds 0.1         ' this ensures that the motors are running
	Label WaitMotors1
		If Abs(Slot(Slot_MotorA + 1)) < 0.05
			If Abs(Slot(Slot_MotorB + 1)) < 0.05
				Return
			EndIf
		EndIf
		Wait Seconds 0.005
	Goto WaitMotors1
Goto WaitMotors

' =====================================================================
'  SERVO-MOTORS MOVEMENTS
' =====================================================================
Variable Numeric POWER_IN
Variable Numeric POWER_OUT
Variable Numeric USB3_IN
Variable Numeric USB3_OUT
Variable Numeric USBC_IN
Variable Numeric USBC_OUT
Variable Numeric HDMI_IN
Variable Numeric HDMI_OUT
Variable Numeric USBPWR_IN
Variable Numeric USBPWR_OUT
Variable Numeric ONOFF_IN
Variable Numeric ONOFF_OUT
Variable Numeric VOLUME_IN_UP
Variable Numeric VOLUME_OUT
Variable Numeric VOLUME_IN_DW
Variable Numeric MEMORY_IN
Variable Numeric MEMORY_OUT
Variable Numeric HEARSET_IN
Variable Numeric HEARSET_OUT
Variable Numeric PEN_DOWN
Variable Numeric PEN_UP
Variable Numeric PEN_TOUCH_TIME

Variable Numeric SLOT_POWER			' digital motor 1
Variable Numeric SLOT_USB3			' digital motor 2
Variable Numeric SLOT_USBC			' digital motor 3
Variable Numeric SLOT_HDMI			' digital motor 4
Variable Numeric SLOT_USBPWR		' digital motor 5
Variable Numeric SLOT_ONOFF			' digital motor 6
Variable Numeric SLOT_VOLUME		' digital motor 7
Variable Numeric SLOT_MEMORY		' digital motor 8
Variable Numeric SLOT_HEARSET		' digital motor 9
Variable Numeric SLOT_PEN			' digital motor 10
Variable Numeric SLOT_ROTAT			' digital motor 11

'Label "START LOOP"
'	v2 = Input "How many loops ?"
'	For v1 = 1 To v2
'		Gosub "ALL IN"
'		Wait Seconds 0.5
'		Gosub "ALL OUT"
'		Wait Seconds 0.5
'	Next
'Return

Label "POWER OUT"
	Move_Slot = SLOT_POWER
	Move_Dest = POWER_OUT
	Move_Speed = 60
	Gosub MoveSlow
	Button 1 Color Chocolate
	Button 2 Color Red
Return

Label "POWER IN"
	Move_Slot = SLOT_POWER
	Move_Dest = POWER_IN
	Move_Speed = 60
	Gosub MoveSlow
	Button 1 Color Lime
	Button 2 Color Chocolate
Return

Label "USB3 OUT"
	Move_Slot = SLOT_USB3
	Move_Dest = USB3_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "USB3 IN"
	Move_Slot = SLOT_USB3
	Move_Dest = USB3_IN
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "USBC IN"
	Move_Slot = SLOT_USBC
	Move_Dest = USBC_IN
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "USBC OUT"
	Move_Slot = SLOT_USBC
	Move_Dest = USBC_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "HDMI OUT"
	Move_Slot = SLOT_HDMI
	Move_Dest = HDMI_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "HDMI IN"
	Move_Slot = SLOT_HDMI
	Move_Dest = HDMI_IN
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "USBPWR OUT"
	Move_Slot = SLOT_USBPWR
	Move_Dest = USBPWR_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "USBPWR IN"
	Move_Slot = SLOT_USBPWR
	Move_Dest = USBPWR_IN
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "ONOFF OUT"
	Move_Slot = SLOT_ONOFF
	Move_Dest = ONOFF_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "ONOFF IN"
	Move_Slot = SLOT_ONOFF
	Move_Dest = ONOFF_IN
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "VOLUME OUT"
	Move_Slot = SLOT_VOLUME
	Move_Dest = VOLUME_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "VOLUME UP"
	Move_Slot = SLOT_VOLUME
	Move_Dest = VOLUME_IN_UP
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "VOLUME DW"
	Move_Slot = SLOT_VOLUME
	Move_Dest = VOLUME_IN_DW
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "MEMORY OUT"
	Move_Slot = SLOT_MEMORY
	Move_Dest = MEMORY_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "MEMORY IN"
	Move_Slot = SLOT_MEMORY
	Move_Dest = MEMORY_IN
	Move_Speed = 30
	Gosub MoveSlow
Return

Label "HEARSET OUT"
	Move_Slot = SLOT_HEARSET
	Move_Dest = HEARSET_OUT
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "HEARSET IN"
	Move_Slot = SLOT_HEARSET
	Move_Dest = HEARSET_IN
	Move_Speed = 60
	Gosub MoveSlow
Return

Label "PEN UP"
	Move_Slot = SLOT_PEN
	Move_Dest = PEN_UP
	Slot(Move_Slot) = Move_Dest
	Wait Seconds 0.1
Return

Label "PEN DOWN"
	Move_Slot = SLOT_PEN
	Move_Dest = PEN_DOWN
	Slot(Move_Slot) = Move_Dest
	Wait Seconds 0.1
Return

Label "PEN SINGLE"
	Gosub "PEN DOWN"
	Wait Seconds 0.35
	Gosub "PEN UP"
Return

Label "PEN DOUBLE"
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.2
	Slot(SLOT_PEN) = PEN_UP
	Wait Seconds 0.1
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.2
	Slot(SLOT_PEN) = PEN_UP
Return

Label "PEN TYPING"
	Move_Slot = SLOT_PEN
	Move_Dest = PEN_DOWN
	Slot(Move_Slot) = Move_Dest
	Wait Seconds PEN_TOUCH_TIME
	Move_Slot = SLOT_PEN
	Move_Dest = PEN_UP
	Slot(Move_Slot) = Move_Dest
	
	'Button 10 Text "PEN UP"
Return

Label "POWER CYCLE"
	Button 4 Color Flashing8 Yellow5 Red5
	Gosub "ONOFF IN"
	Wait Seconds 0.6
	Gosub "ONOFF OUT"
	Wait Seconds 3
	Button 4 Color Red
Return

Label "ENAB.MOTORS"
	Gosub WaitMotors
	Slot(Slot_EnableMotors) = 1000
'If ButtonVisible(2)
'	Button 2 Text "DISAB.MOTORS"
'	Button 2 Color GreenYellow
'EndIf
Return

Label "DISAB.MOTORS"
	Gosub WaitMotors
	Slot(Slot_EnableMotors) = 0
	Slot Slot_RotHV_Drive = 600
'	If ButtonVisible(3)
'	Button 3 Text "ENAB.MOTORS"
'	Button 3 Color Red
'EndIf
Return


' =====================================================================
'  SERVO-MOTORS MOVE SLOW
' =====================================================================
Variable Numeric Move_Slot
Variable Numeric Move_Speed
Variable Numeric Move_Dest
Variable Numeric Move_Pos

Label MoveSlow
	Move_Pos = Slot(Move_Slot)
	If Move_Dest > Move_Pos
		Move_Pos = Move_Pos + Move_Speed
		If Move_Pos > Move_Dest
			Move_Pos = Move_Dest
		EndIf
	Else
		Move_Pos = Move_Pos - Move_Speed
		If Move_Pos < Move_Dest
			Move_Pos = Move_Dest
		EndIf
	EndIf
	Slot(Move_Slot) = Move_Pos
	Wait Seconds 0.01
	If Move_Pos = Move_Dest
		Return
	EndIf
Goto MoveSlow

' =====================================================================
'  STEPPER-MOTORS MOVE SLOW
' =====================================================================
Variable Numeric MoveSlowXY_DestX 	' destination in mm
Variable Numeric MoveSlowXY_DestY 	' destination in mm
Variable Numeric MoveSlowXY_Speed 	' speed in mm/sec

Variable Numeric MSV1
Variable Numeric MSV2
Variable Numeric MSV3
Variable Numeric MSV4

Label MoveSlowXY
	' ----------------------------------- test for very low speed
	If MoveSlowXY_Speed < 0.1
		MoveSlowXY_Speed = 1
	EndIf
	' ----------------------------------- calc the movement in mm
	MSV1 = MoveSlowXY_DestX - DestX
	MSV2 = MoveSlowXY_DestY - DestY
	MSV3 = MSV1 * MSV1
	MSV3 = MSV3 + MSV2 * MSV2
	MSV3 = Sqrt(MSV3)
	' ----------------------------------- calc the loops count
	MSV4 = MSV3 / 0.01 '<<< loop time
	MSV4 = Int(MSV4 / MoveSlowXY_Speed)
	' ----------------------------------- too much speed ?
	If MSV4 = 0
		DestX = MoveSlowXY_DestX
		DestY = MoveSlowXY_DestY
		Gosub CoreXY_MoveToDestXY_AndWait
		Return
	EndIf
	' ----------------------------------- prepare the increments
	MSV1 = (MoveSlowXY_DestX - DestX) / MSV4
	MSV2 = (MoveSlowXY_DestY - DestY) / MSV4
	' ----------------------------------- loop for v4 times
	For MSV3 = 1 To MSV4
		' ------------------------------- increment, move and wait
		DestX = DestX + MSV1
		DestY = DestY + MSV2
		Gosub CoreXY_MoveToDestXY
		Wait Seconds 0.01 '<<< loop time
	Next
	Gosub WaitMotors
Return

' =====================================================================
'  STEPPER-MOTORS DRAW CIRCLE
' =====================================================================
Variable Numeric DrawCircle_X 	    ' Center X in mm
Variable Numeric DrawCircle_Y 	    ' Center Y in mm
Variable Numeric DrawCircle_Radius  ' Radius in mm
Variable Numeric DrawCircle_Speed 	' Speed in mm/sec

Variable Numeric DCV1
Variable Numeric DCV2
Variable Numeric DCV3
Variable Numeric DCV4

Label DrawCircle
	' ----------------------------------- test for very low speed
	If DrawCircle_Speed < 0.1
		DrawCircle_Speed = 1
	EndIf
	' ----------------------------------- calc the movement in mm
	DCV3 = DrawCircle_Radius * Math_PI * 2
	' ----------------------------------- calc the loops count
	DCV4 = DCV3 / 0.01 '<<< loop time
	DCV4 = Int(DCV4 / DrawCircle_Speed)
	' ----------------------------------- too much speed ?
	If DCV4 < 16
		DCV4 = 16
	EndIf
	' ----------------------------------- prepare the increments
	DCV1 = 2 * Math_PI / DCV4
	' ----------------------------------- loop for v4 times
	For DCV3 = 0 To DCV4
		' ------------------------------- calc circle
		DCV2 = DCV3 * DCV1
		DestX = DrawCircle_X + DrawCircle_Radius * Sin(DCV2)
		DestY = DrawCircle_Y + DrawCircle_Radius * Cos(DCV2)
		' ------------------------------- move and wait
		Gosub CoreXY_MoveToDestXY
		Wait Seconds 0.01 '<<< loop time
	Next
	Gosub WaitMotors
Return

' =====================================================================
'  STEPPER-MOTORS and PEN action SLIDE
' =====================================================================
Variable Numeric Slide_DestX 	 ' destination in mm
Variable Numeric Slide_DestY 	 ' destination in mm
Variable Numeric Slide_Speed 	 ' speed in mm/sec
Variable Numeric Slide_PenUpTime ' pen-up time in mS from start

Variable Numeric SLV1
Variable Numeric SLV2
Variable Numeric SLV3
Variable Numeric SLV4
Variable Numeric SLV5

Label SLIDE
	Gosub CoreXY_MoveToDestXY_AndWait
	Slide_DestX = 50                  '50
	Slide_DestY = 130                 '130
	Slide_Speed = 200                 '200
	Slide_PenUpTime = 500             '500
	' ----------------------------------- calc the movement in mm
	SLV1 = Slide_DestX - DestX
	SLV2 = Slide_DestY - DestY
	SLV3 = SLV1 * SLV1
	SLV3 = SLV3 + SLV2 * SLV2
	SLV3 = Sqrt(SLV3)
	' ----------------------------------- calc the loops count
	SLV4 = SLV3 / 0.01 '<<< loop time
	SLV4 = Int(SLV4 / Slide_Speed)
	' ----------------------------------- prepare the increments
	SLV1 = (Slide_DestX - DestX) / SLV4
	SLV2 = (Slide_DestY - DestY) / SLV4
	' ----------------------------------- prepare the pen-up time
	SLV5 = Slide_PenUpTime / 1000
	SLV5 = Int(SLV5 / 0.01) '<<< loop time
	' ----------------------------------- pen down
	Slot(SLOT_PEN) = PEN_DOWN
	' ----------------------------------- loop for v4 times
	For SLV3 = 1 To SLV4
		' ------------------------------- increment, move and wait
		DestX = DestX + SLV1
		DestY = DestY + SLV2
		Gosub CoreXY_MoveToDestXY
		Wait Seconds 0.01 '<<< loop time
		' ------------------------------- pen up
		If SLV3 = SLV5
			Slot(SLOT_PEN) = PEN_UP
		EndIf
	Next
	Gosub WaitMotors
Return

' =====================================================================
'  ROTATIONS VERTICAL/HORIZZONTAL
' =====================================================================
Variable Numeric Slot_RotHV_Drive
Variable Numeric Slot_RotHV_Current
Variable Numeric Slot_RotHV_MaxCurrent

Label "ROTATE VERT"
	Slot 998 = 600			'start Signal Scope
	Slot Slot_RotHV_Drive = 600
	Wait Seconds 0.5
	For v1 = 1 To 1300        ' <<< 1000 = 10 sec timeout (approx)
		Wait Seconds 0.009
		If Slot(Slot_RotHV_Current) > Slot_RotHV_MaxCurrent
			Slot Slot_RotHV_Drive = 600
			Slot 998 = 0	'stop Signal Scope
			Button 12 Text "ROTATE HORZ"
			Goto ExitRotateVert
		EndIf
	Next
	Slot Slot_RotHV_Drive = 600
	Slot 998 = 0			'stop Signal Scope
	Button 12 Text "ROTATE HORZ"
'
Label ExitRotateVert
Return

Label "ROTATE MIDL"
	Slot 998 = 600			'start Signal Scope
	Slot Slot_RotHV_Drive = 600
	Wait Seconds 0.5
	For v1 = 1 To 600        ' <<< 1000 = 10 sec timeout (approx)
		Wait Seconds 0.009
		If Slot(Slot_RotHV_Current) > Slot_RotHV_MaxCurrent
			Slot Slot_RotHV_Drive = 600
			Slot 998 = 0	'stop Signal Scope
			Goto ExitRotateMidl
		EndIf
	Next
	Slot Slot_RotHV_Drive = 600
	Slot 998 = 0			'stop Signal Scope
'
Label ExitRotateMidl
Return

Label "ROTATE HORZ"
	Slot 998 = 600			'start Signal Scope
	Slot Slot_RotHV_Drive = 0
	Wait Seconds 0.3
	For v1 = 1 To 1300        ' <<< 1000 = 10 sec timeout (approx)
		Wait Seconds 0.009
		If Slot(Slot_RotHV_Current) > Slot_RotHV_MaxCurrent
			Slot Slot_RotHV_Drive = 600
			Slot 998 = 0	'stop Signal Scope
			Button 12 Text "ROTATE VERT"
			Goto ExitRotateHorz
		EndIf
	Next
	Slot Slot_RotHV_Drive = 600
	Slot 998 = 0			'stop Signal Scope
	Button 12 Text "ROTATE VERT"
	'
Label ExitRotateHorz
Return

' ================================================
'  JOG WITH MOUSE
' ================================================
Variable Numeric Jog_Enable_Slot
Variable Numeric Jog_RangeX
Variable Numeric Jog_RangeY
Variable Numeric Jog_Active

Label "JOG with Mouse"
	Gosub "ENAB.MOTORS"
	Button 8 Text "JOG STOP"
	Jog_Active = 1
	Label Jog2
		If Slot(Jog_Enable_Slot) > 500
			DestX = MouseX * Jog_RangeX
			DestY = MouseY * Jog_RangeY
			Gosub CoreXY_MoveToDestXY
			Print Format(DestX, "0.000") + " " + Format(DestY, "0.000")
		EndIf
		If Jog_Active = 0
			Return
		EndIf
	Goto Jog2
Return

Label "JOG STOP"
	Button 8 Text "JOG with Mouse"
	Jog_Active = 0
Return

' ================================================
'  JOG WITH keyboard
' ================================================
Label MoveRight
	DestX = DestX + 1
	Gosub CoreXY_MoveToDestXY
	Print Format(DestX, "0.000") + " " + Format(DestY, "0.000")
Return

Label MoveLeft
	DestX = DestX - 1
	Gosub CoreXY_MoveToDestXY
	Print Format(DestX, "0.000") + " " + Format(DestY, "0.000")
Return

Label MoveUp
	DestY = DestY + 1
	Gosub CoreXY_MoveToDestXY
	Print Format(DestX, "0.000") + " " + Format(DestY, "0.000")
Return

Label MoveDown
	DestY = DestY - 1
	Gosub CoreXY_MoveToDestXY
	Print Format(DestX, "0.000") + " " + Format(DestY, "0.000")
Return

Label MoveUpper
	Slot(5) = Slot(5) + 8
Return

Label "CLOSE ALL"
	Load CloseApps
Return

Label "CLEAR BUTTONS"
	For v1 = 1 To 32
		Button v1 Text ""
		Button v1 Color Moccasin
	Next
Return

' ------------ Event STOP ------------------------
Label EventStop
	v1 = DestX
	v2 = DestY
	Window Sizable
End


'WATCH s1
'WATCH len(s1)
'WATCH v1
'WATCH v2
'WATCH v3
'EXEC DestX = 12
