' ===================================================================
'     Name: Control_78_NewHoming.txt               Date: 08 Set 2020
'    Autor: Leonardo DE PALO
'    Scope: Test COREXY from Wingo
' Hardware: Standard Master.
' ====================================================================

Option Speed 9

COM Open 1 115200

SpeedSearch = -0.3333	      ' direction and speed

' -------------------------------------- Options
KeyJog_Speed = 100

' -------------------------------------- Software limits
DestxMax =   256
DestxMin =     5
DestyMax =   144
DestyMin =     5

' ----------------------------------------- MOTORS ASSIGNEMENTS
POWER_IN       = 363	' main power plug insert
POWER_OUT      = 665    ' main power plug out

USB3_IN        = 212
USB3_OUT       = 620

USBC_IN        = 384
USBC_OUT       = 670
HDMI_IN        = 995
HDMI_OUT       = 787
USBPWR_IN      = 205
USBPWR_OUT     = 470
ONOFF_IN       = 750
ONOFF_OUT      = 295

VOLUME_IN_UP   = 697
VOLUME_OUT     = 754
VOLUME_IN_DW   = 794

MEMORY_IN      = 300
MEMORY_OUT     = 604
HEARSET_IN     = 865
HEARSET_OUT    = 565
PEN_DOWN       = 560
PEN_UP         = 189


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

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

' ----------------------------------------- TABLET POWER
SLOT_TabletPower   =  9         ' 1000 = Tablet on

' ----------------------------------------- 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 WITH MOUSE
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

' ----------------------------------------- KEYS
Key Right		Gosub ExecKeys
Key Left 		Gosub ExecKeys
Key Up 			Gosub ExecKeys
Key Down 		Gosub ExecKeys
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

DestX = v1
DestY = v2

Gosub  VOLUME OUT
Gosub  PEN UP
Gosub  MAIN MENU

Stop												'

' -----------------------------------------------------------------
Variable Numeric DestxMax
Variable Numeric DestxMin
Variable Numeric DestyMax
Variable Numeric DestyMin

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

Label  CLOSE ALL
	Load CloseApps
Return

Label EventStop
	Window Sizable
End


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

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

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

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

Label  PLUG
	Gosub  CLEAR BUTTONS
	Button 1 Label  POWER IN
	Button 1 Color MediumAquamarine
	Button 2 Label  POWER OUT
	Button 2 Color MediumAquamarine
	Button 3  Label  MEMORY IN
	Button 3  Color MediumAquamarine
	Button 4  Label  MEMORY OUT
	Button 4  Color MediumAquamarine
	Button 5  Label  USB3 IN
	Button 5  Color MediumAquamarine
	Button 6  Label  USB3 OUT
	Button 6  Color MediumAquamarine
	Button 7  Label  USBC IN
	Button 7  Color MediumAquamarine
	Button 8  Label  USBC OUT
	Button 8  Color MediumAquamarine
	Button 9  Label  HDMI IN
	Button 9  Color MediumAquamarine
	Button 10  Label  HDMI OUT
	Button 10  Color MediumAquamarine
	Button 11  Label  HEARSET IN
	Button 11  Color MediumAquamarine
	Button 12  Label  HEARSET OUT
	Button 12 Color MediumAquamarine
	
	Button 14 Label  MAIN MENU
	Button 14 Color Yellow
	Button 15 Label  CLOSE ALL
	Button 15 Color Red
Return

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

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

Label  POWER
	Gosub  CLEAR BUTTONS
	Button 1 Label  POWER IN
	Button 1 Color Chocolate
	Button 2 Label  POWER OUT
	Button 2 Color Chocolate
	Button 4 Label  POWER ON
	Button 4 Color Yellow6
	Button 5 Label  POWER OFF
	Button 5 Color Yellow6
	
	Button 14 Label  MAIN MENU
	Button 14 Color Yellow
	Button 15 Label  CLOSE ALL
	Button 15 Color Red
Return

Label  VIRTUAL KEY
	Gosub  CLEAR BUTTONS
	Button 1 Label  NUMBER
	Button 1 Color SlateGray
	Button 2 Label  ALPHABET
	Button 2 Color SlateGray
	Button 3 Label  £$%*°ç&[@)^!
	Button 3 Color SlateGray
	Button 5 Label  PS SEQUENCES
	Button 5 Color SlateGray
	Button 6 Label  Get-Service
	Button 6 Color SlateGray
	Button 7 Label  Win10 update history
	Button 7 Color SlateGray
	Button 8 Label  GetSystemInfo
	Button 8 Color SlateGray
	
	Button 14 Label  MAIN MENU
	Button 14 Color Yellow
	Button 15 Label  CLOSE ALL
	Button 15 Color Red
Return


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

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

Label  NUMBER
	PressKeys ALT Tab
	Wait Seconds 0.5
	s1 =  "SPACE"
	PressKeys s1
	PressKeys 1 2 3 4 5 6 7 8 9 0 Enter
Return

Label  ALPHABET
	PressKeys ALT Tab
	Wait Seconds 0.5
	PressKeys A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Enter
	PressKeys B C C  Enter
Return

Label  £$%*°ç&[@)^!
	'PressKey Space
	PressKeys WINDOWS X
	Wait Seconds 2
Return

Label  PS SEQUENCES
	SequenceFileName = "SEQUENCES\Sequence_Dir.seq"
	Gosub ExecSequence
Return

Label  Get-Service
	SequenceFileName = "SEQUENCES\Sequence_Get-Service.seq"
	Gosub ExecSequence
Return

Label  Win10 update history
	SequenceFileName = "SEQUENCES\Sequence_Win10-update-history.seq"
	Gosub ExecSequence
Return

Label  GetSystemInfo
	COM DiscardOutBuffer
	COM WriteString "SendSystemInfo" + CRLF
	FileName = "info.csv"
	Gosub ReceiveFileFromCOM
Return


Label  LUMINOSITY
	Button 1 Color Flashing8 Black Gray5
	Beep
	Wait Key("Space")
	SequenceFileName = "SEQUENCES\Sequence_LuminosityBigPen.seq"
	Gosub ExecSequence
	Button 1 Color SkyBlue
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
	Wait Seconds 0.2
	Gosub  USB3 IN
	Wait Seconds 0.2
	Gosub  USBC IN
	Wait Seconds 0.2
	Gosub  HDMI IN
	Wait Seconds 0.2
	Gosub  USBPWR IN
	Wait Seconds 0.2
	Gosub  HEARSET IN
	Wait Seconds 0.2
	Gosub  MEMORY IN
	Wait Seconds 0.2
	'Gosub  ONOFF IN
	'Gosub  VOLUME IN_UP
	'Wait Seconds 0.5
	Gosub  VOLUME OUT
	Wait Seconds 0.2
Return

Label  ALL OUT
	Gosub  POWER OUT
	Wait Seconds 0.2
	Gosub  USB3 OUT
	Wait Seconds 0.2
	Gosub  USBC OUT
	Wait Seconds 0.2
	Gosub  HDMI OUT
	Wait Seconds 0.2
	Gosub  USBPWR OUT
	Wait Seconds 0.2
	Gosub  HEARSET OUT
	Wait Seconds 0.2
	Gosub  MEMORY OUT
	Wait Seconds 0.2
	Gosub  ONOFF OUT
	Wait Seconds 0.2
	'Gosub  VOLUME IN_DW
	'Wait Seconds 0.5
	Gosub  VOLUME OUT
	Wait Seconds 0.2
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  BURN IN 7-24
	For v9 = 1 To 10000
		SequenceFileName = "SEQUENCES\BurnIn7-24.seq"
		Gosub ExecSequence
		Print "Cycle number:" + v9
'		Wait Seconds 1.5
		Slot(100) = Slot(100) + 1
		Beep
	Next
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
	
	Gosub  NO COLOR
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  Start WXM AppTablet
	SequenceFileName = "SEQUENCES\StartWXM_AppTablet.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)
		'
		Print " Executing command: " + SequenceCmd
		'
		Select UCase(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
		   ' --------------------- single action ----------------------
			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
			' ------------------- display actions ---------------------
			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
			' --------------------- WAIT MOTORS -----------------------
			Case "WAIT MOTORS"
				Gosub WaitMotors
			' --------------------- GET SYSTEM INFO -------------------
			Case "GetSystemInfo"
				Gosub  GetSystemInfo
			' --------------------- Istruction with parameters --------
			CaseElse
				s1 = GetSeparatedString(SequenceCmd, 0)
				Select UCase(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 "PRESSKEYS"
						SequenceCmd = Mid(SequenceCmd, 11)
						PressKeys SequenceCmd
					Case "SENDKEYS"
						SequenceCmd = Mid(SequenceCmd, 10)
						SendKeys SequenceCmd
					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 (with limits)
	DestX = Limit(DestX, DestxMin, DestxMax)
	DestY = Limit(DestY, DestyMin, DestyMax)
	Gosub CoreXY_PrepareCoreAB
	Slot(Slot_MotorA) = CoreA
	Slot(Slot_MotorB) = CoreB
Return

Label CoreXY_MoveToDestXY_NoLimits	' move to destX & destY (without using limits)
	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_NoLimits
	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.33	      ' 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 = 3		  ' move to MM
	Gosub Home
Return

Label Home
	' -------------------------- detach if sensor is active
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		If HomeAxis = 0
			DestX = DestX + DetachMM
		Else
			DestY = DestY + DetachMM
		EndIf
		Gosub CoreXY_MoveToDestXY_NoLimits
		Gosub WaitMotors
	EndIf
    ' -------------------------- search the sensor
Label SearchHome
	If HomeAxis = 0
		DestX = DestX + SpeedSearch
	Else
		DestY = DestY + SpeedSearch
	EndIf
	Gosub CoreXY_MoveToDestXY_NoLimits
	'
	Wait Seconds 0.005
	If Slot(SlotSensor) > 500 Or Key("CTRL")
		Gosub CoreXY_SetNewZero
		Goto DetachSensor
	EndIf
Goto SearchHome
    ' -------------------------- detach from the sensor
Label DetachSensor
	If HomeAxis = 0
		DestX = DestX + SpeedReturn
	Else
		DestY = DestY + SpeedReturn
	EndIf
	Gosub CoreXY_MoveToDestXY_NoLimits
	Wait Seconds 0.005
	If Slot (SlotSensor) < 500 Or Key("CTRL")
		Gosub WaitMotors
		Gosub CoreXY_SetNewZero
		Goto MoveToInitialPosition
	EndIf
Goto DetachSensor
    ' -------------------------- move to InitialPosition
Label MoveToInitialPosition
	If HomeAxis = 0
		DestX = InitialPosition
	Else
		DestY = InitialPosition
	EndIf
	Gosub CoreXY_MoveToDestXY_NoLimits
	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
Variable Numeric SLOT_TabletPower   ' Tabllet power sensing

'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 ReceiveFileFromCOM
	Variable String FileName
	Variable String Str1
	'
	COM DiscardInBuffer
	'
	Str1 = ""
	For v1 = 1 To 9e99
		s1 = COM_Received
		If s1 <> ""
			s1 = Replace(s1, "{{{CRLF}}}", CRLF)
			Str1 = Str1 + s1
			If s1 = "{{{FileEndMarker}}}"
				Str1 = Replace(Str1, "{{{FileEndMarker}}}", "")
				Save DeleteFile FileName
				Save StringToFile Str1 FileName
				Exit
			EndIf
		EndIf
		Wait Seconds 0.01
	Next
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.09
	Slot(SLOT_PEN) = PEN_UP
	Wait Seconds 0.1
	Slot(SLOT_PEN) = PEN_DOWN
	Wait Seconds 0.09
	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 ON
	Gosub  USB3 IN
	Button 4 Color Flashing8 Yellow5 Red5
	Gosub  ONOFF IN
	Wait Seconds 3
	If Slot(SLOT_TabletPower) > 500
		Button 4 Color PaleGreen
		Button 5 Color Red
	Else
		Print "POWER ON FAILURE"
	EndIf
	Gosub  ONOFF OUT
Return

Label  POWER OFF
	Gosub  USB3 IN
	Button 4 Color Yellow6
	Button 5 Color Flashing8 Yellow5 Red5
	Gosub  ONOFF IN
	Wait Seconds 10
	If Slot(SLOT_TabletPower) < 500
		Button 4 Color Black
		Button 5 Color Black
	EndIf
	Gosub  ONOFF OUT
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 Label "ROTATE HORZ"
			Goto ExitRotateVert
		EndIf
	Next
	Slot Slot_RotHV_Drive = 600
	Slot 998 = 0			'stop Signal Scope
	Button 12 Label "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 Label  ROTATE VERT
			Goto ExitRotateHorz
		EndIf
	Next
	Slot Slot_RotHV_Drive = 600
	Slot 998 = 0			'stop Signal Scope
	Button 12 Label  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 Label  "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 Label "JOG with Mouse"
	Jog_Active = 0
Return

' ====================================================================
'  JOG with keyboard
' ====================================================================
Label ExecKeys
	Variable Numeric KeyJog_Speed
	Variable Numeric JogSpeed
	JogSpeed = KeyJog_Speed / 100
	Select True
		Case Key("SHIFT")
			JogSpeed = JogSpeed * 0.1
		Case Key("CTRL")
			JogSpeed = JogSpeed * 0.01
		Case Key("ALT")
			JogSpeed = JogSpeed * 0.001
	EndSelect
	'
	If Key("Right")
		DestX = DestX + JogSpeed
	EndIf
	If Key("Left")
		DestX = DestX - JogSpeed
	EndIf
	'
	If Key("Up")
		DestY = DestY + JogSpeed
	EndIf
	If Key("Down")
		DestY = DestY - JogSpeed
	EndIf
	'
	'If Key("PageUp")
	'	Slot Slot_Z = Limit(Slot(Slot_Z) + JogSpeed, Zmin, Zmax)
	'EndIf
	'If Key("PageDown")
	'	Slot Slot_Z = Limit(Slot(Slot_Z) - JogSpeed, Zmin, Zmax)
	'EndIf
	'
	Gosub CoreXY_MoveToDestXY
	Gosub PrintValues
Return

' ====================================================================
'  PRINT COORDINATES
' ====================================================================
Label PrintValues
	s1 =       "DestX=" + Format(DestX, "0.000")
	s1 = s1 + " DestY=" + Format(DestY, "0.000")
	Print CRLF + s1
Return


'BOOKMARKS 57 44 

'EDITEDLINES 682 1 651 2 620 3 589 4 558 5 527 6 496 7 465 8 434 9 403 10 

'WATCH SequenceLength
'WATCH SequenceCmd
'EXEC DestX = 12
